无法删除JGIT创建的存储库文件夹

v64noz0r  于 2023-04-19  发布在  Git
关注(0)|答案(2)|浏览(154)

我无法删除包含使用jgit创建的克隆存储库的文件夹。我尝试了各种实用程序删除方法,但都不起作用。使用FileUtils.deleteDirectory(file)时抛出的错误如下
java.nio.file.AccessDeniedException: \repositories\Test\Test\.git\objects\pack\pack-21cdd19e90e9e6bd2e8d295ea04201969594fe96.idx
尝试了以下操作,
1.我已经看到了一系列其他的答案,例如[1]和[2]。他们都关心这样的情况,即他们首先使用jgit创建了一个存储库,然后立即将其删除。在我目前的场景中,我想检查是否已经在给定路径下克隆了一个存储库,如果存在则删除。完成此任务后,我想在同一路径上使用jgit克隆一个存储库。
1.我尝试了使用jgit创建仓库的情况,关闭我的服务器。再次启动它,然后简单地调用一个函数来删除之前创建的文件夹。这再次失败,并出现相同的错误。
1.我也完全删除了jgit从我的初始代码和从pom,因为它可能会进入加载到classpath.我仍然得到相同的错误时,试图删除.重新启动后再次尝试只是为了检查,问题仍然存在.
1.但是,当我从另一个Java程序运行相同的删除代码时,它成功了。
[1][How to remove local repository by Java code that was cloned by JGit java library?](https://stackoverflow.com/questions/65124176/how-to-remove-local-repository-by-java-code-that-was-cloned-by-jgit-java-library)
[2][How do I release file system locks after cloning repo via JGit](https://stackoverflow.com/questions/31764311/how-do-i-release-file-system-locks-after-cloning-repo-via-jgit)
下面是原始代码片段以供参考,

File file = null;
    try {
        file = new File(localRepositoryPath);

        // Delete directory if already present
        String localRepositoryParentPath = GitUtils.getRepositoryPath(projectName, null);
        File parentDirectory = new File(localRepositoryParentPath);
        FileUtils.deleteDirectory(parentDirectory);

        // Create directory where repository will be cloned
        if(!file.mkdirs()) {
            throw new GitServiceException(
                GitUtils.getCloneFailureMessage("Unable to create directory",
                    projectName,
                    repoName));
        }
    } catch (NullPointerException | SecurityException | IOException e) {
        logger.info(Arrays.toString(e.getStackTrace()));
        logger.error("IO Error cloning repository {} and project {}. Stacktrace {} ",
            repoName,
            projectName,
            e.getStackTrace());
        throw new GitServiceException(
            GitUtils.getCloneFailureMessage("IO Error while cloning",
                projectName,
                repoName));
    }

    // Clone the git repository
    try (Git git = Git.cloneRepository()
            .setURI(repoUrl)
            .setDirectory(file)
            .setBranchesToClone(List.of("refs/heads/" + branchToClone))
            .setBranch("refs/heads/" + branchToClone)
            .setCredentialsProvider(new UsernamePasswordCredentialsProvider( "token", this.credentials.getGitToken()))
            .call();
    ) {
        logger.info("Successfully cloned branch {} on path {} for repository {} and project {}",
            branchToClone,
            localRepositoryPath,
            repoName,
            projectName);
    } catch (Exception exception) {
        logger.error("Error cloning repository {} and project {}. Stacktrace {} ",
            repoName,
            projectName,
            exception.getStackTrace());
        throw new GitServiceException(
            GitUtils.getCloneFailureMessage("Unable to clone repository locally",
                projectName,
                repoName));
    }

编辑

正在添加错误堆栈跟踪

org.apache.commons.io.IOExceptionList: 1 exceptions: [java.io.IOException: Unable to delete file: \repositories\Test\Test\.git]
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:345)
at org.apache.commons.io.FileUtils.deleteDirectory(FileUtils.java:1206)
at com.adobe.mdloc.controller.TestController.post(TestController.java:57)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.adobe.asr.filter.AsrRequestResponseFilter.doFilterInternal(AsrRequestResponseFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.adobe.asr.logging.http.servlet.AsrLoggingFilter.doFilter(AsrLoggingFilter.java:71)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.adobe.asr.filter.AsrRequestIdFilter.doFilterInternal(AsrRequestIdFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at com.adobe.asr.exception.AsrExceptionFilter.doFilterInternal(AsrExceptionFilter.java:82)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:888)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1597)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)

Caused by: java.io.IOException: Unable to delete file: \repositories\Test\Test\.git
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1425)
at org.apache.commons.io.FileUtils.cleanDirectory(FileUtils.java:338)
... 71 more

Caused by: java.nio.file.AccessDeniedException: \repositories\Test\Test\.git\objects\pack\pack-21cdd19e90e9e6bd2e8d295ea04201969594fe96.idx
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:89)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:274)
at java.base/sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:110)
at java.base/java.nio.file.Files.deleteIfExists(Files.java:1180)
at org.apache.commons.io.file.DeletingPathVisitor.visitFile(DeletingPathVisitor.java:142)
at org.apache.commons.io.file.DeletingPathVisitor.visitFile(DeletingPathVisitor.java:36)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2724)
at java.base/java.nio.file.Files.walkFileTree(Files.java:2796)
at org.apache.commons.io.file.PathUtils.visitFileTree(PathUtils.java:687)
at org.apache.commons.io.file.PathUtils.deleteDirectory(PathUtils.java:328)
at org.apache.commons.io.file.PathUtils.delete(PathUtils.java:303)
at org.apache.commons.io.file.PathUtils.delete(PathUtils.java:280)
at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:1423)
... 72 more
mkshixfv

mkshixfv1#

显然Apache Commons IO函数抛出了这个错误。递归遍历所有文件并使用java.io delete命令删除它们工作得非常好。
下面是用于参考的代码片段,

public static boolean deleteDirectory(File file) {
    File[] children = file.listFiles();
    if (children != null) {
        for (File child : children) {
            deleteDirectory(child);
        }
    }
    return file.delete();
}
irtuqstp

irtuqstp2#

使用.close()方法发布Git clone,这将解决您的问题。

try (Git git = Git.cloneRepository()
        .setURI(repoUrl)
        .setDirectory(file)
        .setBranchesToClone(List.of("refs/heads/" + branchToClone))
        .setBranch("refs/heads/" + branchToClone)
        .setCredentialsProvider(new UsernamePasswordCredentialsProvider( "token", this.credentials.getGitToken()))
        .call()
        .close();

相关问题