在更新后的钩子中查找Git分支名称 [英] Find Git branch name in post-update hook
问题描述
每次更新发送到远程存储库时,我正在执行一个程序来提醒CruiseControl。我为此使用了一个Git post-update钩子。
I'm executing a programme to alert CruiseControl each time an update is sent to our remote repository. I'm using a Git post-update hook for this.
如果我能找出哪个分支已经被提交,那我可以用它来通知CruiseControl哪个分支要建立。有没有办法在后更新钩子中访问分支名称?
It would be great if I could find out which branch had been committed so I could use that to inform CruiseControl which branch to build. Is there any way to access the branch name within a post-update hook?
推荐答案
更新后钩子的第一个参数是完整的分支引用 - 例如,我看到'refs / heads / master'用于推送'origin master'。所以,一个只打印分支修改的钩子脚本就是:
The first parameter to the post-update hook is the branch reference in full - for instance I see 'refs/heads/master' for a push to 'origin master'. So an example hook script that just prints the branch modified is:
#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref $1)
echo Update pushed to branch $branch
exec git update-server-info
为了说明,当上述内容放入远程资源库的hooks / post-update文件中时,执行推送时会打印以下内容:
To illustrate, when the above is placed into your remote repository hooks/post-update file the following is printed when performing a push:
% git push origin master
Counting objects: 5, done
Writing objects: 100% (3/3), 247 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
remote: Update pushed to branch master
To /tmp/xx/a
e02d9cd..ab14a08 master -> master
我们的钩子脚本输出了开始'remote:'的新行。
The new line beginning 'remote:' was output by our hook script.
这篇关于在更新后的钩子中查找Git分支名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!