GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

Cannot retrieve commits from a specific repository

Open jemaf opened this issue 4 years ago • 11 comments

GitPython is failing to retrieve commits from a specific repository I am currently analyzing: https://github.com/jbrowncfa/Cryptobomb.

Exception's stack trace:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/git/repo/base.py", line 524, in iter_commits
    rev = self.head.commit
  File "/usr/local/lib/python3.6/site-packages/git/refs/symbolic.py", line 197, in _get_commit
    obj = self._get_object()
  File "/usr/local/lib/python3.6/site-packages/git/refs/symbolic.py", line 190, in _get_object
    return Object.new_from_sha(self.repo, hex_to_bin(self.dereference_recursive(self.repo, self.path)))
  File "/usr/local/lib/python3.6/site-packages/git/objects/base.py", line 64, in new_from_sha
    oinfo = repo.odb.info(sha1)
  File "/usr/local/lib/python3.6/site-packages/git/db.py", line 37, in info
    hexsha, typename, size = self._git.get_object_header(bin_to_hex(sha))
  File "/usr/local/lib/python3.6/site-packages/git/cmd.py", line 1068, in get_object_header
    return self.__get_object_header(cmd, ref)
  File "/usr/local/lib/python3.6/site-packages/git/cmd.py", line 1057, in __get_object_header
    return self._parse_object_header(cmd.stdout.readline())
  File "/usr/local/lib/python3.6/site-packages/git/cmd.py", line 1019, in _parse_object_header
    raise ValueError("SHA could not be resolved, git returned: %r" % (header_line.strip()))
ValueError: SHA could not be resolved, git returned: b''

Reproducible example:

$ git clone https://github.com/jbrowncfa/Cryptobomb
from git import Repo
repo = Repo("./Cryptobomb")
for commit in repo.iter_commits():
    print(commit.hexsha) # error

Here is the Dockerfile I'm using:

FROM python:3.6

ENV GIT_DISCOVERY_ACROSS_FILESYSTEM=1
RUN apt-get update
RUN apt-get install -y git 

Please let me know if you guys need any more info.

jemaf avatar Jun 02 '20 17:06 jemaf

Thanks a lot, I was able to reproduce the issue, and here is how it looks for me:

➜  GitPython git:(master) ✗ python3 reproduce.py
Traceback (most recent call last):
  File "reproduce.py", line 3, in <module>
    for commit in repo.iter_commits():
  File "/Users/byron/dev/GitPython/git/repo/base.py", line 524, in iter_commits
    rev = self.head.commit
  File "/Users/byron/dev/GitPython/git/refs/symbolic.py", line 197, in _get_commit
    obj = self._get_object()
  File "/Users/byron/dev/GitPython/git/refs/symbolic.py", line 190, in _get_object
    return Object.new_from_sha(self.repo, hex_to_bin(self.dereference_recursive(self.repo, self.path)))
  File "/Users/byron/dev/GitPython/git/objects/base.py", line 64, in new_from_sha
    oinfo = repo.odb.info(sha1)
  File "/Users/byron/dev/GitPython/git/db.py", line 37, in info
    hexsha, typename, size = self._git.get_object_header(bin_to_hex(sha))
  File "/Users/byron/dev/GitPython/git/cmd.py", line 1068, in get_object_header
    return self.__get_object_header(cmd, ref)
  File "/Users/byron/dev/GitPython/git/cmd.py", line 1057, in __get_object_header
    return self._parse_object_header(cmd.stdout.readline())
  File "/Users/byron/dev/GitPython/git/cmd.py", line 1021, in _parse_object_header
    raise ValueError("SHA %s could not be resolved, git returned: %r" % (tokens[0], header_line.strip()))
ValueError: SHA b'4edad68336cc60e626c81a106b54bcfc6ab90b41' could not be resolved, git returned: b'4edad68336cc60e626c81a106b54bcfc6ab90b41 missing'

Byron avatar Jun 05 '20 01:06 Byron

We face the same problem when we analyze repositories. Do you have any updates on this issue?

simisimon avatar Nov 06 '20 13:11 simisimon

Had the same problem, updating git to git-2.30.0-1 solved it for me.

andbue avatar Jan 05 '21 10:01 andbue

I was getting this error message when I was trying to get info about a repository which I was not the owner of (even though I was root). I tried doing it manually with git rev-parse HEAD and got fatal: unsafe repository ('[my repo]' is owned by someone else)

If you're having this problem, see if git rev-parse HEAD works.

I would like to see some better error handling if possible. It seems clear to me that an error was not properly being raised somewhere and a function was silently returning an empty binary string.

Yook74 avatar Apr 18 '22 18:04 Yook74

I would like to see some better error handling if possible. It seems clear to me that an error was not properly being raised somewhere and a function was silently returning an empty binary string.

This is a known issue as some commands are configured to stream data and only look at stdout. That said, git appears to exit with code 129 making it possible to provide a better error message. We could use an issue to keep track of this or a PR to fix it. Here is a related conversation.

Byron avatar Apr 18 '22 23:04 Byron

Maybe git is running into a different owner than the repository.

Inside repo, you can try this command: git config --global -add safe.directory <repo_path>

ramonlins avatar Apr 20 '22 16:04 ramonlins

@ramonlins yes that was the fix for my particular problem

Yook74 avatar Apr 20 '22 16:04 Yook74

thanks @ramonlins , that works for me as well.

However, I do not understand what caused this error message to suddenly appear, while it was working before for a long time without any significant changes in the setup lately...

kapsner avatar May 04 '22 15:05 kapsner

@kapsner a recent update to Git on the apt repositories added a feature which is causing this problem.

Yook74 avatar May 04 '22 15:05 Yook74

For more reading: https://stackoverflow.com/a/71904131/3580553

Starting in Git v2.35.3, safe directory checks can be disabled, which will end all the "unsafe repository" errors This can be done by running: git config --global --add safe.directory '*' It will add the following setting to your global .gitconfig file:

[safe]
    directory = *

Before disabling, make sure you understand this security measure, and why it exists. You should not do this if your repositories are stored on a shared drive. However, if you are the sole user of your machine 100% of the time, and your repositories are stored locally, then disabling this check should, theoretically, pose no increased risk.

frozenpandaman avatar Oct 30 '22 16:10 frozenpandaman

I am getting this error for https://github.com/nodejs/node. git config --global --add safe.directory '*' did not solve the issue.

mamiksik avatar Jan 19 '23 18:01 mamiksik