简体   繁体   中英

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

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM