Skip to content

feat(worktrees): reflog-backed undo for worktree/branch deletion#163

Merged
liam-russell merged 2 commits into
mainfrom
feat/reflog-undo
Jul 7, 2026
Merged

feat(worktrees): reflog-backed undo for worktree/branch deletion#163
liam-russell merged 2 commits into
mainfrom
feat/reflog-undo

Conversation

@liam-russell

@liam-russell liam-russell commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Type

  • feat — new feature or user-visible capability

Summary

  • Deleting a worktree (and its branch, when applicable) now shows a toast with an Undo action that recreates the branch and re-adds the worktree at its original path.
  • The undo window matches the toast's normal 5s auto-dismiss; letting it expire or dismissing it manually just lets the deletion stand as before.

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 reflog by 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 from void to a WorktreeDeleteResult object ({ worktreePath, branch, branchSha }) so the renderer can drive Undo — additive change, no existing caller relied on the void return.

Test plan

  • Added unit tests in packages/git/src/__tests__/worktrees.test.ts covering: 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).
  • Added an E2E test in e2e/specs/worktree-workflow.spec.ts that 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 test all pass locally (unit + full E2E suite, 14 spec files).

Copilot AI review requested due to automatic review settings July 7, 2026 10:17

Copilot AI 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.

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 WorktreeDeleteResult and updates worktree:delete IPC to return it; adds new worktree:restore IPC to reverse a deletion.
  • Implements restoreDeletedWorktree in @sproutgit/git (recreate branch ref if needed, then git worktree add back 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.

Comment thread packages/ui/src/components/Toast.tsx
Comment thread app/src/main/ipc/git.ts Outdated
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
…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.
@liam-russell liam-russell merged commit 1a3ba92 into main Jul 7, 2026
15 checks passed
@liam-russell liam-russell deleted the feat/reflog-undo branch July 7, 2026 10:54
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.

Reflog-backed Undo for destructive actions

2 participants