I am trying to create an InMemoryGitRepository from scratch. Ultimately I would like to try a custom DFSRepository as git backend, but to begin with I was playing around with the default implementation of InMemoryRepository. I know how to clone an existing repo into InMemoryRepository as mentioned here but I am having a tough time on how to create the repo from scratch and add a file. I am trying the following
private static void initInMemoryGit() throws IOException, GitAPIException {
DfsRepositoryDescription repoDesc = new DfsRepositoryDescription("test");
//InMemoryRepository repo = new InMemoryRepository(repoDesc);
InMemoryRepository repo = new InMemoryRepository.Builder().setRepositoryDescription(repoDesc)
.setInitialBranch("master")
.build();
repo.create();
Git git = new Git(repo);
File textFile = new File("/tmp/TestGitRepository", "test.txt");
Files.write(textFile.toPath(), "hi".getBytes());
git.add().addFilepattern(textFile.getName()).call();
git.commit().setMessage("hi").call();
}
However I am getting the following error
Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:1200)
at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:259)
at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:1282)
at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:122)
at oracle.bi.versioncontrol.jgit.JGitVersionInMemory.initInMemoryGit(JGitVersionInMemory.java:114)
at oracle.bi.versioncontrol.jgit.JGitVersionInMemory.main(JGitVersionInMemory.java:51)
Since it is an InMemory Repo, I am not supposed to set the Index File or Working Tree File, but looks like that’s what the error message is saying. What am I missing?