2014-10-17 10 views

risposta

8

È possibile utilizzare RevFilter.MERGE_BASE per questo:

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

Si noti inoltre che non v'è BranchTrackingStatus se tutto quello che ci interessa è avanti/dietro conteggio di un ramo rispetto alla sua filiale remota-tracking.

+0

Grazie, che è perfetto! –

+0

Esiste un equivalente all'aggiunta di "--fork-point" perché non ottengo gli stessi risultati di "git merge-base --fork-point"? –

+0

@PeterKahn: Funziona ?: https://gist.github.com/robinst/997da20d09a82d85a3e9 (Sono abbastanza sicuro che può essere fatto in modo più efficiente, quindi usalo con cura) – robinst

1

Qui è con JGit utilizzando 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; 
    } 
} 
0

jgit ha questo:

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

prendere una prova