From feb9054122736224fca99021218d2a14367d95a1 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 15:30:40 +0300 Subject: [PATCH] Fix KeyError in detailed_match_files for unmatched negation patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- pathspec/util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pathspec/util.py b/pathspec/util.py index ea2dbee..79f6b56 100644 --- a/pathspec/util.py +++ b/pathspec/util.py @@ -156,7 +156,8 @@ def detailed_match_files( else: # Remove files. for file in result_files: - del return_files[file] + if file in return_files: + del return_files[file] return return_files