Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(picker): flash success feedback after alt-x removes worktree (#4669)
Yash Gautam786978fdbb6a
diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex eb4457ae9..49d4990bf 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -583,9 +583,9 @@ impl AltXRemover {/// git removal and, only if the worktree unexpectedly survives/// ([`removal_target_still_present`] — a clean-check race, a locked dir, a/// failing `pre-remove` hook), reverts the morph back to the worktree row via- /// [`revert_morph`] and surfaces why. (The branch can't flip integrated in the- /// millisecond between the prediction and the delete, so the only realistic- /// failure is the worktree removal itself.)+ /// [`revert_morph`] and surfaces why. When the target is gone, it checks the+ /// actual branch ref before choosing the confirmation and final row state:+ /// an approved `pre-remove` hook may integrate the branch after planning.////// Returns [`RemovalEffect::Morphed`] on the in-place morph, or/// [`RemovalEffect::Dropped`] when it falls back to@@ -651,6 +651,7 @@ impl AltXRemover {let repo = planning_repo.clone();let approvals = Arc::clone(&self.approvals);+ let items = Arc::clone(&self.items);let render_tx = Arc::clone(&self.render_tx);let stashed_warnings = Arc::clone(&self.stashed_warnings);let shortcut_table = Arc::clone(&self.shortcut_table);@@ -673,6 +674,34 @@ impl AltXRemover {// the worktree dir survives — undo the morph and say so.if removal_target_still_present(&repo, &result) {revert_morph(revert, &header_flash, &stashed_warnings, &render_tx);+ } else {+ let branch_kept = repo.branch(&branch).exists_locally().unwrap_or(false);+ if branch_kept {+ flash_header(+ &header_flash,+ &render_tx,+ info_message(cformat!("Removed <bold>{branch}</> worktree; kept branch"))+ .to_string(),+ );+ } else {+ items+ .lock()+ .unwrap()+ .retain(|item| item.output().as_ref() != branch.as_str());+ if let Some(tx) = render_tx.get() {+ let _ = tx.try_send(Event::Action(resync_pool_action(Arc::clone(&items))));+ let _ = tx.try_send(Event::Action(run_preview_when_settled(+ Arc::new(AtomicUsize::new(0)),+ Arc::new(AtomicUsize::new(0)),+ )));+ }+ flash_header(+ &header_flash,+ &render_tx,+ info_message(cformat!("Removed <bold>{branch}</> worktree and branch"))+ .to_string(),+ );+ }}});@@ -4083,6 +4112,8 @@ pub mod tests {.expect("feature worktree is listed");let items = Arc::new(Mutex::new(Vec::new()));let remover = test_remover(Arc::clone(&items), repo.clone());+ let (render_tx, _render_rx) = tokio::sync::mpsc::channel(8);+ remover.render_tx.set(render_tx).unwrap();let (row, token, rendered, morphed) =setup_morphable_row(&remover, "feature", &reported_path);items.lock().unwrap().push(Arc::clone(&row));@@ -4107,13 +4138,21 @@ pub mod tests {original_line,"the morphed row's display line is rewritten to the `/ branch` line");-// The worktree removal itself runs in the background.let deadline = Instant::now() + Duration::from_secs(5);- while reported_path.exists() && Instant::now() < deadline {+ while remover.header_flash.current().is_none() && Instant::now() < deadline {std::thread::sleep(Duration::from_millis(20));}assert!(!reported_path.exists(), "the worktree is removed");+ let flash = remover.header_flash.current();+ assert!(+ flash.as_deref().is_some_and(|message| {+ message.contains("Removed")+ && message.contains("feature")+ && message.contains("kept branch")+ }),+ "successful removal confirms the worktree was removed and the branch kept: {flash:?}"+ );// The unmerged branch is retained after its worktree is removed.await_branch_presence(&repo, "feature", true);// The removal succeeded, so the morph stands (no revert).@@ -4123,6 +4162,99 @@ pub mod tests {);}+ /// An approved pre-remove hook can integrate the branch after the picker+ /// predicts it will be kept. The background completion must observe that+ /// the branch was actually deleted, remove the morphed row, and report both+ /// removals rather than leaving a phantom branch row.+ #[test]+ fn test_apply_drops_morphed_row_when_pre_remove_hook_integrates_branch() {+ let test = worktrunk::testing::TestRepo::with_initial_commit();+ let repo = worktrunk::git::Repository::at(test.path()).unwrap();+ let wt_dir = tempfile::tempdir().unwrap();+ let wt_path = wt_dir.path().join("feature");+ repo.run_command(&[+ "worktree",+ "add",+ "-b",+ "feature",+ wt_path.to_str().unwrap(),+ ])+ .unwrap();++ fs::write(wt_path.join("new.txt"), "unmerged work").unwrap();+ worktrunk::shell_exec::Cmd::new("git")+ .args(["add", "."])+ .current_dir(&wt_path)+ .run()+ .unwrap();+ worktrunk::shell_exec::Cmd::new("git")+ .args(["commit", "-m", "unmerged work"])+ .current_dir(&wt_path)+ .run()+ .unwrap();++ let command = "git update-ref refs/heads/main refs/heads/feature";+ fs::create_dir_all(test.path().join(".config")).unwrap();+ fs::write(+ test.path().join(".config/wt.toml"),+ format!("pre-remove = {command:?}\n"),+ )+ .unwrap();++ let pid = repo.project_identifier().unwrap();+ let approvals_dir = tempfile::tempdir().unwrap();+ let approvals_path = approvals_dir.path().join("approvals.toml");+ let mut approvals = Approvals::default();+ approvals+ .approve_commands(pid.to_string(), vec![command.to_string()], &approvals_path)+ .unwrap();++ let reported_path = repo+ .list_worktrees()+ .unwrap()+ .iter()+ .find(|wt| wt.branch.as_deref() == Some("feature"))+ .map(|wt| wt.path.clone())+ .expect("feature worktree is listed");+ let items = Arc::new(Mutex::new(Vec::new()));+ let mut remover = test_remover(Arc::clone(&items), repo.clone());+ remover.approvals = Arc::new(approvals);+ let (render_tx, _render_rx) = tokio::sync::mpsc::channel(8);+ remover.render_tx.set(render_tx).unwrap();+ let (row, token, _, morphed) = setup_morphable_row(&remover, "feature", &reported_path);+ items.lock().unwrap().push(Arc::clone(&row));++ remover.apply(token);++ assert!(+ morphed.load(std::sync::atomic::Ordering::Relaxed),+ "the row morphs optimistically before the background removal"+ );+ let deadline = Instant::now() + Duration::from_secs(5);+ while remover.header_flash.current().is_none() && Instant::now() < deadline {+ std::thread::sleep(Duration::from_millis(20));+ }++ assert!(!reported_path.exists(), "the worktree is removed");+ assert!(+ !repo.branch("feature").exists_locally().unwrap(),+ "the pre-remove hook integrates the branch so removal deletes it"+ );+ assert!(+ items.lock().unwrap().is_empty(),+ "the row whose branch was deleted is removed from the picker items"+ );+ let flash = remover.header_flash.current();+ assert!(+ flash.as_deref().is_some_and(|message| {+ message.contains("Removed")+ && message.contains("feature")+ && message.contains("worktree and branch")+ }),+ "the success message confirms both the worktree and branch were removed: {flash:?}"+ );+ }+/// A kept-branch worktree removal whose row carries no `MorphHandle` (or whose/// layout hasn't landed) can't morph in place, so `morph_and_remove_in_background`/// falls back to the drop path: `apply` reports `Dropped` and the row leaves the