python-pathspec
python-pathspec copied to clipboard
files inside an ignored sub-directory are not matched
Consider the case of a directory structure like :
.
|-- directoryD
| |-- fileE
| `-- fileF
|-- directoryG
| |-- directoryH
| | |-- fileI
| | |-- fileJ
| | |-- fileK
| | |-- fileL
| | |-- fileM
| | `-- fileN
| `-- fileO
|-- fileA
|-- fileB
|-- fileC
`-- .gitignore
The contents of the .gitignore
file are :
fileB
directoryD/*
directoryG/*
Now if we use pathspec
to match all the files with the specs defined in .gitignore
then here are the responses of match_file(filepath)
function:
|-- directoryD : False
| |-- fileE : True
| `-- fileF : True
|-- directoryG : False
| `-- directoryH : True
| |-- fileI : False
| |-- fileJ : False
| |-- fileK : False
| |-- fileL : False
| |-- fileM : False
| `-- fileN : False
| `-- fileO : True
|-- fileA : False
|-- fileB : True
|-- fileC : True
`-- .gitignore
If you compare it with the behaviour of .gitignore
inside a git repository, the files inside directoryH
should all return True
as a reponse to the match_file
function. Or am I getting something wrong ?
You're right, there is a discrepancy in the behavior. directoryG/*
should match everything beneath directoryG
which should include the files under directoryH
.
UPDATE: My evaluation in this comment was completely wrong and confused the issue.
I suspect the discrepancy is that a .gitignore does additional filtering beyond that of Git's wildmatch algorithm. If I fixed this in my implementation of wildmatch, it could unexpectedly change the results of anyone already using this for a purpose other than emulating .gitingore.
The core issue is that blacklisting files from an excluded directory is not currently implemented:
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded.
See: PATTERN FORMAT bullet 4.
Hey is there any workaround?
While I understand the dilemma above with breaking existing projects, as time goes on more projects are probably using this and probably a good number expect this to match the behavior Git has when trying to exactly match something as a .gitignore file. I've spent a few days on and off scratching my head trying to work out why files weren't being excluded properly (using Git itself as an interactive test to see what matching worked), only to find this open issue.
This is now fixed and will be available in v0.10.0 when it is released.