threads / discuss / 40161

List tags for a certain branch

Subject: List tags for a certain branch

## tl;dr

4 messages between Aug 23, 2015 and Aug 23, 2015.

replies: 3people: 2as markdown or json

CoDEmanX· Aug 23, 2015, 15:27 UTC · lore
Hi everyone,

the question how to list tags, that point to commits contained in a certain branch came up on StackOverflow couple times, and this appears to be the only fast solution (example for local devel branch):

     git log --simplify-by-decoration --decorate --pretty=%d 
"refs/heads/devel" | fgrep 'tag: '

It would be much much simpler, if the tag command supporter an optional parameter to specify a branch:

     git tag --list --branch devel
It should result in something like:

Test-Tag1 Test-Tag2 Test-Tag3 Another-Tag And-Another

... even if all three Test-Tag* tags point to the same commit!
What above mentioned git log-solution does in this situtation is:
  (HEAD, tag: Test-Tag1, tag: Test-Tag2, Test-Tag3, fork/devel, devel)
  (tag: Another-Tag)
  (tag: And-Another)
BTW: git describe FULL_HASH can't be used to return all tags that point 
to the given hash. The only workaround seems to be:
     git for-each-rev ref/tags | grep FULL_HASH

I hope you consider to add the proposed --branch option to a future git version. Thanks!

SO question that started the discussion:
http://stackoverflow.com/questions/32166548/how-to-list-all-tags-within-a-certain-git-branch

Best, Cody

Jeff King· Aug 23, 2015, 17:07 UTC · re: CoDEmanX · lore

Re: List tags for a certain branch

On Sun, Aug 23, 2015 at 05:27:46PM +0200, CoDEmanX wrote:
Show 19 quoted lines
> the question how to list tags, that point to commits contained in a certain
> branch came up on StackOverflow couple times, and this appears to be the
> only fast solution (example for local devel branch):
> 
>     git log --simplify-by-decoration --decorate --pretty=%d
> "refs/heads/devel" | fgrep 'tag: '
> 
> It would be much much simpler, if the tag command supporter an optional
> parameter to specify a branch:
> 
>     git tag --list --branch devel
> 
> It should result in something like:
> 
> Test-Tag1
> Test-Tag2
> Test-Tag3
> Another-Tag
> And-Another

I think the option you are looking for is "--merged", which currently only the "branch" command nows about. So right now you can do:

  git branch --merged devel

to get a list of branches that are contained in "devel". There is work underway to unify the selection/filter code for git-branch and git-tag, so in a future version of git you should be able to do "git tag --merged" to get the tags that are "merged" to a particular branch.

-Peff

PS I'm not sure if we should pick a more generic name than "--merged" when git-tag learns this feature. For branches, it makes sense to ask "which branches are merged to this other branch". But the operation is really "which items are ancestors of the commit I gave". It is the opposite of "--contains" in that sense. Sort of a "--contained-in".

CoDEmanX· Aug 23, 2015, 18:06 UTC · re: Jeff King · lore

Re: List tags for a certain branch

> in a future version of git you should be able to do "git tag 
--merged" to get the tags that are "merged" to a particular branch. 
Would it return every tag in the branch, even if it was created in that branch, and not merged from somewhere else? If no, than it's not what I am looking for. 
(Or more precisely: would it give a list of all tags, that point to commits present in the specified branch, regardless of whether these commits were originally done in that branch or merged from another branch? I hope my understanding of git is correct here...)
I was able to achieve my goal with a Python script, that
- gets a list of all commit hashes for a given branch and creates a set
- creates a dictionary / hashmap from refs/tags/*, using commit hashes as keys and tag names as values
- creates a set from dictionary keys
- does an intersection operation using both sets
- and maps the remaining hashes back to tag names.
See the updated SO question:
http://stackoverflow.com/a/32169239/2044940
Thank you Peff!
Jeff King· Aug 23, 2015, 18:58 UTC · re: CoDEmanX · lore

Re: List tags for a certain branch

On Sun, Aug 23, 2015 at 08:06:39PM +0200, CoDEmanX wrote:
Show 11 quoted lines
> > in a future version of git you should be able to do "git tag 
> --merged" to get the tags that are "merged" to a particular branch. 
> 
> Would it return every tag in the branch, even if it was created in
> that branch, and not merged from somewhere else? If no, than it's not
> what I am looking for. 
> 
> (Or more precisely: would it give a list of all tags, that point to
> commits present in the specified branch, regardless of whether these
> commits were originally done in that branch or merged from another
> branch? I hope my understanding of git is correct here...)

Yes, it would show all tags that are present in the history leading up to that branch tip[1]. Git tracks a DAG of the history; it does not remember "which branch" a particular action happened on.

-Peff
[1] Actually the argument to "--merged" does not need to be a branch at
    all. It is really a commit, so you could specify a tag, an extended
    sha-1 expression, etc.

← back to recent threads