tags:

views:

27

answers:

2

How can I list tags contained by a given branch, the opposite of:

git tag --contains <commit>

Which "only list tags which contain the specified commit".

If something like this does not exist, how do I test whether a commit is contained by another commit so that I can script this?

I could do this:

commit=$(git rev-parse $branch)
for tag in $(git tag)
do
    git log --pretty=%H $tag | grep -q -E "^$commit$"
done

But I hope there is a better way as this might take a long time in a repository with many tags and commits.

A: 

git describe (or some variant of it) might be what you're looking for.

ebneter
+1  A: 

This might be close to what you want:

git rev-list --simplify-by-decoration --pretty=format:%d "$committish" | fgrep 'tag: '

But, the more common situation is to just find the most recent tag:

git describe --tags --abbrev=0 "$committish"
  • --tags will search against lightweight tags, do not use it if you only want to consider annotated tags.
  • Do not use --abbrev=0 if you want to also see the usual “number of ‘commits on top’ and abbreviated hash” suffix (e.g. v1.7.0-17-g7e5eb8).
Chris Johnsen