我正在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,因此我不应该设置索引文件或工作树文件,但看起来这就是错误消息所说的.我错过了什么?

推荐答案

默认情况下,静态嵌套类InMemoryRepository.Builder构建空存储库.它也让我很头疼,但如果您调用repo.isBare(),您可以看到即使在构建过程中没有调用setBare(),它实际上也会返回true.

您可以使用Git.init()创建新的非空存储库,并应用返回的InitCommand中的配置.

public static void initInMemoryGit() throws IOException, GitAPIException {
    File textFile = new File("./tmp/TestGitRepository/test.txt");
    try (Git git = Git.init().setInitialBranch("master").setDirectory(new File("./tmp/TestGitRepository")).call()) {
        git.add().addFilepattern(textFile.getName()).call();
        git.commit().setMessage("hi").call();
    }
}

在我之前的回复中,我专注于创建测试存储库,而忘记了内存要求.InMemoryRepository可以保存从文件系统读取或从远程拉取的仓库.如果您的目标只是测试一些内容,则可以编写一个测试类,在测试周期的开始和结束时设置和拆除文件系统上的存储库.基于您的代码的测试类可能如下所示:

public class TestGit {

    private static Git git;

    @BeforeAll
    public static void setup() throws GitAPIException {
        git = Git.init().setInitialBranch("master").setDirectory(new File("./tmp/TestGitRepository")).call();
    }

    @Test
    public void testCommit() throws GitAPIException {
        File textFile = new File("./tmp/TestGitRepository/test.txt");

        git.add().addFilepattern(textFile.getName()).call();
        git.commit().setMessage("hi").call();

        List<RevCommit> commits = StreamSupport.stream(git.log().setMaxCount(1).call().spliterator(), false).toList();
        RevCommit commit = commits.get(0);

        Assertions.assertEquals(commit.getFullMessage(), "hi");
    }

    @AfterAll
    public static void tearDown() throws IOException {
        git.close();
        deleteDirectory(new File("./tmp/TestGitRepository/.git"));
    }

    private static boolean deleteDirectory(File dir) {
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                deleteDirectory(file);
            }
        }
        return dir.delete();
    }
}

Java相关问答推荐

PDFBox SmallMap不尊重Map.入口哈希码合同

@GetMapping和@GetExchange有什么区别?

如何使用Spring Data从MongoDB文档中包含的数组中 Select 单个元素?

PostgreSQL货币兑换率查询

Java WireMock定义存根在Cucumber并行执行的多线程测试中失败

如何使用jooq generator将表名和列名映射为人类可读的?

如果一个子类没有构造函数,超类也没有构造函数,那么为什么我可以构造子类的实例呢?

取消按钮,但没有任何操作方法引发和异常

Java Swing:初始化身份验证类后未检测到ATM_Interface键事件

如何修复IndexOutOfBoundsException在ClerView适配器的onRowMoved函数?

如何将其他属性引用到log4j2 yaml配置中?

声明MessageChannel Bean的首选方式

Domino Designer 14中的保存代理添加了重影库

Kotlin Val是否提供了与Java最终版相同的可见性保证?

有没有可能在时间范围内得到多种解决方案?

如何用内置Java从JavaFX应用程序中生成.exe文件?

寻找Thread.sky()方法的清晰度

如何修复Spring Boot应用程序中的RestDocumentationGenerationException:java.io.FileNotFoundException:/curl-request.adoc(只读文件系统)?

org.springframework.web.HttpRequestMethodNotSupportedException:请求方法';帖子';不支持

如何设置默认序列生成器分配大小