fix: scope diff file-header detection to between-hunk positions#1878
Conversation
parseDiffOutput tested every diff line against the file-header regexes unconditionally, including lines inside an active hunk body. A removed line whose original content starts with "-- " (e.g. a Markdown horizontal rule) becomes a "--- " line once diff-prefixed and was misdetected as a source-file header, silently dropped, and desynced the old-line cursor for the rest of the hunk. Symmetrically, an added line starting with "++ b/" becomes a "+++ b/..." line and was misdetected as a new-file header, flushing the in-progress run under a phantom file key and misattributing every later line in the hunk to it. DiffLineTracker now derives an insideHunk() state from the old/new line-count bounds declared by the most recent hunk header, and parseDiffOutput only attempts file-header matching while that is false: before a file's first hunk, or once the previous hunk's declared counts are fully consumed. Real diffs never have hunk-header counts that lie about their body length, so this makes header detection position-aware without weakening it for any real diff shape. No user-facing behavior, commands, or architecture changed. docs check acknowledged. Fixes #1761 Impact: 4 functions changed, 5 affected
Codegraph Impact Analysis4 functions changed → 5 callers affected across 4 files
|
Greptile SummaryThis PR fixes a parsing bug in
Confidence Score: 5/5Safe to merge — the change is narrowly scoped to the diff parser, well-tested, and leaves unrelated code paths untouched. The fix correctly uses end-exclusive hunk bounds derived from the declared @@ counts. All edge cases (zero-count sides, absent count defaulting to 1, No newline markers, sequential hunks, new/deleted-file hunks) behave correctly. The two regression tests precisely mirror the bug scenarios, and the handles-multiple-files fixture fix is necessary and correct. No files require special attention — both changed files are clean. Important Files Changed
Reviews (1): Last reviewed commit: "fix: scope diff file-header detection to..." | Re-trigger Greptile |
Summary
parseDiffOutputtested every diff line against file-header regexes (---,+++ b/) unconditionally. A removed line whose original content starts with--becomes--- ...once diff-prefixed — indistinguishable from a real file header by content alone, causing it to be silently dropped and desyncing the old-line cursor for the rest of the hunk. Same latent issue for added lines starting with++ b/.oldHunkEnd/newHunkEndtracking (computed from the most recent@@ -a,b +c,d @@header) and aninsideHunk()check; file-header detection now only runs when not currently inside a hunk body. Hunk-header detection itself is untouched (unambiguous by construction).Closes #1761
Test plan
npm test— full suite green (3521 passed, 0 failed)npm run lint— cleangit diff --unified=0output on temp fixtures — confirmed both fail with the exact predicted corruption against pre-fix code, pass post-fixStacked on #1877 (base branch
fix/issue-1759-rust-tracer-fixture-mainrs) — only the check.ts/test diff is this issue's change.