Skip to content

Commit 5813c91

Browse files
committed
add inheriting of ignore patterns
1 parent 7cb0f15 commit 5813c91

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cycode/cli/files_collector/walk_ignore.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from collections import defaultdict
3-
from typing import Iterable, List
3+
from typing import Generator, Iterable, List, Tuple
44

55
import pathspec
66
from pathspec.util import StrPath
@@ -48,7 +48,7 @@ def _should_include_path(ignore_patterns: List[str], path: StrPath) -> bool:
4848
return not path_spec.match_file(path) # works with both files and directories; negative match
4949

5050

51-
def walk_ignore(path: str) -> List[str]:
51+
def walk_ignore(path: str) -> Generator[Tuple[str, List[str], List[str]], None, None]:
5252
global_ignore_patterns = _get_global_ignore_patterns(path)
5353
path_to_ignore_patterns = defaultdict(list)
5454

@@ -58,7 +58,14 @@ def walk_ignore(path: str) -> List[str]:
5858
filepath = os.path.join(dirpath, filename)
5959
if filename in _SUPPORTED_IGNORE_PATTERN_FILES:
6060
logger.debug('Apply ignore file: %s', filepath)
61-
# TODO(MarshalX): accumulate ignore pattern from previous levels
61+
62+
parent_dir = os.path.dirname(dirpath)
63+
if dirpath not in path_to_ignore_patterns and parent_dir in path_to_ignore_patterns:
64+
# inherit ignore patterns from parent directory on first occurrence
65+
logger.debug('Inherit ignore patterns: %s', {'inherit_from': parent_dir, 'inherit_to': dirpath})
66+
path_to_ignore_patterns[dirpath].extend(path_to_ignore_patterns[parent_dir])
67+
68+
# always read ignore patterns for the current directory
6269
path_to_ignore_patterns[dirpath].extend(get_file_content(filepath).splitlines())
6370

6471
ignore_patterns = global_ignore_patterns + path_to_ignore_patterns.get(dirpath, [])

0 commit comments

Comments
 (0)