Repository navigation
feat(diff): per-hunk stage/unstage in the combined diff viewer - #5950
dannyisbad wants to merge 3 commits into
Conversation
Adds inline "Stage hunk" / "Unstage hunk" controls to each hunk in the combined diff's working-tree sections, closing the gap left by file-level staging (stablyai#334, stablyai#2429). A structured patch API underpins it: git is the source of truth for hunks (`git diff [--cached]` text is parsed, a minimal sub-patch is rebuilt for the selected hunk, and applied with `git apply --cached [--reverse]`). git apply's atomicity is the staleness guard — a patch that no longer applies fails cleanly and the diff refreshes instead of corrupting the index. Works across local, SSH, and runtime worktrees via dedicated git.diffPatch / git.applyPatch operations (no widening of the relay exec allowlist). Gated off for binary, read-only branch/commit, errored, and dirty (unsaved-edit) diffs. - shared/git-hunk-patch.ts: pure unified-diff parser + sub-patch builder - stdin-capable git runner (local) + spawn-stdin relay handler - status.ts getFileDiffPatch / applyIndexPatch - full wiring: ipc, preload, renderer client, runtime rpc, ssh provider, web - renderer: per-hunk Monaco content-widget decorator + fetch hook - tests at shared, local-git, relay, and renderer layers
|
Ready to review this PR? Stage has broken it down into 10 individual chapters for you: Chapters generated by Stage for commit 416e95a on Jun 21, 2026 1:31am UTC. |
📝 WalkthroughWalkthroughThis PR implements per-hunk git index staging and unstaging in the diff viewer. A shared 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 6
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 02cbd57e-1ca7-42fb-985b-a8f5162df0ed
📒 Files selected for processing (26)
src/main/git/runner.tssrc/main/git/status-hunk-patch.test.tssrc/main/git/status.tssrc/main/ipc/filesystem.tssrc/main/providers/ssh-git-provider.tssrc/main/providers/types.tssrc/main/runtime/orca-runtime-git.tssrc/main/runtime/orca-runtime.tssrc/main/runtime/rpc/methods/git-params.tssrc/main/runtime/rpc/methods/git.tssrc/preload/api-types.tssrc/preload/index.tssrc/relay/git-handler-hunk-staging.test.tssrc/relay/git-handler.tssrc/renderer/src/assets/main.csssrc/renderer/src/components/editor/CombinedDiffViewer.tsxsrc/renderer/src/components/editor/DiffSectionItem.tsxsrc/renderer/src/components/editor/useDiffHunkStageDecorator.test.tsxsrc/renderer/src/components/editor/useDiffHunkStageDecorator.tsxsrc/renderer/src/components/editor/useDiffSectionHunkStaging.tssrc/renderer/src/components/editor/useDiffSectionHunks.test.tsxsrc/renderer/src/components/editor/useDiffSectionHunks.tssrc/renderer/src/runtime/runtime-git-client.tssrc/renderer/src/web/web-preload-api.tssrc/shared/git-hunk-patch.test.tssrc/shared/git-hunk-patch.ts
- applyIndexPatch / relay applyPatch now reject a patch that touches any path other than the validated filePath (git apply honors every file header), via patchTouchesOnlyPath; filePath threaded through provider + relay contracts - coerce `reverse` to a strict boolean at the IPC boundary - register the abort listener before writing stdin (close a small race) - bound the relay gitWithStdin stdout/stderr to MAX_GIT_BUFFER - assert the index is unchanged after a stale-patch rejection - make the null-editor decorator test assert the real target - add concise docstrings to the new exported functions/hooks
|
Addressed the review in
typecheck, oxlint, and the suite are green. |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 53d2c240-c49b-43a1-8628-294984b62576
📒 Files selected for processing (15)
src/main/git/runner.tssrc/main/git/status-hunk-patch.test.tssrc/main/git/status.tssrc/main/ipc/filesystem.tssrc/main/providers/ssh-git-provider.tssrc/main/providers/types.tssrc/main/runtime/orca-runtime-git.tssrc/relay/git-handler.tssrc/renderer/src/components/editor/useDiffHunkStageDecorator.test.tsxsrc/renderer/src/components/editor/useDiffHunkStageDecorator.tsxsrc/renderer/src/components/editor/useDiffSectionHunkStaging.tssrc/renderer/src/components/editor/useDiffSectionHunks.tssrc/renderer/src/runtime/runtime-git-client.tssrc/shared/git-hunk-patch.test.tssrc/shared/git-hunk-patch.ts
🚧 Files skipped from review as they are similar to previous changes (10)
- src/renderer/src/components/editor/useDiffHunkStageDecorator.tsx
- src/main/providers/types.ts
- src/renderer/src/components/editor/useDiffSectionHunks.ts
- src/renderer/src/components/editor/useDiffHunkStageDecorator.test.tsx
- src/main/git/runner.ts
- src/renderer/src/components/editor/useDiffSectionHunkStaging.ts
- src/renderer/src/runtime/runtime-git-client.ts
- src/main/git/status-hunk-patch.test.ts
- src/main/runtime/orca-runtime-git.ts
- src/relay/git-handler.ts
…lse-reject git octal-quotes non-ASCII paths in `diff --git` headers (e.g. "a/caf\303\251.ts") but leaves spaces literal. Generating the diff with core.quotePath=false keeps unicode literal too, so patchTouchesOnlyPath's matcher handles both spaces and non-ASCII without a fragile quoted-path regex. Tested via real-git e2e for "my file.ts" and "café.ts".
|
hi -- is this feature on hold? 😬 |
What this adds
Per-hunk Stage hunk / Unstage hunk in the combined diff viewer. Hovering a hunk in the working-tree changes reveals a pill that stages just that hunk (or unstages it, on a staged section) — closing the gap left by file-level staging (#334, and the overlapping #2429).
This follows @nwparker's scoped breakdown on #2429:
git.diffPatch), parses it, rebuilds a minimal patch for the selected hunk, and applies it withgit apply --cached [--reverse](newgit.applyPatch).git apply's atomicity is the staleness guard — if the worktree moved under the virtualized diff, the patch fails cleanly and the view refreshes, so the index is never corrupted.git.execallowlist is left closed (a dedicatedgit.applyPatchhandler pipes the patch over its own stdin path).Scoped to whole-hunk stage/unstage (what @AmethystLiang invited on #334). Sub-hunk/line-range selection is a clean follow-up on the same patch API.
Tests
src/shared/git-hunk-patch.test.ts— unified-diff parse + sub-patch rebuild (binary, new/deleted file, no-newline-at-eof, CRLF round-trip, multi-hunk subset).src/main/git/status-hunk-patch.test.ts— localgetFileDiffPatch+applyIndexPatchagainst real git (stage hunk, unstage via reverse, stale-patch rejection).src/relay/git-handler-hunk-staging.test.ts— relaygit.diffPatch/git.applyPatchround-trip against real git + path-traversal rejection.src/renderer/src/components/editor/useDiffSectionHunks.test.tsx+useDiffHunkStageDecorator.test.tsx— fetch/parse hook and the hover decorator (reveal, click, disabled gating).pnpm typecheck,pnpm lint(oxlint),pnpm build:desktop, and the full suite pass.AI code review summary
gitSpawn; no hardcoded path separators or modifier keys. macOS/Linux/Windows safe.status.ts), SSH (SshGitProvider→ relay handler with its own stdin pipe), and runtime (RPC). Older relays withoutgit.diffPatchdegrade gracefully (per-hunk controls just don't appear; file-level staging still works).git diff/git apply --cached); provider-neutral, nothing GitHub-specific.git diffper stageable section, refetched only when the diff reloads; the control is hover-driven and adds no view zones, so it doesn't perturb the combined diff's height virtualization.git applyis restricted to--cached(index only); the relay handler takes no path arguments (patch on stdin), and git apply itself rejects patch paths escaping the repo; file paths are validated for worktree confinement at every boundary.Screenshots
X handle: @dannymundyy
ELI5
In the combined diff viewer you can stage or unstage a single hunk instead of the whole file. Hover a hunk to get a stage/unstage control for just that change.