如何使用 JGit 从 SHA1 ID 字符串中获取 RevCommit 或 ObjectId? [英] How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?

查看:90
本文介绍了如何使用 JGit 从 SHA1 ID 字符串中获取 RevCommit 或 ObjectId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是这个问题的反面:JGit 如何从 RevCommit 获取 SHA1?.

This question is the inverse of this question: JGit how do i get the SHA1 from a RevCommit?.

如果我以字符串形式获得特定提交的 SHA1 ID,我如何在 JGit 中获取 ObjectId 或关联的 RevCommit?

If I am given the SHA1 ID of a particular commit as a string, how can I obtain the ObjectId or associated RevCommit in JGit?

这是一个可能的答案,它遍历所有RevCommit:

Here is a possible answer, which iterates through all RevCommits:

RevCommit findCommit(String SHAId)
{
    Iterable<RevCommit> commits = git_.log().call();    
    for (RevCommit commit: commits)
    {
        if (commit.getName().equals(SHAId))
            return commit;
    }    
    return null;
}

还有什么比上面的这个实现更好的吗?

Is there anything better than this implementation above?

推荐答案

首先将字符串转换为 ObjectId 然后让 RevWalk 查看它可能更容易

It is probably easier to first convert the string into an ObjectId and then have the RevWalk look it up.

ObjectId commitId = ObjectId.fromString("ab434...");
try (RevWalk revWalk = new RevWalk(repository)) {
  RevCommit commit = revWalk.parseCommit(commitId);
}

这篇关于如何使用 JGit 从 SHA1 ID 字符串中获取 RevCommit 或 ObjectId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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