Skip to content

Commit

Permalink
Make nested function a top-level function
Browse files Browse the repository at this point in the history
The function to match a given path with every discovered .gitignore
file does not need to be a nested function and can be a top-level
function. The arguments did not change, but the naming of local
variables was improved for readability.

Signed-off-by: Antonio Ossa Guerra <aaossa@uc.cl>
  • Loading branch information
aaossa committed Nov 8, 2022
1 parent 5aee3b7 commit f257725
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/black/files.py
Expand Up @@ -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]],
Expand Down Expand Up @@ -212,26 +225,14 @@ 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)
if normalized_path is None:
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.
Expand Down

0 comments on commit f257725

Please sign in to comment.