Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JGit从Git检索提交消息日志_Java_Git_Jgit - Fatal编程技术网

Java 使用JGit从Git检索提交消息日志

Java 使用JGit从Git检索提交消息日志,java,git,jgit,Java,Git,Jgit,我只想从Git存储库中检索commitlog,它包含您在特定repository上所做的所有提交的消息。我找到了一些实现这一点的代码片段,并以一个异常结束 试试看{ FileRepositoryBuilder=新建FileRepositoryBuilder(); Repository repo=builder.setGitDir(新文件(“https://github.com/name/repository.git)),readEnvironment().findGitDir().build()

我只想从Git存储库中检索commitlog,它包含您在特定repository上所做的所有提交的消息。我找到了一些实现这一点的代码片段,并以一个异常结束

试试看{
FileRepositoryBuilder=新建FileRepositoryBuilder();
Repository repo=builder.setGitDir(新文件(“https://github.com/name/repository.git)),readEnvironment().findGitDir().build();
RevWalk walk=新的RevWalk(回购);
ObjectId head=repo.resolve(Constants.head);
RevCommit=walk.parseCommit(head);
吉特=新吉特(回购);
Iterable gitLog=git.log().call();
Iterator it=gitLog.Iterator();
while(it.hasNext())
{
RevCommit logMessage=it.next();
System.out.println(logMessage.getFullMessage());
}
}
捕获(例外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

If
https://github.com/name/repository.git
是要从中获取日志的存储库的URL,您必须首先克隆它:

CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setDirectory( new File( "/path/to/local/repo" ) );
cloneCommand.setURI( "https://github.com/name/repository.git" );
Git git = cloneCommand.call();
...
git.getRepository().close();
这将在
/path/to/local/repo
中创建远程存储库的本地克隆。请注意,
repo
目录在调用cloneCommand之前必须不存在或为空。然后可以使用
git.log()
检查此存储库


确保在使用完存储库后将其关闭,以避免泄漏文件句柄。

这是正确的,一段代码将执行上述操作

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());
}
FileRepositoryBuilder=newfilerepositorybuilder();
Repository repo=builder.setGitDir(新文件(“localrepository”+“\\.git”)).setMustExist(true.build();
吉特=新吉特(回购);
Iterable log=git.log().call();
for(Iterator Iterator=log.Iterator();Iterator.hasNext();){
RevCommit rev=iterator.next();
添加(rev.GetFullMessages());
}

Herrmann。。这看起来不错,但我需要的是我已经有了本地源代码,这就是我添加到git repo中的源代码。但是现在我想显示我的提交消息,不想再次克隆我已有的目录。如何做到这一点?您可以像这样打开现有的本地存储库:
repo=builder.setGitDir(新文件(“/path/to/local/repo/.git”)。setMustExist(true.build()谢谢。。非常好…我还列出了我的提交日志。