Skip to content

Wrong change_type for diff('HEAD') #882

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

Closed
aruehl opened this issue Jun 14, 2019 · 3 comments
Closed

Wrong change_type for diff('HEAD') #882

aruehl opened this issue Jun 14, 2019 · 3 comments

Comments

@aruehl
Copy link

aruehl commented Jun 14, 2019

I've seen Issue #531 and #534, but this failure exists further on.

change_type, new_file and deleted_file have wrong values.

It seems for me, that the detection for added and removed files are interchanged.

Here is what I've done:

$ git init
$ echo "removed" > removed.txt
$ git add removed.txt
$ git commit -m "removed.txt added"
$ echo "added" > added.txt
$ git add added.txt
$ git rm removed.txt
$ git status -s
A  added.txt
D  removed.txt
$

executing the script

from git import Repo, IndexFile
repo = Repo(".")
for diff_item in repo.index.diff("HEAD"):
        print(diff_item.a_path)
        print(diff_item.change_type)
        print(diff_item.new_file)
        print(diff_item.deleted_file)
        print()
$ python3 difffailure.py
added.txt
D
False
True

removed.txt
A
True
False

$ python3 --version
Python 3.6.6
$ python3 -m pip show GitPython
Name: GitPython
Version: 2.1.11
[...]
@aruehl aruehl changed the title Wrong change_type for diff('HEAD) Wrong change_type for diff('HEAD') Jun 14, 2019
@elpiekay
Copy link

I don't think it's a bug.

In git status -s, it shows what have been changed from HEAD to index. In repo.index.diff("HEAD"), it shows what have been changed from index to HEAD.

To get the right diff, you may use repo.index.diff("HEAD", R=True) instead.

@Byron
Copy link
Member

Byron commented Jul 20, 2019

Maybe this part of the docs helps to clarify what it is doing.

Do you still think the behaviour is incorrect?

In the meantime, I assume it's not a bug and we can reopen as needed.

@Flamefire
Copy link

I had the same issue just now and the docs are not clear enough to me:

Use the diff framework if you want to implement git-status like functionality.

A diff between the index and the commit’s tree your HEAD points to

   use repo.index.diff(repo.head.commit)

If I add a file to the index the diff from index to commit should show it as added. But here it is the other way round.

The documentation says "between" here which doesn't specify the "direction" of the diff.

To show why I was confused:

repo.index.remove(files)
diff = repo.index.diff(repo.head.commit)

I.e. I use repo.index in both cases and would expect the diff to reflect the operation on the index

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