Skip to content

Fix trailing-space trimming after escaped backslashes - #142

Merged
cpburnz merged 2 commits into
cpburnz:masterfrom
Str0k:githubpower/t_01b4cd93
Sep 20, 2026
Merged

cpburnz merged 2 commits into
cpburnz:masterfrom
Str0k:githubpower/t_01b4cd93

Conversation

@Str0k

@Str0k Str0k commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

GitIgnoreBasicPattern and GitIgnoreSpecPattern preserve a trailing space whenever the preceding character is a backslash. With an even number of backslashes, Git treats that space as unescaped and trims it. This patch checks backslash parity before deciding whether to preserve the space.

A pattern containing two backslashes followed by a space should match a filename ending in one literal backslash. The new regression module covers both pattern classes and PathSpec; odd backslash runs continue to preserve escaped spaces. The behavior was also compared locally against git check-ignore and across the simple, re2 and hyperscan backends.

Validation: a new regression test fails on the unchanged base and passes with this patch; reproduced in an independent clean checkout.

Full-suite results (including pre-existing failures, if any):

{
  "base": {
    "exit": 1,
    "tests": 881,
    "failures": 6,
    "errors": 0,
    "skipped": 0,
    "failed_cases": [
      "tests.test_07_gitignore_trailing_space.TrailingSpaceAfterEscapedBackslashTest.test_00_even_backslash_run_trailing_space_stripped",
      "tests.test_07_gitignore_trailing_space.TrailingSpaceAfterEscapedBackslashTest.test_01_four_backslashes_trailing_space_stripped",
      "tests.test_07_gitignore_trailing_space.TrailingSpaceAfterEscapedBackslashTest.test_04_regex_normalization",
      "tests.test_07_gitignore_trailing_space.TrailingSpaceAfterEscapedBackslashTest.test_05_pathspec_end_to_end"
    ]
  },
  "patch": {
    "exit": 0,
    "tests": 881,
    "failures": 0,
    "errors": 0,
    "skipped": 0,
    "failed_cases": []
  }
}

AI assistance: implementation and review used Hermes with self-hosted GLM 5.3. Automated test evidence was checked separately. This does not represent a human review.

Additional checks were reproduced on fresh base and patched checkouts:

Check Base Patch
mypy 2 type errors 2 type errors
pyright 2 type errors 2 type errors
unittest 221 tests; exit 1 221 tests; exit 0

The type-checker errors below are also present on the unchanged base; this patch adds none.

Pre-existing type-checker diagnostics
{
  "mypy": [
    {
      "file": "pathspec/_typing.py",
      "code": "no-redef",
      "message": "Name \"deprecated\" already defined (possibly by an import)"
    },
    {
      "file": "pathspec/_typing.py",
      "code": "return-value",
      "message": "Incompatible return value type (got \"_Wrapped[[VarArg(Any), KwArg(Any)], Any, [VarArg(Any), KwArg(Any)], Any]\", expected \"F\")"
    }
  ],
  "pyright": [
    {
      "file": "pathspec/_typing.py",
      "code": "reportAssignmentType",
      "message": "Type \"type[deprecated]\" is not assignable to declared type \"(message: str, /, *, category: type[Warning] | None = DeprecationWarning, stacklevel: int = 1) -> ((F@deprecated) -> F@deprecated)\"\n\u00a0\u00a0Type \"type[deprecated]\" is not assignable to type \"(message: str, /, *, category: type[Warning] | None = DeprecationWarning, stacklevel: int = 1) -> ((F@deprecated) -> F@deprecated)\"\n\u00a0\u00a0\u00a0\u00a0Parameter 1: type \"str\" is incompatible with type \"LiteralString\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"str\" is not assignable to \"LiteralString\""
    },
    {
      "file": "pathspec/_typing.py",
      "code": "reportReturnType",
      "message": "Type \"_Wrapped[..., Any, ..., Any]\" is not assignable to return type \"F@deprecated\"\n\u00a0\u00a0Type \"_Wrapped[..., Any, ..., Any]\" is not assignable to type \"F@deprecated\""
    }
  ]
}

@cpburnz

cpburnz commented Sep 20, 2026

Copy link
Copy Markdown
Owner

This bug is very obvious now that you point it out. Thanks for fixing it.

@cpburnz
cpburnz merged commit 3d23757 into cpburnz:master Sep 20, 2026
64 checks passed
cpburnz added a commit that referenced this pull request Sep 20, 2026
This reverts commit 3d23757, reversing
changes made to d7b1505.

The _trim_trailing_spaces() function's doc-string suggests it was copied from
the Git source code which is GPL licensed, and cannot be included in MIT/MPL2
code.
cpburnz added a commit that referenced this pull request Sep 20, 2026
cpburnz added a commit that referenced this pull request Sep 20, 2026
cpburnz added a commit that referenced this pull request Sep 21, 2026
cpburnz added a commit that referenced this pull request Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants