Skip to content

Commit

Permalink
honor exclude when specifying file path
Browse files Browse the repository at this point in the history
fixes #438

I Have not added or amended tests yet, but if we agree on the approach, I can work on them
  • Loading branch information
itajaja committed Sep 24, 2019
1 parent 0acad54 commit f864ffc
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions black.py
Expand Up @@ -441,8 +441,10 @@ def main(
gen_python_files_in_dir(p, root, include_regex, exclude_regex, report)
)
elif p.is_file() or s == "-":
# if a file was explicitly given, we don't care about its extension
sources.add(p)
# if a file was explicitly given, we bypass include
sources.update(
gen_python_files([p], root, include=None, exclude=exclude_regex, report=report)
)
else:
err(f"invalid path: {s}")
if len(sources) == 0:
Expand Down Expand Up @@ -3406,10 +3408,10 @@ def get_imports_from_children(children: List[LN]) -> Generator[str, None, None]:
return imports


def gen_python_files_in_dir(
path: Path,
def gen_python_files(
paths: [Path],
root: Path,
include: Pattern[str],
include: Optional[Pattern[str]],
exclude: Pattern[str],
report: "Report",
) -> Iterator[Path]:
Expand All @@ -3421,7 +3423,7 @@ def gen_python_files_in_dir(
`report` is where output about exclusions goes.
"""
assert root.is_absolute(), f"INTERNAL ERROR: `root` must be absolute but is {root}"
for child in path.iterdir():
for child in paths:
try:
normalized_path = "/" + child.resolve().relative_to(root).as_posix()
except ValueError:
Expand All @@ -3444,11 +3446,22 @@ def gen_python_files_in_dir(
yield from gen_python_files_in_dir(child, root, include, exclude, report)

elif child.is_file():
include_match = include.search(normalized_path)
if include_match:
# if an include regex is not provided, the file is included
if not include or include.search(normalized_path):
yield child


def gen_python_files_in_dir(
path: Path,
root: Path,
include: Pattern[str],
exclude: Pattern[str],
report: "Report",
) -> Iterator[Path]:
return gen_python_files(path.iterdir())



@lru_cache()
def find_project_root(srcs: Iterable[str]) -> Path:
"""Return a directory containing .git, .hg, or pyproject.toml.
Expand Down

0 comments on commit f864ffc

Please sign in to comment.