Skip to content

fix(git): keep +/-/@@ diff markers at column 0 in compact_diff - #3646

Closed
ninearif wants to merge 1 commit into
rtk-ai:developfrom
ninearif:fix/compact-diff-column-zero-markers
Closed

ninearif wants to merge 1 commit into
rtk-ai:developfrom
ninearif:fix/compact-diff-column-zero-markers

Conversation

@ninearif

@ninearif ninearif commented Aug 23, 2026 •

Copy link
Copy Markdown

Summary

  • compact_diff() indents hunk lines by two spaces, moving +/-/@@ off column 0 — so rtk git diff | grep -E '^[+-]' silently matches nothing (false negative for any diff-parsing pipeline). Reproduced on the latest release, 0.45.0.
  • Emit hunk headers and hunk lines at column 0 exactly as git prints them; re-spell the per-file summary as (+N -M) so it can't read as an added line.
  • Output shrinks by 2 bytes per hunk line; adds a regression test.

Test plan

  • cargo fmt --all --check && cargo clippy --all-targets && cargo test (2,705 tests pass, clippy clean)
  • Manual testing: rtk git diff output inspected before/after on a real repo (transcripts below)
  • New unit test test_compact_diff_keeps_diff_markers_at_column_zero covers markers at column 0, context-line leading space, and the summary-line disguise

Problem

compact_diff() indents every hunk header and hunk line by two spaces. That moves the + / - / @@ markers off column 0, so any consumer that reads RTK's git diff / git show output like a unified diff silently gets nothing.

Because the Claude Code hook rewrites git → rtk git transparently, the agent does not know the output was reformatted. The failing pipeline exits 1 with empty stdout — which looks exactly like "no matching changes". It is a convincing false negative, not an error.

Reproduction (rtk 0.45.0, macOS)

One file, one edit — replace a word and add two bullet lines:

$ git diff
diff --git a/lorem.md b/lorem.md
index 2ad72b9..18d813e 100644
--- a/lorem.md
+++ b/lorem.md
@@ -1,6 +1,8 @@
 # Lorem ipsum

 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
-Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+Sed do eiusmod tempor incididunt ut labore et dolore magna ALIQUA.
 Ut enim ad minim veniam, quis nostrud exercitation ullamco.
+- Excepteur sint occaecat cupidatat non proident.
+- Sunt in culpa qui officia deserunt mollit anim id est laborum.
 Duis aute irure dolor in reprehenderit in voluptate velit.

The same diff through rtk git diff (actual output):

$ rtk git diff
lorem.md | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Changes:

lorem.md
  @@ -1,6 +1,8 @@
  -Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  +Sed do eiusmod tempor incididunt ut labore et dolore magna ALIQUA.
   Ut enim ad minim veniam, quis nostrud exercitation ullamco.
  +- Excepteur sint occaecat cupidatat non proident.
  +- Sunt in culpa qui officia deserunt mollit anim id est laborum.
   Duis aute irure dolor in reprehenderit in voluptate velit.
  +3 -1

Every marker now sits at column 2, so:

$ git diff     | grep -cE '^[+-]'        # raw
6
$ rtk git diff | grep -cE '^[+-]'        # rtk
0
$ rtk git diff | grep -E '^\+.*occaecat' >/dev/null; echo $?
1

0 matches, exit 1 — indistinguishable from a genuinely clean result. Note also the bare +3 -1 per-file summary on the last line: once markers are back at column 0, a line shaped like that would itself read as an added line, so it needs re-spelling too.

We hit this twice in one day while measuring an Odoo 17→18 schema delta: two independent safety checks (git diff A B | grep -E '^[+-].*fields\.' for model renames and for Selection key changes) both reported "0 findings". Both had findings. A later session committed on the strength of an "edit verified, grep shows nothing unexpected" check that had matched nothing for the same reason.

This contradicts the project's own Transparency principle in CONTRIBUTING.md:

If an LLM parses git diff output, RTK's filtered version must still look like git diff output.

Fix

  • Emit hunk headers (@@) and hunk lines (+, -, context ) at column 0, exactly as git prints them. File names and the truncation hints keep their current shape.
  • Re-spell the per-file summary +N -M as (+N -M) so it can never be mistaken for an added line (same notation diff_cmd.rs already uses).
  • Add test_compact_diff_keeps_diff_markers_at_column_zero as a regression test.

Side effect: two fewer bytes per hunk line, so the compacted output gets slightly smaller.

Same repo, this branch's build (actual output):

$ rtk git diff
lorem.md | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Changes:

lorem.md
@@ -1,6 +1,8 @@
-Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+Sed do eiusmod tempor incididunt ut labore et dolore magna ALIQUA.
 Ut enim ad minim veniam, quis nostrud exercitation ullamco.
+- Excepteur sint occaecat cupidatat non proident.
+- Sunt in culpa qui officia deserunt mollit anim id est laborum.
 Duis aute irure dolor in reprehenderit in voluptate velit.
  (+3 -1)

$ rtk git diff | grep -cE '^[+-]'
4

(4, not 6: the raw count includes the --- a/ / +++ b/ file headers, which compact mode drops by design. All four real changed lines are found, and the (+3 -1) summary no longer masquerades as one.)

cargo fmt --check, cargo clippy -D warnings and the full cargo test (2,705 tests) pass.

Affected callers

compact_diff is shared by git diff, git show, git stash show, rtk pipe, gh/glab diff views — all of them gain column-0 markers.

@CLAassistant

CLAassistant commented Aug 23, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ninearif

Copy link
Copy Markdown
Author

recheck

compact_diff indented every hunk header and hunk line by two spaces.
That moved the unified-diff markers off the line start, so anything
reading `rtk git diff` through a pipe as a diff — `grep -E '^[+-]'`,
patch, diff-aware tooling — matched nothing and exited 1. Because the
hook rewrites `git` to `rtk git` transparently, that reads as a clean
"no matching changes" result: a silent false negative.

Emit hunk headers and hunk lines exactly as git prints them, and spell
the per-file summary as `(+N -M)` so it cannot be mistaken for an added
line (the notation diff_cmd.rs already uses). Output shrinks by two
bytes per hunk line. Adds a regression test.
@ninearif
ninearif force-pushed the fix/compact-diff-column-zero-markers branch from 16e9ca8 to 7c0865a Compare August 23, 2026 10:28
kylehgc added a commit to kylehgc/rtk that referenced this pull request Aug 24, 2026
The Avant/Apres example still showed the pre-fix indented format (and was
already stale in other ways: a '~' modified marker and '(+5/-2)' tally that
compact_diff never emitted). Redraw it from real output of the new format:
markers at column 0, per-file tally indented as '  (+N -M)'.

Fork-authored amendment to the rtk-ai#3646 adoption; CONTRIBUTING requires doc
updates for changes to documented behavior.

Co-Authored-By: Claude Fable 5 <[email protected]>
kylehgc added a commit to kylehgc/rtk that referenced this pull request Aug 24, 2026
fix(git): keep +/-/@@ diff markers at column 0 in compact_diff (adopt upstream rtk-ai#3646)
@KuSh

KuSh commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Closing as a duplicate of #3269, which makes the same fix to compact_diff.

Both PRs de-indent the hunk header, the +/- lines and the context lines so anchored greps work again. The bug is live on 0.46.0 — rtk git diff | grep -c '^-' returns 0 today on a diff with deletions — so this is a real silent false negative, and your framing of it in the test comment is exactly right.

Two things decided it for #3269. It has a full green test matrix, where this PR only ever ran check-target (skipped), so its behaviour was never CI-verified. And it leaves the per-file tally format alone: the +N -M → (+N -M) change here is an output change with no bug behind it, since the tally already starts with two spaces and was never at risk of being matched by ^[+-]. The two regression tests are otherwise equivalent in strength.

Thanks for catching this — it's a genuine correctness bug, not cosmetics, and it's why this whole area is being worked through now.

@KuSh KuSh closed this Aug 30, 2026
KuSh pushed a commit to kylehgc/rtk that referenced this pull request Aug 30, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants