Skip to content

Commit

Permalink
Merge pull request #122 from troylar/replace_fnmatch_with_pathspec
Browse files Browse the repository at this point in the history
Replace fnmatch with pathspec for more thorough gitignore support
  • Loading branch information
MichaelAquilina committed Jun 18, 2018
2 parents ac7694a + a1ba4ac commit ccc34f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ tqdm>=4.8.4
scandir>=1.5
inotify-simple>=1.1.7
enum34>=1.1.6
pathspec>=0.5.6
7 changes: 4 additions & 3 deletions s4/clients/local.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

import fnmatch
import pathspec
import gzip
import json
import logging
Expand Down Expand Up @@ -31,12 +31,13 @@ def get_local_client(target):
def traverse(path, ignore_files=None):
if not os.path.exists(path):
return

if ignore_files is None:
ignore_files = []

for item in scandir(path):
if any(fnmatch.fnmatch(item.name, pattern) for pattern in ignore_files):
full_path = os.path.join(path, item.name)
spec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ignore_files)
if (spec.match_file(full_path)):
logger.debug('Ignoring %s', item)
continue

Expand Down
17 changes: 17 additions & 0 deletions tests/clients/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ def test_correct_output(self):
expected_output = ['bar.md', 'baz/bar', 'baz/zoo', 'foo', 'SomeProject/hello.py']
assert sorted(actual_output) == sorted(expected_output)

def test_ignore_subfolder(self):
items = [
'dev/files/test.txt',
'dev/venv/python2.7/pip-selfcheck.json',
'dev/venv/python2.7/lib/file',
'dev/.git/index',
'dev/main.py',
]
for item in items:
utils.write_local(os.path.join(self.target_folder, item))
actual_output = list(local.traverse(
self.target_folder,
ignore_files={'venv/', '.git/'}
))
expected_output = ['dev/main.py', 'dev/files/test.txt']
assert sorted(actual_output) == sorted(expected_output)


class TestLocalSyncClient(object):
def test_get_client_name(self, local_client):
Expand Down

0 comments on commit ccc34f9

Please sign in to comment.