Skip to content

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

Add PostSwitch hook support for remove worktree

Maximilian Roos166323bd206d
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index a28647e26..072ca1827 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -74,6 +74,7 @@ impl<'a> MergeCommandCollector<'a> {
hooks.push(HookType::PostMerge);
if self.will_remove {
hooks.push(HookType::PreRemove);
+ hooks.push(HookType::PostSwitch);
}
}
diff --git a/src/main.rs b/src/main.rs
index 716790ce1..8a9ff6f20 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1364,7 +1364,8 @@ fn main() {
&repo_root,
force,
);
- let approved = approve_hooks(&ctx, &[HookType::PreRemove])?;
+ let approved =
+ approve_hooks(&ctx, &[HookType::PreRemove, HookType::PostSwitch])?;
// If declined, skip hooks but continue with removal
if !approved {
crate::output::print(info_message(
@@ -1382,6 +1383,7 @@ fn main() {
let result = handle_remove_current(!delete_branch, force_delete, background)
.context("Failed to remove worktree")?;
// Approval was handled at the gate
+ // Post-switch hooks are spawned internally by handle_remove_output
handle_remove_output(&result, None, background, verify)
} else {
use worktrunk::git::ResolvedWorktree;
@@ -1471,6 +1473,7 @@ fn main() {
}
// Remove current worktree last (if it was in the list)
+ // Post-switch hooks are spawned internally by handle_remove_output
if let Some((_path, branch)) = current {
match handle_remove_current(!delete_branch, force_delete, background) {
Ok(result) => {
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 941c930b2..105cb7471 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -523,6 +523,35 @@ fn handle_branch_only_output(
Ok(())
}
+/// Spawn post-switch hooks in the destination worktree after a directory change.
+///
+/// Called when removing a worktree causes a cd to the main worktree.
+/// Only runs if `verify` is true (hooks approved) and `changed_directory` is true.
+fn spawn_post_switch_after_remove(
+ main_path: &std::path::Path,
+ verify: bool,
+ changed_directory: bool,
+) -> anyhow::Result<()> {
+ if !verify || !changed_directory {
+ return Ok(());
+ }
+ let Ok(config) = WorktrunkConfig::load() else {
+ return Ok(());
+ };
+ let dest_repo = Repository::at(main_path);
+ let dest_branch = dest_repo.current_branch()?;
+ let repo_root = dest_repo.worktree_base()?;
+ let ctx = CommandContext::new(
+ &dest_repo,
+ &config,
+ dest_branch,
+ main_path,
+ &repo_root,
+ false, // force=false for CommandContext
+ );
+ ctx.spawn_post_switch_commands()
+}
+
/// Handle output for RemovedWorktree removal
#[allow(clippy::too_many_arguments)]
fn handle_removed_worktree_output(
@@ -592,6 +621,7 @@ fn handle_removed_worktree_output(
"Removed worktree (detached HEAD, no branch to delete)",
))?;
}
+ spawn_post_switch_after_remove(main_path, verify, changed_directory)?;
super::flush()?;
return Ok(());
};
@@ -686,6 +716,7 @@ fn handle_removed_worktree_output(
None,
)?;
+ spawn_post_switch_after_remove(main_path, verify, changed_directory)?;
super::flush()?;
Ok(())
} else {
@@ -778,6 +809,7 @@ fn handle_removed_worktree_output(
print_switch_message_if_changed(changed_directory, main_path)?;
+ spawn_post_switch_after_remove(main_path, verify, changed_directory)?;
super::flush()?;
Ok(())
}