Skip to content

attr_file: fix handling of directory patterns with trailing spaces#4614

Merged
ethomson merged 1 commit intolibgit2:masterfrom
pks-t:pks/gitignore-trailing-spaces
Apr 16, 2018
Merged

attr_file: fix handling of directory patterns with trailing spaces#4614
ethomson merged 1 commit intolibgit2:masterfrom
pks-t:pks/gitignore-trailing-spaces

Conversation

@pks-t
Copy link
Copy Markdown
Member

@pks-t pks-t commented Apr 6, 2018

When comparing whether a path matches a directory rule, we pass the
both the path and directory name to fnmatch with
GIT_ATTR_FNMATCH_DIRECTORY being set. fnmatch expects the pattern to
contain no trailing directory '/', which is why we try to always strip
patterns of trailing slashes. We do not handle that case correctly
though when the pattern itself has trailing spaces, causing the match to
fail.

Fix the issue by stripping trailing spaces and tabs for a rule previous
to checking whether the pattern is a directory pattern with a trailing
'/'. Add a test to catch future breakage.

if (--spec->length == 0)
return GIT_ENOTFOUND;

/* Remove trailing spaces. */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this make 5cb6a2c#L216-225 redundant?

@segevfiner
Copy link
Copy Markdown
Contributor

Please verify that #4614 fixes your issue
#4610 (comment)

It does. 👍

When comparing whether a path matches a directory rule, we pass the
both the path and directory name to `fnmatch` with
`GIT_ATTR_FNMATCH_DIRECTORY` being set. `fnmatch` expects the pattern to
contain no trailing directory '/', which is why we try to always strip
patterns of trailing slashes. We do not handle that case correctly
though when the pattern itself has trailing spaces, causing the match to
fail.

Fix the issue by stripping trailing spaces and tabs for a rule previous
to checking whether the pattern is a directory pattern with a trailing
'/'. This replaces the whitespace-stripping in our ignore file parsing
code, which was stripping whitespaces too late. Add a test to catch
future breakage.
@pks-t
Copy link
Copy Markdown
Member Author

pks-t commented Apr 12, 2018

@segevfiner: you're right, it makes the code in the ignore parser redundant. Removed that code

@pks-t pks-t force-pushed the pks/gitignore-trailing-spaces branch from 8f2d0b4 to 251d877 Compare April 12, 2018 08:09
@ethomson ethomson merged commit 69870a6 into libgit2:master Apr 16, 2018
@ethomson
Copy link
Copy Markdown
Member

Thanks @segevfiner for reporting the issue and verifying the fix. Thanks @pks-t for fixing!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants