I wrote a script
#!/bin/bash
commit_msg=$1
echo "Your commit message: $commit_msg"
in hooks/commit-msg
which validates the commit message in
git commit -m "fixed a bug"
But when I run the hook, I have:
Your commit message: .git/COMMIT_EDITMSG
instead of
Your commit message: fixed a bug
How can I capture the commit message into a variable?
I've read How to capture a git commit message and run an action
but it didn't help me because that hook was for post-receive
and I need it for commit-msg
so I don't have my commit message in
git log -1 HEAD --pretty=format:%s
because my hook blocks from doing a commit.
From the docs:
The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer
Therefore, you need to read the contents of the given file to provide the message:
commit_msg=$(cat "${1:?Missing commit message file}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With