【发布时间】:2015-07-02 23:31:51
【问题描述】:
我想创建一个Java程序,
- 连接到某个 Git 存储库,
- 将文本附加到文件中,
- 提交和
- 将更改推送到该存储库。
理想情况下,所有这些都应该发生在内存中。
我正在使用 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 存储库中?
【问题讨论】:
-
你可能要发出
fetch -
我不是这方面的专业人士,但我从未听说过有人尝试进行内存中的 git 访问——不过我有一个建议。尝试将存储库加载到临时目录中(使用文件.deleteOnExit 函数(通过 File.mkdirs 创建目录时),然后将所有这些文件加载到内存中,保存所做的任何修改,然后将 git 与临时目录重新同步..
-
no HEAD可能意味着您需要先签出某个分支(checkout master或其他任何分支) -
@LeGEC 你能画出 git 命令的顺序吗?现在,我正在这样做:1)
git init2)git remote add origin ...3)git pull origin。当我从命令行使用 git 时,它通常是这样工作的。 -
@DmitriPisarenko :尝试了解在您的
new Git(repo)命令和您的git.init().call()命令之后的活动分支是什么。去查看 JGit 的文档以了解更多关于它是如何工作的细节。