On a GitHub project, when we go to any branches page, we can see graphs which describes commit ahead/behind numbers of a branch w.r.t. master.
How can we determine those ahead behind numbers using JGit?
I used BranchTrackingStatus
class for this, but I'm getting BranchTrackingStatus
object always null.
Here is the code which I used
private static Lis<Integer> getCounts(Repository repository, String branchName) throws IOException{
BranchTrackingStatus trackingStatus = BranchTrackingStatus.of(repository, branchName);
List<Integer> counts = new ArrayList<Integer>();
if (trackingStatus != null){
counts.add(trackingStatus.getAheadCount());
counts.add(trackingStatus.getBehindCount());
} else {
counts.add(0);
counts.add(0);
}
return counts;
}
public void show(String repoName, String baseBranchName) throws IOException, GitAPIException{
Repository repository = repoManager.openRepository(new Project.NameKey(repoName));
List<Ref> call = new Git(repository).branchList().call();
for (Ref ref : call) {
List<Integer> counts = getCounts(repository, ref.getName());
System.out.println("Commits ahead : "+counts.get(0));
System.out.println("Commits behind : "+counts.get(1));
}
}