下面你可以找到一個的Java 8個流API解決方案:
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());
});
它遍歷所有分支機構,並挑選最新提交的這些分支,然後將其按日期後裔秩序和選秀權排序提交的名單第一個。如果存在,它打印到控制檯輸出是這樣的:
Wed Aug 30 09:49:42 CEST 2017: test file added (Szymon Stepniak)
當然的行爲對最後提交的存在是示範性的,它可以與任何其他業務邏輯很容易地更換。我希望它有幫助。
你能澄清一下_latest_的意思嗎?你是說按日期最小的?來自所有分支? –
你的意思是'HEAD'最終指向的提交嗎? –
是的確切地說,從所有分支按日期採集。爲所有分支尋找他是否很重要,因爲我認爲無論分支機構如何,我都可以相對於回購來尋找他? –