Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

bash - How to capture a git commit message and run an action

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...