2015-04-06 51 views
6

我想在分支上进行提交(例如master)。使用pygit2创建提交

我正在使用pygit2pygit2.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属性。

我错过了什么吗?

+0

我没有看到你的代码提到某处的分支,也许你应该指定某处? – 2015-04-06 10:48:45

+0

引用的值是'ref/heads/master' – user1479699 2015-04-06 10:54:10

+0

我很难为pygit2找到体面的文档,所以我只是猜测:我看到在做了一些状态更改后调用了各种'write()'方法。 'create_commit()'是隐式写入还是忘记这么做? – 2015-04-06 10:56:56

回答

3

通过编写

tree = repository.TreeBuilder().write() 

要创建一个空的树,你再给这个作为树的承诺,这意味着你已经删除的每个文件(如果你运行你可以看到运行你的代码后运行git show HEAD)。

你想要做的,而不是什么是

tree = index.write_tree() 

存储在索引树(创建为准丢失)中的数据资源库,并且当你运行像git commit命令会发生什么。然后,您可以像现在这样将此树传递给提交创建方法。