Git hooks, post-receive loop through commit

随声附和 提交于 2019-12-07 15:13:21

问题


Using git hooks on the server side, is it possible to loop through the new commit messages that were sent from the client to the server everytime someone pushes to the remote repository?

I need to extract information from each message,

hash,date,commit author,branch

I cant find any good documentation on git hooks figure it out. I've read through git post-receive hook that grabs commit messages and posts back to URL

and I don't understand a simple line of code


回答1:


As is explained in the githooks man page, the post-receive hook gets a line for each ref, containing

<old-value> SP <new-value> SP <ref-name> LF

where <old-value> is the old object name stored in the ref, <new-value> is the new object name to be stored in the ref and <ref-name> is the full name of the ref.

So, if you put this in .git/hooks/post-receive:

#!/bin/sh
while read oldvalue newvalue refname
do
   git log -1 --format='%H,%cd,%an' $newvalue
   git branch --contains $newvalue | cut -d' ' -f2
done

The while statement makes it loop over each line, reading the three fields from the line into the variables $oldvalue, $newvalue and $refname

The git log line will output hash,date,commit author to standard out.

The git branch line will try to output the branch. (Alternatively you could use echo $refname, which will output in the format refs/heads/master)



来源:https://stackoverflow.com/questions/17368084/git-hooks-post-receive-loop-through-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!