Find Git branch name in post-update hook [duplicate]

徘徊边缘 提交于 2019-11-26 19:19:05

问题


This question already has an answer here:

  • Writing a git post-receive hook to deal with a specific branch 7 answers

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.

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?


回答1:


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

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

The new line beginning 'remote:' was output by our hook script.



来源:https://stackoverflow.com/questions/7331519/find-git-branch-name-in-post-update-hook

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