diff --git a/src/black/files.py b/src/black/files.py index c58970e53e3..ccd7f5e37f4 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -182,6 +182,19 @@ def normalize_path_maybe_ignore( return root_relative_path +def path_is_ignored( + path: Path, gitignore_dict: Dict[Path, PathSpec], report: Report +) -> bool: + for gitignore_path, pattern in gitignore_dict.items(): + relative_path = normalize_path_maybe_ignore(path, gitignore_path, report) + if relative_path is None: + break + if pattern.match_file(relative_path): + report.path_ignored(path, "matches a .gitignore file content") + return True + return False + + def path_is_excluded( normalized_path: str, pattern: Optional[Pattern[str]], @@ -212,18 +225,6 @@ def gen_python_files( `report` is where output about exclusions goes. """ - def is_ignored( - gitignore_dict: Dict[Path, PathSpec], child: Path, report: Report - ) -> bool: - for _dir, _gitignore in gitignore_dict.items(): - relative_path = normalize_path_maybe_ignore(child, _dir, report) - if relative_path is None: - break - if _gitignore is not None and _gitignore.match_file(relative_path): - report.path_ignored(child, "matches the .gitignore file content") - return True - return False - assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}" for child in paths: normalized_path = normalize_path_maybe_ignore(child, root, report) @@ -231,7 +232,7 @@ def is_ignored( continue # First ignore files matching .gitignore, if passed - if gitignore_dict is not None and is_ignored(gitignore_dict, child, report): + if gitignore_dict is not None and path_is_ignored(child, gitignore_dict, report): continue # Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options.