Get Tagged Message 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 not 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 creating a 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 identifier of the returned object is never RevTag. Exception Message:

Object ... is not a tag . 

How to create a RevTag to parse Ref?

+4
source share
3 answers

You do not have to parse tags with RevWalk#parseTag(). This method is only for analyzing annotated tags.

, parseTag ( ?)

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

, , , , .., .

( , ) , , .

:

+4
// Create list with all tags from repository(annotated or lightweight):

     LogCommand log = git.log();
     List<Ref> call call = git.tagList().call();
     RevWalk walk = new RevWalk(git.getRepository());
     List<Ref> getTags = new ArrayList<Ref>();

        for (Ref ref: call)
        {
            Ref peeledRef = git.getRepository().peel(ref);
            if (peeledRef.getPeeledObjectId() != null)
            {
                log.add(peeledRef.getPeeledObjectId());
                getTags.add(peeledRef);
            }
            else
            {
                log.add(ref.getObjectId());
                getTags.add(ref);
            }
        }

//for each ref from getTags list, create a RevTag

      for (Ref obj: getTags)
        {

            String tagId = obj.getPeeledObjectId().getName();

       //use this equals if you want to get forr your commit its assciated tag 

            if (tagId.equals(your_commit.getName()))
            {

                RevTag tag = walk.parseTag(obj.getObjectId());
                System.out.println("Commit " + your_commit.getName() + " has tag " + tag.getTagName() + " with message tag " + tag.getFullMessage());

            }
        }
0

: -

    private static void ReadTagFromName(Repository repository, String tagName) throws IOException {
        try (RevWalk walk = new RevWalk(repository)) {

            Ref annotatedTag = repository.findRef(tagName);
            RevObject any = walk.parseAny(annotatedTag.getObjectId());
            String message;
            while (any != null) {
                if (any instanceof RevCommit) {
                    RevCommit rc = (RevCommit) any;
                    message = rc.getFullMessage();
                    System.out.println(String.format("[%s][%s]", tagName, message.substring(0, message.length() - 1)));
                } else if (any instanceof RevTag) {
                    RevTag tagObject = (RevTag) any;
                    message = tagObject.getFullMessage();
                    System.out.println(String.format("[%s][%s]", tagName, message.substring(0, message.length() - 1)));
                }
                any = walk.next();
            }
            walk.dispose();
        }
    }
0

Source: https://habr.com/ru/post/1584868/


All Articles