BranchTrackingStatus.of()
assumes that branchName
denotes a local branch, either by its full name (e.g. refs/heads/master
) or its short name (e.g. master
). It returns null
if the given branchName
cannot be found or the tracking branch is not configured or does not exist.
To compare two arbitrary branches, you could adopt the BranchTrackingStatus
code like so:
void calculateDivergence(Ref local, Ref tracking) throws IOException {
try (RevWalk walk = new RevWalk(repository)) {
RevCommit localCommit = walk.parseCommit(local.getObjectId());
RevCommit trackingCommit = walk.parseCommit(tracking.getObjectId());
walk.setRevFilter(RevFilter.MERGE_BASE);
walk.markStart(localCommit);
walk.markStart(trackingCommit);
RevCommit mergeBase = walk.next();
walk.reset();
walk.setRevFilter(RevFilter.ALL);
aheadCount = RevWalkUtils.count(walk, localCommit, mergeBase);
behindCount = RevWalkUtils.count(walk, trackingCommit, mergeBase);
}
}