Skip to content
Advertisement

Get latest tag in branch with JGit

I am trying to get the JGit equivalent of the command

JavaScript

I have tried getting all the tags in the project and then finding the greatest tag in the branch via the BranchListCommand, but that is to slow.

I am stuck and I cannot find anything that help via my searches.

Does anyone have any idea how to do the intended result?

Advertisement

Answer

You can fetch all tags and then walk the commits on the branch and check for each commit if there is a tag for this ref.

i.e. fetch all tags via:

JavaScript

then fetch all commits of the branch via:

JavaScript

And then for each commit check if a tag exists at this ref.

When run on a fairly large repository on a branch with more than 60k commits, walking all the commits this way is done in 1.2 seconds.

JavaScript

An even larger branch takes 8 seconds when walking 380k commits.

JavaScript

I have added a ready-to-run snippet to the jgit-cookboot, see ListTagsOnBranch.java

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement