GitPython update hook SHA could not be resolved
Asked Answered
R

3

8

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?

Regeniaregensburg answered 23/3, 2017 at 9:30 Comment(3)
Is 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
setting GIT_DIR env variable in the script fixed the issue, thank you.Regeniaregensburg
I'm a bit surprised: the code in the gitpython library seems sophisticated enough to be aware of, and hence take care of, $GIT_DIR. File it as a bug, perhaps? :-)Cletuscleve
E
5

The root cause of this issue could be related to the ownership of the files in the repository, if you manually execute the operation that you are trying via GitPython you will find an error like this

fatal: detected dubious ownership in repository at '/home/<your dir>'
To add an exception for this directory, call:

    git config --global --add safe.directory /home/<your dir>

Following the suggestion to add the exception the issue should get fixed!

Expeditionary answered 17/7, 2023 at 17:31 Comment(0)
M
2

The question is really old, but I post this to point to a solution that worked for me, in case other people have the same problem.

I had the same problem with a script which worked fine in a REPL but caused this error when ran with Apache and WSGI. It seems this problem was reported a few times (here or here), apparently without a bugfix.

However, this comment make a suggestion which worked for me: use GitDB as git object database when creating the Repo object. So, the following worked for me.

import pathlib

import git
from gitdb import GitDB

repo_path = pathlib.Path(__file__).parent.absolute()
repo = git.Repo(repo_path, odbt=GitDB)
last_commit_sha = repo.head.commit.hexsha
Mithras answered 27/9, 2023 at 16:27 Comment(0)
E
0

Faced the same issue and it is related with changes coming with the new git version 2.39.3

Quick solution will be get installed a previous version of git like 2.31.1 until new version of GitPython gets released with support to the newest git versions.

Expeditionary answered 14/6, 2023 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.