Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

git

jgit

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

like image 593
Slippy Avatar asked Mar 18 '23 18:03

Slippy


1 Answers

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.

like image 61
VonC Avatar answered Mar 26 '23 01:03

VonC