在 git Push 上需要带注释的标签并拒绝轻量级标签

发布于 2024-11-25 11:38:44 字数 41 浏览 1 评论 0原文

是否可以在 git 存储库上设置不允许将轻量级标签推送到其中的策略?

Is it possible to set up a policy on a git repo that disallows lightweight tags from being pushed to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2024-12-02 11:38:44

Git hook 页面提到:

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

该引用依次引用 update.sample Chris 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
        ;;

The Git hook page mentions:

The default update hook, when enabled — and with hooks.allowunannotated config option unset or set to false — prevents unannotated tags to be pushed.

That references in turn the update.sample Chris Johnsen mentions in the comments.

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
        ;;
晚风撩人 2024-12-02 11:38:44

从 git push 的角度来看,更新钩子是在接收(远程)存储库上调用的。有时,您无权在远程存储库上安装挂钩;据我所知,GitHub 就是这种情况(它很高兴允许推送轻量级标签)。

为了防止从本地存储库推送轻量级标签,您可以将其添加到 .git/hooks/pre-push 中的读取循环主体中,如从 pre-push 复制的那样。示例

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

The update hook is invoked on the receiving (remote) repository from the perspective of git push. Sometimes, you do not have access to install hooks on remote repositories; as far as I know, this is the case for GitHub (which happily allows pushing lightweight tags).

To prevent lightweight tags from being pushed from the local repository, you could add this to the body of the reading loop in .git/hooks/pre-push, as copied from 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

However, the best solution in my opinion is to sidestep the whole problem.
Annotated tags can be pushed automatically together with any refs from which these tags are reachable. Configuration variable push.followTags enables this behavior, so you can do the right thing by default and hardly ever have to push tags explicitly:

git config --global push.followTags true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文