diff --git a/CHANGES.md b/CHANGES.md index 00c210ba86f..00508eb9d32 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -48,6 +48,8 @@ +- Improve performance when a large directory is listed in `.gitignore` (#4415) + ### Output diff --git a/src/black/files.py b/src/black/files.py index c0cadbfd890..3be1ed9b00f 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -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): diff --git a/tests/data/ignore_directory_gitignore_tests/.gitignore b/tests/data/ignore_directory_gitignore_tests/.gitignore new file mode 100644 index 00000000000..4573ac5b3ac --- /dev/null +++ b/tests/data/ignore_directory_gitignore_tests/.gitignore @@ -0,0 +1,3 @@ +large_ignored_dir/ +large_ignored_dir_two +abc.py diff --git a/tests/data/ignore_directory_gitignore_tests/abc.py b/tests/data/ignore_directory_gitignore_tests/abc.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/a.py b/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/a.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner/b.py b/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner/b.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner2/c.py b/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner2/c.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner3/d.py b/tests/data/ignore_directory_gitignore_tests/large_ignored_dir_two/inner3/d.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/data/ignore_directory_gitignore_tests/z.py b/tests/data/ignore_directory_gitignore_tests/z.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/test_black.py b/tests/test_black.py index 85141ae9124..34699b80663 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -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]