使用JGit获取存储库中的最新提交 [英] Get the latest commit in a repository with JGit

查看:197
本文介绍了使用JGit获取存储库中的最新提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道我可以通过使用获取提交元数据。

我想使用JGit获取最后一个提交元数据(最新的提交元数据) / b>

  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;
}
}


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

try (RevWalk walk = new RevWalk(repository)) {
    RevCommit commit = walk.parseCommit(repository.resolve(commitHash));
}

But how to get the latest commit hash?

Is there any other way to get the youngest by date RevCommit in a repository directly?

解决方案

Compare by dates of last commits in all branches. 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;
    }
}

这篇关于使用JGit获取存储库中的最新提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆