Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use .gitignore files in the initial source directories #3237

Merged
merged 4 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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