Search code examples
git-taggit-describe

git version relative to a specific tag


We're using multiple tag identifiers in our repo. Eg. ABC-1.3.5.234 and DEF-1.2.1.25. The describe command gives me almost what I want:

git describe --long

ABC-1.3.5.234-33-deadbeef

But I really wanted to know the value relative to the most recent DEF tag in my history. Is there a way to specify which tag I want to use as the base for calculating relative distance? Can I do it with a regex?


Solution

  • The git describe man page is clear:

    If an exact match was not found, git describe will walk back through the commit history to locate an ancestor commit which has been tagged. The ancestor's tag will be output along with an abbreviation of the input committish's SHA-1.

    If multiple tags were found during the walk then the tag which has the fewest commits different from the input committish will be selected and output.
    Here fewest commits different is defined as the number of commits which would be shown by git log tag..input will be the smallest number of commits possible.

    So you might have to write a script which:

    • git describe
    • take the commit found and git describe that commit
    • repeat until you get the tag you were looking for
    • sum all the number 'n' of additional commits on top of the found tagged objects during that loop
    • build and output the resulting string: DEF-xxx-n-DEF_SHA1.

    Since 2013, as illustrated in Hendrik's answer, this work better:

    git describe --all --match <tagPattern>
    

    But only if, as I describe in "how to match a hidden ref using git describe", you are using Git 2.15 (Q3 2017).
    Before 2.15, --all would have been ignored.

    Note: Git 2.15 also allows multiple patterns for git describe --match

    That allows even more advanced searches like:

    For example, suppose you wish to find the first official release tag that contains a certain commit.
    If we assume that official release tags are of the form "v*" and pre-release candidates include "*rc*" in their name, we can now find the first release tag that introduces the commit abcdef:

    git describe --contains --match="v*" --exclude="*rc*" abcd