如何在 JGit 中使用内存数据库进行 git 拉取?

How to do a git pull with an in-memory database in 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();

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)

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

要附加一个文件,您需要一个非裸存储库(一个有工作目录的存储库。This mailing list post 指出,

The porcelain commands assume a File basis in some cases, particularly when it comes to the working tree. You can perform inserts into an in-memory repository if you do the work yourself, but the porcelain commands do not work in that way.

虽然您可以 do without the procelain commands,但我也建议(如上面评论的@Wayne)克隆到一个临时存储库,附加到文件,推送然后删除存储库。