我如何对 git hooks 中的新标签做出反应?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5809910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:47:56  来源:igfitidea点击:

How do I react to new tags in git hooks?

gitgithooks

提问by futuraprime

I'd like to set up a git hook that creates a CDN-style directory structure based on incoming tags. So, for example, if the last tag in the local repository is "v1.2.1" and I pull a commit with "v1.2.2", it should see the new tag and clone the repository into a new directly (../1.2.2) accordingly.

我想设置一个 git hook,它根据传入的标签创建一个 CDN 风格的目录结构。因此,例如,如果本地存储库中的最后一个标签是“v1.2.1”并且我使用“v1.2.2”拉提交,它应该会看到新标签并将存储库直接克隆到一个新标签(../1.2 .2) 相应地。

I'm pretty sure I want to attach this to post-receive, however I can't find anything in the documentation about git hooks about how to read the incoming tags. Are they delivered on a different hook? Do I actually need to have the shell script run a git command to see if any of the new commits have new tags?

我很确定我想将此附加到接收后,但是我在关于 git hooks 的文档中找不到任何关于如何读取传入标签的内容。它们是在不同的挂钩上交付的吗?我真的需要让 shell 脚本运行一个 git 命令来查看是否有任何新提交有新标签吗?

Thanks!

谢谢!

回答by VonC

Tags are refs like any other (like commit).
If tags are pushed to a repo with a post-receive hook, that hook will be called and will list all updated refs, that is both old and new values of all the refs in addition to their names (on its standard input).

标签和其他任何东西一样都是引用(比如提交)。
如果标签被推送到一个带有post-receive hook的 repo ,这个 hook 将被调用并列出所有更新的 refs,除了它们的名字(在它的标准输入上)之外,它是所有 refs 的旧值和新值。

See this server post-receive emailhook for example.

例如,请参阅此服务器接收后电子邮件挂钩。

#!/bin/bash

. $(dirname ##代码##)/functions

process_ref() {
    oldrev=$(git rev-parse )
    newrev=$(git rev-parse )
    refname=""

    set_change_type
    set_rev_types
    set_describe_tags

    case "$refname","$rev_type" in
      refs/tags/*,tag)
        # annotated tag
        refname_type="annotated tag"
        function="atag"
        short_refname=${refname##refs/tags/}
        # change recipients
        if [ -n "$announcerecipients" ]; then
          recipients="$announcerecipients"
        fi
      ;;
    esac 
}

while read REF; do process_ref $REF; done

For this to work you also must install the functions filefrom the aforementioned example hook repository.

为此,您还必须安装上述示例挂钩存储库中的函数文件