]> 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:

Present a more user-friendly error if .gitignore is invalid (#2414)
authorNipunn Koorapati <nipunn1313@gmail.com>
Fri, 20 Aug 2021 23:54:53 +0000 (16:54 -0700)
committerGitHub <noreply@github.com>
Fri, 20 Aug 2021 23:54:53 +0000 (19:54 -0400)
Fixes #2359.

This commit now makes Black exit with an user-friendly error message if a
.gitignore file couldn't be parsed -- a massive improvement over an opaque
traceback!

12 files changed:
CHANGES.md
setup.py
src/black/__init__.py
src/black/files.py
tests/data/invalid_gitignore_tests/.gitignore [new file with mode: 0644]
tests/data/invalid_gitignore_tests/a.py [new file with mode: 0644]
tests/data/invalid_gitignore_tests/pyproject.toml [new file with mode: 0644]
tests/data/invalid_nested_gitignore_tests/a.py [new file with mode: 0644]
tests/data/invalid_nested_gitignore_tests/a/.gitignore [new file with mode: 0644]
tests/data/invalid_nested_gitignore_tests/a/a.py [new file with mode: 0644]
tests/data/invalid_nested_gitignore_tests/pyproject.toml [new file with mode: 0644]
tests/test_black.py

index a4b8e01fb93a8f651c18caed7e1a02e5aee372bf..3a96029bf5cd3ccfcb481ad717a5aa8462839962 100644 (file)
@@ -6,6 +6,7 @@
 
 - Add support for formatting Jupyter Notebook files (#2357)
 - Move from `appdirs` dependency to `platformdirs` (#2375)
+- Present a more user-friendly error if .gitignore is invalid (#2414)
 
 ### Integrations
 
index 92b78f1abe116f9060ed53375f4cf0172ec28d00..215fa6cff61267488383582ce5673acb28d6edb6 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -77,7 +77,7 @@ setup(
         "tomli>=0.2.6,<2.0.0",
         "typed-ast>=1.4.2; python_version < '3.8'",
         "regex>=2020.1.8",
-        "pathspec>=0.8.1, <1",
+        "pathspec>=0.9.0, <1",
         "dataclasses>=0.6; python_version < '3.7'",
         "typing_extensions>=3.10.0.0; python_version < '3.10'",
         "mypy_extensions>=0.4.3",
index 29fb244f8b751651238fd99fefb055a9ada26eb1..60f4fa34e9ddf28ad097be35704b2d3e3790f3a1 100644 (file)
@@ -9,6 +9,7 @@ import io
 from multiprocessing import Manager, freeze_support
 import os
 from pathlib import Path
+from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
 import regex as re
 import signal
 import sys
@@ -428,18 +429,21 @@ def main(
             content=code, fast=fast, write_back=write_back, mode=mode, report=report
         )
     else:
-        sources = get_sources(
-            ctx=ctx,
-            src=src,
-            quiet=quiet,
-            verbose=verbose,
-            include=include,
-            exclude=exclude,
-            extend_exclude=extend_exclude,
-            force_exclude=force_exclude,
-            report=report,
-            stdin_filename=stdin_filename,
-        )
+        try:
+            sources = get_sources(
+                ctx=ctx,
+                src=src,
+                quiet=quiet,
+                verbose=verbose,
+                include=include,
+                exclude=exclude,
+                extend_exclude=extend_exclude,
+                force_exclude=force_exclude,
+                report=report,
+                stdin_filename=stdin_filename,
+            )
+        except GitWildMatchPatternError:
+            ctx.exit(1)
 
         path_empty(
             sources,
index ba60c84a27528ed378aa2f7073ebd7c4d830aef5..4d7b47aaa9fcbb4b7abe96ded72c9811d0155402 100644 (file)
@@ -18,6 +18,7 @@ from typing import (
 )
 
 from pathspec import PathSpec
+from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
 import tomli
 
 from black.output import err
@@ -122,7 +123,11 @@ def get_gitignore(root: Path) -> PathSpec:
     if gitignore.is_file():
         with gitignore.open(encoding="utf-8") as gf:
             lines = gf.readlines()
-    return PathSpec.from_lines("gitwildmatch", lines)
+    try:
+        return PathSpec.from_lines("gitwildmatch", lines)
+    except GitWildMatchPatternError as e:
+        err(f"Could not parse {gitignore}: {e}")
+        raise
 
 
 def normalize_path_maybe_ignore(
diff --git a/tests/data/invalid_gitignore_tests/.gitignore b/tests/data/invalid_gitignore_tests/.gitignore
new file mode 100644 (file)
index 0000000..cdf4cb4
--- /dev/null
@@ -0,0 +1 @@
+!
diff --git a/tests/data/invalid_gitignore_tests/a.py b/tests/data/invalid_gitignore_tests/a.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/data/invalid_gitignore_tests/pyproject.toml b/tests/data/invalid_gitignore_tests/pyproject.toml
new file mode 100644 (file)
index 0000000..3908e45
--- /dev/null
@@ -0,0 +1 @@
+# Empty configuration file; used in tests to avoid interference from Black's own config.
diff --git a/tests/data/invalid_nested_gitignore_tests/a.py b/tests/data/invalid_nested_gitignore_tests/a.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/data/invalid_nested_gitignore_tests/a/.gitignore b/tests/data/invalid_nested_gitignore_tests/a/.gitignore
new file mode 100644 (file)
index 0000000..cdf4cb4
--- /dev/null
@@ -0,0 +1 @@
+!
diff --git a/tests/data/invalid_nested_gitignore_tests/a/a.py b/tests/data/invalid_nested_gitignore_tests/a/a.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/data/invalid_nested_gitignore_tests/pyproject.toml b/tests/data/invalid_nested_gitignore_tests/pyproject.toml
new file mode 100644 (file)
index 0000000..3908e45
--- /dev/null
@@ -0,0 +1 @@
+# Empty configuration file; used in tests to avoid interference from Black's own config.
index 942446ec3435d03d0aa580f9d7fccbd867bf3de9..5c720507216c9efcdd4844d3e95063c011c59f5c 100644 (file)
@@ -1727,6 +1727,30 @@ class BlackTestCase(BlackBaseTestCase):
         )
         self.assertEqual(sorted(expected), sorted(sources))
 
+    def test_invalid_gitignore(self) -> None:
+        path = THIS_DIR / "data" / "invalid_gitignore_tests"
+        empty_config = path / "pyproject.toml"
+        result = BlackRunner().invoke(
+            black.main, ["--verbose", "--config", str(empty_config), str(path)]
+        )
+        assert result.exit_code == 1
+        assert result.stderr_bytes is not None
+
+        gitignore = path / ".gitignore"
+        assert f"Could not parse {gitignore}" in result.stderr_bytes.decode()
+
+    def test_invalid_nested_gitignore(self) -> None:
+        path = THIS_DIR / "data" / "invalid_nested_gitignore_tests"
+        empty_config = path / "pyproject.toml"
+        result = BlackRunner().invoke(
+            black.main, ["--verbose", "--config", str(empty_config), str(path)]
+        )
+        assert result.exit_code == 1
+        assert result.stderr_bytes is not None
+
+        gitignore = path / "a" / ".gitignore"
+        assert f"Could not parse {gitignore}" in result.stderr_bytes.decode()
+
     def test_empty_include(self) -> None:
         path = THIS_DIR / "data" / "include_exclude_tests"
         report = black.Report()