eclipsetagsmessagejgit

Get message of tags using JGit


I need for each commit, to get the name and message of the associated tag.

I managed to get the tag name associated with my commit . But I can't get the message. I tried like this:

String nameTag = "";

List<Ref> call = new Git(git.getRepository()).tagList().call(); // get all tags from repository

for (Ref ref: call) {
    if ((ref.getObjectId().getName()).equals(commit.getName())) {
        Map<ObjectId, String> names = git.nameRev().add(ref.getObjectId()).addPrefix("refs/tags/").call();
        nameTag = names.get(ref.getObjectId());
        System.out.println("Commit " + commit.getName() + "has tag" + nameTag);
    }
}

I tried to create RevTag for each ref found:

AnyObjectId obj = ref.getObjectId();
if(obj instanceof RevTag) {
    RevTag tag = walk.parseTag(obj);
    System.out.println(tag.getFullMessage()); 
}

But the returned object id is never RevTag. Exception message is:

Object ... is not a tag . 

How can I create a RevTag parsing a Ref?


Solution

  • You don't neccessarily have to parse tags with RevWalk#parseTag(). This method is only to parse annotated tags.

    To tell one from the other you can even use parseTag (or is there any better way?)

    RevTag tag;
    try {
      tag = revWalk.parseTag(ref.getObjectId());
      // ref points to an annotated tag
    } catch(IncorrectObjectTypeException notAnAnnotatedTag) {
      // ref is a lightweight (aka unannotated) tag
    }
    

    An annotated tag points to a commit object and thus has an author, date, message etc. and the commit object in turn points to the tagged commit.

    A lightweight tag directly references the tagged commit (much like a branch, but read-only) and hence cannot have a message.

    More about annotated vs. lighweight tags: