Repository navigation
feat(remove): back up uncommitted changes before --force discards them - #4545
Conversation
`wt remove --force` now saves the worktree's staged, unstaged and untracked changes (ignored files excepted) as a commit under `refs/wt-backup/<branch>` before removing it, the same ref and reflog the squash backup uses, and prints a `wt switch --create --base <sha>` command to recover them. The snapshot is built through a temporary index, so the real index is untouched; its parent is the worktree's HEAD, which keeps the branch tip reachable after `-D`. A failed backup refuses the removal rather than lose the changes. Closes #4544 Co-Authored-By: Claude <[email protected]>
worktrunk-bot
left a comment
There was a problem hiding this comment.
Two small follow-ups, which I'll push as one commit:
…dule exception Move the backup/recovery hint block into SafetyBackup::print_recovery_hints so wt remove --force and wt step squash print the same thing, and name changes inside submodules alongside ignored files as not saved by the --force backup. Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Claude <[email protected]>
|
@worktrunk-bot the body says a corrupt index now makes
|
A failed backup now refuses with its own message instead of git's stderr alone, and prints no "Discarding uncommitted changes" warning for changes that were kept. Covered by an untracked nested repo with no commits.
worktrunk-bot
left a comment
There was a problem hiding this comment.
The new refusal panics in debug builds and prints a generic "Command failed" header in release. I'll push the fix.
A multi-line bail! trips the debug assert in format_command_error (panic, exit 101) and renders as a generic "Command failed" in release. .context() already renders the refusal as the header with git's stderr in the gutter. Pin the test to exit 1 so a panic no longer passes it. Co-Authored-By: Claude <[email protected]>
|
Done. The "Discarding uncommitted changes" warning now prints only after the backup succeeds, and a failed backup still refuses the removal (fa72076). The body now names the refusal cases as an unreadable non-ignored file or an untracked nested repo with no commits, and says a worktree whose
CI, including |
wt remove --forcenow saves a worktree's uncommitted changes before discarding them, using the samerefs/wt-backup/<branch>ref and reflog that the squash backup inwt merge/wt step squashalready uses. The output then prints the backup and a recovery command:Closes #4544.
What gets saved
git add -Awould stage. The snapshot is built through aTempIndex, so the real index and the stash list are left alone.HEAD, so the branch tip also stays reachable after-Ddeletes the branch. On an unborn branch the commit has no parent.--forceactually discards something. A clean--forceremoval creates no ref.Decisions for review
-floses nothing you can't get back.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 meanswt remove -fnow refuses a worktree thatgit add -Acan't snapshot, such as one holding an unreadable non-ignored file or an untracked nested repo with no commits. A worktree whosegit statusfails (for example, a corrupt index) still has no backup and is removed as before (Explain discarded changes and provide safe squash recovery #4347). The alternative is to warn and continue.refs/wt-backup/today, and this PR doesn't change that.Code
create_safety_backupand the newcreate_removal_backup(src/git/repository/working_tree.rs) share arecord_backuphelper for thecommit-tree/update-refstep. The helper looks up the branch name without the cache, and falls back tosymbolic-refso an unborn branch is still named. The removal side lives instage_worktree_removal(src/git/remove.rs). That function runs right before the worktree is renamed into trash, so it coverswt remove,wt merge --remove, and the foreground path.Tests
test_remove_force_with_untracked_filesnow 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.test_remove_force_refuses_when_backup_failschecks that an untracked nested repo with no commits leaves the worktree in place, and that no discard warning is printed.remove_force_with_*snapshots and thewt remove --helpsnapshot gain the new lines. The docs (wt removehelp, FAQ) mention the backup.