How do I read tagger information from a GIT tag?
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
opti开发者_开发技巧on so I could grab just the info I need.
Any ideas?
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:
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 show $TAG
will show you the information for the tag, as well as the commit it points to.
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 $1 | xargs git cat-file -p' -
And call it with:
$ git showtag my-tag-name
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)
TYPE=%(objecttype)
COMMIT=%(objectname)
TAGGER=%(tagger)
EMAIL=%(taggeremail)
DATE=%(taggerdate)
CONTENTS=%(contents)
'
--shell does the quoting for Shell scripts. There is also --perl, --python and --tcl. If you don't want to write whole format as a command line option, you can also put it in a file.txt and do this:
git for-each-ref refs/tags/<tag> --shell --format="$(cat file.txt)"
Output:
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.
'
More information here: https://git-scm.com/docs/git-for-each-ref
精彩评论