python-pathspec icon indicating copy to clipboard operation
python-pathspec copied to clipboard

files inside an ignored sub-directory are not matched

Open spMohanty opened this issue 6 years ago • 4 comments

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 ?

spMohanty avatar Mar 18 '18 21:03 spMohanty

You're right, there is a discrepancy in the behavior. directoryG/* should match everything beneath directoryG which should include the files under directoryH.

cpburnz avatar Apr 05 '18 02:04 cpburnz

UPDATE: My evaluation in this comment was completely wrong and confused the issue.


I have not forgotten about this issue but I haven't had time to fix it yet.

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.

cpburnz avatar May 17 '18 13:05 cpburnz

Hey is there any workaround?

bukowa avatar Feb 09 '21 23:02 bukowa

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.

sausag3 avatar Dec 08 '21 02:12 sausag3

This is now fixed and will be available in v0.10.0 when it is released.

cpburnz avatar Aug 23 '22 03:08 cpburnz