在更新后挂钩中查找 Git 分支名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7331519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:57:57  来源:igfitidea点击:

Find Git branch name in post-update hook

gitshellcruisecontrolgithooks

提问by user489998

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。为此,我正在使用 Git 更新后挂钩。

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?

如果我能找出哪个分支已经提交,那么我就可以使用它来通知 CruiseControl 要构建哪个分支,那就太好了。有没有办法在更新后挂钩中访问分支名称?

回答by patthoyts

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:

post-update 挂钩的第一个参数是完整的分支引用 - 例如,我看到 'refs/heads/master' 用于推送到 'origin master'。因此,仅打印修改后的分支的示例钩子脚本是:

#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref )
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.

以“remote:”开头的新行是我们的钩子脚本输出的。