How is a merge-base done in JGit?

I’m looking at the JGit Documentation, currently at version 3.5.1.201410131835-r, and I cannot find the equivalent of git-merge-base.

I would like to determine if a branch is up-to-date, behind, ahead, or diverged, as shown in git: check if pull needed.

  • Bitbucket: Conflict in remote repo, can't figure out how to resolve
  • How to mass-resolve Git Conflicts with only File Mode changes?
  • How to remove \r when pushing commits to Gerrit from Windows?
  • How to merge remote master to local branch
  • Contributing using git
  • How to use Ansible git module pull a branch with local changes?
  • What is the most concise way in JGit to find as good common ancestors as possible for a merge?

  • git non-fast-forward error
  • project name doesn't change on the remote repository after renaming from eclipse
  • Differences between these methods of importing existing Git repositories into GitHub?
  • How to get past the “Can't pull because it has unmerged file” error message?
  • Git connection time out trying to clone via proxy
  • In a git merge conflict, what are the BACKUP, BASE, LOCAL, and REMOTE files that are generated?
  • 3 Solutions collect form web for “How is a merge-base done in JGit?”

    You can use RevFilter.MERGE_BASE for that:

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

    Also note that there is BranchTrackingStatus if all you are interested in is ahead/behind count of a branch compared to its remote-tracking branch.

    Here it is with JGit using 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;
        }
    }
    

    jgit has this:

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

    take a try

    Git Baby is a git and github fan, let's start git clone.