使用 JGit 获取标签消息 [英] Get message of tags using JGit

查看:136
本文介绍了使用 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);
    }
}

我尝试为找到的每个引用创建 RevTag:

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

但返回的对象 id 永远不会是 RevTag.异常消息是:

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

Object ... is not a tag . 

如何创建解析参考的 RevTag?

How can I create a RevTag parsing a Ref?

推荐答案

您不必使用 RevWalk#parseTag() 来解析标签.此方法仅用于解析注解标签.

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

你甚至可以使用 parseTag(或者有什么更好的方法?)

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:

这篇关于使用 JGit 获取标签消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆