Git hooks, post-receive loop through commit

余生颓废 提交于 2019-12-05 22:15:10
Klas Mellbourn

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)

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