Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitPython: Get current tag (detached head)

I use the library gitpython

If the local git is on a checked out tag, I want to get the name of the tag.

repo=git.Repo(repo_dir)
repo.tag # --> tags. But which is the current?

On the command line, the git tool knows it. Example

user@host> git status
HEAD detached at release/1.2.3

I want to get the string "release/1.2.3" via gitpython.

like image 923
guettli Avatar asked Sep 11 '15 12:09

guettli


2 Answers

You can iterate through tags and compare each tag commit with current head commit:

next((tag for tag in repo.tags if tag.commit == repo.head.commit), None)
like image 146
AndreyT Avatar answered Nov 18 '22 05:11

AndreyT


It looks like you might be able to get what you want with GitCmd calling describe.

g = Git(git_dir)
rval = g.describe()

I don't see any way to directly access this information.

like image 6
Wolf Avatar answered Nov 18 '22 05:11

Wolf