问题描述
我们有一个要求,即不允许具有对GitHub存储库的写访问权的协作者创建具有某些名称的分支。他们可以在本地创建,但不能推送到远程,也不能直接使用GitHub UI创建。我知道我们可以使用git hook来做类似的事情,并在服务器端(在github企业服务器上)为那个仓库启用它,但是却在努力寻找方法。那有可能吗? 如果是这样,我想要一个shell脚本或类似的东西,我可以将其用作挂钩来拒绝创建/推送以字符串release开头的任何分支。我通过在github企业服务器上添加一个预接收钩子来尝试以下操作,但效果不佳。使用branch_hook.sh文件创建了一个仓库,并配置了该钩子并在仓库中启用了它来进行测试。
#!/bin/bash
branch=`git rev-parse --abbrev-ref HEAD`
if [[ "$branch" == release* ]]; then
echo "Your branch starts with release and is not allowed to be
pushed. Please create one that doesn't start with the release"
exit 1
fi
看起来GitHub服务器根本没有验证分支名称。如果我在git repo下本地执行它,它可以正常工作,但git commit或push根本不考虑它。 根据VonC的建议,我尝试了此操作,但似乎不起作用。 由于我无法在https://gist.github.com/caniszczyk/1327469处获得脚本 工作,所以尝试了我自己的东西
#!/bin/bash
# Reject branch pushes that contain commits under those branches that have names starting with release
first_commit='0000000000000000000000000000000000000000'
while read -r oldrev newrev refname; do
[ "$newrev" = "$first_commit" ] && continue
[ "$oldrev" = "$first_commit" ] && range="$newrev" || range="$oldrev..$newrev"
for commit in $(git rev-list "$range" --not --all); do
if [[ "${refname#refs/heads/}" == release* ]]; then
echo "ERROR: Your push was rejected because the commit"
echo "ERROR: $commit in ${refname#refs/heads/}"
echo "ERROR: is not allowed as ${refname#refs/heads/} is not supported branch name"
echo "ERROR: Please fix your branch name or contact your repository admin."
exit 1
fi
done
done
当您在本地创建分支(刚创建的或新创建的,并添加了一些提交)并尝试推送到远程时,这非常有用,但是由于钩子在其中不起作用,因此无法阻止使用GitHub UI创建分支场景,因为它不是推送事件。因此,我想知道如何在https://gist.github.com/caniszczyk/1327469上运行脚本,如果这是在两种情况下均适用的真正解决方案。
请提供任何建议或可能的解决方案。