Git钩子,post-receive循环遍历提交

3
使用服务器端的git hooks,每次有人推送到远程仓库时,是否可以循环遍历从客户端发送到服务器的新提交信息?
我需要从每个消息中提取信息,
哈希、日期、提交作者、分支
我找不到任何关于git hooks的好文档来解决它。 我已经阅读过 git post-receive hook that grabs commit messages and posts back to URL 但我不理解其中一行代码。
1个回答

3
正如githooks手册所解释的那样,post-receive挂钩会为每个引用获取一行,其中包含

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

其中<old-value>是存储在引用中的旧对象名称,<new-value>是要存储在引用中的新对象名称,而<ref-name>是引用的完整名称。

因此,如果将此放入.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

while语句使其循环遍历每一行,将该行的三个字段读入变量$oldvalue$newvalue$refname

git log命令将输出哈希值、日期和提交作者到标准输出。

git branch命令将尝试输出分支。(或者您可以使用echo $refname,它会以refs/heads/master格式输出)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接