Git钩子,通过提交后接收循环 [英] Git hooks, post-receive loop through commit

查看:134
本文介绍了Git钩子,通过提交后接收循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用服务器端的git hooks,是否有可能在每次有人推送到远程存储库时,循环从客户端发送到服务器的新提交消息?

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

我找不到有关git hooks的任何好的文档. 我已经读完了 git post-receive钩子抓取提交消息并将其发布回URL

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

我不懂简单的代码行

推荐答案

正如

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

<旧值> SP<新值> SP<引用名称> LF

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

其中,< old-value>是存储在引用中的旧对象名称,< new-value>是要存储在ref中的新对象名称,< ref-name>是引用的全名.参考

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.

因此,如果将其放在.git/hooks/post-receive中:

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

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

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

git log行将输出哈希,日期,将作者提交给标准输出.

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

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

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)

这篇关于Git钩子,通过提交后接收循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆