Logo Questions Linux Laravel Mysql Ubuntu Git Menu

GitPython: getting the latest tag of a remote repo

I'd like to be able to check what's the latest tag for a given repo (I'm using CPython here as an example).

The following works:

g = git.cmd.Git()
blob = g.ls_remote('https://github.com/python/cpython', sort='-v:refname', tags=True)
blob.split('\n')[0].split('/')[-1]  # 'v3.9.0a6'

because the blob looks something like this:

'bc1c8af8ef2563802767404c78c8ec6d6a967897\trefs/tags/v3.9.0a6\ndcd4 (...)'

But: is there a cleaner way of getting the latest tag?

Preferably using gitpython but any other package is also OK.

like image 587
ponadto Avatar asked Nov 06 '22 08:11


1 Answers

git ls-remote is a stable and documented command of the git suite, so parsing its output is already a valid and stable way to get the info you need.
(I dug out a release note which specifies is was rewritten in C in version 1.5.4 (released somewhere in 2007-2008), which implies it already existed as a script in previous versions)

AFAIK, there isn't yet a python wrapper around it to find the existence or value of said reference through python methods, but the implementation of said methods would look a lot like what you wrote yourself.

like image 108
LeGEC Avatar answered Nov 14 '22 00:11