javagitjgitjimfs

Clone git repository in-memory


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?


Solution

  • 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.