Unverified Commit 104aec55 authored by Nipunn Koorapati's avatar Nipunn Koorapati Committed by GitHub
Browse files

Present a more user-friendly error if .gitignore is invalid (#2414)

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!
parent ef7c45f2
Loading
Loading
Loading
Loading
+1 −0
Changes for CHANGES.md: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -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

+1 −1
Changes for setup.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ def get_long_description() -> str:
        "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",
+4 −0
Changes for src/black/__init__.py: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
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,6 +429,7 @@ def main(
            content=code, fast=fast, write_back=write_back, mode=mode, report=report
        )
    else:
        try:
            sources = get_sources(
                ctx=ctx,
                src=src,
@@ -440,6 +442,8 @@ def main(
                report=report,
                stdin_filename=stdin_filename,
            )
        except GitWildMatchPatternError:
            ctx.exit(1)

        path_empty(
            sources,
+5 −0
Changes for src/black/files.py: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
)

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()
    try:
        return PathSpec.from_lines("gitwildmatch", lines)
    except GitWildMatchPatternError as e:
        err(f"Could not parse {gitignore}: {e}")
        raise


def normalize_path_maybe_ignore(
+1 −0
Changes for tests/data/invalid_gitignore_tests/.gitignore: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
!
Loading