Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » Delete a repo after cloning
Delete a repo after cloning [message #1734656] Fri, 10 June 2016 08:14 Go to next message
Markus Kramer is currently offline Markus KramerFriend
Messages: 2
Registered: June 2016
Junior Member
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 #1734801 is a reply to message #1734656] Sun, 12 June 2016 22:39 Go to previous messageGo to next message
Rüdiger Herrmann is currently offline Rüdiger HerrmannFriend
Messages: 579
Registered: July 2009
Senior Member
Markus,

depending on which version of JGit you use, you may have run into this bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=474093

HTH
--
Rüdiger Herrmann
http://codeaffine.com

On 10.06.2016 20:03, Markus Kramer wrote:
> 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 14:03 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
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
Re: Delete a repo after cloning [message #1734997 is a reply to message #1734992] Tue, 14 June 2016 14:11 Go to previous messageGo to next message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
Another thing I tried with the non-working code of the previous post: I started the non-working code with http://file-leak-detector.kohsuke.org/ in order to find out whether some of our code still has a file handle open ... nope. All file handles to pack/index files are closed and still we are getting a AccessDeniedException when trying to delete the index file with java.nio. And java.io can delete it. That's strange.

Ciao
Chris
Re: Delete a repo after cloning [message #1735100 is a reply to message #1734997] Wed, 15 June 2016 14:26 Go to previous messageGo to next message
Markus Kramer is currently offline Markus KramerFriend
Messages: 2
Registered: June 2016
Junior Member
Hi,

Thanks a lot for your help, it is indeed confusing that this is dependent on the delete method applied, which i did not yet try but consider a valid workaround for my use case.
I also tried actually deleting that repo from Windows Explorer which also rejected deletion while the Java VM with the WebApplication that did the Clone was still running. As soon as i killed the JVM Windows Explorer was able to delete stuff right away.

Thanks,

Markus

Re: Delete a repo after cloning [message #1735186 is a reply to message #1735100] Thu, 16 June 2016 08:23 Go to previous message
Christian Halstrick is currently offline Christian HalstrickFriend
Messages: 241
Registered: July 2009
Senior Member
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

Ciao
Chris
Previous Topic:Unable to connect repository
Next Topic:How to Create a Note for a BLOB Object?
Goto Forum:
  


Current Time: Fri Jul 28 13:13:15 GMT 2017

Powered by FUDForum. Page generated in 0.02837 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software