Skip to content
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

repo.index.diff("HEAD", create_patch=True) always empty #852

Closed
jadinm opened this issue Mar 13, 2019 · 2 comments
Closed

repo.index.diff("HEAD", create_patch=True) always empty #852

jadinm opened this issue Mar 13, 2019 · 2 comments

Comments

@jadinm
Copy link

@jadinm jadinm commented Mar 13, 2019

I am using GitPython v2.1.11 with Python 3.6. My git version is 2.17.1.

I see inconsistent behavior of the diff method of a repo index if create_patch option set to True.
I am running the following script in a git repository with staged and unstaged changes.

from git import Repo

repo = Repo(".")

print(repo.index.diff("HEAD"))
print(repo.index.diff("HEAD", create_patch=True)) # Problem here

print(repo.index.diff(None))
print(repo.index.diff(None, create_patch=True))

But it prints:

[<git.diff.Diff object at 0x7fd54c328678>, <git.diff.Diff object at 0x7fd549e405e8>, <git.diff.Diff object at 0x7fd54c328558>]
[]
[<git.diff.Diff object at 0x7fd54c328678>]
[<git.diff.Diff object at 0x7fd54c328678>]

Somehow, the diff is empty if I set create_patch to True and the first argument to anything but None.
Do you know why it happens ?

@jadinm
Copy link
Author

@jadinm jadinm commented Mar 13, 2019

I found out the problem.

When running repo.index.diff("HEAD", create_patch=True), the command used is:

$ git diff -R <reference-hash> --cached --abbrev=40 --full-index -M -p --no-color

And this produces:

diff --git b/other_file.txt a/other_file.txt
deleted file mode 100644
index d733472942281cd2f19f52ab4b5ddecb4151bb72..0000000000000000000000000000000000000000
--- b/other_file.txt
+++ /dev/null
@@ -1 +0,0 @@
-Other file

But the regex that is used for parsing (i.e., Diff.re_header) expects a/other_file.txt to be before b/other_file.txt.
The inversion is due to the "-R" flag in the command line.


A temporary fix for my use of GitPython is to set R to True to prevent the reversal.

print(repo.index.diff("HEAD", create_patch=True, R=True))

@Byron
Copy link
Member

@Byron Byron commented Jul 20, 2019

Thanks a lot for sharing the issue, and for posting the workaround here as well!

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

Successfully merging a pull request may close this issue.

None yet
2 participants