Repository navigation
fix(diff): keep +/- markers at column 0 in condense_unified_diff - #3687
Conversation
|
Note for reviewers on the amendment ( What changed and why:
No behavior change to the fix itself; base-vs-head output is identical to the original commit on every input we tested. |
The stdin diff path (git diff | rtk diff -) emitted changed lines two-space indented, pushing their +/- markers to column 2. Anchored greps over the output (grep -E '^[+-]') matched nothing — a silent false negative for any script or CI gate built on diff conventions. Same class as the compact_diff fix in #118 (upstream rtk-ai#3646), same design rule applied: change lines at column 0; the [file] header and overflow line are unchanged and never start with +/-. Co-Authored-By: Claude Fable 5 <[email protected]>
…d of clone Amendment from self-review of the original commit: - The regression test used a single-file diff, but condense_unified_diff flushes a file's changes at two separate sites — once per `+++` for the preceding file, once after the loop for the last one. A single-file fixture only ever reaches the second: mutation testing showed the in-loop site could be reverted to the indenting form with the whole suite still green. The fixture now carries two files and fails against that mutant. - The anti-indent assertion was a blanket "no line starts with a space", which the ` ... +N more` trailer makes false for any diff with more than ten changes. Scoped to change lines. - The flush loops now move the lines (`result.append(&mut changes)`) instead of cloning each one; the mid-loop `changes.clear()` stays for the branch-not-taken path. The `Vec<String>` annotation is dropped — inference comes from `changes.push(line.to_string())`, so it was never needed (the original commit message's claim about the removed `format!` was wrong; `format!` accepts any Display and contributes no type information). Gate: cargo fmt --all / clippy --all-targets / test --all all exit 0.
`" +"` / `" -"` match a one-space indent, but the form this guards against is two spaces, so the assertion passed on the code it rejects and only the equality loop above it killed the mutant. Match on leading whitespace instead. Verified by neutralising that loop and reverting the flush sites: the assertion now fails on its own. The trailer the old comment carved out for is gone with rtk-ai#2652, so the scoping it justified is no longer needed. Co-Authored-By: Claude Opus 5 <[email protected]>
b7f389b to
fc2cc72
Compare
KuSh
left a comment
There was a problem hiding this comment.
Approving. Verified in depth; the change is correct and the one thing I found was in the test rather than the fix.
The fix: result.append(&mut changes) is behaviourally equivalent to the old borrow-and-format! loop minus the indent — append drains changes, and the retained changes.clear() still covers the branch-not-taken path (empty current_file, or zero added/removed). Type inference resolves changes to Vec<String> via push(line.to_string()), so dropping the annotation is fine. The [file] … (+N -M) header never starts with +/-, so anchored greps see exactly the change lines and nothing else. never_worse is unaffected — output shrinks by two bytes per line. condense_unified_diff has one caller (run_stdin), which prints it, so nothing downstream re-parses this.
Confirmed live on 0.46.0 that this is a real user-facing failure, not a theoretical one:
$ cat u.diff | rtk diff - | grep -c '^-'
0 # a diff with one deletion
Two commits pushed to your branch, shout if you'd rather have written either differently:
-
Rebased onto
develop— the branch was 73 commits behind. One conflict, in both flush sites: #2652 landed in the meantime and removed the... +N moretrailer that yourappendhunks carried as context. Resolved by keeping yourappendand dropping the trailer, which is what both changes independently wanted. -
fc2cc72— made the anti-indent assertion able to fail.l.starts_with(" +") || l.starts_with(" -")matches a one-space indent, but the form it guards against is two spaces (" -fn old() {}"), so it passed on exactly the code it rejects. Only thel == wantequality loop above it was killing the mutant. It now matches on leading whitespace generally. Verified by neutralising the equality loop and reverting both flush sites: the assertion fails on its own, atdiff_cmd.rs:420.Your comment carved the assertion narrowly because the
... +N moretrailer was legitimately indented — correct reasoning at the time, and it's what made the one-space form look deliberate. #2652 removed that trailer, so the carve-out is no longer needed.
I also dropped the (#3646) reference from the test comment: that PR is closed in favour of #3269, which makes the same fix to compact_diff. #3269 is still open, so rtk git diff keeps the two-space indent for now — this PR fixes the stdin path only, and the two are independent (different functions, different files, no ordering constraint between them).
Thanks for the two-file fixture, and for spelling out in the PR comment why one file wasn't enough. Reasoning about which flush site a fixture actually reaches is the part most regression tests skip.
Companion to #3646, which fixed the same class in
compact_diff.Bug
The stdin diff path (
git diff | rtk diff -,run_stdin→condense_unified_diff) emitted changed lines asformat!(" {}", c). The+/-markers landed at column 2, so anchored greps (grep -E '^[+-]') over the output matched nothing — a silent false negative for scripts and CI gates built on diff conventions.Fix
Emit change lines at column 0 by moving them with
result.append(&mut changes)— they already carry their markers, and the move also drops a per-lineStringclone.[file] name (+N -M)headers and the... +N moreoverflow line are unchanged; neither starts with+/-on real git output, so anchored greps see the change lines.Verification
printf '<unified diff>' | rtk diff - | grep -cE '^[+-]'→ 0. After: 4 on a two-file diff, all markers at column 0.+++for the preceding file, once after the loop for the last). Mutation testing showed a single-file fixture lets the first site revert with the suite still green; the two-file fixture kills both mutants." +"/" -") because the... +N moretrailer is legitimately indented.cargo fmt --all && cargo clippy --all-targets && cargo test --all, 0 failures.Not in scope
Deeper classification issues in this function (e.g. a removed line whose content is
-- notearrives as--- noteand is dropped as a header) are pre-existing, reproducible, and left untouched here — this PR only fixes the emission indentation and its test coverage.🤖 Generated with Claude Code