使用JGit从Git检索提交消息日志 [英] Retrieving Commit Message Log from Git Using JGit
本文介绍了使用JGit从Git检索提交消息日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想从Git存储库中检索commitlog,该存储库包含您在特定repopsitory上完成的所有提交的消息。我找到了一些用于实现此目的的代码片段,并以异常结束。
I just want to retrieve the commitlog from Git repository that has messages for all the commit you've done on a specific repopsitory. I had found some code snippets for achieve this and ends with an exception.
try {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(new File("https://github.com/name/repository.git")).readEnvironment().findGitDir().build();
RevWalk walk =new RevWalk(repo);
ObjectId head = repo.resolve(Constants.HEAD);
RevCommit commit =walk.parseCommit(head);
Git git =new Git(repo);
Iterable<RevCommit> gitLog = git.log().call();
Iterator<RevCommit> it = gitLog.iterator();
while(it.hasNext())
{
RevCommit logMessage = it.next();
System.out.println(logMessage.getFullMessage());
}
}
catch(Exception e) {
e.printStackTrace();
}
然而它给了我:
org.eclipse.jgit.api.errors.NoHeadException: No HEAD exists and no explicit starting revision was specified exception.
如何摆脱这种情况?我正在使用org.eclipse.jgit JAR版本2.0.0.201206130900-r
How do I get rid of this? I am using org.eclipse.jgit JAR version 2.0.0.201206130900-r
推荐答案
这是正确的一段代码会做的上面..
This is correct of piece of code will do the above..
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(new File("localrepositary"+"\\.git")).setMustExist(true).build();
Git git = new Git(repo);
Iterable<RevCommit> log = git.log().call();
for (Iterator<RevCommit> iterator = log.iterator(); iterator.hasNext();) {
RevCommit rev = iterator.next();
logMessages.add(rev.getFullMessage());
}
这篇关于使用JGit从Git检索提交消息日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文