1

I clone a remote repository to my local environment using Java JGit library (latest version that is 5.9.0.202009080501-r).

Git git = Git.cloneRepository().setURI(repositoryUrl).setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).setDirectory(targetDir).call();

After using the contents of the repository I want to remove it from my local environment by deleting the directory of this local repository.

FileUtils.deleteDirectory(gitDirectory);

The problem is that I cannot remove the .git directory from the Java code because access is denied on files in the '.git\objects\pack' directory

java.io.IOException: Unable to delete file: 'local repository directory'.git\objects ... Caused by: java.nio.file.AccessDeniedException: 'local repository directory'.git\objects\pack\pack-*************.idx**

It is the JGit library that locks these files. After stopping the Java program I can delete these files manually. But I want to remove them from the code because on the server there is no way to stop an application just to delete something.

I am investigating this problem for days by now. What I tried so far:

  1. Close everything that the Jgit library provides.

git.close();

git.getRepository().close();

git.getRepository().getObjectDatabase().close();

Git.shutdown();

  1. Some threads state that files are locked by the Garbage collector of the Jgit, I tried to turn it off by configuration parameters
StoredConfig configuration = git.getRepository().getConfig();
configuration.setBoolean( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTODETACH, false );
configuration.setInt( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTOPACKLIMIT, 0 );
configuration.setInt( CONFIG_GC_SECTION, null, CONFIG_KEY_AUTO, 0 );
configuration.save();
  1. Some threads suggested that this problem can be solved by the below configuration of the WindowCacheConfig.
WindowCacheConfig config = new WindowCacheConfig();
config.setPackedGitMMAP(false);
WindowCache.reconfigure(config);
  1. Tried to play around with the with different settings of the JGit garbage collector. I tried multiple combinations and values (true/false) of the below settings. In most cases these settings made it worse because created an additional .bitmap file in the pack directory that was again impoossible to delete.
git.gc().setPreserveOldPacks(false).call();
git.gc().setPrunePreserved(true).call();
git.gc().setAggressive(true).call();

None of the above attempts helped, the result is always the same AccessDeniedException. Any help is appreciated.

1 Answer 1

1

Suppose you have this:

 File dir = new File( "c:/users/master/documents/test" );
 dir.mkdir();
 Git git = Git.cloneRepository()
              .setDirectory( dir )
              .setURI( "http://git.com/scm/devenv/jira.git" )
              .call();
          
 // you do your stuff with the cloned files here

You call this when you're done:

git.close();          
git = null;
Git.shutdown();
removeRecursively( dir );

Add this method:

private static void removeRecursively(File f) {
    if (f.isDirectory()) {
        for (File c : f.listFiles()) {
           removeRecursively(c);
        }
    }
    f.delete();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your response. I tried it, but unfortunately it doesn't work. There is the same 'AccessDeniedException'.
Hi Timon, apologies for the previous comment. I retested it and this code really works! But the key is the deletion method. My original deletion fails for some reason and your recursive method can delete the local repository. If the right deletion code is used, the Git.shutdown() is not required, it is enough to close the repository with git.close(). Thank you very much for leading me to this conclusion!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.