如何在JGit中完成合并?

时间:2014-10-17 22:49:58

标签: java git jgit

我正在查看JGit Documentation,目前版本为 3.5.1.201410131835-r ,我找不到等效的git-merge-base

我想确定一个分支是最新的,后面的,前面的还是分歧的,如git: check if pull needed所示。

JGit中最简洁的方法是找到合并的最佳共同祖先?

3 个答案:

答案 0 :(得分:8)

您可以使用RevFilter.MERGE_BASE

RevWalk walk = new RevWalk(repository);
walk.setRevFilter(RevFilter.MERGE_BASE);
walk.markStart(commit1);
walk.markStart(commit2);
RevCommit mergeBase = walk.next();

另请注意,如果您感兴趣的是与远程跟踪分支相比,分支的前后数是BranchTrackingStatus

答案 1 :(得分:1)

这是JGit使用BranchTrackingStatus:

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

答案 2 :(得分:0)

jgit有这个:

RevCommit org.eclipse.jgit.merge.Merger.getBaseCommit(RevCommit a, RevCommit b) throws IncorrectObjectTypeException, IOException

试一试