feat(worktrees): reflog-backed undo for worktree/branch deletion#163
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an undoable workflow for destructive worktree deletion by capturing branch state at delete-time (including branch SHA when the ref is deleted), exposing that via IPC to the renderer, and wiring a toast “Undo” action to restore the worktree/branch within the toast’s dismissal window.
Changes:
- Introduces
WorktreeDeleteResultand updatesworktree:deleteIPC to return it; adds newworktree:restoreIPC to reverse a deletion. - Implements
restoreDeletedWorktreein@sproutgit/git(recreate branch ref if needed, thengit worktree addback at the original path). - Adds unit + E2E coverage for delete-capture + restore and adds a toast action API to render an Undo button.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/ui/src/index.ts | Re-exports new toast action type. |
| packages/ui/src/components/Toast.tsx | Adds optional toast action rendering (Undo button). |
| packages/types/src/ipc.ts | Adds WORKTREE_RESTORE and updates IPC typing (worktree:delete result + worktree:restore). |
| packages/types/src/git.ts | Adds WorktreeDeleteResult type used across IPC and git layer. |
| packages/git/src/worktrees.ts | Returns delete undo state and implements restoreDeletedWorktree. |
| packages/git/src/index.ts | Exposes restoreDeletedWorktree from the git package entrypoint. |
| packages/git/src/tests/worktrees.test.ts | Unit tests for delete capture + restore success/failure paths. |
| e2e/specs/worktree-workflow.spec.ts | E2E test for toast Undo restoring worktree/branch in UI. |
| app/src/renderer/toast-context.tsx | Extends toast function signature to accept an optional action. |
| app/src/renderer/routes/workspace.tsx | Wires delete -> toast Undo -> restore mutation flow. |
| app/src/renderer/routes/__root.tsx | Stores toast actions in state so the UI can render Undo. |
| app/src/renderer/queries.ts | Updates delete mutation return type and adds restore mutation + query invalidation. |
| app/src/preload/index.ts | Exposes restoreWorktree and updates deleteWorktree return type in the preload API. |
| app/src/main/worktree-lifecycle.ts | Propagates delete undo state from git layer through the lifecycle function. |
| app/src/main/ipc/git.ts | Adds WORKTREE_RESTORE handler and returns delete result from WORKTREE_DELETE. |
7f8385f to
348388f
Compare
Capture the worktree's path/branch and the branch's SHA before deleting it, then surface an Undo action on the resulting toast that recreates the branch ref and re-adds the worktree at its original path. Closes #93
348388f to
b4bb89e
Compare
…utton Address Copilot review on #163: worktree:restore now re-validates worktreePath against managedWorktreesPath (mirroring delete's own check) instead of trusting the renderer-supplied WorktreeDeleteResult verbatim, closing an arbitrary-path-write via a forged/stale IPC payload. Also adds type="button" to the toast Undo button and drops an unnecessary non-null assertion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type
Summary
Why
Worktree/branch deletion was previously guarded only by a confirm dialog, with no way to recover from a mistake short of manually replaying
git reflogby hand. Closes #93.Screenshots / recordings
Verified manually via the new E2E test — after removing a worktree, the "Worktree removed" success toast shows a green "Undo" button next to the copy/dismiss icons, and clicking it restores the worktree and its branch in the sidebar.
Breaking changes
None.
WORKTREE_DELETE's IPC result changed fromvoidto aWorktreeDeleteResultobject ({ worktreePath, branch, branchSha }) so the renderer can drive Undo — additive change, no existing caller relied on thevoidreturn.Test plan
packages/git/src/__tests__/worktrees.test.tscovering: SHA capture + full restore, restore when the branch was preserved (no SHA needed), and the two failure paths (no branch to restore; branch gone with no captured SHA).e2e/specs/worktree-workflow.spec.tsthat creates a worktree, removes it via the context menu, clicks the toast's Undo button, and asserts the worktree/branch reappear in the sidebar.pnpm lint && pnpm typecheck && pnpm testall pass locally (unit + full E2E suite, 14 spec files).