2010-11-15 72 views
18

到目前为止,我有:如何从git标签中读取标签信息?

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

但这不是解析最简单的事情。我希望能有类似git-log--pretty选项,这样我就可以抓取我需要的信息。

任何想法?谢谢

回答

21

更直接的方式得到相同的信息是:

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 
+0

这太好了,谢谢!很好的例子。 – quornian 2014-04-10 16:37:53

17

git show $TAG将向您显示标记的信息以及它指向的提交。

如果你有一些已经为你工作,但笨重的打字,你总是可以设置一个别名:

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

里调用:

$ git showtag my-tag-name 
+1

谢谢。我应该提到,我还设法达到'git show --quiet --pretty =“的格式:”$ TAG“,但是大部分与上面相同。 – quornian 2010-11-15 15:35:23

+0

@quornian:你可以使用Git的别名功能。我在答案中提供了一个例子。 – mipadi 2010-11-15 18:09:35

4

这已经是很久以前回答,但仍然是即使它不是最好的解决办法顶部的搜索结果了,所以这里有云:

命令:

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

--shell d oes引用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' 
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. 
' 

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