Repository navigation
Explain discarded changes and provide safe squash recovery - #4347
Conversation
worktrunk-bot
left a comment
There was a problem hiding this comment.
wt remove --force now runs git status in the target worktree before removing it, and that call can fail. When it does, the removal is refused, and --force is the only way to remove a worktree that can't be read. Before this PR the force path ran no status anywhere: planning skips ensure_clean under force_worktree, and stage_worktree_removal skipped it too. With a corrupt worktree index (fatal: …/index: index file smaller than expected, exit 128), git worktree remove --force still succeeds, but wt remove --force now stops on the ?. A broken submodule under --ignore-submodules=none should fail the same way. The list is only there to tell the user what will be discarded, so it shouldn't decide whether the removal runs. Suggestion inline.
|
@worktrunk-bot Please review the current head
|
…hem (#4545) `wt remove --force` now saves a worktree's uncommitted changes before discarding them, using the same `refs/wt-backup/<branch>` ref and reflog that the squash backup in `wt merge` / `wt step squash` already uses. The output then prints the backup and a recovery command: ``` ▲ Discarding uncommitted changes (--force): ?? devbox.lock ↳ Backup created @ refs/wt-backup/feature-untracked (690353c) ↳ To recover in a new worktree, run wt switch --create --base 690353c… --no-hooks recovery/690353c ``` Closes #4544. ## What gets saved - Staged, unstaged, and untracked files go into the snapshot, which is what `git add -A` would stage. The snapshot is built through a `TempIndex`, so the real index and the stash list are left alone. - Ignored files, such as build output, are not saved. For submodules, only the checked-out commit is recorded; uncommitted changes inside a submodule are not saved. - The backup commit's parent is the worktree's `HEAD`, so the branch tip also stays reachable after `-D` deletes the branch. On an unborn branch the commit has no parent. - The backup is taken only when `--force` actually discards something. A clean `--force` removal creates no ref. ## Decisions for review - **Always on, no flag.** This follows the issue thread: a flag would grow the CLI, and always saving means `-f` loses nothing you can't get back. - **A failed backup refuses the removal.** The error is `Failed to back up uncommitted changes; worktree not removed`, followed by git's own error, and the "Discarding uncommitted changes" warning is printed only after the backup succeeds. That follows the data-safety guidance in AGENTS.md. It means `wt remove -f` now refuses a worktree that `git add -A` can't snapshot, such as one holding an unreadable non-ignored file or an untracked nested repo with no commits. A worktree whose `git status` fails (for example, a corrupt index) still has no backup and is removed as before (#4347). The alternative is to warn and continue. - **Cost.** Untracked files that aren't ignored are written as blobs. A large unignored artifact makes the removal slower and grows the object store until the reflog entry expires. Nothing prunes `refs/wt-backup/` today, and this PR doesn't change that. ## Code `create_safety_backup` and the new `create_removal_backup` (`src/git/repository/working_tree.rs`) share a `record_backup` helper for the `commit-tree` / `update-ref` step. The helper looks up the branch name without the cache, and falls back to `symbolic-ref` so an unborn branch is still named. The removal side lives in `stage_worktree_removal` (`src/git/remove.rs`). That function runs right before the worktree is renamed into trash, so it covers `wt remove`, `wt merge --remove`, and the foreground path. ## Tests - `test_remove_force_with_untracked_files` now checks that the backup ref holds the untracked file's content, that its parent is the removed branch tip, and that an ignored file is excluded. - A new unit test covers the unborn-branch case. - `test_remove_force_refuses_when_backup_fails` checks that an untracked nested repo with no commits leaves the worktree in place, and that no discard warning is printed. - The four `remove_force_with_*` snapshots and the `wt remove --help` snapshot gain the new lines. The docs (`wt remove` help, FAQ) mention the backup. --------- Co-authored-by: worktrunk-bot <[email protected]> Co-authored-by: Claude <[email protected]>
Removing a dirty worktree recommends
git stash -uwhen untracked files need preserving. Forced removal lists the changes it will discard from a fresh inventory after approved pre-remove hooks. An unreadable index produces Git’s diagnostic and still permits forced removal; SIGINT and SIGTERM cancel before deletion and stop the remaining batch through the existing native cancellation path.Squashing staged work prints the backup ref and a command to recover its exact saved commit in a new worktree. Recovery preserves current branches, staging, and unstaged or untracked files. Git continues to own the backup and reflog.
Validated against main
913ff0547: the required pre-merge gate passed 5,208 tests with one skipped, including forced-removal disclosure, corrupt-index diagnostics, native cancellation, and executed recovery commands. Formatting, Clippy, documentation sync and doctests pass. Independent source/caller review found no remaining issues.