Below you can find a Java 8 Stream API solution:
final List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
final RevWalk revWalk = new RevWalk(git.getRepository());
branches.stream()
.map(branch -> {
try {
return revWalk.parseCommit(branch.getObjectId());
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.sorted(Comparator.comparing((RevCommit commit) -> commit.getAuthorIdent().getWhen()).reversed())
.findFirst()
.ifPresent(commit -> {
System.out.printf("%s: %s (%s)%n", commit.getAuthorIdent().getWhen(), commit.getShortMessage(), commit.getAuthorIdent().getName());
});
It iterates over all branches and picks recent commits in those branches, then it sorts list of commits by date in descendant order and picks the first one. If it exists it prints to console output something like this:
Wed Aug 30 09:49:42 CEST 2017: test file added (Szymon Stepniak)
Of course the behavior on last commit existence is exemplary and it can be easily replaced with any additional business logic. I hope it helps.