How is merging done in JGit?

I am looking at the JGit Documentation , currently on 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, in front or diverging, as shown in git: check if pull is required .

What is the most concise way in JGit to find the most common ancestors to merge?

+5
source share
3 answers

You can use RevFilter.MERGE_BASE for this:

 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 a BranchTrackingStatus if everything that interests you is in front of / behind the branch counting compared to its remote branch tracking.

+8
source

Here using 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; } } 
+1
source

jgit has the following meaning:

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

try

0
source

Source: https://habr.com/ru/post/1204953/


All Articles