|
|
Re: Delete a repo after cloning [message #1734992 is a reply to message #1734801] |
Tue, 14 June 2016 14:03 |
Christian Halstrick Messages: 274 Registered: July 2009 |
Senior Member |
|
|
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;
}
});
}
Ciao
Chris
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03119 seconds