Skip to content
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

Exclude folder using exclamation mark ('!') doesn't work #6

Closed
wangxuepeng opened this issue Nov 1, 2014 · 8 comments
Closed

Exclude folder using exclamation mark ('!') doesn't work #6

wangxuepeng opened this issue Nov 1, 2014 · 8 comments
Labels

Comments

@wangxuepeng
Copy link

I'm using pattern
"""
!test1/
*.txt
"""

to scan a folder, from gitingore description
(http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository)

ignore all files in the build/ directory

build/

this should exclude everything under test1 folder, but the run result shows the line dose not work, the result still show all contents under test 1 folder.

[An Example Folder Structure]
d:\dev\eclipse_workspace\test_scan\src\test1\a.txt
d:\dev\eclipse_workspace\test_scan\src\test1\b.txt
d:\dev\eclipse_workspace\test_scan\src\test1\c\c1.txt
d:\dev\eclipse_workspace\test_scan\src\test2\a.txt
d:\dev\eclipse_workspace\test_scan\src\test2\b.txt
d:\dev\eclipse_workspace\test_scan\src\test2\c\c1.txt

[Code]
SCAN_PATTERN = """
!test1/
*.txt
"""

spec = pathspec.PathSpec.from_lines(pathspec.GitIgnorePattern, SCAN_PATTERN.splitlines())
spec.match_tree('d:\dev\eclipse_workspace\test_scan')

[Run Result]
d:\dev\eclipse_workspace\test_scan\src\test1\a.txt
d:\dev\eclipse_workspace\test_scan\src\test1\b.txt
d:\dev\eclipse_workspace\test_scan\src\test1\c\c1.txt
d:\dev\eclipse_workspace\test_scan\src\test2\a.txt
d:\dev\eclipse_workspace\test_scan\src\test2\b.txt
d:\dev\eclipse_workspace\test_scan\src\test2\c\c1.txt

Can you please have a look? Thanks.

@cpburnz
Copy link
Owner

cpburnz commented Nov 1, 2014

@029xue Try switching the order of your patterns to:

*.txt
!test1/

If you have !test1/ first, the files under that directory will be ignored first, but with *.txt following it will then re-include the .txt files within test1/.

A second possibility is that the Windows paths are not matching properly. Can you try just using:

!test1/

And let me know if that excludes the test1/ directory.

@wangxuepeng
Copy link
Author

Hi,
Thanks for the quick response.

I tested
*.txt
!test1/

and it shows no difference with

!test1/
*.txt

and, if just leave
!test1/

The result is empty, no any files is selected.

@cpburnz
Copy link
Owner

cpburnz commented Nov 3, 2014

The underlying issue is that Windows file paths are not being matched properly because Windows tends to use \ instead of / to separate directories. I will work on fix for this.

@cpburnz cpburnz added the bug label Nov 3, 2014
@wangxuepeng
Copy link
Author

Thanks very much, waiting your updates :)

@cpburnz
Copy link
Owner

cpburnz commented Nov 7, 2014

@029xue I made some changes, and with my testing it should work now. Please note that the order of your patterns should be:

*.txt
!test1/

To first include all *.txt files, and then to exclude the ones from the test1/ directory.

Also, spec.match_tree() and spec.match_files() now return an iterable instead of a set, so you'll have to convert it to a list or set manually. E.g.,

list(spec.match_tree('d:\dev\eclipse_workspace\test_scan'))

The changes are only reflected on Github right now. If my changes work for you, let me know and I'll update the version on PyPI.

@wangxuepeng
Copy link
Author

Thanks, works like charm now.

So the patterns are working like filters from up to down? or just the excludes need be put in the last?

@cpburnz
Copy link
Owner

cpburnz commented Nov 8, 2014

Yes, the filters work from up to down. The order is what really matters. This is so that you can include (or exclude) a large amount of files, but then further down you really want to include (or exclude) a specific directory or smaller set of files.

@cpburnz cpburnz closed this as completed Nov 8, 2014
@wangxuepeng
Copy link
Author

That make sense, thanks very much for the good library :)

davidfraser added a commit to davidfraser/python-path-specification that referenced this issue Aug 7, 2015
In 0.3.2, the fix to Issue cpburnz#6 in commit 45829da also changed the API of the spec object to return iterators instead of sets (see cpburnz#6 (comment)). This can break existing code using the library, so should be clearly mentioned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants