Skip to content

Commit

Permalink
Use .gitignore files in the initial source directories (#3237)
Browse files Browse the repository at this point in the history
Solves #2598 where Black wouldn't
use .gitignore at folder/.gitignore if you ran `black folder` for
example.

Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
  • Loading branch information
martinResearch and ichard26 authored Aug 31, 2022
1 parent 2c90480 commit 767604e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
- Black now uses the presence of debug f-strings to detect target version. (#3215)
- Fix misdetection of project root and verbose logging of sources in cases involving
`--stdin-filename` (#3216)
- Immediate `.gitignore` files in source directories given on the command line are now
also respected, previously only `.gitignore` files in the project root and
automatically discovered directories were respected (#3237)

### Documentation

Expand Down
5 changes: 5 additions & 0 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@ def get_sources(
if exclude is None:
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
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
sources.update(
Expand Down
7 changes: 7 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,13 @@ def test_nested_gitignore(self) -> None:
)
assert sorted(expected) == sorted(sources)

def test_nested_gitignore_directly_in_source_directory(self) -> None:
# https://github.com/psf/black/issues/2598
path = Path(DATA_DIR / "nested_gitignore_tests")
src = Path(path / "root" / "child")
expected = [src / "a.py", src / "c.py"]
assert_collected_sources([src], expected)

def test_invalid_gitignore(self) -> None:
path = THIS_DIR / "data" / "invalid_gitignore_tests"
empty_config = path / "pyproject.toml"
Expand Down

0 comments on commit 767604e

Please sign in to comment.