JGit: Retrieve tag associated with a git commit

為{幸葍}努か 提交于 2019-12-04 12:15:53

问题


I want to use JGit API to retrieve the tags associated with a specific commit hash (if there is any)?

Please provide code snippet for the same.


回答1:


Git object model describes tag as an object containing information about specific object ie. commit (among other things) thus it's impossible in pure git to get information you want (commit object don't have information about related tags). This should be done "backwards", take tag object and then refer to specific commit.

So if you want get information about tags specified for particular commit you should iterate over them (tags) and choose appropriate.

List<RevTag> list = git.tagList().call();
ObjectId commitId = ObjectId.fromString("hash");
Collection<ObjectId> commits = new LinkedList<ObjectId>();
for (RevTag tag : list) {
    RevObject object = tag.getObject();
    if (object.getId().equals(commitId)) {;
        commits.add(object.getId());
    }
}



回答2:


If you know that there is exactly one tag for your commit, you could use describe, in more recent versions of JGit (~ November 2013).

Git.wrap(repository).describe().setTarget(ObjectId.fromString("hash")).call()

You could parse the result, to see if a tag exists, but if there can be multiple tags, you should go with Marcins solution.



来源:https://stackoverflow.com/questions/7501646/jgit-retrieve-tag-associated-with-a-git-commit

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