编写 git post-receive hook 来处理特定分支
这是我当前在公司服务器中的裸存储库中的钩子: git push origin master
这个钩子会推送到 Assembla。 我需要的是,当有人将更改推送到我们服务器上的该分支时,仅推送一个分支(理想情况下是主分支),并忽略对其他分支的推送。是否可以从裸存储库中选择分支并仅将该分支推送到 Assembla?
Here's my current hook in a bare repo that lives in the company's server:git push origin master
This hooks pushes to Assembla.
What i need is to push only one branch (master, ideally) when someone pushes changes to that branch on our server, and ignore pushes to other branches. Is it possible to select the branch from a bare repo and push only that branch to Assembla?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
post-receive 钩子从 stdin 获取参数,格式为:
由于这些参数来自 stdin,而不是命令行参数,因此您需要使用
read
而不是$1 $2 $3
。post-receive 钩子可以一次接收多个分支(例如,如果有人执行 git push --all ),因此我们还需要将
read
包装在 <代码> while循环。一个工作片段看起来像这样:
A post-receive hook gets its arguments from stdin, in the form:
Since these arguments are coming from stdin, not from a command line argument, you need to use
read
instead of$1 $2 $3
.The post-receive hook can receive multiple branches at once (for example if someone does a
git push --all
), so we also need to wrap theread
in awhile
loop.A working snippet looks something like this:
post-receive hook 在 stdin 上获取的最后一个参数是 ref 被更改的内容,因此我们可以使用它来检查该值是否为“refs/heads/master”。有点类似于我在接收后挂钩中使用的 ruby:
请注意,它会为每个推送的 ref 获取一行,因此如果您推送的不仅仅是 master,它仍然可以工作。
The last parameter that a post-receive hook gets on stdin is what ref was changed, so we can use that to check if that value was "refs/heads/master." A bit of ruby similar to what I use in a post-receive hook:
Note that it gets a line for each ref that was pushed, so if you pushed more than just master, it will still work.
Stefan的答案对我不起作用,但是 这个 做到了:
Stefan's answer didn't work for me, but this did:
上述解决方案都不适合我。经过多次调试后,事实证明使用“read”命令不起作用——相反,以通常的方式解析命令行参数可以正常工作。
这是我刚刚在 CentOS 6.3 上成功测试的确切更新后挂钩。
更新:更奇怪的是,预接收钩子通过标准输入获取输入,因此用“read”读取(哇,从来没想过我会这么说)。对于我来说,更新后的钩子仍然可以使用 1 美元。
Neither of the solutions above worked for me. After much, much debugging, it turns out that using the 'read' command doesn't work -- instead, parsing command line arguments the usual way works fine.
Here is the exact post-update hook that I just successfully tested now on CentOS 6.3.
UPDATE: on an even stranger note, the pre-receive hook takes its input via stdin, therefore read with 'read' (wow, never thought I'd say that). The post-update hook still works with $1 for me.
@pauljz 的答案对于某些 git 挂钩(例如
pre-push
)效果很好,但pre-commit
无法访问这些变量oldrev newrev refname
所以我创建了这个替代版本,它适用于预提交,或者真正的钩子。这是一个预提交挂钩,如果我们不在
master
分支上,它将运行husky
脚本。我希望这对某人有帮助。您可以根据需要轻松修改
if
和fi
语句之间的任何内容。The answer from @pauljz works fine for certain git hooks like
pre-push
, butpre-commit
does not have access to those variablesoldrev newrev refname
So I created this alternate version which works for pre-commit, or really and hook. This is a
pre-commit
hook that will run ahusky
script if we're NOT on themaster
branch.I hope that helps someone. You can easily modify for your needs, anything in between the
if
andfi
statements.我为自己编写了一个 PHP 脚本来执行此功能。
https://github.com/fotuzlab/githubdump-php
最好将此文件托管在您的服务器上repo root 并在 github webhooks 中定义 url。将第 8 行的“allcommits”更改为您的分支名称,并在第 18 行添加您的代码/函数。
例如
I had written a PHP script for myself to do this functionality.
https://github.com/fotuzlab/githubdump-php
Host this file on your server, preferably repo root and define the url in github webhooks. Change 'allcommits' on line 8 with your branch name and add your code/function at line 18.
e.g.
简单的方法,在
git hook
中编写Simple - 有关此链接的更多信息 挂钩系统
Simple approach, in
git hook
writeSimple - more info on this great link hooking system