GitPython 更新挂钩 SHA 无法解析

Ale*_*Zel 7 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))
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

如果我手动运行脚本:

python3.5 /path_to_repo/.git/hooks/update refs/heads/master SHA1 SHA2
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*ado 0

面临同样的问题,它与新版本带来的变化有关git version 2.39.3

快速解决方案将安装以前版本的 git,2.31.1直到新版本的 GitPython 发布并支持最新的 git 版本。