15

我想在一个分支上提交(例如 master)。

我正在使用pygit2( pygit2.clone_repository)进行存储库克隆

然后我更改存储库中的现有文件。

之后我运行它来提交:

index = repository.index
index.add_all()
index.write()
author = pygit2.Signature(user_name, user_mail)
commiter = pygit2.Signature(user_name, user_mail)
tree = repository.TreeBuilder().write()
oid = repository.create_commit(reference, author, commiter, message,tree,[repository.head.get_object().hex])

但是当我去存储库并运行时git status

On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file:   test.txt

修改后的文件似乎添加了提交,但提交没有成功。使用返回的 Oid,我可以在 pygit2 存储库中找到 commit 属性。

我错过了什么 ?

4

2 回答 2

7

通过写作

tree = repository.TreeBuilder().write()

您正在创建一棵空树,然后将其作为提交树,这意味着您已经删除了每个文件(如果您git show HEAD在运行代码后运行,您可以看到这些文件)。

你想做的是

tree = index.write_tree()

它将索引中的数据存储为存储库中的树(创建缺少的任何一个),并且当您运行类似git commit. 然后,您可以像现在一样将此树传递给提交创建方法。

于 2015-04-08T23:17:14.373 回答
2

问题是您只是创建了提交,但没有更新您的 HEAD 参考。创建提交后,手动更新您的 HEAD 引用可以解决此问题。

repo.head.set_target(oid)
于 2018-03-20T03:53:46.170 回答