Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture a git commit message and run an action

Tags:

git

bash

githooks

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.

like image 345
James Nine Avatar asked Feb 02 '11 01:02

James Nine


People also ask

How do you supply a commit message?

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.

Which command is used to give a 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."


2 Answers

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.

like image 54
James Nine Avatar answered Sep 20 '22 20:09

James Nine


You probably want a git hook for that

like image 26
Pablo Fernandez Avatar answered Sep 21 '22 20:09

Pablo Fernandez