Fix gitignore parsing for anchored patterns and escaped characters

- Move escaped character handling after negation pattern detection
- Fix is_anchored logic to only check for leading "/" instead of any "/"
- Properly remove directory markers with removesuffix("/")

Addresses gitignore parsing issues mentioned in #348
This commit is contained in:
Jaen committed 2025-07-24 23:23:28 +03:00
1 parent e49dbdc737
commit 1d65c85485
1 file changed
+10 -9
+10 -9
View File
@@ -185,10 +185,6 @@ class GitignoreParser:
if not line or line.lstrip().startswith("#"):
continue
# Handle escaped characters at the beginning
if line.startswith(("\\#", "\\!")):
line = line[1:]
# Store whether this is a negation pattern
is_negation = line.startswith("!")
if is_negation:
@@ -200,13 +196,18 @@ class GitignoreParser:
if not line:
continue
# Determine if pattern is anchored to the gitignore directory
is_anchored = "/" in line[:-1] or line.startswith("/")
# Remove leading slash for processing
if line.startswith("/"):
# Handle escaped characters at the beginning
if line.startswith(("\\#", "\\!")):
line = line[1:]
# Determine if pattern is anchored to the gitignore directory and remove leading slash for processing
is_anchored = line.startswith("/")
if is_anchored:
line = line[1:]
# Remove directory marker at the end since we do not support directory-only patterns
line = line.removesuffix("/")
# Adjust pattern based on gitignore file location
if rel_dir:
if is_anchored: