我正在尝试使用jGit克隆远程现有的repo,下面是以下指南:
https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/CloneRemoteRepository.java我使用CFML为我的例子:
Git = createObject( 'java', 'org.eclipse.jgit.api.Git' );
localPath = createObject( 'java', 'java.io.File' ).init( expandPath( 'temp' ) );
result = Git.cloneRepository()
.setURI( 'https://github.com/github/testrepo.git' )
.setDirectory( localPath )
.call();
result.close();
克隆工作得很好,但是直到我停止Java进程,temp.gitobjectspack
中的"包"文件上的文件锁才被释放。
然后我也注意到API文档似乎有点模棱两可关于结果的.close()
方法的行为。http://download.eclipse.org/jgit/site/4.0.1.201506240215-r/apidocs/org/eclipse/jgit/lib/Repository.html close ()
减少使用计数,可能会关闭资源。
也许吗?这是什么意思?我需要做些什么才能"放弃任何底层资源",如.close()
方法帮助实现的AutoCloseable
接口中指定的?
org.eclipse.jgit.api.Git
上使用静态方法克隆一个新的repo。 就在我点击提交的时候,经过几天的摸索,我偶然发现了我认为是答案的东西。
这个食谱示例只在cloneRepository()
的call()
方法(一个Git
实例)的结果上调用.close()
方法。API文档声明该方法还应该调用底层Repository
实例的.close
方法:
http://download.eclipse.org/jgit/site/4.0.1.201506240215-r/apidocs/org/eclipse/jgit/api/Git.html close ()
如果存储库是由该类中的静态工厂方法打开的,则该方法在底层存储库实例上调用repository .close()。
然而,我发现如果我自己获得Repository
实例并调用它的.close()
方法,所有的文件系统锁都会被释放。我认为这是我所遵循的JGit烹饪书参考中的遗漏,并将提交issue/pull。
下面是工作的CFML代码。注意下面的两个.close()
调用。
Git = createObject( 'java', 'org.eclipse.jgit.api.Git' );
localPath = createObject( 'java', 'java.io.File' ).init( expandPath( 'temp' ) );
result = Git.cloneRepository()
.setURI( 'https://github.com/github/testrepo.git' )
.setDirectory( localPath )
.call();
result.getRepository().close();
result.close();
我也很纠结。下面是我解决这个问题的方法。
CloneCommand cloneCommand = Git.cloneRepository();
URIish urIish = new URIish(getVersionControlPath().toString());
cloneCommand.setURI(urIish.toString());
Date date = new Date();
String testgit = "testgit_" + date.getTime();
cloneCommand.setDirectory(getVersionControlPath().getParent().resolve(testgit).toFile());
Git call = cloneCommand.call();
call.close();