我正在try 从头开始创建InMemoryGitRepository
.最终,我想try 将自定义DFSRepository
作为git后台,但首先我正在try InMemoryRepository
的默认实现.我知道如何将现有的仓库克隆到InMemoryRepository
中,正如here所提到的,但我很难从头创建仓库并添加文件.我正在try 以下操作
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();
}
然而,我遇到了以下错误:
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)
由于它是InMemory Repo,因此我不应该设置索引文件或工作树文件,但看起来这就是错误消息所说的.我错过了什么?