Skip to content

GitPython 3.0.3 - 3.1.0 unable to get diffs from bare repo #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
romuaa opened this issue Mar 3, 2020 · 9 comments
Open

GitPython 3.0.3 - 3.1.0 unable to get diffs from bare repo #993

romuaa opened this issue Mar 3, 2020 · 9 comments

Comments

@romuaa
Copy link

romuaa commented Mar 3, 2020

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.

diff_fail.script.txt

gitpython_3.0.2.output.txt

gitpython_3.1.0.output.txt

@Byron
Copy link
Member

Byron commented Mar 8, 2020

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

@dongs0104
Copy link
Contributor

dongs0104 commented Mar 17, 2020

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

  1. create repo
  2. change HEAD for wrong refs at repo.git/HEAD file, such as ref: refs/"remove_branch" or dangling HEAD
  3. using Diff function

3.0.3 is working 3.0.4~ are raise error on this b303cb0

@romuaa
Copy link
Author

romuaa commented Mar 17, 2020

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.

@Byron
Copy link
Member

Byron commented Mar 21, 2020

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.

@Byron
Copy link
Member

Byron commented Mar 21, 2020

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 ?

@romuaa
Copy link
Author

romuaa commented Mar 23, 2020

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`

@dongs0104
Copy link
Contributor

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.

@romuaa
Copy link
Author

romuaa commented Mar 24, 2020

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.

@bazilek
Copy link

bazilek commented Apr 14, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants