4

I am about to release the first version of my first git-managed project and am going to tag it with an annotated tag ("first alpha release"). Later on, to find out which was the first alpha release, I would like to search the contents of the tag annotations for "first alpha". How do I do so?

I know git log --grep will search the contents of commit messages and git show will tell me the contents of a tag annotation, but I can't figure out from the manpages or Google what command will do a search over tag annotations. Do I have to dump the records where the tag annotations are stored and search using another tool? I am envisioning git show $(git tag)|grep "first alpha" and hoping there is a better way.

2 Answers 2

5
+50

This does use external grep but seems to be more elegant than parsing the output of git show:

git tag -l -n | grep "first alpha"

and you will get very nicely the output: test_1.2.3 first alpha

Note the -n flag, here I assume your annotation are one line long. For longer messages you will need to give a number after -n (lets say -n99) and will be more complicated regarding the grep flags. https://www.kernel.org/pub/software/scm/git/docs/git-tag.html

One way to search multiline annotations is with gawk (example bash command line shown):

git tag -l -n99 | gawk -v pat='<some regex>' -- '/^\S/ {tag=$1} $0~pat { print tag }'

/^\S/ {tag=$1} saves each tag name in turn, and $0~pat { print tag }' prints the tag name when a match is found.

2
  • 1
    Hidden in plain sight! Thank you --- I had overlooked the -n option. I see from testing that multiline annotations are indented, which makes it easier to process them. I will take the liberty of editing your answer to include the gawk oneliner I wrote just now to complete the solution.
    – cxw
    Jul 8, 2016 at 17:28
  • A convenient shell script now on GitHub.
    – cxw
    Jul 8, 2016 at 17:45
3

You need git show on the dereferenced tag for exploring the content of the annotated tag "first alpha release":

git show $(git show-ref -d -s --tags "first tag release"|tail -1| awk '{print $1}')

That being said, a git show directly with the tag would give the same result, except for the tag metadata displayed first.

git show $(git tag|grep "first alpha")

That would show the content of all tags whose tag name includes "first alpha".

1
  • Thank you for the tip on git show-ref --- I hadn't used it before. Upvote! This answer appears to work well for tag names, but not for the content of the tag annotations (unless I'm missing something), since patterns are matched in the ref names. The dereference goes all the way to the tagged commit, so the $(...) gives commit hashes rather than tag-object hashes. I appreciate your time on this answer!
    – cxw
    Jul 8, 2016 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.