Skip to content

Comments

Fix KeyError in detailed_match_files for unmatched negation patterns#105

Open
bysiber wants to merge 1 commit intocpburnz:masterfrom
bysiber:fix/detailed-match-files-keyerror
Open

Fix KeyError in detailed_match_files for unmatched negation patterns#105
bysiber wants to merge 1 commit intocpburnz:masterfrom
bysiber:fix/detailed-match-files-keyerror

Conversation

@bysiber
Copy link
Contributor

@bysiber bysiber commented Feb 20, 2026

detailed_match_files() raises KeyError when a negation pattern (include=False) matches a file that wasn't previously added by an inclusion pattern.

For example:

from pathspec import PathSpec

# Pattern list where negation is broader than inclusion
spec = PathSpec.from_lines('gitwildmatch', [
    '*.txt',
    '!*.log',
])

# detailed_match_files processes patterns in order:
# 1. '*.txt' (include=True) → adds .txt files to result
# 2. '!*.log' (include=False) → tries to remove .log files → KeyError
spec.match_files(['test.txt', 'test.log'], details=True)

The !*.log negation pattern matches test.log, but that file was never added to the result set by a prior inclusion pattern. The del return_files[file] on line 160 of util.py raises KeyError.

The fix adds a membership check before deletion — if a file isn't in the result set, the negation is simply a no-op, which is consistent with how gitignore processes negation (it can only un-ignore files that were previously ignored).

When a negation pattern (include=False) matches files that were not
previously added by an inclusion pattern, the 'del return_files[file]'
raises a KeyError.

This happens when a negation pattern is broader than the preceding
inclusion patterns. For example, '*.txt' followed by 'checkout -b fix/detailed-match-files-keyerror master.log' — the
negation matches .log files that were never included.

Add a membership check before deletion to skip files that aren't in
the result set, consistent with how gitignore negation works (it can
only un-ignore files that were previously ignored).
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.

1 participant