编写一个git post-receive hook来处理特定的分支 [英] Writing a git post-receive hook to deal with a specific branch
问题描述
以下是我在当前公司服务器上裸回购的钩子:
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?
推荐答案
post-receive钩子获取它来自stdin的参数,格式为< oldrev> < NEWREV> < refname>
。由于这些参数来自stdin,而不是来自命令行参数,所以您需要使用 read
而不是 $ 1 $ 2 $ 3
。
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
.
post-receive hook可以一次接收多个分支(例如,如果有人执行了 git push --all
),所以我们还需要将 read
包装在中,同时
循环。
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 hook来处理特定的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!