Repository navigation
Hunk-level staging and per-hunk discard in the Git pane (#191) - #1576
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| fs.writeFileSync(file, patch.endsWith("\n") ? patch : `${patch}\n`); | ||
| const res = gitTry( | ||
| cwd, | ||
| ["apply", ...flags, "--recount", `--include=${relPath}`, file], |
There was a problem hiding this comment.
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 👍 / 👎.
| 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); |
There was a problem hiding this comment.
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]>
ee428d4 to
71becac
Compare
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 optionalpatches: Array<{ path, patch }>. Each patch is a subset of that file'sgit diff HEADhunks, and its path must also be inpaths. 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 fromHEAD(GIT_INDEX_FILEin a temp dir):git addedgit apply --cachedgit commit --onlyThe user's real index stays untouched, including anything already staged outside the selection. Hunks you didn't select stay as worktree changes.
New
revertHunkreverse-applies a single-hunk patch to the worktree (git apply -R). The other hunks in the file stay.Shared
applyPatchhelper: it writes the patch to a temp file and runsgit applywith:--recount, because trimmed diff output can drop a trailing blank context line--include=<path>, so a patch can't touch any other fileIPC/API
git:revertHunkchannel, wired throughipc.js,preload.js,ipcChannels.ts,ipc.ts,useCoder.tsanddevCoder.ts.git:commithandler now forwardspatches.UI (
ThreadView.tsx)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 aponytail:comment inapplyPatch.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.tsxandtest/support/fakeCoder.tsupdated for the new API.🤖 Generated with Claude Code