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

Fix: black only respects the root gitignore. #2225

Merged
merged 10 commits into from May 16, 2021
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,8 @@

### _Black_

- Respect `.gitignore` files in all levels, not only `root/.gitignore` file (apply
`.gitignore` rules like `git` does) (#2225)
- Fix typos discovered by codespell (#2228)
- Restored compatibility with Click 8.0 on Python 3.6 when LANG=C used (#2227)

Expand Down
2,632 changes: 1,316 additions & 1,316 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/black/files.py
Expand Up @@ -212,7 +212,7 @@ def gen_python_files(
extend_exclude,
force_exclude,
report,
gitignore,
gitignore + get_gitignore(child) if gitignore else None,
)

elif child.is_file():
Expand Down
1 change: 1 addition & 0 deletions tests/data/gitignore_levels_tests/.gitignore
@@ -0,0 +1 @@
ignored_in_root.py
1 change: 1 addition & 0 deletions tests/data/gitignore_levels_tests/child_dir/.gitignore
@@ -0,0 +1 @@
ignored_in_child.py
@@ -0,0 +1 @@
# should be excluded
@@ -0,0 +1 @@
# should be excluded
1 change: 1 addition & 0 deletions tests/data/gitignore_levels_tests/child_dir/included.py
@@ -0,0 +1 @@
# should be included
1 change: 1 addition & 0 deletions tests/data/gitignore_levels_tests/ignored_in_child.py
@@ -0,0 +1 @@
# should be included
1 change: 1 addition & 0 deletions tests/data/gitignore_levels_tests/ignored_in_root.py
@@ -0,0 +1 @@
# should be excluded
1 change: 1 addition & 0 deletions tests/data/gitignore_levels_tests/included.py
@@ -0,0 +1 @@
# should be included
28 changes: 27 additions & 1 deletion tests/test_black.py
Expand Up @@ -1406,7 +1406,7 @@ def test_include_exclude(self) -> None:
)
self.assertEqual(sorted(expected), sorted(sources))

def test_gitingore_used_as_default(self) -> None:
def test_gitignore_used_as_default(self) -> None:
path = Path(THIS_DIR / "data" / "include_exclude_tests")
include = re.compile(r"\.pyi?$")
extend_exclude = re.compile(r"/exclude/")
Expand Down Expand Up @@ -1703,6 +1703,32 @@ def test_gitignore_exclude(self) -> None:
)
self.assertEqual(sorted(expected), sorted(sources))

def test_respect_gitignore_levels(self) -> None:
path = Path(THIS_DIR / "data" / "gitignore_levels_tests")
include = re.compile(r"\.pyi?$")
exclude = re.compile(r"")
root_gitignore = black.files.get_gitignore(path)
report = black.Report()
expected: List[Path] = [
Path(path / "included.py"),
Path(path / "ignored_in_child.py"),
Path(path / "child_dir/included.py"),
]
this_abs = THIS_DIR.resolve()
sources = list(
black.gen_python_files(
path.iterdir(),
this_abs,
include,
exclude,
None,
None,
report,
root_gitignore,
)
)
self.assertEqual(sorted(expected), sorted(sources))

def test_empty_include(self) -> None:
path = THIS_DIR / "data" / "include_exclude_tests"
report = black.Report()
Expand Down