如何通过JGit克隆repo后释放文件系统锁

Bra*_*ood 8 java filehandle jgit

我正在使用jGit克隆远程现有仓库,遵循指南:

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();
Run Code Online (Sandbox Code Playgroud)

克隆工作得很好,但在temp\.git\objects\pack我停止Java进程之前,文件锁不会在"pack"文件中发布.

然后我也注意到API文档对于结果.close()方法的行为似乎有些过于谨慎:http: //download.eclipse.org/jgit/site/4.0.1.201506240215-r/apidocs/org/eclipse/jgit/ LIB/Repository.html#close()方法

减少使用次数,并可能关闭资源.

也许?那是什么意思?为了"放弃任何底层资源",我需要做什么,如AutoCloseable.close()方法帮助实现的接口中指定的那样?

在SO上有几个类似的问题,但没有一个涉及使用静态方法org.eclipse.jgit.api.Git来克隆新的repo.

Bra*_*ood 7

因此,在经过几天的戳戳之后,我点击了提交,我偶然发现了我认为的答案.

食谱示例仅调用.close()上的结果的方法cloneRepository()call()方法(A Git实例).API文档声明该方法还应该调用.close底层Repository实例的方法:

http://download.eclipse.org/jgit/site/4.0.1.201506240215-r/apidocs/org/eclipse/jgit/api/Git.html#close()

如果存储库是由此类中的静态工厂方法打开的,则此方法将在底层存储库实例上调用Repository.close().

但是,我发现如果我Repository自己获取实例并调用其.close()方法,则会释放所有文件系统锁.我认为这是我正在遵循的JGit食谱参考中的遗漏,并将提交问题/拉.

这是工作的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();
Run Code Online (Sandbox Code Playgroud)