Skip to content

feat(diff): per-hunk stage/unstage in the combined diff viewer - #5950

Open
dannyisbad wants to merge 3 commits into
stablyai:mainfrom
dannyisbad:feat/diff-hunk-staging
Open

dannyisbad wants to merge 3 commits into
stablyai:mainfrom
dannyisbad:feat/diff-hunk-staging

Conversation

@dannyisbad

@dannyisbad dannyisbad commented Jun 21, 2026 •

Copy link
Copy Markdown

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 is the source of truth for hunks. The renderer fetches the per-file unified diff (new git.diffPatch), parses it, rebuilds a minimal patch for the selected hunk, and applies it with git apply --cached [--reverse] (new git.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.
  • Local + SSH + runtime via dedicated RPCs; the relay git.exec allowlist is left closed (a dedicated git.applyPatch handler pipes the patch over its own stdin path).
  • Gated off for binary, read-only branch/commit, errored, and dirty (unsaved-edit) sections.
  • The control is a hover-revealed pill positioned with absolute layout (not a view zone), so it never affects the combined diff's height virtualization or scrolls with content.

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 — local getFileDiffPatch + applyIndexPatch against real git (stage hunk, unstage via reverse, stale-patch rejection).
  • src/relay/git-handler-hunk-staging.test.ts — relay git.diffPatch / git.applyPatch round-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

  • Cross-platform: the stdin git runner routes through the existing WSL-aware gitSpawn; no hardcoded path separators or modifier keys. macOS/Linux/Windows safe.
  • SSH / remote / local: all three paths implemented and tested — local (status.ts), SSH (SshGitProvider → relay handler with its own stdin pipe), and runtime (RPC). Older relays without git.diffPatch degrade gracefully (per-hunk controls just don't appear; file-level staging still works).
  • Agent / integration compat: no agent- or integration-specific logic touched.
  • Git provider compat: purely local-git plumbing (git diff / git apply --cached); provider-neutral, nothing GitHub-specific.
  • Performance: the patch fetch is one git diff per 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.
  • UI quality: the pill mirrors the existing diff-comment affordance's tokens/sizing (STYLEGUIDE §match-the-nearest-sibling); no new color/shadow/font values.
  • Security: git apply is 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

  • Hover → "Stage hunk" pill on an unstaged hunk.
  • After staging one hunk → the file splits into CHANGES + STAGED CHANGES, the diff shows only what's left, Commit enables.
pr-hover-stage 04-after-stage

X handle: @dannymundyy


Open in Stage

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.

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
@ghost

ghost commented Jun 21, 2026 •

Copy link
Copy Markdown

@AmethystLiang
AmethystLiang self-requested a review June 21, 2026 00:42
@coderabbitai

coderabbitai Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR implements per-hunk git index staging and unstaging in the diff viewer. A shared git-hunk-patch.ts module adds parseFileDiff, buildHunkPatch, and patchTouchesOnlyPath utilities for splitting unified diffs, reconstructing single-hunk patches, and validating patch scope. A new gitExecFileWithStdin runner feeds stdin to git processes for git apply --cached. New getFileDiffPatch and applyIndexPatch helpers are added to status.ts with integration tests covering forward/reverse patching, stale-patch rejection, path validation, and non-ASCII filenames. These operations are exposed through IPC handlers, the IGitProvider interface, SshGitProvider relay forwarding, RPC schemas and methods, relay-side handlers with path-traversal protection, RuntimeGitCommands, and the runtime client. On the renderer side, three new hooks (useDiffSectionHunks, useDiffHunkStageDecorator, useDiffSectionHunkStaging) fetch hunks, show a hover-activated stage button in Monaco, and apply single-hunk patches with git status refresh. DiffSectionItem and CombinedDiffViewer wire the staging context through the diff UI layers.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The PR title 'feat(diff): per-hunk stage/unstage in the combined diff viewer' directly and clearly describes the main change—adding per-hunk staging/unstaging functionality to the combined diff viewer. It is concise, specific, and accurately reflects the primary feature added.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description covers the feature, screenshots, testing, AI review, security, and cross-platform notes, with only minor template formatting gaps.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 02cbd57e-1ca7-42fb-985b-a8f5162df0ed

📥 Commits

Reviewing files that changed from the base of the PR and between 16347ab and 2f0a023.

📒 Files selected for processing (26)
  • src/main/git/runner.ts
  • src/main/git/status-hunk-patch.test.ts
  • src/main/git/status.ts
  • src/main/ipc/filesystem.ts
  • src/main/providers/ssh-git-provider.ts
  • src/main/providers/types.ts
  • src/main/runtime/orca-runtime-git.ts
  • src/main/runtime/orca-runtime.ts
  • src/main/runtime/rpc/methods/git-params.ts
  • src/main/runtime/rpc/methods/git.ts
  • src/preload/api-types.ts
  • src/preload/index.ts
  • src/relay/git-handler-hunk-staging.test.ts
  • src/relay/git-handler.ts
  • src/renderer/src/assets/main.css
  • src/renderer/src/components/editor/CombinedDiffViewer.tsx
  • src/renderer/src/components/editor/DiffSectionItem.tsx
  • src/renderer/src/components/editor/useDiffHunkStageDecorator.test.tsx
  • src/renderer/src/components/editor/useDiffHunkStageDecorator.tsx
  • src/renderer/src/components/editor/useDiffSectionHunkStaging.ts
  • src/renderer/src/components/editor/useDiffSectionHunks.test.tsx
  • src/renderer/src/components/editor/useDiffSectionHunks.ts
  • src/renderer/src/runtime/runtime-git-client.ts
  • src/renderer/src/web/web-preload-api.ts
  • src/shared/git-hunk-patch.test.ts
  • src/shared/git-hunk-patch.ts

Comment thread src/main/git/runner.ts Resolved
Comment thread src/main/git/status-hunk-patch.test.ts Resolved
Comment thread src/main/git/status.ts Resolved
Comment thread src/main/ipc/filesystem.ts Resolved
Comment thread src/relay/git-handler.ts Resolved
- 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
@dannyisbad

Copy link
Copy Markdown
Author

Addressed the review in aa589e9:

  • Patch ↔ path binding (the main one): applyIndexPatch and the relay git.applyPatch handler now reject any patch that touches a path other than the validated filePath (git apply honors every file header it's given), via a new patchTouchesOnlyPath. Threaded filePath through the provider + relay contracts so the bind holds on every path (local / SSH / runtime).
  • Coerce reverse to a strict boolean at the IPC boundary (matches git:push).
  • Register the abort listener before writing stdin to close a small race.
  • Bound the relay gitWithStdin stdout/stderr to MAX_GIT_BUFFER.
  • Assert the index is unchanged after a stale-patch rejection; tightened the null-editor decorator test.
  • Added concise docstrings to the new exported functions/hooks.

typecheck, oxlint, and the suite are green.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 53d2c240-c49b-43a1-8628-294984b62576

📥 Commits

Reviewing files that changed from the base of the PR and between 2f0a023 and aa589e9.

📒 Files selected for processing (15)
  • src/main/git/runner.ts
  • src/main/git/status-hunk-patch.test.ts
  • src/main/git/status.ts
  • src/main/ipc/filesystem.ts
  • src/main/providers/ssh-git-provider.ts
  • src/main/providers/types.ts
  • src/main/runtime/orca-runtime-git.ts
  • src/relay/git-handler.ts
  • src/renderer/src/components/editor/useDiffHunkStageDecorator.test.tsx
  • src/renderer/src/components/editor/useDiffHunkStageDecorator.tsx
  • src/renderer/src/components/editor/useDiffSectionHunkStaging.ts
  • src/renderer/src/components/editor/useDiffSectionHunks.ts
  • src/renderer/src/runtime/runtime-git-client.ts
  • src/shared/git-hunk-patch.test.ts
  • src/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

Comment thread src/shared/git-hunk-patch.ts Resolved
…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".
@alyyousuf7

Copy link
Copy Markdown

hi -- is this feature on hold? 😬

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants