2

I am currently developing an app which clones Git repositories thanks to JGit (http://wiki.eclipse.org/JGit/User_Guide) every time a user logs on. When the user wants to quit the app, I want to delete the clone. Here's the problem : when cloning a repository, a folder .git is created, in which can be found a file .pack (.git/objects/pack/sutpideFile.pack) and which cannot be deleted, because the Java Platform is locking it (when trying to delete it by hand, get the error 'The action can't be completed because file is open in Java(TM) Platform SE binary').

THIS IS A KNOWN PROBLEM with Jgit : .pack file from git repo can't be deleted using File delete() method. Thus I have used the solution proposed here : https://github.com/ajoberstar/grgit/issues/33 which is to add those three lines before my deleting method :
WindowCacheConfig config = new WindowCacheConfig();
config.setPackedGitMMAP(true);
WindowCache.install(config);

BUT what really bothers me because I do not understand is that this solution works only once: I launch the server (TomCat), connect, and then disconnect. Here, the whole folder is deleted. However, when I re-connect and disconnect (without re-launching the sever), there rebels the files and am I not able anymore to delete it until I shut down the server.

Has anybody the slighest idea why it works, but only once ?

Thanks for your help,

EDIT : Well, so I just needed to add git.getRepository().close(); when I finish to use the Git object. Then the deletion is possible !

Community
  • 1
  • 1
Ekatsim
  • 23
  • 4
  • Have you tried the second solution from the question you pointed to ? i.e. http://stackoverflow.com/a/30184334/4807777 – Laurentiu L. Aug 26 '15 at 09:32
  • Does [`File.deleteOnExit()`](http://docs.oracle.com/javase/8/docs/api/java/io/File.html#deleteOnExit--) help ? – Hamza Abbad Aug 26 '15 at 09:33
  • @LaurentiuL. : yep, close(); already tried ! – Ekatsim Aug 26 '15 at 10:05
  • @HamzaAbbad : after a try, it doesn't work either. I saw on this page that the FileLock class could also be used, but that failed. However thanks to you both = ) – Ekatsim Aug 26 '15 at 10:08
  • @LaurentiuL. ooooh wait... actually, it seems that I read it too fast.. For several days, I tried to use the close() method on the Git object... whereas I should have done it on the Repository... Thank you so much ! It works !! You rock !! – Ekatsim Aug 26 '15 at 12:26
  • @Ekatsim You are welcome! – Laurentiu L. Aug 26 '15 at 12:31

1 Answers1

0

This is a know bug in JGit, see the discussion at How do I release file system locks after cloning repo via JGit

Basically you currently need to add the call to "Git.getRepository().close()" in order to free all file system locks until a new version of JGit is released.

result = Git.cloneRepository()
        .setURI( 'https://github.com/github/testrepo.git' )
        .setDirectory( localPath )
        .call();

// this is currently necessary to free all file locks
result.getRepository().close();

result.close();

JGit 4.1 is scheduled to have a fix for this included.

Community
  • 1
  • 1
centic
  • 15,565
  • 9
  • 68
  • 125