2

Currently, I'm using this command to show the contents of the annotated tag:

git tag --list --format='%(contents)' name-of-tag

Which worked fine until we started GPG signing the annotated tags. Now the result is:

<tag contents>
-----BEGIN PGP SIGNATURE-----

<signature contents>
-----END PGP SIGNATURE-----

Effectively, the signature is appended to the tag contents! How do I filter it out in one simple command?

What sort of works:

git tag --list --format='%(contents:subject)' tag-name

But only until the first empty line. What if I also want empty lines in my tag?

Resorting to shell scripting would not be ideal since we execute the commands with GitPython.

1
  • 2
    As the posted answer kind of implies, this happens because the signature is literally appended to tag contents. Tag signing predates commit signing by many years, and back then they went with a simpler implementation that probably reflected general Git usage at that time. (It was written in much less C and more shell-script by proportion; the new style of hidden headers when signing a commit would likely have been considerably more difficult to pre-process when verifying, if one wanted to do the verification in shell as was common in Git at the time.) Commented Oct 1 at 13:13

1 Answer 1

5

You can still output the content of an annotated tag without its signature with git tag --format, but you need to use the syntax '%(contents:body)'. This will filter out the signature, yielding only the tag's message (body). See Git's reference (git tag --format shares the same syntax of git-for-each-ref --format).

git tag --list --format='%(contents:body)' name-of-tag

The command git tag --list --format='%(contents:subject)' tag-name sort of works, because it prints the subject, i.e. the first line of your message. Quoting the docs

The first paragraph of the message, which typically is a single line, is taken as the "subject" of the commit or the tag message.


As suggested in the comments by @jthill, if you want to list both the tag's subject and body without the signature, you can use the following version of the command. Make sure that the string expression is preceded by a $ so that the escape sequences are properly expanded as new lines.

git tag --list --format=$'%(subject)%(if)%(contents:body)%(then)\n\n%(contents:body)%(end)' name-of-tag
Sign up to request clarification or add additional context in comments.

2 Comments

To get both with the newlines all proper, $'%(subject)%(if)%(contents:body)%(then)\n\n%(contents:body)%(end)'
Great addition. I'll include it in the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.