无法解析GitPython更新挂钩SHA

时间:2017-03-23 09:30:48

标签: python git githooks gitpython

我正在用python3.5和GitPython模块编写更新钩子。

对于初学者,我只想迭代已添加/删除/修改的文件并将其保存到文件中。 这是钩子:

import git
import sys

branch, oldref, newref = sys.argv[1:]
print('oldref: {0}'.format(oldref))
print('newref: {0}'.format(newref))

repo = git.Repo('/srv/test')
oldcommit = repo.commit(oldref)
newcommit = repo.commit(newref)

with open(r'/tmp/hook.stdout', 'w') as outfile:
    for diff in oldcommit.diff(newcommit):
        outfile.write('{0}\n'.format(diff.b_path))

当我尝试推送存储库时,我收到以下错误:

remote: oldref: a38d324b84327e428127182b2d36b03afa73c11c
remote: newref: 1fbc70042891ecddef104f1938259b30f5f1e912
remote: Traceback (most recent call last):
remote:   File "hooks/update", line 10, in <module>
remote:     oldcommit = repo.commit(oldref)
remote:   File "/usr/local/lib/python3.5/dist-packages/git/repo/base.py", line 433, in commit
remote:     return self.rev_parse(text_type(rev) + "^0")
remote:   File "/usr/local/lib/python3.5/dist-packages/git/repo/fun.py", line 193, in rev_parse
remote:     obj = name_to_object(repo, rev[:start])
remote:   File "/usr/local/lib/python3.5/dist-packages/git/repo/fun.py", line 130, in name_to_object
remote:     return Object.new_from_sha(repo, hex_to_bin(hexsha))
remote:   File "/usr/local/lib/python3.5/dist-packages/git/objects/base.py", line 64, in new_from_sha
remote:     oinfo = repo.odb.info(sha1)
remote:   File "/usr/local/lib/python3.5/dist-packages/git/db.py", line 37, in info
remote:     hexsha, typename, size = self._git.get_object_header(bin_to_hex(sha))
remote:   File "/usr/local/lib/python3.5/dist-packages/git/cmd.py", line 940, in get_object_header
remote:     return self.__get_object_header(cmd, ref)
remote:   File "/usr/local/lib/python3.5/dist-packages/git/cmd.py", line 929, in __get_object_header
remote:     return self._parse_object_header(cmd.stdout.readline())
remote:   File "/usr/local/lib/python3.5/dist-packages/git/cmd.py", line 891, in _parse_object_header
remote:     raise ValueError("SHA could not be resolved, git returned: %r" % (header_line.strip()))
remote: ValueError: SHA could not be resolved, git returned: b''
remote: error: hook declined to update refs/heads/master

如果我手动运行脚本:

python3.5 /path_to_repo/.git/hooks/update refs/heads/master SHA1 SHA2

它运行正常并将文件名保存到文件中,我尝试使用不同的SHA,如果我使用我在错误消息中看到的SHA运行脚本(oldref,newref)我得到一个空文件但是没有错误消息(因为提交已被拒绝,我认为newref SHA不会出现在远程存储库中)。 我错过了什么?

0 个答案:

没有答案