10

How to capture a git commit message and execute an action

 3 years ago
source link: https://www.codesd.com/item/how-to-capture-a-git-commit-message-and-execute-an-action.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to capture a git commit message and execute an action

advertisements

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.


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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK