Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all tags of a repository with JGit

Tags:

java

tags

jgit

I want to get a list of all tags of a repository together with the tagger and the commit-IDs with JGit.

First I tried the Git.tagList().call() command and parse the tags:

objectIdOfTag = oneResultOfTheTagList.getObjectId(); 
// or should I call getPeeledObjectId() here?

RevWalk walk = new RevWalk(repository);
RevTag tag = walk.parseTag(objectIdOfTag);

This works for my repository, but I am unsure if this is the correct way: Is it correct to call Ref.getObjectId() or should I call Ref.getPeeledObjectId()? (What is a "peeled ObjectId"?)

Is there a difference between lightweight and annotated tags when retrieving the tag list with JGit?

like image 988
Sonson123 Avatar asked Feb 16 '13 18:02

Sonson123


1 Answers

getObjectId is correct here. In the case of an annotated tag, you want to get the ID of the annotated tag object, not the ID of the commit the tag finally points to. See also the Javadoc of Ref.

To parse the tag, you will have to handle both the lightweight and annotated cases:

RevObject object = walk.parseAny(objectIdOfTag);
if (object instanceof RevTag) {
    // annotated
} else if (object instanceof RevCommit) {
    // lightweight
} else {
    // invalid
}
like image 88
robinst Avatar answered Nov 16 '22 00:11

robinst