Skip to content

Commit afed2c0

Browse files
ichard26fabioz
andcommitted
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 <[email protected]>
1 parent e269f44 commit afed2c0

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/black/__init__.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,7 @@ def get_sources(
623623
) -> Set[Path]:
624624
"""Compute the set of files to be formatted."""
625625
sources: Set[Path] = set()
626-
627-
if exclude is None:
628-
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
629-
gitignore = get_gitignore(ctx.obj["root"])
630-
else:
631-
gitignore = None
626+
root = ctx.obj["root"]
632627

633628
for s in src:
634629
if s == "-" and stdin_filename:
@@ -663,6 +658,11 @@ def get_sources(
663658

664659
sources.add(p)
665660
elif p.is_dir():
661+
if exclude is None:
662+
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
663+
gitignore = get_gitignore(root)
664+
else:
665+
gitignore = None
666666
sources.update(
667667
gen_python_files(
668668
p.iterdir(),

0 commit comments

Comments
 (0)