Skip to content

Commit

Permalink
Load .gitignore and exclude regex at time of use
Browse files Browse the repository at this point in the history
Loading .gitignore and compiling the exclude regex can take more than
15ms. We shouldn't and don't need to pay this cost if we're simply
formatting files given on the command line directly.

I would've loved to lazily import pathspec, but the patch won't be clean
until the file collection and discovery logic is refactored first.

Co-authored-by: Fabio Zadrozny <fabiofz@gmail.com>
  • Loading branch information
ichard26 and fabioz committed Aug 27, 2022
1 parent e269f44 commit afed2c0
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/black/__init__.py
Expand Up @@ -623,12 +623,7 @@ def get_sources(
) -> Set[Path]:
"""Compute the set of files to be formatted."""
sources: Set[Path] = set()

if exclude is None:
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
gitignore = get_gitignore(ctx.obj["root"])
else:
gitignore = None
root = ctx.obj["root"]

for s in src:
if s == "-" and stdin_filename:
Expand Down Expand Up @@ -663,6 +658,11 @@ def get_sources(

sources.add(p)
elif p.is_dir():
if exclude is None:
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
gitignore = get_gitignore(root)
else:
gitignore = None
sources.update(
gen_python_files(
p.iterdir(),
Expand Down

0 comments on commit afed2c0

Please sign in to comment.