我只想从 Git 存储库检索提交日志,其中包含您在特定存储库上完成的所有提交的消息。我找到了一些实现此目的的代码片段,并以异常结束。
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
这是执行上述操作的正确代码。
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());
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)