Clone git repository in-memory

2.3k Views Asked by At

I've been trying to clone a tiny git configuration repository into memory using JGIT and JIMFS using something like

FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path gitPath = Files.createDirectories(fs.getPath("/git")); 
Git.cloneRepository().setURI(...).setBranch(...).setDirectory(gitPath.toFile())
                    .setCredentialsProvider(...).call()

But since JIMFS works with the path Path API (since it doesn't use the default Filesystem), while JGIT uses the File API, JIMFS doesn't implement to toFile() call:

@Override
public File toFile() {
    // documented as unsupported for anything but the default file system
    throw new UnsupportedOperationException();
}

So I get is this UnsupportedOperationException. Is there a simple way of getting this (or a similar) setup to work without resorting to a temp directory on the disk?

2

There are 2 best solutions below

0
On BEST ANSWER

JGit offers an InMemoryRepository for testing and experimental use. But even this repository backend would store the work directory of non-bare repositories on disk.

Unless JGit changes its FileRepository implementation to use the Paths API, I don't see a way to use Jimfs to store repositories.

Some commands allow specifying a WorkingTreeIterator, which in theory, would allow read-access to a working tree on an alternate storage. However, not all commands support this concept write-access is also currently missing.

0
On

You can clone repository into memory and copy it file by file to own fs. See example of reading files into internal in memory repo here: https://stackoverflow.com/a/54486558/449553