GitPython
GitPython copied to clipboard
GitPython 3.0.3 - 3.1.0 unable to get diffs from bare repo
GitPython 3.0.2 works properly. GitPython 3.0.3 is unable to read the bare repo. GitPython 3.0.4 and 3.0.5 are not able to get diffs GitPython 3.0.6 is unable to read the bare repo. GitPython 3.0.7 - 3.0.9, 3.1.0 are not able to get diffs
Test script and script outputs attached. Test script is a Python 3 script named to diff_fail.script.txt because .py not supported as an upload.
Thanks for posting, and for providing the means to reproduce the issue. It surprises me that GitPython seems to have regressed.
Which OS are you using, and which Git version? Sometimes this matters a lot. Thanks
I tested it on python:3.7.3-stretch docker, 3.1.0 version gitpython, git version is 2.11.0
Traceback (most recent call last):
File "lib/python3.7/site-packages/git/cmd.py", line 83, in pump_stream
handler(line)
File "lib/python3.7/site-packages/git/diff.py", line 528, in handle_diff_line
'', change_type, score)
File "lib/python3.7/site-packages/git/diff.py", line 277, in __init__
for submodule in repo.submodules:
File "lib/python3.7/site-packages/git/repo/base.py", line 337, in submodules
return Submodule.list_items(self)
File "lib/python3.7/site-packages/git/util.py", line 921, in list_items
out_list.extend(cls.iter_items(repo, *args, **kwargs))
File "lib/python3.7/site-packages/git/objects/submodule/base.py", line 1156, in iter_items
pc = repo.commit(parent_commit) # parent commit instance
File "lib/python3.7/site-packages/git/repo/base.py", line 478, in commit
return self.rev_parse(str(rev) + "^0")
File "/Users/aromu/workspaces/tnd/git_collector/venv/lib/python3.7/site-packages/git/repo/fun.py", line 212, in rev_parse
obj = name_to_object(repo, rev[:start])
File "lib/python3.7/site-packages/git/repo/fun.py", line 146, in name_to_object
raise BadName(name)
gitdb.exc.BadName: Ref 'HEAD' did not resolve to an object
I think it is our repo has wrong HEAD, so error is correct and announce it. reproduce
- create repo
- change HEAD for wrong refs at
repo.git/HEAD
file, such asref: refs/"remove_branch"
or dangling HEAD - using Diff function
3.0.3 is working 3.0.4~ are raise error on this https://github.com/gitpython-developers/GitPython/commit/b303cb0c5995bf9c74db34a8082cdf5258c250fe
Sorry for late reply. For me it occurs on macOS Mojave 10.14.6, Python 3.7.6 with git version 2.25.0 Haven't tested it on our Linux servers since I caught it while developing.
Thanks for the update. The question I can't really answer to myself is if the behaviour of GitPython 3.0.2 is desirable, as it seems to ignore the dangling ref or doesn't run into that issue for other reasons. What does git itself do?
Since the issue seems to come from the diff implementation, it must be something that changed there in more recent versions of GitPython. Maybe that can be tracked down to lead to an improved quality of life, especially under the assumption that git itself won't fail like that.
Having just seen issue 999, it looks like we got the answer: Git itself will fail to diff if there is a dangling HEAD reference. Thus the behaviour of GitPython can be considered adequate, assuming git always knows what's best :D.
Thus I recommend closing this issue, what do you think, @romuaa ?
Indeed it seems to be the case that the git diff fails. Unfortunately I don't know enough about git to understand why it does so. Also I quite don't understand why GitPython 3.0.2 still works.
$ git diff-tree c9ec46881167c97ea0112754d10a913a39e58f15 83f1b4bba8f2b861965214ea95c0ea773c4b1872 -r --abbrev=40 --full-index -M --raw --no-color fatal: bad object c9ec46881167c97ea0112754d10a913a39e58f15`
that commit is about involving submodule changes, submodule logic is need to access to 'HEAD' which is default ref.
Your git 'HEAD' can not link any refs in repo, so I recommend checking your 'HEAD' or using version 3.0.4.
If it helps at all that commit hash is actually from https://github.com/romuaa/test_stuff you should be able to access that. There's only some test stuff with couple of 'feature' branches. No sub modules in use. GitPython 3.0.2 works fine and anything above fails.
got the same issue for repo with bunch of submodules for simple diff
repo = Repo('.')
new = repo.commit('HEAD')
old = repo.commit('HEAD^')
new.diff(old)
GitPython>=3.0.6
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/git/cmd.py", line 83, in pump_stream
handler(line)
File "/usr/local/lib/python3.7/site-packages/git/diff.py", line 528, in handle_diff_line
'', change_type, score)
File "/usr/local/lib/python3.7/site-packages/git/diff.py", line 277, in __init__
for submodule in repo.submodules:
File "/usr/local/lib/python3.7/site-packages/git/repo/base.py", line 337, in submodules
return Submodule.list_items(self)
File "/usr/local/lib/python3.7/site-packages/git/util.py", line 921, in list_items
out_list.extend(cls.iter_items(repo, *args, **kwargs))
File "/usr/local/lib/python3.7/site-packages/git/objects/submodule/base.py", line 1192, in iter_items
sm._name = n
AttributeError: 'Tree' object has no attribute '_name'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.7/site-packages/git/cmd.py", line 86, in pump_stream
raise CommandError(['<%s-pump>' % name] + cmdline, ex)
git.exc.CommandError: Cmd('<stdout-pump>') failed due to: AttributeError(''Tree' object has no attribute '_name'')
cmdline: <stdout-pump> git diff-tree a865822725a822e23ec3d00af96c553ee5dde56e bbea82a8061d391e091dee417271180da9529002 -r --abbrev=40 --full-index -M --raw --no-color
git diff-tree output example
:100644 100644 82f25d4819a4bfd0e10c2d775842d55985c7cbda 60306a884be0e11e6a17fa25fdcae9dd670c6a2e M .gocd.yaml