jgit - Delete .git directory (or get files without it)

5.7k views Asked by At

I need to clone a git repo in java. I am using jGit.

The line of code I am using is :

Git clone = Git.cloneRepository().setURI(URIofRepo).setDirectory(localPath).call();

Where URIofRepo is : the github link to my repo and Where localPath is : the directory I want the clone to happen.

This works perfectly. However, since the use of the project I am doing doesn't require a clone for continued work, I simply want the clone to have the contents of the github repo WITHOUT the .git directory.

I tried also using the following :

    File dirToDelete = new File (path   + "/.git");
    FileUtils.deleteDirectory(dirToDelete);

However I got an IO exception saying I am unable to delete the file as follows :

Exception in thread "main" java.io.IOException: Unable to delete file: C:\testing\testRepo1.git\objects\pack\pack-7ca7f11688adda065d62f3394d0e055346beff22.pack

3

There are 3 answers

2
VonC On BEST ANSWER

It is possible that the current eclipse process keep an handle on the pack file, preventing its deletion.
As RĂ¼diger Herrmann suggests in the comments:

If the open file handle is what prevents the file from being deleted, make sure to close the repository that is returned by init:

clone.getRepository().close()

Another approach would be to download an archive of the GitHub repo through its archive link.

Or using JGit to create an archive from your current local repo (see ArchiveTest.java), and unzip that archive for you to use.

1
Abhijeet Apsunde On

Whether or not your .close() will have an effect depends on how the repository was created. So even though you are calling close on a Git instance, it may or may not release the resources. I wasted so much of my time before I realized this.

From Jgit documentation :

If the repository was opened by a static factory method in this class, then this method calls Repository.close() on the underlying repository instance. (Whether this actually releases underlying resources, such as file handles, may vary; see Repository for more details.)

If the repository was created by a caller and passed into Git(Repository) or a static factory method in this class, then this method does not call close on the underlying repository.

1
Vishal On

This issue of unable to delete local repository still persisted even after trying following:

clone.getRepository().close()

I could fix the issue and successfully delete local repository after setting "options" value to RECURSIVE delete while calling delete() as follows:

 FileUtils.deleteDirectory(dirToDelete, 1);

This is what delete() document says:

public static void delete(File f, int options) throws IOException Delete file or folder

Parameters: f - File to be deleted

options - deletion options, RECURSIVE for recursive deletion of a subtree, RETRY to retry when deletion failed. Retrying may help if the underlying file system doesn't allow deletion of files being read by another thread.