Jgit - delete .git directory (or get files without it)

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

The line of code used:

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

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

It works great. However, since using the project that I am running does not require a clone to continue working, I just want the clone to have the contents of the github repository WITHOUT the .git directory.

I also tried using the following:

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

However, I have an IO exception saying that I cannot delete the file as follows:

An exception in the stream "main" java.io.IOException: cannot delete the file: C: \ testing \ testRepo1.git \ Objects \ package \ pack-7ca7f11688adda065d62f3394d0e055346beff22.pack

+5
source share
3 answers

It is possible that the current eclipse process stores a handle to the package file, preventing it from being deleted.
As Rรผdiger Herrmann suggests in the comments :

If the open file descriptor is what prevents file deletion, be sure to close the repository that init returns:

 clone.getRepository().close() 

Another approach would be to download the GitHub repository archive through its archive link .

Or using JGit to create an archive from the current local repo (see ArchiveTest.java ) and unzip this archive for use.

+5
source

This issue of the inability to delete the local repository still persists even after trying:

 clone.getRepository().close() 

I can fix the problem and successfully delete the local repository after setting the "options" value to RECURSIVE delete when calling delete () as follows:

  FileUtils.deleteDirectory(dirToDelete, 1); 

This is what the delete () document says:

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

Parameters: f - File to delete

options - delete options, RECURSIVE for recursively deleting a subtree, RETRY for retrying when deleting. Retrying can help if the underlying file system does not allow files to be read by another stream to be deleted.

+1
source

Whether the effect of your .close () has an effect depends on how the repository was created. Therefore, even if you call close on a Git instance, it may or may not release resources. I spent a lot of time before I realized this.

From the Jgit documentation:

If the repository was opened by the static factory method in this class, then this method calls the Repository.close () method on the base instance of the repository. (Whether this may actually release underlying resources, such as file descriptors, may differ, see "Repository" for more details.)

If the repository was created by the caller and passed to Git (the repository) or the static factory method in this class , then this method does not cause closure in the base repository.

0
source

Source: https://habr.com/ru/post/1208017/


All Articles