summaryrefslogtreecommitdiff
path: root/internal/io
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-06-17 14:12:24 +0300
committerPaul Buetow <paul@buetow.org>2025-06-17 14:12:24 +0300
commitb2cb4ca0563cc73af20460fe3b319263a96a6989 (patch)
tree0ab024e8983edcd8d25174b6da7749be15c8f9a9 /internal/io
parent069dff93d8c6c8f0ba28d9f6123fa9b1430c2f92 (diff)
Fix grep context lines bug in channelless implementation
- 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 <noreply@anthropic.com>
Diffstat (limited to 'internal/io')
-rw-r--r--internal/io/fs/directprocessor.go18
1 files changed, 9 insertions, 9 deletions
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