quornian quornian - 10 months ago 73
Git Question

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

Any ideas? Thanks

Answer Source

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
    # 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
    # Lightweight tag - just a commit, basically
    COMMIT=$(git rev-parse $TAG^{commit})
    echo "$TAG: not a tag" >&2