好吧,我找到了解决方案。
我不知道,但我必须提供所有参数,否则 Commit 对象在尝试序列化时会抛出 BadObject 异常。
from git import *
from time import (time, altzone)
import datetime
from cStringIO import StringIO
from gitdb import IStream
repo = Repo('path/to/repo')
message = 'Commit message'
tree = repo.index.write_tree()
parents = [ repo.head.commit ]
# Committer and Author
cr = repo.config_reader()
committer = Actor.committer(cr)
author = Actor.author(cr)
# Custom Date
time = int(datetime.date(2013, 1, 1).strftime('%s'))
offset = altzone
author_time, author_offset = time, offset
committer_time, committer_offset = time, offset
# UTF-8 Default
conf_encoding = 'UTF-8'
comm = Commit(repo, Commit.NULL_BIN_SHA, tree,
author, author_time, author_offset,
committer, committer_time, committer_offset,
message, parents, conf_encoding)
创建提交对象后,必须放置适当的 SHA,我不知道这是如何完成的,但对 GitPython 源代码的一些研究让我得到了答案。
stream = StringIO()
new_commit._serialize(stream)
streamlen = stream.tell()
stream.seek(0)
istream = repo.odb.store(IStream(Commit.type, streamlen, stream))
new_commit.binsha = istream.binsha
然后将提交设置为 HEAD 提交
repo.head.set_commit(new_commit, logmsg="commit: %s" % message)