我想在一个分支上进行提交(例如 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
修改后的文件好像是添加了commit但是commit没有成功。使用返回的 Oid,我可以在 pygit2 存储库中找到提交属性。
我错过了什么吗?
最佳答案
通过写作
tree = repository.TreeBuilder().write()
你正在创建一个空树,然后你将它作为提交的树,这意味着你已经删除了每个文件(如果你运行 git show HEAD
你可以看到在运行你的代码之后)。
你想做的是
tree = index.write_tree()
它将索引中的数据存储为存储库中的树(创建丢失的任何一个),当您运行 git commit
等命令时会发生这种情况。然后,您可以像现在一样将此树传递给提交创建方法。
关于git - 使用 pygit2 创建提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29469649/