如何在JGit中使用内存数据库执行git pull?

DP_*_*DP_ 6 java git jgit

我想创建一个Java程序,其中

  1. 连接到某个Git存储库,
  2. 将文本附加到文件,
  3. 承诺和
  4. 将更改推送到该存储库.

理想情况下,所有这些都应该发生在内存中.

我正在使用JGit与Git进行交互:

InMemoryRepository repo = new InMemoryRepository(new DfsRepositoryDescription());
Git git = new Git(repo);
git.init().call();
PullCommand pull = git.pull();

StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", "https://XXX");
config.save();

PullResult result = pull.call();
Run Code Online (Sandbox Code Playgroud)

pull.call()以下异常结果:

org.eclipse.jgit.api.errors.NoHeadException: Pull on repository without HEAD currently not supported
    at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:191)
Run Code Online (Sandbox Code Playgroud)

如何将存储库的内容检索到内存中的JGit存储库?

Rüd*_*ann 3

要附加文件,您需要一个非裸存储库(具有工作目录的存储库。 此邮件列表帖子指出,

在某些情况下,瓷器命令假定基于文件,特别是当涉及到工作树时。如果您自己完成这项工作,则可以执行插入内存存储库的操作,但瓷器命令不能以这种方式工作。

虽然您可以不使用 procelain 命令,但我还建议(如上面评论的@Wayne)克隆到临时存储库,追加到文件,推送然后删除存储库。