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.

  • applying email patch with git
  • Bundler: Gem Installation Error “The term 'bundle' is not recognised”
  • error: git was not found - installing laravel with composer windows
  • git on Windows: fatal: protocol error: bad line character: @-
  • After merge of release branch, why is master 1 commit ahead of develop?
  • Can you change .git folder location
  • What is the most concise way in JGit to find as good common ancestors as possible for a merge?

  • How to automatically invoke a script before a git add?
  • Additional HipChat Heroku Deploy Hook Options
  • Should I generate new SSH key pair for every workstation or reuse a single one?
  • Creating a new root directory in an Eclipse project under version control
  • Git shows a line has been removed and added although nothing changed?
  • Is any way to git shallow clone in SourceTree
  • 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.