Skip to content

Commit 767604e

Browse files
Use .gitignore files in the initial source directories (#3237)
Solves #2598 where Black wouldn't use .gitignore at folder/.gitignore if you ran `black folder` for example. Co-authored-by: Richard Si <[email protected]>
1 parent 2c90480 commit 767604e

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
- Black now uses the presence of debug f-strings to detect target version. (#3215)
4545
- Fix misdetection of project root and verbose logging of sources in cases involving
4646
`--stdin-filename` (#3216)
47+
- Immediate `.gitignore` files in source directories given on the command line are now
48+
also respected, previously only `.gitignore` files in the project root and
49+
automatically discovered directories were respected (#3237)
4750

4851
### Documentation
4952

src/black/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,11 @@ def get_sources(
653653
if exclude is None:
654654
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
655655
gitignore = get_gitignore(root)
656+
p_gitignore = get_gitignore(p)
657+
# No need to use p's gitignore if it is identical to root's gitignore
658+
# (i.e. root and p point to the same directory).
659+
if gitignore != p_gitignore:
660+
gitignore += p_gitignore
656661
else:
657662
gitignore = None
658663
sources.update(

tests/test_black.py

+7
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,13 @@ def test_nested_gitignore(self) -> None:
19901990
)
19911991
assert sorted(expected) == sorted(sources)
19921992

1993+
def test_nested_gitignore_directly_in_source_directory(self) -> None:
1994+
# https://github.com/psf/black/issues/2598
1995+
path = Path(DATA_DIR / "nested_gitignore_tests")
1996+
src = Path(path / "root" / "child")
1997+
expected = [src / "a.py", src / "c.py"]
1998+
assert_collected_sources([src], expected)
1999+
19932000
def test_invalid_gitignore(self) -> None:
19942001
path = THIS_DIR / "data" / "invalid_gitignore_tests"
19952002
empty_config = path / "pyproject.toml"

0 commit comments

Comments
 (0)