编写一个 git post-receive 钩子来处理特定的分支 [英] Writing a git post-receive hook to deal with a specific branch
问题描述
这是我当前在公司服务器中的裸仓库中的钩子:git push origin master
这个钩子推送到 Assembla.当有人将更改推送到我们服务器上的该分支时,我需要只推送一个分支(理想情况下是主分支),而忽略对其他分支的推送.是否可以从裸仓库中选择分支并将该分支仅推送到 Assembla?
post-receive hook 从 stdin 获取参数,格式如下:
<新版本><参考名称>
由于这些参数来自标准输入,而不是来自命令行参数,您需要使用 read
而不是 $1 $2 $3
.
post-receive 钩子可以一次接收多个分支(例如如果有人做了一个git push --all
),所以我们还需要包装read
在 while
循环中.
一个工作片段看起来像这样:
#!/bin/bash而读取 oldrev newrev refname做分支=$(git rev-parse --symbolic --abbrev-ref $refname)如果 [ 主人";=$分支"];然后# 做点什么菲完毕
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?
A post-receive hook gets its arguments from stdin, in the form:
<oldrev> <newrev> <refname>
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 the read
in a while
loop.
A working snippet looks something like this:
#!/bin/bash
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" = "$branch" ]; then
# Do something
fi
done
这篇关于编写一个 git post-receive 钩子来处理特定的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!