Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tags of a commit

Given an object of GitPython Commit, how can I get the tags related to this commit?

I'd enjoy having something like:

next(repo.iter_commits()).tags
like image 281
Uko Avatar asked Dec 02 '25 22:12

Uko


1 Answers

The problem is that tags point to commits, not the other way around. To get this information would require a linear scan of all tags to find out which ones point to the given commit. You could probably write something yourself that would do it. The following would get you a commit-to-tags dictionary:

tagmap = {}
for t in repo.tags():
  tagmap.setdefault(r.commit(t), []).append(t)

And for a given commit, you can get any tags associated with it from:

tags = tagmap[repo.commit(commit_id)]
like image 170
larsks Avatar answered Dec 05 '25 12:12

larsks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!