这是我目前在公司服务器中的一个裸仓库中的钩子:
git push origin master
这个钩子推送到 Assembla。当有人将更改推送到我们服务器上的那个分支时,我需要只推送一个分支(理想情况下是主分支),而忽略对其他分支的推送。是否可以从裸仓库中选择分支并仅将该分支推送到 Assembla?
7 回答
一个 post-receive 钩子从标准输入获取它的参数,形式如下:
<oldrev> <newrev> <refname>
由于这些参数来自标准输入,而不是来自命令行参数,因此您需要使用read
而不是$1 $2 $3
.
post-receive 钩子可以一次接收多个分支(例如,如果有人做了 a git push --all
),所以我们还需要将 包装read
在一个while
循环中。
一个工作片段看起来像这样:
#!/bin/bash
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "master" = "$branch" ]; then
# Do something
fi
done
post-receive 钩子在 stdin 上获得的最后一个参数是 ref 被更改的内容,因此我们可以使用它来检查该值是否为“refs/heads/master”。有点类似于我在接收后挂钩中使用的红宝石:
STDIN.each do |line|
(old_rev, new_rev, ref_name) = line.split
if ref_name =~ /master/
# do your push
end
end
请注意,它会为每个推送的 ref 获取一行,因此如果您推送的不仅仅是 master,它仍然可以工作。
Stefan的回答对我不起作用,但这确实:
#!/bin/bash
echo "determining branch"
if ! [ -t 0 ]; then
read -a ref
fi
IFS='/' read -ra REF <<< "${ref[2]}"
branch="${REF[2]}"
if [ "master" == "$branch" ]; then
echo 'master was pushed'
fi
if [ "staging" == "$branch" ]; then
echo 'staging was pushed'
fi
echo "done"
上述两种解决方案都不适合我。经过大量调试后,事实证明使用“读取”命令不起作用——相反,以通常的方式解析命令行参数可以正常工作。
这是我刚刚在 CentOS 6.3 上成功测试的确切更新后挂钩。
#!/bin/bash
echo "determining branch"
branch=`echo $1 | cut -d/ -f3`
if [ "master" == "$branch" ]; then
echo "master branch selected"
fi
if [ "staging" == "$branch" ]; then
echo "staging branch selected"
fi
exec git update-server-info
更新:更奇怪的是,预接收钩子通过标准输入获取输入,因此使用“读取”读取(哇,从没想过我会这么说)。更新后挂钩对我来说仍然适用于 1 美元。
@pauljz 的答案适用于某些 git 钩子,例如pre-push
,但pre-commit
无法访问这些变量oldrev newrev refname
所以我创建了这个替代版本,它适用于预提交,或者真的和钩子。如果我们不在分支上,这是一个pre-commit
将运行脚本的钩子。husky
master
#!/bin/bash
# git 'commit' does not have access to these variables: oldrev newrev refname
# So get the branch name off the head
branchPath=$(git symbolic-ref -q HEAD) # Something like refs/heads/myBranchName
branch=${branchPath##*/} # Get text behind the last / of the branch path
echo "Head: $branchPath";
echo "Current Branch: $branch";
if [ "master" != "$branch" ]; then
# If we're NOT on the Master branch, then Do something
# Original Pre-push script from husky 0.14.3
command_exists () {
command -v "$1" >/dev/null 2>&1
}
has_hook_script () {
[ -f package.json ] && cat package.json | grep -q "\"$1\"[[:space:]]*:"
}
cd "frontend" # change to your project directory, if .git is a level higher
# Check if precommit script is defined, skip if not
has_hook_script precommit || exit 0
# Node standard installation
export PATH="$PATH:/c/Program Files/nodejs"
# Check that npm exists
command_exists npm || {
echo >&2 "husky > can't find npm in PATH, skipping precommit script in package.json"
exit 0
}
# Export Git hook params
export GIT_PARAMS="$*"
# Run npm script
echo "husky > npm run -s precommit (node `node -v`)"
echo
npm run -s precommit || {
echo
echo "husky > pre-commit hook failed (add --no-verify to bypass)"
exit 1
}
fi
我希望这对某人有所帮助。您可以根据需要轻松修改if
andfi
语句之间的任何内容。
我为自己编写了一个 PHP 脚本来执行此功能。
https://github.com/fotuzlab/githubdump-php
将此文件托管在您的服务器上,最好是 repo root 并在 github webhooks 中定义 url。将第 8 行的“allcommits”更改为您的分支名称,并在第 18 行添加您的代码/函数。
例如
function githubdump($payload_object) {
// Write your code here.
exec('git push origin master');
}