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

use*_*998 33 git shell cruisecontrol githooks

每次将更新发送到我们的远程存储库时,我正在执行一个程序来提醒CruiseControl.我正在使用Git post-update钩子.

如果我能找到已经提交的分支,那将是很好的,所以我可以使用它来通知CruiseControl要构建哪个分支.有没有办法在更新后的钩子中访问分支名称?

pat*_*yts 44

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

#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref $1)
echo Update pushed to branch $branch
exec git update-server-info
Run Code Online (Sandbox Code Playgroud)

为了说明,当将上述内容放入远程存储库挂钩/更新后文件时,执行推送时会打印以下内容:

% 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
Run Code Online (Sandbox Code Playgroud)

以'remote:'开头的新行由我们的钩子脚本输出.

  • 对于那些没有填充$ 1的环境,请参阅pauljz的答案:http://stackoverflow.com/questions/7351551/writing-a-git-post-receive-hook-to-deal-with-a-specific-branch (2认同)