Skip to content
\n

Here is the relevant content of my pre-receive hook file:

\n
from git import Repo\n\nrepo = Repo(\"/home/ldt/Projects/dns/dns/.git\")\nprint(repo.git_dir)\nprint(repo.head.abspath)\nprint(repo.head.commit)\n
\n

However, when I trigger this with a push, this is the result:

\n
Enumerating objects: 12, done.\nCounting objects: 100% (12/12), done.\nDelta compression using up to 4 threads\nCompressing objects: 100% (9/9), done.\nWriting objects: 100% (9/9), 876 bytes | 876.00 KiB/s, done.\nTotal 9 (delta 6), reused 0 (delta 0)\nremote: /home/ldt/Projects/dns/dns/.git\nremote: /home/ldt/Projects/dns/dns/.git/HEAD\nremote: Traceback (most recent call last):\nremote:   File \"hooks/pre-receive\", line 11, in <module>\nremote:     print(repo.head.commit)\nremote:   File \"/home/ldt/.virtualenvs/dns-0cwPatsd-py3.8/lib/python3.8/site-packages/git/refs/symbolic.py\", line 197, in _get_commit\nremote:     obj = self._get_object()\nremote:   File \"/home/ldt/.virtualenvs/dns-0cwPatsd-py3.8/lib/python3.8/site-packages/git/refs/symbolic.py\", line 190, in _get_object\nremote:     return Object.new_from_sha(self.repo, hex_to_bin(self.dereference_recursive(self.repo, self.path)))\nremote:   File \"/home/ldt/.virtualenvs/dns-0cwPatsd-py3.8/lib/python3.8/site-packages/git/objects/base.py\", line 64, in new_from_sha\nremote:     oinfo = repo.odb.info(sha1)\nremote:   File \"/home/ldt/.virtualenvs/dns-0cwPatsd-py3.8/lib/python3.8/site-packages/git/db.py\", line 37, in info\nremote:     hexsha, typename, size = self._git.get_object_header(bin_to_hex(sha))\nremote:   File \"/home/ldt/.virtualenvs/dns-0cwPatsd-py3.8/lib/python3.8/site-packages/git/cmd.py\", line 1074, in get_object_header\nremote:     return self.__get_object_header(cmd, ref)\nremote:   File \"/home/ldt/.virtualenvs/dns-0cwPatsd-py3.8/lib/python3.8/site-packages/git/cmd.py\", line 1063, in __get_object_header\nremote:     return self._parse_object_header(cmd.stdout.readline())\nremote:   File \"/home/ldt/.virtualenvs/dns-0cwPatsd-py3.8/lib/python3.8/site-packages/git/cmd.py\", line 1025, in _parse_object_header\nremote:     raise ValueError(\"SHA could not be resolved, git returned: %r\" % (header_line.strip()))\nremote: ValueError: SHA could not be resolved, git returned: b''\n
\n

I don't get it... How could it work in the REPL but not in code? Could there be some sort of difference in the shell environment that GitPython uses? But if so, what?

","upvoteCount":1,"answerCount":6,"acceptedAnswer":{"@type":"Answer","text":"

Thanks for the heads-up. Since it's something with the way GitPython obtains object information, you could try to instantiate the repository with a different object database implementation.

\n

The one implemented in pure python would certainly provide better error messages, maybe it works even.

\n

Something I can imagine to be the reason for this issue in the first place is a different run environment, as the git command is influenced by plenty of environment variables which are set in hooks only.

","upvoteCount":1,"url":"https://github.com/gitpython-developers/GitPython/discussions/1180#discussioncomment-408868"}}}

Cannot get commits in code, but it works in REPL? #1180

Answered by Byron
SwampFalc asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for the heads-up. Since it's something with the way GitPython obtains object information, you could try to instantiate the repository with a different object database implementation.

The one implemented in pure python would certainly provide better error messages, maybe it works even.

Something I can imagine to be the reason for this issue in the first place is a different run environment, as the git command is influenced by plenty of environment variables which are set in hooks only.

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Byron
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1121 on February 26, 2021 11:18.