【问题标题】:How do I read tagger information from a GIT tag?如何从 GIT 标签中读取标签信息?
【发布时间】:2011-05-10 07:27:17
【问题描述】:

到目前为止我有:

git rev-parse <tagname> | xargs git cat-file -p

但这不是最容易解析的东西。我希望有类似于git-log--pretty 选项的东西,这样我就可以获取我需要的信息。

有什么想法吗?

【问题讨论】:

    标签: git git-tag


    【解决方案1】:

    获取相同信息的更直接方法是:

    git cat-file tag <tagname>
    

    这使用单个命令并避免管道。

    我在 bash 脚本中使用如下:

    if git rev-parse $TAG^{tag} -- &>/dev/null
    then
        # Annotated tag
        COMMIT=$(git rev-parse $TAG^{commit})
        TAGGER=($(git cat-file tag $TAG | grep '^tagger'))
        N=${#TAGGER} # Number of fields
        DATE=${TAGGER[@]:$N-2:2} # Last two fields
        AUTHOR=${TAGGER[@]:1:$N-3} # Everything but the first and last two
        MESSAGE=$(git cat-file tag $TAG | tail -n+6)
    elif git rev-parse refs/tags/$TAG -- &>/dev/null
    then
        # Lightweight tag - just a commit, basically
        COMMIT=$(git rev-parse $TAG^{commit})
    else
        echo "$TAG: not a tag" >&2
    fi
    

    【讨论】:

    • 我认为现在使用git for-each-ref 的新答案是更好的解决方案
    【解决方案2】:

    git show $TAG 会显示标签的信息,以及它指向的提交。

    如果你有一些已经适合你的东西,但输入起来很笨拙,你总是可以设置一个别名:

    [alias]
            showtag = !sh -c 'git rev-parse $1 | xargs git cat-file -p' -
    

    然后调用它:

    $ git showtag my-tag-name
    

    【讨论】:

    • 谢谢。我应该提到我也设法到达git show --quiet --pretty="format:" $TAG,但这与上面的基本相同。
    • @quornian:你可以使用 Git 的别名功能。我在回答中提供了一个示例。
    • 谢谢,效果很好!只是想提醒读者alias 需要添加到文件.gitconfig,通常它在您的主文件夹中。如果在执行此git showtag 之后没有换行符,您可以通过以下方式添加它:showtag = !sh -c 'git rev-parse $1 | xargs git cat-file -p &amp;&amp; echo ""' -
    【解决方案3】:

    这个问题很久以前就已经得到了回答,但它仍然是最热门的搜索结果,尽管它不再是最好的解决方案,所以这里是:

    命令:

    git for-each-ref refs/tags/$TAG --shell --format='
    TAG=%(refname)
    TYPE=%(objecttype)
    COMMIT=%(objectname)
    TAGGER=%(tagger)
    EMAIL=%(taggeremail)
    DATE=%(taggerdate)
    CONTENTS=%(contents)
    '
    

    --shell 对 Shell 脚本进行引用。还有--perl--python--tcl。 如果您不想将整个格式写入命令行选项,您也可以将其放入 file.txt 并执行以下操作:

    git for-each-ref refs/tags/<tag> --shell --format="$(cat file.txt)"
    

    输出:

    TAG='refs/tags/4.1.0-RC1'
    TYPE='tag'
    COMMIT='973cc103f942330550866588177fe53ea5765970'
    TAGGER='ml_'
    EMAIL='<ml@example.org>'
    DATE='Fri Sep 16 14:14:50 2016 +0200'
    CONTENTS='Release 3:
    * INSTALL.md added.
    * GIT.md modified.
    '
    

    更多信息在这里: https://git-scm.com/docs/git-for-each-ref

    【讨论】:

    • 我认为这是现在最好的答案(比我接受的答案更好)
    • 请注意,对于轻量级标签,TYPE 将是 commit
    猜你喜欢
    • 2011-09-13
    • 2013-12-26
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 2011-01-18
    • 1970-01-01
    • 2018-01-19
    相关资源
    最近更新 更多