gitignore_parser icon indicating copy to clipboard operation
gitignore_parser copied to clipboard

Fix incorrect anchoring at start of string

Open vlovich opened this issue 5 years ago • 4 comments

re.search will find substrings unless we anchor. I think all gitignore rules should be anchored but I'm not 100% sure (hard to tell without test coverage). Another solution could be to replace .search with .match but I don't see any obvious difference in behavior.

Fixes #10

vlovich avatar Jul 08 '20 16:07 vlovich

@vlovich I saw tests were added 9 days ago - it would be great if you could add a test that covers this case :+1:

@mherrmann once #15 is merged the test suite will be run on PR's to ensure they pass.

inverse avatar Aug 07 '20 19:08 inverse

@vlovich Can you resolve conflicts?

excitoon avatar Aug 26 '22 07:08 excitoon

There has been a deprecation that requires that flags be at the front of the regular expression. I don't think this is directly related to this issue; however the deprecation warning comes up when anchoring. The following change fixes this warning:

https://github.com/mherrmann/gitignore_parser/blob/1710b2374eb984459be7a948f7a44d151f96a4b3/gitignore_parser.py#L107 regex = regex[:5] + '^' + regex[5:]

This changes strings from being ^(?ms)<something> to (?ms)^\something

Javagedes avatar Aug 30 '22 20:08 Javagedes

@Javagedes It won't do anything. ^ can appear anywhere in the RE. The bug, however, affects only non-anchored matches.

Also, re.search in this application is like 100% slower than re.match() so I suggest to switch to latter. Check how I fixed it: https://github.com/excitoon/gitignorefile/blob/master/gitignorefile/init.py#L292

This exact regexp, BTW is 5-10% faster than which pathspec uses currently, I think it is best possible.

Also @mherrmann, you don't need these flags at all, they only generate warnings #30 and don't do anything useful. If you like them, consider adding them as re.M | re.S in the last argument to re.match()/re.search().

excitoon avatar Aug 30 '22 23:08 excitoon

The deprecation mentioned above is now a hard error in python 3.11. So this library is now totally broken on the latest python.

boxed avatar Jan 17 '23 19:01 boxed

@boxed added this to matrix test the repo https://github.com/mherrmann/gitignore_parser/pull/40

Highlights the problem exactly :)

inverse avatar Jan 17 '23 20:01 inverse

I'd be happy to merge a PR that fixes for Python 3.11.

mherrmann avatar Jan 21 '23 08:01 mherrmann

Superseded by #58.

mherrmann avatar Oct 04 '23 14:10 mherrmann