Skip to content

Commit 14b6e61

Browse files
fix: Enhace black efficiently to skip directories listed in .gitignore (#4415)
1 parent b1c4dd9 commit 14b6e61

File tree

10 files changed

+13
-0
lines changed

10 files changed

+13
-0
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848

4949
<!-- Changes that improve Black's performance. -->
5050

51+
- Improve performance when a large directory is listed in `.gitignore` (#4415)
52+
5153
### Output
5254

5355
<!-- Changes to Black's terminal output and error messages -->

src/black/files.py

+2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ def _path_is_ignored(
309309
for gitignore_path, pattern in gitignore_dict.items():
310310
try:
311311
relative_path = path.relative_to(gitignore_path).as_posix()
312+
if path.is_dir():
313+
relative_path = relative_path + "/"
312314
except ValueError:
313315
break
314316
if pattern.match_file(relative_path):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
large_ignored_dir/
2+
large_ignored_dir_two
3+
abc.py

tests/data/ignore_directory_gitignore_tests/abc.py

Whitespace-only changes.

tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/a.py

Whitespace-only changes.

tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner/b.py

Whitespace-only changes.

tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner2/c.py

Whitespace-only changes.

tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner3/d.py

Whitespace-only changes.

tests/data/ignore_directory_gitignore_tests/z.py

Whitespace-only changes.

tests/test_black.py

+6
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,12 @@ def test_gitignore_that_ignores_subfolders(self) -> None:
25372537
expected = [target / "b.py"]
25382538
assert_collected_sources([target], expected, root=root)
25392539

2540+
def test_gitignore_that_ignores_directory(self) -> None:
2541+
# If gitignore with a directory is in root
2542+
root = Path(DATA_DIR, "ignore_directory_gitignore_tests")
2543+
expected = [root / "z.py"]
2544+
assert_collected_sources([root], expected, root=root)
2545+
25402546
def test_empty_include(self) -> None:
25412547
path = DATA_DIR / "include_exclude_tests"
25422548
src = [path]

0 commit comments

Comments
 (0)