Search code examples
gitversion-controltagsfull-text-searchrelease-management

How do I search the content of tag annotations in git?


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.


Solution

  • 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.