From b2cb4ca0563cc73af20460fe3b319263a96a6989 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 17 Jun 2025 14:12:24 +0300 Subject: Fix grep context lines bug in channelless implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fixed critical bug where matching lines were incorrectly treated as after context - After context logic now only applies to non-matching lines, not matches - Consecutive matches no longer interfere with after context counting - All grep context options now work correctly: --before, --after, --max - TestDGrepContext1 and TestDGrepContext2 now pass with channelless implementation - Full compatibility with original channel-based behavior maintained - All integration tests passing The bug was in GrepProcessor.ProcessLine() where any line with afterRemaining > 0 was treated as after context, including matching lines. Fixed by moving after context logic inside the !isMatch condition block. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/io/fs/directprocessor.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'internal/io') diff --git a/internal/io/fs/directprocessor.go b/internal/io/fs/directprocessor.go index 006934f..d4bf3ed 100644 --- a/internal/io/fs/directprocessor.go +++ b/internal/io/fs/directprocessor.go @@ -356,18 +356,18 @@ func (gp *GrepProcessor) Cleanup() error { func (gp *GrepProcessor) ProcessLine(line []byte, lineNum int, filePath string, stats *stats, sourceID string) ([]byte, bool) { isMatch := gp.regex.Match(line) - // Handle after context lines - if gp.afterRemaining > 0 { - gp.afterRemaining-- - // Send this line as context even if it doesn't match - if stats != nil { - stats.updateLineMatched() // Count context lines as transmitted - } - return gp.formatLine(line, lineNum, filePath, stats, sourceID), true - } // Handle lines that don't match the regex if !isMatch { + // Handle after context lines (only for non-matching lines) + if gp.afterRemaining > 0 { + gp.afterRemaining-- + // Send this line as context + if stats != nil { + stats.updateLineMatched() // Count context lines as transmitted + } + return gp.formatLine(line, lineNum, filePath, stats, sourceID), true + } // If we have before context, buffer this line if gp.beforeContext > 0 { // Make a copy of the line for buffering -- cgit v1.2.3