开发者

How to capture a git commit message and run an action

开发者 https://www.devze.com 2023-02-08 04:43 出处:网络
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.

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开发者_如何学Go 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.


You probably want a git hook for that

0

精彩评论

暂无评论...
验证码 换一张
取 消