Skip to content

Commit

Permalink
Fix to use root gitignore in every source entry
Browse files Browse the repository at this point in the history
When passing multiple src directories, the root gitignore was only
applied to the first processed source. The reason is that, in the
first source, exclude is `None`, but then the value gets overridden by
`re_compile_maybe_verbose(DEFAULT_EXCLUDES)`, so in the next iteration
where the source is a directory, the condition is not met and sets the
value of `gitignore` to `None`.

To fix this problem, we set the new value of `exclude` is a new
variable (`_exclude`). This prevents entering the wrong condition on
the second iteration and also keeps the origina value if the condition
is not met.

Also, the value of both `_exclude` and `gitignore` is calculated only
once by using `None` as default value. This help us to reduce the
number of computations (only calculate the value once, if needed), and
also retains the original value of `gitignore`, which is then merged
with the value of `p_gitignore`. That's why we also define
`_gitignore` to contain the result of the merge (add) operation.

Signed-off-by: Antonio Ossa Guerra <aaossa@uc.cl>
  • Loading branch information
aaossa committed Oct 20, 2022
1 parent 575220f commit 4b6f4e3
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Sized,
Tuple,
Union,
cast,
)

import click
Expand Down Expand Up @@ -625,6 +626,9 @@ def get_sources(
sources: Set[Path] = set()
root = ctx.obj["root"]

_exclude = exclude
_gitignore = gitignore = None

for s in src:
if s == "-" and stdin_filename:
p = Path(stdin_filename)
Expand Down Expand Up @@ -659,25 +663,27 @@ 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)
if _exclude is None:
_exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
if gitignore is None:
gitignore = get_gitignore(root)
p_gitignore = get_gitignore(p)
# No need to use p's gitignore if it is identical to root's gitignore
# (i.e. root and p point to the same directory).
if gitignore != p_gitignore:
gitignore += p_gitignore
else:
gitignore = None
if gitignore == p_gitignore:
_gitignore = gitignore
else:
_gitignore = gitignore + p_gitignore
sources.update(
gen_python_files(
p.iterdir(),
ctx.obj["root"],
include,
exclude,
cast(Pattern[str], _exclude),
extend_exclude,
force_exclude,
report,
gitignore,
_gitignore,
verbose=verbose,
quiet=quiet,
)
Expand Down

0 comments on commit 4b6f4e3

Please sign in to comment.