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: Enhace black efficiently to skip directories listed in .gitignore #4415

Merged
merged 5 commits into from
Aug 2, 2024
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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():
Mr-Sunglasses marked this conversation as resolved.
Show resolved Hide resolved
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.
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
Loading