Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JGit: Retrieve tag associated with a git commit

Tags:

jgit

I want to use JGit API to retrieve the tags associated with a specific commit hash (if there is any)?

Please provide code snippet for the same.

like image 853
Kamal Avatar asked Sep 21 '11 14:09

Kamal


People also ask

How do I find my git tag list?

In order to list Git tags, you have to use the “git tag” command with no arguments. You can also execute “git tag” with the “-n” option in order to have an extensive description of your tag list. Optionally, you can choose to specify a tag pattern with the “-l” option followed by the tag pattern.

How do you check if a commit has a tag?

Check the documentation for git describe . It finds the nearest tag to a given commit (that is a tag which points to an ancestor of the commit) and describes that commit in terms of the tag. In newer versions, git describe --tags --abbrev=0 REV will be useful when you don't want the junk on the tag.

What is tag in git commit?

Tags are ref's that point to specific points in Git history. Tagging is generally used to capture a point in history that is used for a marked version release (i.e. v1. 0.1). A tag is like a branch that doesn't change. Unlike branches, tags, after being created, have no further history of commits.

Can you tag a commit?

In order to create a Git tag for a specific commit, use the “git tag” command with the tag name and the commit SHA for the tag to be created. If you want to create an annotated tag for a specific commit, you can use the “-a” and “-m” options we described in the previous section.


1 Answers

Git object model describes tag as an object containing information about specific object ie. commit (among other things) thus it's impossible in pure git to get information you want (commit object don't have information about related tags). This should be done "backwards", take tag object and then refer to specific commit.

So if you want get information about tags specified for particular commit you should iterate over them (tags) and choose appropriate.

List<RevTag> list = git.tagList().call();
ObjectId commitId = ObjectId.fromString("hash");
Collection<ObjectId> commits = new LinkedList<ObjectId>();
for (RevTag tag : list) {
    RevObject object = tag.getObject();
    if (object.getId().equals(commitId)) {;
        commits.add(object.getId());
    }
}
like image 191
Marcin Pietraszek Avatar answered Sep 18 '22 17:09

Marcin Pietraszek