Skip to content

Commit

Permalink
fix: Enhace black efficiently to skip directories listed in .gitignore (
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Sunglasses authored Aug 2, 2024
1 parent b1c4dd9 commit 14b6e61
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

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

- Improve performance when a large directory is listed in `.gitignore` (#4415)

### Output

<!-- Changes to Black's terminal output and error messages -->
Expand Down
2 changes: 2 additions & 0 deletions src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ def _path_is_ignored(
for gitignore_path, pattern in gitignore_dict.items():
try:
relative_path = path.relative_to(gitignore_path).as_posix()
if path.is_dir():
relative_path = relative_path + "/"
except ValueError:
break
if pattern.match_file(relative_path):
Expand Down
3 changes: 3 additions & 0 deletions tests/data/ignore_directory_gitignore_tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
large_ignored_dir/
large_ignored_dir_two
abc.py
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,12 @@ def test_gitignore_that_ignores_subfolders(self) -> None:
expected = [target / "b.py"]
assert_collected_sources([target], expected, root=root)

def test_gitignore_that_ignores_directory(self) -> None:
# If gitignore with a directory is in root
root = Path(DATA_DIR, "ignore_directory_gitignore_tests")
expected = [root / "z.py"]
assert_collected_sources([root], expected, root=root)

def test_empty_include(self) -> None:
path = DATA_DIR / "include_exclude_tests"
src = [path]
Expand Down

0 comments on commit 14b6e61

Please sign in to comment.