I'm new to git and I want to be able to capture the commit message after a push to the origin/master and run a bash script (on the server) based on what the string contains.
For example, if my git commit message says: [email] my commit message
If the commit message contains [email]
then do a specified action, otherwise, don't do it.
Here's a sample bash script I'm thinking of using in the post-receive
hook:
#!/bin/bash
MESSAGE= #commit message variable?
if [[ "$MESSAGE" == *[email]* ]]; then
echo "do action here"
else
echo "do nothing"
fi
Basically all I need to know is what the variable name for the commit message is, to use in the above bash script? Also, I'm not sure if this is the right hook to do this or not.
The easiest way to create a Git commit with a message is to execute “git commit” with the “-m” option followed by your commit message.
Git commit -m The -m option of commit command lets you to write the commit message on the command line. This command will not prompt the text editor. It will run as follows: $ git commit -m "Commit message."
I think I figured out the answer to my own question; the variable can be obtained using the git-log
command:
git log -1 HEAD --pretty=format:%s
so, my script would be:
#!/bin/bash
MESSAGE=$(git log -1 HEAD --pretty=format:%s)
if [[ "$MESSAGE" == *\[email\]* ]]; then
echo "do action here"
else
echo "do nothing"
fi
I hope this might help anyone else who is searching for the answer.
You probably want a git hook for that
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