如何使用JGit查找分支的第一次提交?

问题描述 投票:1回答:1

我想使用Eclipse的JGit库查找特定分支的第一次提交。例如,如果我有

master -- a -- c -- d -- e
          \
  feature  b -- f -- g -- h
            \
    another  j -- k -- l

// findBranchRoot() is the magic method we want to create
repository.resolve("feature").findBranchRoot().getId(); // this would return b
repository.resolve("another").findBranchRoot().getId(); // this would return j

有人知道该怎么做吗?

java git version-control branch jgit
1个回答
0
投票

如果first commit表示分支的最新/最新提交,则可以使用ObjectId返回的Repository::resolve来获得带有RevCommitRevWalk(JGit的提交对象表示) ]:

ObjectId commitId = repository.resolve("refs/heads/feature");
try(RevWalk revWalk = new RevWalk(repository)) {
  RevCommit commit = revWalk.parseCommit(commitId);
}

我建议将完全合格的参考传递给resolve(如示例中所示)。否则,您可能会得到AmbiguousObjectException或该方法返回具有相同短名的注释或标签。

© www.soinside.com 2019 - 2024. All rights reserved.