【问题标题】:Prevent users from pushing all tags to a Bitbucket repository阻止用户将所有标签推送到 Bitbucket 存储库
【发布时间】:2023-01-14 18:36:57
【问题描述】:
我有一个存储库,我经常在其中删除不再需要的旧标签,或者在已经重新定位的分支机构的头部。有时我们团队中的开发人员会使用命令 git push --tags 推送到 git,这会将所有被修剪的标签推送回原点。我们鼓励我们的开发人员指定他们想要推送的标签,而不是使用 --tags 但错误会发生,有时我们会再次出现所有旧标签。
所以首先,我想知道这在 Bitbucket 中是否可行,如果不行,是否有解决方法可以提供相同的限制?
该问题仅在人类错误地使用错误命令时才会发生,因此如果有一个解决方案会警告或需要为每个用户实施,则不需要完全阻止,尽管集中规则是首选。
我用谷歌搜索了这个问题的答案并找到了这个链接https://confluence.atlassian.com/bitbucketserverkb/how-do-i-block-all-tags-from-being-pushed-to-a-repository-822021700.html 但是在测试这个解决方案时我发现了一些问题:
- 从那时起,Bitbucket 界面发生了一些变化,当尝试创建
/refs/tags/** 的分支模式时,我收到错误消息 Consecutive wildcard characters are not allowed
- 尝试使用
/refs/tags/* 的分支模式(允许)时,它仍然允许我使用 git push --tags 推送所有标签,这不是预期的结果。
- 即使这确实有效,我认为它会阻止推送任何标签,而不是仅推送包含多个标签的标签。
【问题讨论】:
标签:
git
tags
bitbucket
push
restriction
【解决方案1】:
我不知道是否有使用 Bitbucket 的特定方法,但这里有一个预接收 Git 挂钩,它会拒绝超过 1 个标签的推送。
#!/bin/sh
tag_count=0
while read ref_infos
do
eval "ref_name=`echo $ref_infos | cut -d' ' -f3`"
case $ref_name in ('refs/tags/'*)
# Only increment the tag count for created/updated tags, not deleted ones
eval "ref_target=`echo $ref_infos | cut -d' ' -f2`"
if test $ref_target != '0000000000000000000000000000000000000000'; then
tag_count=$((tag_count + 1))
fi
esac
if test $tag_count -gt 1; then
echo 'You are trying to push more than 1 tag at once, which is forbidden.'
echo 'Please push your tags one by one, and ensure they are relevant to the remote.'
exit 1
fi
done
对于自托管的 Bitbucket 实例,您的存储库预接收挂钩应该位于:
<installation-path>/Bitbucket/shared/data/repositories/<ID>/hooks/pre-receive.d/
经过一番谷歌搜索后,我找到了 this page 关于 Bitbucket 上的钩子。
使用 Java 插件框架,您甚至应该能够在 Bitbucket 的 Cloud 版本上执行此操作,但这也意味着您必须为此开发一个插件,例如 in this example,这更加繁琐。
如果您愿意,可以在客户端使用相同的代码作为预推送 Git 挂钩。
钩子的路径是<repository-path>/.git/hooks/。