Skip to content

Commit

Permalink
Use normalize path ignore to resolve and format root
Browse files Browse the repository at this point in the history
For invalid source, log it with colour red when displaying the SRCs log else keep it to the default blue colour. This is done to handle permission related errors and out of tree exceptions gracefully
  • Loading branch information
Shivansh-007 committed Dec 19, 2021
1 parent baa73d6 commit fa7df7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/black/__init__.py
Expand Up @@ -425,11 +425,21 @@ def main(
else:
out(f"Identified `{root}` as project root.", fg="blue")

normalized_paths = [
str(Path(source).resolve().relative_to(root)) for source in src
normalized = [
(normalize_path_maybe_ignore(Path(source), root), source)
for source in src
]
srcs_string = '", "'.join(normalized_paths)
out(f'Sources to be formatted: "{srcs_string}"', fg="blue")
normalized.append((None, "src/test"))
normalized.append(("src/blackd", "src/blackd"))
srcs_string = ", ".join(
[
f'"{_norm}"'
if _norm
else f'\033[31m"{source} (skipping - invalid)"\033[34m'
for _norm, source in normalized
]
)
out(f"Sources to be formatted: {srcs_string}", fg="blue")

if config:
config_source = ctx.get_parameter_source("config")
Expand Down
12 changes: 9 additions & 3 deletions src/black/files.py
Expand Up @@ -137,7 +137,9 @@ def get_gitignore(root: Path) -> PathSpec:


def normalize_path_maybe_ignore(
path: Path, root: Path, report: Report
path: Path,
root: Path,
report: Optional[Report] = None,
) -> Optional[str]:
"""Normalize `path`. May return `None` if `path` was ignored.
Expand All @@ -147,12 +149,16 @@ def normalize_path_maybe_ignore(
abspath = path if path.is_absolute() else Path.cwd() / path
normalized_path = abspath.resolve().relative_to(root).as_posix()
except OSError as e:
report.path_ignored(path, f"cannot be read because {e}")
if report:
report.path_ignored(path, f"cannot be read because {e}")
return None

except ValueError:
if path.is_symlink():
report.path_ignored(path, f"is a symbolic link that points outside {root}")
if report:
report.path_ignored(
path, f"is a symbolic link that points outside {root}"
)
return None

raise
Expand Down

0 comments on commit fa7df7e

Please sign in to comment.