-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add --force-exclude argument #1032
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
de5723e
add --force-exclude option
itajaja eba7046
use path_empty() to reduce the complexity of main()
yukw777 80965ae
Merge pull request #1 from yukw777/fix-438-lint
itajaja d978ee0
Merge branch 'fix-438' of https://github.com/itajaja/black into itaja…
JelleZijlstra e35a10f
make force-exclude also exclude
JelleZijlstra dea24fb
fix lint errors
JelleZijlstra 84d1308
Merge remote-tracking branch 'upstream/master' into itajaja-fix-438
JelleZijlstra e41a2db
debug travis failure
JelleZijlstra 3f2acae
more travis debugging
JelleZijlstra 6945ced
fix typo
JelleZijlstra 4bb9f06
what's in gitignore?
JelleZijlstra 6b88e77
clear the lru cache
JelleZijlstra 3d53be6
make gitignore match only the part of the path within the project
JelleZijlstra 5cd220c
normalize the path first
JelleZijlstra 3661593
Merge remote-tracking branch 'upstream/master' into itajaja-fix-438
JelleZijlstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
Pattern, | ||
Sequence, | ||
Set, | ||
Sized, | ||
Tuple, | ||
TypeVar, | ||
Union, | ||
|
@@ -360,6 +361,14 @@ def target_version_option_callback( | |
), | ||
show_default=True, | ||
) | ||
@click.option( | ||
"--force-exclude", | ||
type=str, | ||
help=( | ||
"Like --exclude, but in these case files and directories will be excluded " | ||
"even when they are passed explicitly as arguments" | ||
), | ||
) | ||
@click.option( | ||
"-q", | ||
"--quiet", | ||
|
@@ -412,6 +421,7 @@ def main( | |
verbose: bool, | ||
include: str, | ||
exclude: str, | ||
force_exclude: Optional[str], | ||
src: Tuple[str, ...], | ||
config: Optional[str], | ||
) -> None: | ||
|
@@ -453,27 +463,48 @@ def main( | |
except re.error: | ||
err(f"Invalid regular expression for exclude given: {exclude!r}") | ||
ctx.exit(2) | ||
try: | ||
force_exclude_regex = ( | ||
re_compile_maybe_verbose(force_exclude) if force_exclude else None | ||
) | ||
except re.error: | ||
err(f"Invalid regular expression for exclude given: {exclude!r}") | ||
ctx.exit(2) | ||
report = Report(check=check, diff=diff, quiet=quiet, verbose=verbose) | ||
root = find_project_root(src) | ||
sources: Set[Path] = set() | ||
path_empty(src, quiet, verbose, ctx) | ||
path_empty(src, "No Path provided. Nothing to do 😴", quiet, verbose, ctx) | ||
for s in src: | ||
p = Path(s) | ||
if p.is_dir(): | ||
sources.update( | ||
gen_python_files_in_dir( | ||
p, root, include_regex, exclude_regex, report, get_gitignore(root) | ||
gen_python_files( | ||
p.iterdir(), | ||
root, | ||
include_regex, | ||
exclude_regex, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this should also obey the force_exclude_regex. It will be surprising if I force-exclude a file and it gets included anyway because it's in a directory I included. |
||
report, | ||
get_gitignore(root), | ||
) | ||
) | ||
elif p.is_file() or s == "-": | ||
# if a file was explicitly given, we don't care about its extension | ||
elif s == "-": | ||
sources.add(p) | ||
elif p.is_file(): | ||
sources.update( | ||
gen_python_files( | ||
[p], root, None, force_exclude_regex, report, get_gitignore(root) | ||
) | ||
) | ||
else: | ||
err(f"invalid path: {s}") | ||
if len(sources) == 0: | ||
if verbose or not quiet: | ||
out("No Python files are present to be formatted. Nothing to do 😴") | ||
ctx.exit(0) | ||
|
||
path_empty( | ||
sources, | ||
"No Python files are present to be formatted. Nothing to do 😴", | ||
quiet, | ||
verbose, | ||
ctx, | ||
) | ||
|
||
if len(sources) == 1: | ||
reformat_one( | ||
|
@@ -495,14 +526,14 @@ def main( | |
|
||
|
||
def path_empty( | ||
src: Tuple[str, ...], quiet: bool, verbose: bool, ctx: click.Context | ||
src: Sized, msg: str, quiet: bool, verbose: bool, ctx: click.Context | ||
) -> None: | ||
""" | ||
Exit if there is no `src` provided for formatting | ||
""" | ||
if not src: | ||
if len(src) == 0: | ||
if verbose or not quiet: | ||
out("No Path provided. Nothing to do 😴") | ||
out(msg) | ||
ctx.exit(0) | ||
|
||
|
||
|
@@ -3527,11 +3558,11 @@ def get_gitignore(root: Path) -> PathSpec: | |
return PathSpec.from_lines("gitwildmatch", lines) | ||
|
||
|
||
def gen_python_files_in_dir( | ||
path: Path, | ||
def gen_python_files( | ||
paths: Iterable[Path], | ||
root: Path, | ||
include: Pattern[str], | ||
exclude: Pattern[str], | ||
include: Optional[Pattern[str]], | ||
exclude: Optional[Pattern[str]], | ||
report: "Report", | ||
gitignore: PathSpec, | ||
) -> Iterator[Path]: | ||
|
@@ -3543,7 +3574,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: | ||
# First ignore files matching .gitignore | ||
if gitignore.match_file(child.as_posix()): | ||
report.path_ignored(child, f"matches the .gitignore file content") | ||
|
@@ -3568,18 +3599,18 @@ def gen_python_files_in_dir( | |
if child.is_dir(): | ||
normalized_path += "/" | ||
|
||
exclude_match = exclude.search(normalized_path) | ||
exclude_match = exclude.search(normalized_path) if exclude else None | ||
if exclude_match and exclude_match.group(0): | ||
report.path_ignored(child, f"matches the --exclude regular expression") | ||
continue | ||
|
||
if child.is_dir(): | ||
yield from gen_python_files_in_dir( | ||
child, root, include, exclude, report, gitignore | ||
yield from gen_python_files( | ||
child.iterdir(), root, include, exclude, report, gitignore, | ||
) | ||
|
||
elif child.is_file(): | ||
include_match = include.search(normalized_path) | ||
include_match = include.search(normalized_path) if include else True | ||
if include_match: | ||
yield child | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should say
force_exclude
, notexclude
.