要求在git push时使用带注释的标签并拒绝使用轻量级标签。

5

是否有可能在git仓库上设置策略,禁止推送轻量级标签?


Git自动安装到新存储库中的update.sample钩子包括此精确功能(以及其他一些有用的部分)。请查看$GIT_DIR/hooks/update.sample中的任何现有存储库。 - Chris Johnsen
2个回答

4

Git hook page提到:

默认的更新钩子,在启用时 - 并且未设置hooks.allowunannotated配置选项或将其设置为false - 防止推送未注释的标签。

这反过来引用了update.sampleChris Johnsen在评论中提到。

case "$refname","$newrev_type" in
    refs/tags/*,commit)
        # un-annotated tag
        short_refname=${refname##refs/tags/}
        if [ "$allowunannotated" != "true" ]; then
            echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2
            echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2
            exit 1
        fi
        ;;

1
更新钩子是从git push的角度在接收(远程)存储库上调用的。有时,您无法访问远程存储库以安装挂钩;据我所知,这是GitHub的情况(它很高兴地允许推送轻量级标签)。
为了防止从本地存储库推送轻量级标签,您可以将以下内容添加到.git/hooks/pre-push的读取循环体中,如从pre-push.sample复制:
case "$local_ref" in
    refs/tags/*)
        if [ `git cat-file -t "$local_ref"` == 'commit' ]
        then
            echo >&2 "Tag $local_ref is not annotated, not pushing"
            exit 1
        fi
        ;;
esac

然而,我认为最好的解决方案是回避整个问题。注释标签可以自动与任何可达到这些标签的引用一起推送。配置变量push.followTags启用此行为,因此您可以默认执行正确的操作,几乎不必显式推送标签:

git config --global push.followTags true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接