Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix: fire post-switch hooks when removing current worktree (#1452)

worktrunk-bot29f1c37d32cf
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 234ee1134..62eb5473b 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -774,6 +774,7 @@ fn handle_branch_only_output(
///
/// Only runs if `verify` is true (hooks approved).
fn spawn_hooks_after_remove(
+ repo: &Repository,
main_path: &std::path::Path,
removed_worktree_path: &std::path::Path,
removed_branch: &str,
@@ -787,7 +788,7 @@ fn spawn_hooks_after_remove(
let Ok(config) = UserConfig::load() else {
return Ok(());
};
- let repo = Repository::at(main_path)?;
+
// When removing the current worktree, user cd's to main_path → use post_hook logic
// (suppresses path if shell integration will cd there).
// When removing a different worktree, user stays at cwd → use pre_hook logic
@@ -801,7 +802,7 @@ fn spawn_hooks_after_remove(
// All hooks use remove_ctx for spawning: log files are named after the removed
// branch since both post-remove and post-switch are consequences of that removal.
// Template variables differ per hook type (prepared separately below).
- let remove_ctx = CommandContext::new(&repo, &config, Some(removed_branch), main_path, false);
+ let remove_ctx = CommandContext::new(repo, &config, Some(removed_branch), main_path, false);
let mut hooks = remove_ctx.prepare_post_remove_commands(
removed_branch,
removed_worktree_path,
@@ -815,7 +816,7 @@ fn spawn_hooks_after_remove(
if changed_directory {
let dest_branch = repo.worktree_at(main_path).branch()?;
let switch_ctx =
- CommandContext::new(&repo, &config, dest_branch.as_deref(), main_path, false);
+ CommandContext::new(repo, &config, dest_branch.as_deref(), main_path, false);
hooks.extend(prepare_background_hooks(
&switch_ctx,
worktrunk::HookType::PostSwitch,
@@ -1152,6 +1153,7 @@ fn handle_removed_worktree_output(ctx: RemovedWorktreeOutputContext<'_>) -> anyh
}
// Post-remove hooks for detached HEAD use "HEAD" as the branch identifier
spawn_hooks_after_remove(
+ &repo,
main_path,
worktree_path,
"HEAD",
@@ -1210,6 +1212,7 @@ fn handle_removed_worktree_output(ctx: RemovedWorktreeOutputContext<'_>) -> anyh
print_switch_message_if_changed(changed_directory, main_path)?;
spawn_hooks_after_remove(
+ &repo,
main_path,
worktree_path,
branch_name,
@@ -1264,6 +1267,7 @@ fn handle_removed_worktree_output(ctx: RemovedWorktreeOutputContext<'_>) -> anyh
)?;
spawn_hooks_after_remove(
+ &repo,
main_path,
worktree_path,
branch_name,
diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rs
index cef4aed0b..09b738836 100644
--- a/tests/integration_tests/user_hooks.rs
+++ b/tests/integration_tests/user_hooks.rs
@@ -1912,3 +1912,37 @@ check = "echo 'MANUAL_PRE_SWITCH' > pre_switch_marker.txt"
"Manual pre-switch hook should have created marker"
);
}
+
+/// When removing the current worktree, post-switch hooks should fire
+/// because the user is implicitly switched back to the primary worktree.
+/// Regression test for https://github.com/max-sixty/worktrunk/issues/1450
+///
+/// Config is committed before creating the worktree, so both worktrees
+/// have .config/wt.toml — isolating the bug to the deleted-cwd problem.
+#[rstest]
+fn test_remove_current_worktree_fires_post_switch_hook(mut repo: TestRepo) {
+ // Write and commit project config BEFORE creating the worktree,
+ // so the feature worktree also has .config/wt.toml
+ repo.write_project_config(
+ r#"post-switch = "echo 'POST_SWITCH_AFTER_REMOVE' > post_switch_marker.txt""#,
+ );
+ repo.commit("Add project config with post-switch hook");
+
+ let feature_path = repo.add_worktree("feature");
+
+ // Remove from WITHIN the feature worktree (current worktree removal)
+ repo.wt_command()
+ .args(["remove", "feature", "--force-delete", "--yes"])
+ .current_dir(&feature_path)
+ .output()
+ .unwrap();
+
+ // Post-switch hook should fire in the primary worktree
+ let marker = repo.root_path().join("post_switch_marker.txt");
+ wait_for_file_content(&marker);
+ let content = fs::read_to_string(&marker).unwrap();
+ assert!(
+ content.contains("POST_SWITCH_AFTER_REMOVE"),
+ "Post-switch hook should run when removing current worktree, got: {content}"
+ );
+}