使用JGit获取存储库中的最新提交 [英] Get the latest commit in a repository with JGit
问题描述
我知道我可以通过使用 我想使用JGit获取最后一个提交元数据(最新的提交元数据) / b> 但是如何获得最新的提交哈希? 有没有其他的方式可以直接在版本库中获取最新的 比较所有分支中最后一次提交的日期。 I want to get the last commit metadata (the youngest one by date) in a repository using JGit. I know that I can get the commit metadata using But how to get the latest commit hash? Is there any other way to get the youngest by date Compare by dates of last commits in all branches.
这篇关于使用JGit获取存储库中的最新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
try(RevWalk walk = new RevWalk(repository)){
RevCommit commit = walk .parseCommit(repository.resolve(commitHash));
}
RevCommit
?
ListMode.ALL
可以更改为 ListMode.REMOTE
来仅比较远程分支。
或...流利的setter .setListMode(whatever)
可以省略从本地存储库中读取。
RevCommit youngestCommit = null;
列表< Ref> branches = new Git(repository).branchList()。setListMode(ListMode.ALL).call();
try(RevWalk walk = new RevWalk(git.getRepository())){
for(Ref branch:branches){
RevCommit commit = walk.parseCommit(branch.getObjectId());
if(commit.getAuthorIdent()。getWhen()。compareTo(
youngestCommit.getAuthorIdent()。getWhen())> 0)
youngestCommit = commit;
}
}
try (RevWalk walk = new RevWalk(repository)) {
RevCommit commit = walk.parseCommit(repository.resolve(commitHash));
}
RevCommit
in a repository directly?ListMode.ALL
can be changed to ListMode.REMOTE
to compare only remote branches.
Or... the fluent setter .setListMode(whatever)
can be omitted to read from the local repository. RevCommit youngestCommit = null;
List<Ref> branches = new Git(repository).branchList().setListMode(ListMode.ALL).call();
try(RevWalk walk = new RevWalk(git.getRepository())) {
for(Ref branch : branches) {
RevCommit commit = walk.parseCommit(branch.getObjectId());
if(commit.getAuthorIdent().getWhen().compareTo(
youngestCommit.getAuthorIdent().getWhen()) > 0)
youngestCommit = commit;
}
}