Delete a repo after cloning [message #1734656] |
Fri, 10 June 2016 04:14  |
Eclipse User |
|
|
|
Hi,
i am currently trying to implement a tool using JGit that accesses a certain file from a remote GIT repo and cleans up its own check right after.
I am using the following code to get a clone of repo to my local disk:
CloneCommand gitclone=new CloneCommand();
gitclone.setURI(baseurl);
gitclone.setDirectory(tempdir.toFile());
Git git=gitclone.call()
Then once i have aquired the file contents i am trying to get rid of the repo by calling
git.close();
git=null;
and then doing a recurisve delete on tempdir (using a Java 8 File.walkFIleTree
Unfortunately that cleanup action fails to delete some files as they still seem to be open.
Anyone with a suggestion how to achieve a proper cleanup of the created repo clone?
Thanks,
Markus
|
|
|
|
Re: Delete a repo after cloning [message #1734992 is a reply to message #1734801] |
Tue, 14 June 2016 10:03   |
Eclipse User |
|
|
|
I don't think that's related to problems mentioned in https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093 . I tried it on my windows box: If you don't close the Git instance you can't delete all files. But if you close the instance you can the delete the File hierarchy with java.io.File methods, but not with java.nio.file.FileTreeWalker. I have no explanation why. But deleting with java.io.File seems to be a workaround.
This code works
public static void main(String args[])
throws IOException, GitAPIException, JGitInternalException, InterruptedException {
File tmpDir = new File(System.getProperty("java.io.tmpdir"),
"JGitTest_CloneBranchExample_" + System.currentTimeMillis());
tmpDir.mkdirs();
System.out.println("Working dir: " + tmpDir);
try (Git r = Git.cloneRepository().setDirectory(tmpDir).setURI("https://github.com/chalstrick/dondalfi.git")
.call()) {
System.out.println("HEAD: " + r.log().call().iterator().next());
}
rm(tmpDir);
}
static void rm(File f) {
if (f.isDirectory())
for (File c : f.listFiles())
rm(c);
if (!f.delete())
System.err.println("Couldn't delete file " + f);
}
But this code doesn't work:
public static void main(String args[]) throws IOException, GitAPIException, JGitInternalException, InterruptedException {
File tmpDir = new File(System.getProperty("java.io.tmpdir"),
"JGitTest_CloneBranchExample_" + System.currentTimeMillis());
tmpDir.mkdirs();
System.out.println("Working dir: " + tmpDir);
try (Git r = Git.cloneRepository().setDirectory(tmpDir).setURI("https://github.com/chalstrick/dondalfi.git")
.call()) {
System.out.println("HEAD: " + r.log().call().iterator().next());
}
Files.walkFileTree(tmpDir.toPath(), new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
|
|
|
|
|
Re: Delete a repo after cloning [message #1735186 is a reply to message #1735100] |
Thu, 16 June 2016 04:23  |
Eclipse User |
|
|
|
Really, that's an interesting outcome. If it is really the case that Windows Explorer can't delete a folder hierarchy but java.io.File methods can this would be a surprise and worth looking into it deeper. I'll try
|
|
|
Powered by
FUDForum. Page generated in 0.04185 seconds