Skip to content

fix(ignore): preserve escaped trailing spaces in gitignore patterns - #3492

Open
baima365-web wants to merge 1 commit into
BurntSushi:masterfrom
baima365-web:fix-gitignore-escaped-trailing-spaces
Open

fix(ignore): preserve escaped trailing spaces in gitignore patterns#3492
baima365-web wants to merge 1 commit into
BurntSushi:masterfrom
baima365-web:fix-gitignore-escaped-trailing-spaces

Conversation

@baima365-web

Copy link
Copy Markdown

Summary

.gitignore lines like foo\ (escaped space followed by more spaces) were over-trimmed: only a single trailing \ was preserved, so the pattern collapsed to foo\ and failed with dangling '\'.

Git keeps the escaped trailing space. Match that by trimming only unescaped trailing spaces.

Fix

  • Replace line.trim_right() with logic that only trims unescaped trailing spaces
  • Preserve escaped spaces (preceded by backslash) while removing unescaped ones

Fixes #3485

@ltrzesniewski

Copy link
Copy Markdown
Contributor

I didn't read the surrounding code, but you should probably check if the number of preceding backslashes is odd.

…trailing spaces

Update the trailing space trimming logic to properly handle multiple
preceding backslashes per ltrzesniewski's suggestion:

- Count the number of consecutive backslashes before each trailing space
- If odd count: space is escaped, preserve it and stop trimming
- If even count (including zero): space is unescaped, trim it

This fixes cases like '\\ ' (two backslashes + space) which should be
trimmed to '\\' (escaped backslash), while '\\ ' (three backslashes +
space) should be preserved as '\\ ' (escaped backslash + escaped space).

Fixes BurntSushi#3485
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