quornian
quornian

Reputation: 10163

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 option so I could grab just the info I need.

Any ideas?

Upvotes: 35

Views: 35859

Answers (4)

xmedeko
xmedeko

Reputation: 7802

git tag --format knows the same formatting like `git for-each-ref FIELD NAMES, so it's possible to write e.g.:

git tag -l --format='%(tag) (taggername) %(taggeremail:mailmap,trim) %(taggerdate:iso-strict) %(contents)' <tagname>

Or list annotated tags only with their details:

git tag -l --omit-empty --format='%(if)%(tag)%(then)%(tag) %(taggername) %(taggeremail:mailmap,trim) %(taggerdate:iso-strict) %(contents:subject)%(end)'

Upvotes: 1

ml-
ml-

Reputation: 412

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='<[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

Upvotes: 14

Neil Mayhew
Neil Mayhew

Reputation: 14907

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

Upvotes: 45

mipadi
mipadi

Reputation: 410662

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

Upvotes: 36

Related Questions