GitPython icon indicating copy to clipboard operation
GitPython copied to clipboard

Diff shows a_path even though the file is added

Open ishepard opened this issue 7 years ago • 3 comments

When a file is added, I think that a_path should be None and b_path should be equal to the name of the added file. This is the case when we create a diff setting create_patch=True. However, since by default this is False, diff returns a_path and b_path equals to the name of the added file. If you want to test it:

mkdir test
cd test
git init
touch asd.txt
git add asd.txt
git commit -m "add asd"
cd ..
import git
repo = git.Repo('test/')
diffs = repo.head.commit.diff(git.NULL_TREE)
for diff in diffs:
    print(diff.a_path)
    print(diff.b_path)
    print(diff.change_type)

this will return the following output:

asd.txt
asd.txt
A

how is it possible that, if the file is added, a_path is not None?

PS: this works also if the previous commit is not NULL_TREE, already tried.

ishepard avatar Apr 20 '18 08:04 ishepard

Thanks a lot for letting us know, I just reproduced the issue! This is not the first issue discovered related to the diffing logic/code.

Byron avatar Jun 05 '18 10:06 Byron

Note that this behavior is currently contrary to the documentation: https://gitpython.readthedocs.io/en/stable/reference.html#git.diff.Diff This lead to quite a bit of confusion for us when trying to use this function.

vidartf avatar Aug 02 '19 11:08 vidartf

@vidartf Thanks for letting me know! Maybe you can find the time to provide a PR adjusting the docs so that it becomes what you would have wanted to see. Generally, the diff implementation is confusing to many, and I am not quite sure if it's the API itself, the docs, or an incorrect implementation

Byron avatar Aug 12 '19 02:08 Byron