]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix: black only respects the root gitignore. (#2225)
authorHadi Alqattan <alqattanhadizaki@gmail.com>
Sun, 16 May 2021 17:51:27 +0000 (20:51 +0300)
committerGitHub <noreply@github.com>
Sun, 16 May 2021 17:51:27 +0000 (13:51 -0400)
Commit history before merge:

Black now respects .gitignore files in all levels, not only root/.gitignore file
(apply .gitignore rules like git does).

* Fix: typo
* Fix: respect .gitignore files in all levels.
* Add: CHANGELOG note.
* Fix: TypeError: unsupported operand type(s) for +: 'NoneType' and 'PathSpec'
* Update docs.
* Fix: no parent .gitignore
* Add a comment since the if expression is a bit hard to understand
* Update tests - conver no parent .gitignore case.
* Use main's Pipfile.lock instead

  The original changes in Pipfile.lock are whitespace only. The changes
  turned the JSON's file indentation from 4 to 2. Effectively this
  happened: `json.dumps(json.loads(old_pipfile_lock), indent=2) + "\n"`.

  Just using main's Pipfile.lock instead of undoing the changes because
  1) I don't know how to do that easily and quickly, and 2) there's a
  merge conflict.

Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
* Merge remote-tracking branch 'upstream/main' into i1730 …

  conflicts for days ay?

14 files changed:
CHANGES.md
docs/usage_and_configuration/file_collection_and_discovery.md
src/black/files.py
tests/data/nested_gitignore_tests/pyproject.toml [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/.gitignore [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/a.py [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/b.py [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/c.py [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/child/.gitignore [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/child/a.py [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/child/b.py [new file with mode: 0644]
tests/data/nested_gitignore_tests/root/child/c.py [new file with mode: 0644]
tests/data/nested_gitignore_tests/x.py [new file with mode: 0644]
tests/test_black.py

index f2c795440f81965bcc42271d584907346a538d63..603554cd8b7d9f417edaeec6644cdd44a3bca54a 100644 (file)
@@ -4,6 +4,8 @@
 
 ### _Black_
 
+- Respect `.gitignore` files in all levels, not only `root/.gitignore` file (apply
+  `.gitignore` rules like `git` does) (#2225)
 - Restored compatibility with Click 8.0 on Python 3.6 when LANG=C used (#2227)
 
 ### _Blackd_
index 54c76cd9a0f27f70f7e9b02e2700023a94677975..1f436182dda415a92e84acb44f9299b19b6af58d 100644 (file)
@@ -30,8 +30,7 @@ then write the above files to `.cache/black/<version>/`.
 ## .gitignore
 
 If `--exclude` is not set, _Black_ will automatically ignore files and directories in
-`.gitignore` file, if present. The `.gitignore` file must be in the project root to be
-used and nested `.gitignore` aren't supported.
+`.gitignore` file(s), if present.
 
 If you want _Black_ to continue using `.gitignore` while also configuring the exclusion
 rules, please use `--extend-exclude`.
index 1be560643a1cae6500cb2fb81b645542b6feef39..a0c92e8c41539b282c68117b81752562d757ed03 100644 (file)
@@ -204,6 +204,8 @@ def gen_python_files(
             continue
 
         if child.is_dir():
+            # If gitignore is None, gitignore usage is disabled, while a Falsey
+            # gitignore is when the directory doesn't have a .gitignore file.
             yield from gen_python_files(
                 child.iterdir(),
                 root,
@@ -212,7 +214,7 @@ def gen_python_files(
                 extend_exclude,
                 force_exclude,
                 report,
-                gitignore,
+                gitignore + get_gitignore(child) if gitignore is not None else None,
             )
 
         elif child.is_file():
diff --git a/tests/data/nested_gitignore_tests/pyproject.toml b/tests/data/nested_gitignore_tests/pyproject.toml
new file mode 100644 (file)
index 0000000..9ba7ec2
--- /dev/null
@@ -0,0 +1,3 @@
+[build-system]
+requires = ["setuptools>=41.0", "setuptools-scm", "wheel"]
+build-backend = "setuptools.build_meta"
diff --git a/tests/data/nested_gitignore_tests/root/.gitignore b/tests/data/nested_gitignore_tests/root/.gitignore
new file mode 100644 (file)
index 0000000..2987e7b
--- /dev/null
@@ -0,0 +1 @@
+a.py
diff --git a/tests/data/nested_gitignore_tests/root/a.py b/tests/data/nested_gitignore_tests/root/a.py
new file mode 100644 (file)
index 0000000..7135cfd
--- /dev/null
@@ -0,0 +1 @@
+# should be excluded (root/.gitignore)
diff --git a/tests/data/nested_gitignore_tests/root/b.py b/tests/data/nested_gitignore_tests/root/b.py
new file mode 100644 (file)
index 0000000..bdeeca3
--- /dev/null
@@ -0,0 +1 @@
+# should be included
diff --git a/tests/data/nested_gitignore_tests/root/c.py b/tests/data/nested_gitignore_tests/root/c.py
new file mode 100644 (file)
index 0000000..bdeeca3
--- /dev/null
@@ -0,0 +1 @@
+# should be included
diff --git a/tests/data/nested_gitignore_tests/root/child/.gitignore b/tests/data/nested_gitignore_tests/root/child/.gitignore
new file mode 100644 (file)
index 0000000..6df81dd
--- /dev/null
@@ -0,0 +1 @@
+b.py
diff --git a/tests/data/nested_gitignore_tests/root/child/a.py b/tests/data/nested_gitignore_tests/root/child/a.py
new file mode 100644 (file)
index 0000000..7135cfd
--- /dev/null
@@ -0,0 +1 @@
+# should be excluded (root/.gitignore)
diff --git a/tests/data/nested_gitignore_tests/root/child/b.py b/tests/data/nested_gitignore_tests/root/child/b.py
new file mode 100644 (file)
index 0000000..c91d479
--- /dev/null
@@ -0,0 +1 @@
+# should be excluded (child/.gitignore)
diff --git a/tests/data/nested_gitignore_tests/root/child/c.py b/tests/data/nested_gitignore_tests/root/child/c.py
new file mode 100644 (file)
index 0000000..bdeeca3
--- /dev/null
@@ -0,0 +1 @@
+# should be included
diff --git a/tests/data/nested_gitignore_tests/x.py b/tests/data/nested_gitignore_tests/x.py
new file mode 100644 (file)
index 0000000..e69de29
index 5ab25cd160158d094b0122bdfd6beba60a9555fc..098a9ec91573117433547a34922c25965a295444 100644 (file)
@@ -1406,7 +1406,7 @@ class BlackTestCase(BlackBaseTestCase):
         )
         self.assertEqual(sorted(expected), sorted(sources))
 
-    def test_gitingore_used_as_default(self) -> None:
+    def test_gitignore_used_as_default(self) -> None:
         path = Path(THIS_DIR / "data" / "include_exclude_tests")
         include = re.compile(r"\.pyi?$")
         extend_exclude = re.compile(r"/exclude/")
@@ -1703,6 +1703,33 @@ class BlackTestCase(BlackBaseTestCase):
         )
         self.assertEqual(sorted(expected), sorted(sources))
 
+    def test_nested_gitignore(self) -> None:
+        path = Path(THIS_DIR / "data" / "nested_gitignore_tests")
+        include = re.compile(r"\.pyi?$")
+        exclude = re.compile(r"")
+        root_gitignore = black.files.get_gitignore(path)
+        report = black.Report()
+        expected: List[Path] = [
+            Path(path / "x.py"),
+            Path(path / "root/b.py"),
+            Path(path / "root/c.py"),
+            Path(path / "root/child/c.py"),
+        ]
+        this_abs = THIS_DIR.resolve()
+        sources = list(
+            black.gen_python_files(
+                path.iterdir(),
+                this_abs,
+                include,
+                exclude,
+                None,
+                None,
+                report,
+                root_gitignore,
+            )
+        )
+        self.assertEqual(sorted(expected), sorted(sources))
+
     def test_empty_include(self) -> None:
         path = THIS_DIR / "data" / "include_exclude_tests"
         report = black.Report()