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

commit.diff(branch, create_patch=True) gets "+" and "-" backwards #1291

Open
lhannest opened this issue Jul 13, 2021 · 2 comments
Open

commit.diff(branch, create_patch=True) gets "+" and "-" backwards #1291

lhannest opened this issue Jul 13, 2021 · 2 comments

Comments

@lhannest
Copy link

@lhannest lhannest commented Jul 13, 2021

When I do this:

>>> import git
>>> r = git.Repo('.')
>>> from pprint import pprint
>>> pprint(r.active_branch.commit.diff('master', create_patch=True)[0].diff.decode('utf-8').split('\n'))

Then I see the same first file as when I do git diff master in the terminal. But, it gets the "+" and "-" backwards. In python I see lines such as:

...
'-import static org.mockito.Matchers.anyLong;',
'-import static org.mockito.Matchers.anyString;',
'-import static org.mockito.Mockito.times;',
...

But this is a file I've added in the active branch that isn't in the master! In the terminal when I do git diff master I see:

...
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.times;
...

On the other hand, when I use pprint(r.git.diff('master').split('\n')) then I get the right result:

...
'+import static org.mockito.Matchers.anyLong;',
'+import static org.mockito.Matchers.anyString;',
'+import static org.mockito.Mockito.times;',
...
@lhannest
Copy link
Author

@lhannest lhannest commented Jul 13, 2021

I guess this is related to #852 (comment)

If I add R=True to the line:

pprint(r.active_branch.commit.diff('master', create_patch=True, R=True)[0].diff.decode('utf-8').split('\n')) 

Then I seem to get the right results. But this still seems like a bug! Will this be fixed someday?

I'm worried that if I add R=True now, then someday when this is fixed my program will silently stop working correctly.

@Byron
Copy link
Member

@Byron Byron commented Jul 13, 2021

Apparently this was believed to be fixed here: 14d7034. Maybe in your particular case it still is inverted? Or is the version you are using not the most recent one?

Thanks to a particularly occluded API GitPython can be really confusing, making it hard to understand what's diffed against what. Thus I would not be surprised if there are more issues slumbering there or the fix above isn't correct in all cases.

If you figure it out a PR would be welcome.

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

No branches or pull requests

2 participants