Load .gitignore and exclude regex at time of use

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>
This commit is contained in:
Richard Si 2022-08-05 14:04:43 -04:00
parent e269f44b25
commit afed2c0190

View File

@ -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:
@ -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(),