"index not backed by a repository" 为 'pull' 提交从索引写入树时 libgit2 出错

"index not backed by a repository" error from libgit2 when writing tree from index for 'pull' commit

当我执行 index.write_tree())index 是使用 merge_commits 合并产生的 Index)时,我收到错误 "Failed to write tree. the index file is not backed up by an existing repository"。我有一个现有的存储库。 [这个post的原版是bare的,我改成non-bare了,还是不行。]我哪里做错了?

更一般地说,我正在尝试实现 git pull(先获取然后合并)。在 catch-up 案例中,我了解到在 fetch 和 merge 之后,我需要写出一个 commit。这就是我想要做的。我怎么做?

基本上,我的代码是

 let index = repo.merge_commits(&our_commit, &their_commit, Some(&MergeOptions::new()))?;
 if !index.has_conflicts() {
   let new_tree_oid = index.write_tree()?; // error occurs here
   let new_tree = repo.find_tree(new_tree_oid)?;
   //...
 }

这是使用 rust git2 板条箱,它包装了 libgit2 库。

您有一个内存索引,您没有存储库的索引。这是阻止您编写它的区别。

处理内存索引有几个选项:

  1. 您可以使用 git_index_write_tree_to 将其转换为树(然后您可以创建使用该树的提交)。 write_tree_to 函数可让您指定要写入的存储库。
  2. 您可以将其设为 存储库的索引,使用git_repository_set_index

虽然我会质疑你为什么使用 git_merge_commits 而不是 git_merge,它应该为你处理所有这些,包括处理冲突。如果您真的在进行 git pull 仿真,那么您将需要应对这一问题。