使用 JGit 获取标签信息

Get message of tags using JGit

我需要为每个提交获取关联标签的名称和消息。

我设法获得了与我的提交关联的标签名称。但我无法收到消息。我这样试过:

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

我尝试为找到的每个参考创建 RevTag:

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

但返回的对象 ID 永远不是 RevTag。异常消息是:

Object ... is not a tag . 

如何创建解析 Ref 的 RevTag?

您不必使用 RevWalk#parseTag() 解析标签。此方法仅用于解析带注释的标签。

你甚至可以使用 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
}

带注释的标签指向提交对象,因此具有作者、日期、消息等,而提交对象又指向带标签的提交。

轻量级标记直接引用标记的提交(很像分支,但只读)因此不能有消息。

关于带注释的标签与轻量级标签的更多信息:

  • Why should I care about lightweight vs. annotated tags?
  • What is the difference between an annotated and unannotated tag?
// 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());

            }
        }

要从标签中获取消息,我使用以下代码:-

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