I'm writing an update hook with python3.5 and the GitPython module.
For starters I simply wanted to iterate over the files that have been added/deleted/modified and save that to a file. Here is the hook:
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))
When I try and push the the repository I get the following error:
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
If I run the script manually:
python3.5 /path_to_repo/.git/hooks/update refs/heads/master SHA1 SHA2
It runs fine and saves the file names to the file, I've tried with varies SHAs, if I run the script with the SHAs I see in the error message (oldref, newref) I get an empty file but no error message (since the commit has been rejected I suppose the newref SHA doesn't appear in the remote repository). What am I missing?
a38d324b84327e428127182b2d36b03afa73c11c
a commit? If it is actually a tree or blob, for instance,a38d324b84327e428127182b2d36b03afa73c11c^0
will reject it as it cannot be turned into a commit. (Other possibilities include that perhaps gitpython does not account for$GIT_DIR
in the environment, or you are using a new enough Git that it is using alternate object paths and stripping out too much from the environment.) – Cletuscleve$GIT_DIR
. File it as a bug, perhaps? :-) – Cletuscleve