import colorama # noqa: F401
-@lru_cache()
+@lru_cache
def find_project_root(
srcs: Sequence[str], stdin_filename: Optional[str] = None
) -> Tuple[Path, str]:
return SpecifierSet(",".join(str(s) for s in specifiers))
-@lru_cache()
+@lru_cache
def find_user_pyproject_toml() -> Path:
r"""Return the path to the top-level user configuration for black.
return user_config_path.resolve()
-@lru_cache()
+@lru_cache
def get_gitignore(root: Path) -> PathSpec:
"""Return a PathSpec matching gitignore content if present."""
gitignore = root / ".gitignore"
return root_relative_path
-def path_is_ignored(
- path: Path, gitignore_dict: Dict[Path, PathSpec], report: Report
+def _path_is_ignored(
+ root_relative_path: str,
+ root: Path,
+ gitignore_dict: Dict[Path, PathSpec],
+ report: Report,
) -> bool:
+ path = root / root_relative_path
+ # Note that this logic is sensitive to the ordering of gitignore_dict. Callers must
+ # ensure that gitignore_dict is ordered from least specific to most specific.
for gitignore_path, pattern in gitignore_dict.items():
- relative_path = normalize_path_maybe_ignore(path, gitignore_path, report)
- if relative_path is None:
+ try:
+ relative_path = path.relative_to(gitignore_path).as_posix()
+ except ValueError:
break
if pattern.match_file(relative_path):
- report.path_ignored(path, "matches a .gitignore file content")
+ report.path_ignored(
+ path.relative_to(root), "matches a .gitignore file content"
+ )
return True
return False
continue
# First ignore files matching .gitignore, if passed
- if gitignore_dict and path_is_ignored(child, gitignore_dict, report):
+ if gitignore_dict and _path_is_ignored(
+ normalized_path, root, gitignore_dict, report
+ ):
continue
# Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options.