如何从 git 标签中读取标签信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4185888/
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
How do I read tagger information from a git tag?
提问by quornian
So far I have:
到目前为止,我有:
git rev-parse <tagname> | xargs git cat-file -p
but this isn't the easiest thing to parse. I was hoping for something similar to git-log
's --pretty
option so I could grab just the info I need.
但这不是最容易解析的事情。我希望有类似于git-log
's--pretty
选项的东西,这样我就可以获取我需要的信息。
Any ideas? Thanks
有任何想法吗?谢谢
回答by Neil Mayhew
A more direct way of getting the same info is:
获取相同信息的更直接方法是:
git cat-file tag <tagname>
This uses a single command and avoids the pipe.
这使用单个命令并避免使用管道。
I used this in a bash script as follows:
我在 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
回答by mipadi
git show $TAG
will show you the information for the tag, as well as the commit it points to.
git show $TAG
将向您显示标签的信息,以及它指向的提交。
If you have something that already works for you, but is unwieldy to type, you could always set an alias:
如果你有一些已经适合你的东西,但很难输入,你总是可以设置一个别名:
[alias]
showtag = !sh -c 'git rev-parse | xargs git cat-file -p' -
And call it with:
并调用它:
$ git showtag my-tag-name
回答by ml_
This has already been answered a long time ago but still is the top search result even though it's not the best solution anymore, so here it goes:
很久以前就已经回答了这个问题,但即使它不再是最佳解决方案,它仍然是最热门的搜索结果,所以它是这样的:
Command:
命令:
git for-each-ref refs/tags/$TAG --shell --format='
TAG=%(refname)
COMMIT=%(objectname)
TAGGER=%(tagger)
EMAIL=%(taggeremail)
DATE=%(taggerdate)
CONTENTS=%(contents)
'
--shelldoes the quoting for Shell scripts. There is also --perl, --pythonand --tcl. If you don't want to write whole format as a command line option, you can also put it in a file.txtand do this:
--shell对 Shell 脚本进行引用。还有--perl、--python和--tcl。如果您不想将整个格式编写为命令行选项,您也可以将其放入file.txt并执行以下操作:
git for-each-ref refs/tags/<tag> --shell --format="$(cat file.txt)"
Output:
输出:
TAG='refs/tags/4.1.0-RC1'
COMMIT='973cc103f942330550866588177fe53ea5765970'
TAGGER='ml_'
EMAIL='<[email protected]>'
DATE='Fri Sep 16 14:14:50 2016 +0200'
CONTENTS='Release 3:
* INSTALL.md added.
* GIT.md modified.
'
More information here: https://git-scm.com/docs/git-for-each-ref
更多信息在这里:https: //git-scm.com/docs/git-for-each-ref