Skip to content

Split diff line by '\t' for metadata and path #398

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

Merged
merged 2 commits into from
Mar 16, 2016

Conversation

jonathanchu
Copy link
Contributor

What's this PR do?

This protects against .split(None), which uses consecutive whitespace as a separator and may overlook paths where a single space is the filename. While having a filename consisting of only a single space is unlikely in practical usage, it is entirely possible.

For example, take this diff line:

screenshot 2016-03-15 14 18 42

Notice there is a tab after the change_type and a space for the path. The deleted file is a file named ' ' (just one space) at the git root. To replicate this issue, add and commit this file, then delete and commit. This will produce the following output from git diff:

$ git diff --name-status <SHA1> <SHA2>
D
M       path/to/another/file.py
...

This would cause the initial .split(None, 5) to fail as it will count all consecutive whitespace as a separator, disregarding the ' ' (single space) filename.

This protects against `.split(None)` which uses consecutive whitespace
as a separator to overlook paths where a single space is the filename.
For example, in this diff line:

line = ':100644 000000 e69de29
0000000000000000000000000000000000000000 D       '

The deleted file is a file named ' ' (just one space).  It's entirely
possible to commit this, remove, and to produce the following output
from `git diff`:

git diff --name-status <SHA1> <SHA2>
D
M       path/to/another/file.py
...

This would cause the initial `.split(None, 5)` to fail as it will count
all consecutive whitespace as a separator, disregarding the ' ' (single
space)  filename.
@Byron
Copy link
Member

Byron commented Mar 15, 2016

Thanks for the elaborate explanation and the fix! Do you think it's feasible to add a test for this as well, maybe with a fixture?

@jonathanchu
Copy link
Contributor Author

And thanks for the quick response @Byron! :) Sure thing, I was just looking into that after I created this PR actually.

I'll take a look at the current tests tonight and will see if I can work something in. I think it should be feasible and definitely worth it.

@Byron Byron added this to the v1.0.3 - Fixes milestone Mar 15, 2016
This tests the edge case of doing a diff against a single whitespace
filename and returns the proper change type.  All other normal usage of
this diff classmethod should remain unchanged.
@jonathanchu
Copy link
Contributor Author

@Byron can you review my test and fixture in e328ffd, if this fits and is in line with the rest of the library? Thanks!

@Byron
Copy link
Member

Byron commented Mar 16, 2016

Thanks so much, it looks great!

Byron added a commit that referenced this pull request Mar 16, 2016
Split diff line by '\t' for metadata and path
@Byron Byron merged commit c877794 into gitpython-developers:master Mar 16, 2016
@jonathanchu
Copy link
Contributor Author

🍻 :)

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

Successfully merging this pull request may close these issues.

2 participants