我正在查看 JGit 文档,当前版本为 3.5.1.201410131835-r,我找不到与 git-merge-base 等价的版本。
我想确定一个分支是最新的,落后的,领先的,还是发散的,如git: check if pull needed.
JGit 中为合并找到尽可能好的共同祖先的最简洁的方法是什么?
您可以为此使用
RevFilter.MERGE_BASE
:
RevWalk walk = new RevWalk(repository);
walk.setRevFilter(RevFilter.MERGE_BASE);
walk.markStart(commit1);
walk.markStart(commit2);
RevCommit mergeBase = walk.next();
另请注意,如果您感兴趣的只是分支与其远程跟踪分支相比的超前/落后计数,则还有 BranchTrackingStatus。
Per https://www.eclipse.org/forums/index.php/t/1091725/,您可能需要使用
walk.parseCommit()
重新解析提交。
这是使用 BranchTrackingStatus 的 JGit:
public enum TrackingStatus {
SAME, BEHIND, AHEAD, DIVERGED
}
public TrackingStatus getTrackingStatus() throws IOException, GitAPIException {
Repository userRepo = new FileRepository(<path_to_.git_file>);
Git git = new Git(userRepo);
git.fetch().call();
BranchTrackingStatus bts = BranchTrackingStatus.of(git.getRepository(),
git.getRepository().getBranch());
int aheadCount = bts.getAheadCount();
int behindCount = bts.getBehindCount();
if (aheadCount == 0 && behindCount == 0) {
return TrackingStatus.SAME;
} else if (aheadCount > 0 && behindCount == 0) {
return TrackingStatus.AHEAD;
} else if (aheadCount == 0 && behindCount > 0) {
return TrackingStatus.BEHIND;
} else {
return TrackingStatus.DIVERGED;
}
}
jgit 有这个:
RevCommit org.eclipse.jgit.merge.Merger.getBaseCommit(RevCommit a, RevCommit b) throws IncorrectObjectTypeException, IOException
试试看
这个方法是
protected
,不能直接用:
protected RevCommit getBaseCommit(RevCommit a, RevCommit b) throws IncorrectObjectTypeException, IOException