From 5cfd6cd573f7cea4ad4253b458bf8fe6395d6664 Mon Sep 17 00:00:00 2001 From: Martin de La Gorce Date: Wed, 24 Aug 2022 20:14:14 +0100 Subject: [PATCH 1/3] Read .gitignore in initial source directories Solves https://github.com/psf/black/issues/2598 where Black wouldn't use .gitignore at folder/.gitignore if you ran `black folder` for example. --- src/black/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 86a0b637442..b2d12a5f550 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -652,7 +652,7 @@ def get_sources( elif p.is_dir(): if exclude is None: exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) - gitignore = get_gitignore(root) + gitignore = get_gitignore(root) + get_gitignore(p) else: gitignore = None sources.update( From cfcf59b8c625514a549127874f021f6df9ee3b28 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Wed, 31 Aug 2022 13:04:27 -0400 Subject: [PATCH 2/3] Add changelog entry and test --- CHANGES.md | 3 +++ tests/test_black.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 34c54710775..d8e61871144 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -42,6 +42,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/tests/test_black.py b/tests/test_black.py index 5da247b71b0..377b4e3242c 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" From baa741b1b8c6497eb3bfd3e1cba1e5272da478b0 Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Wed, 31 Aug 2022 14:53:27 -0400 Subject: [PATCH 3/3] Minor optimization --- src/black/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index b2d12a5f550..ded4a736822 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -652,7 +652,12 @@ def get_sources( elif p.is_dir(): if exclude is None: exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES) - gitignore = get_gitignore(root) + get_gitignore(p) + 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(