Get message of tags using JGit

烈酒焚心 提交于 2021-02-19 02:23:47

问题


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?


回答1:


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:

  • Why should I care about lightweight vs. annotated tags?
  • What is the difference between an annotated and unannotated tag?



回答2:


// 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());

            }
        }



回答3:


To obtain the message from a tag I use this code :-

    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();
        }
    }


来源:https://stackoverflow.com/questions/29892210/get-message-of-tags-using-jgit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!