如何在 JGit 中完成合并基础?

问题描述 投票:0回答:4

我正在查看 JGit 文档,当前版本为 3.5.1.201410131835-r,我找不到与 git-merge-base 等价的版本。

我想确定一个分支是最新的,落后的,领先的,还是发散的,如git: check if pull needed.

JGit 中为合并找到尽可能好的共同祖先的最简洁的方法是什么?

java git jgit
4个回答
12
投票

您可以为此使用

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()
重新解析提交。


1
投票

这是使用 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;
    }
}

0
投票

jgit 有这个:

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

试试看


0
投票

这个方法是

protected
,不能直接用:

protected RevCommit getBaseCommit(RevCommit a, RevCommit b) throws IncorrectObjectTypeException, IOException

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