git - 使用 pygit2 创建提交

标签 git libgit2 pygit2

我想在一个分支上进行提交(例如 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/

相关文章:

git2go 获取远程标签

python - 使用 PyGitHub 在特定提交处获取存储库的所有文件内容

python - 在 Mac 上的 Python virtualenv 中安装 pygit2/libgit2

python - 如何在python中打印pygit对象的内容

Git:在其他分支中拆分提交

Git 子模块状态 - 如何显示子模块中的当前分支

c - 如何在 Github 上托管的存储库上使用 git_branch_create()?

c - 使用 libgit2 获取自最后一个标签以来的提交次数

git - AWK - 解析 git status 命令中的第二列

git - 使用 git fast-export 从给定的提交开始导出存储库