From 767604e03f5e454ae5b5c268cd5831c672f46de8 Mon Sep 17 00:00:00 2001 From: Martin de La Gorce Date: Wed, 31 Aug 2022 20:47:42 +0100 Subject: [PATCH] Use .gitignore files in the initial source directories (#3237) Solves https://github.com/psf/black/issues/2598 where Black wouldn't use .gitignore at folder/.gitignore if you ran `black folder` for example. Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com> --- CHANGES.md | 3 +++ src/black/__init__.py | 5 +++++ tests/test_black.py | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index a5ce3b1fbe2..6aa81a8f5c2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -44,6 +44,9 @@ - Black now uses the presence of debug f-strings to detect target version. (#3215) - Fix misdetection of project root and verbose logging of sources in cases involving `--stdin-filename` (#3216) +- Immediate `.gitignore` files in source directories given on the command line are now + also respected, previously only `.gitignore` files in the project root and + automatically discovered directories were respected (#3237) ### Documentation diff --git a/src/black/__init__.py b/src/black/__init__.py index 86a0b637442..ded4a736822 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -653,6 +653,11 @@ def get_sources( if exclude is None: exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) gitignore = get_gitignore(root) + p_gitignore = get_gitignore(p) + # No need to use p's gitignore if it is identical to root's gitignore + # (i.e. root and p point to the same directory). + if gitignore != p_gitignore: + gitignore += p_gitignore else: gitignore = None sources.update( diff --git a/tests/test_black.py b/tests/test_black.py index 089e043d639..abd4d00b8e8 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1990,6 +1990,13 @@ def test_nested_gitignore(self) -> None: ) assert sorted(expected) == sorted(sources) + def test_nested_gitignore_directly_in_source_directory(self) -> None: + # https://github.com/psf/black/issues/2598 + path = Path(DATA_DIR / "nested_gitignore_tests") + src = Path(path / "root" / "child") + expected = [src / "a.py", src / "c.py"] + assert_collected_sources([src], expected) + def test_invalid_gitignore(self) -> None: path = THIS_DIR / "data" / "invalid_gitignore_tests" empty_config = path / "pyproject.toml"