Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitPython tags sorted

I am trying to get the latest tag in the repo using GitPython lib. Usually I was doing it this way:

repo = Repo(project_root)
last_tag = str(repo.tags[-1])

But once version 1.10 was released, I am always getting 1.9 ;( I know it's related to output git tag -l being listing same order. So it will be 1.1, 1.10, 1.2, ..., 1.9

The question is how to get the latest tag using GitPython? (I am aware of git tag -l | sort -V and I know how to solve this not using the repo object. But maybe someone knows what am I missing in getting sorted tags list in this lib)

Custom sorting function is always an option too, but still, I wonder if there a way to do it with GitPython?

like image 977
Ilia Shakitko Avatar asked Jul 31 '14 10:07

Ilia Shakitko


People also ask

How to list git tags?

Listing the available tags in Git is straightforward. Just type git tag (with optional -l or --list ). You can also search for tags that match a particular pattern. The command finds the most recent tag that is reachable from a commit.

How to check git tag details?

View Git tag details (Git) To use Git to view details about Git tags in a local repo, run one of the following commands: git tag to view a list of Git tag names. git show to view information about a specific Git tag. git ls-remote to view information about Git tags in a CodeCommit repository.

How to check tags in git branch?

In order to checkout a Git tag, use the “git checkout” command and specify the tagname as well as the branch to be checked out. Note that you will have to make sure that you have the latest tag list from your remote repository.


3 Answers

The IterableList object returned by repo.tags in GitPython extends the list Python class, which means that you can sort it the way you want. To get the latest tag created, you can simply do:

import git
repo = git.Repo('path/to/repo')
tags = sorted(repo.tags, key=lambda t: t.commit.committed_datetime)
latest_tag = tags[-1]
like image 149
robotic_chaos Avatar answered Oct 22 '22 15:10

robotic_chaos


I have just had a look and found the code responsible for the sorting. Therefore I see no other way but to reverse the sorting order yourself, like

reversed(repo.tags)

If preferred, you can also use the underlying git command, and parse the result yourself, such as in this example:

repo.git.tag(l=True) # equals git tag -l

That way, whatever git tag can do, you can do (which could be interesting for listing tags in order of traversal, starting from a given commit).

like image 4
Byron Avatar answered Oct 22 '22 15:10

Byron


The above will work if there are different commits associated with the tags (which is generally the case). However a more accurate way would be as follows which picks up the tag date:

import git
repo = git.Repo('path/to/repo')
tags = sorted(repo.tags, key=lambda t: t.tag.tagged_date)
latest_tag = tags[-1]
like image 4
developerinlondon Avatar answered Oct 22 '22 14:10

developerinlondon