Skip to content

Commit 1c4d549

Browse files
authored
CM-42771 - Fix top level .gitignore priorities (#275)
1 parent 34b86d5 commit 1c4d549

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

cycode/cli/files_collector/walk_ignore.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def _walk_to_top(path: str) -> Iterable[str]:
2424

2525
def _collect_top_level_ignore_files(path: str) -> List[str]:
2626
ignore_files = []
27-
for dir_path in _walk_to_top(path):
27+
top_paths = reversed(list(_walk_to_top(path))) # we must reverse it to make top levels more prioritized
28+
for dir_path in top_paths:
2829
for ignore_file in _SUPPORTED_IGNORE_PATTERN_FILES:
2930
ignore_file_path = os.path.join(dir_path, ignore_file)
3031
if os.path.exists(ignore_file_path):

cycode/cli/utils/ignore_utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,10 @@ def walk(self, **kwargs) -> Generator[Tuple[str, List[str], List[str]], None, No
396396

397397
# decrease recursion depth of os.walk() by ignoring subdirectories because of topdown=True
398398
# slicing ([:]) is mandatory to change dict in-place!
399-
dirnames[:] = [
400-
dirname for dirname in dirnames if not self.is_ignored(os.path.join(rel_dirpath, dirname, ''))
401-
]
399+
dirnames[:] = [d for d in dirnames if not self.is_ignored(os.path.join(rel_dirpath, d))]
402400

403401
# remove ignored files
404-
filenames = [os.path.basename(f) for f in filenames if not self.is_ignored(os.path.join(rel_dirpath, f))]
402+
filenames = [f for f in filenames if not self.is_ignored(os.path.join(rel_dirpath, f))]
405403

406404
yield dirpath, dirnames, filenames
407405

tests/cli/files_collector/test_walk_ignore.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,20 @@ def test_walk_ignore(fs: 'FakeFilesystem') -> None:
140140
assert normpath('/home/user/project/subproject/ignored.pyc') not in result
141141
# presented:
142142
assert normpath('/home/user/project/subproject/presented.py') in result
143+
144+
145+
def test_walk_ignore_top_level_ignores_order(fs: 'FakeFilesystem') -> None:
146+
fs.create_file('/home/user/.gitignore', contents='*.log')
147+
fs.create_file('/home/user/project/.gitignore', contents='!*.log') # rollback *.log ignore for project
148+
fs.create_dir('/home/user/project/subproject')
149+
150+
fs.create_file('/home/user/ignored.log')
151+
fs.create_file('/home/user/project/presented.log')
152+
fs.create_file('/home/user/project/subproject/presented.log')
153+
154+
path = normpath('/home/user/project')
155+
result = _collect_walk_ignore_files(path)
156+
assert len(result) == 3
157+
assert normpath('/home/user/ignored.log') not in result
158+
assert normpath('/home/user/project/presented.log') in result
159+
assert normpath('/home/user/project/subproject/presented.log') in result

0 commit comments

Comments
 (0)