Skip to content

Hunk-level staging and per-hunk discard in the Git pane (#191) - #1576

Merged
currentbits merged 1 commit into
mainfrom
coder/file-and-hunk-level-staging-before-commi-8bf7ba
Oct 10, 2026
Merged

currentbits merged 1 commit into
mainfrom
coder/file-and-hunk-level-staging-before-commi-8bf7ba

Conversation

@currentbits

Copy link
Copy Markdown
Owner

What

Each hunk in the Changes pane now has a stage checkbox and a discard button, so you can commit only part of a file or throw away one hunk without losing the rest of the file.

How

Backend (electron/worktrees.js)

  • commit() takes an optional patches: Array<{ path, patch }>. Each patch is a subset of that file's git diff HEAD hunks, and its path must also be in paths. Validation rejects empty patches and patches for files that aren't selected.

  • Partial commits go through commitPartial, which builds the commit in a throwaway index seeded from HEAD (GIT_INDEX_FILE in a temp dir):

    • whole files are git added
    • selected hunks are applied with git apply --cached
    • after the commit, the committed paths are reset in the real index, matching git commit --only

    The user's real index stays untouched, including anything already staged outside the selection. Hunks you didn't select stay as worktree changes.

  • New revertHunk reverse-applies a single-hunk patch to the worktree (git apply -R). The other hunks in the file stay.

  • Shared applyPatch helper: it writes the patch to a temp file and runs git apply with:

    • --recount, because trimmed diff output can drop a trailing blank context line
    • --include=<path>, so a patch can't touch any other file

IPC/API

  • New git:revertHunk channel, wired through ipc.js, preload.js, ipcChannels.ts, ipc.ts, useCoder.ts and devCoder.ts.
  • The git:commit handler now forwards patches.

UI (ThreadView.tsx)

  • Per-hunk stage checkboxes and discard buttons in the Changes pane.
  • Commit sends a selected-hunks patch for files that are only partly selected.
  • Merge stays file-level and refuses when partial leftovers remain.

Known limitation

Patches and the temp index live under os.tmpdir(), which git running inside WSL can't see. Hunk staging and discard on WSL repos would need those files under the git dir. This is marked with a ponytail: comment in applyPatch.

Tests

  • electron/test/worktrees-commit.test.js: partial-commit and hunk-revert coverage.
  • test/changesPanel.test.tsx: per-hunk stage and discard UI.
  • test/reviewItineraryPanel.test.tsx and test/support/fakeCoder.ts updated for the new API.

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T08:02:30.876127Z ee428d4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ee428d4cc2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread electron/worktrees.js Outdated
fs.writeFileSync(file, patch.endsWith("\n") ? patch : `${patch}\n`);
const res = gitTry(
cwd,
["apply", ...flags, "--recount", `--include=${relPath}`, file],

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Treat the include argument as a literal path

For paths containing glob metacharacters, such as the common Next.js filename app/[id]/page.tsx, this argument does not select the named file. Git documents --include as accepting a path pattern and silently ignoring patches that do not match an include pattern (git-apply documentation); consequently, hunk discard can return success without changing the file, while a mixed partial commit can commit other files but omit this selected hunk. Escape pattern metacharacters or otherwise validate the patch target literally before invoking git apply.

Useful? React with 👍 / 👎.

Comment thread electron/worktrees.js Outdated
const relPath = assertRelPathInCwd(cwd, String(opts.path || ""));
const patch = String(opts.patch || "");
if (!patch.trim()) throw new Error("Empty patch");
applyPatch(cwd, ["-R"], relPath, patch);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Remove discarded staged hunks from the index

When the selected hunk was already staged, reverse-applying it without --index changes only the working tree; Git explicitly documents that, without --index or --cached, the patch is applied only to files (git-apply documentation). The original change therefore remains staged while the worktree is restored to HEAD, so the pane can appear to have discarded the hunk even though a later index-based commit can still include it. The discard operation needs to update both the index and worktree for staged hunks.

Useful? React with 👍 / 👎.

Each hunk in the Changes pane gets a stage checkbox and a discard button.
Commit sends a selected-hunks patch for partially staged files; the backend
builds the commit in a throwaway index seeded from HEAD, so the real index
and already-staged siblings are untouched. git.revertHunk reverse-applies
one hunk to the worktree. Merge stays file-level and refuses on partial
leftovers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@currentbits
currentbits force-pushed the coder/file-and-hunk-level-staging-before-commi-8bf7ba branch from ee428d4 to 71becac Compare October 10, 2026 08:09
@currentbits
currentbits merged commit 2643704 into main Oct 10, 2026
5 of 6 checks passed
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.

1 participant