Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Refactor handle_switch_output to accept execute command
Maximilian Roosbf5657b1d0c8
diff --git a/src/commands/select.rs b/src/commands/select.rsindex 73fe507d6..11e8321cb 100644--- a/src/commands/select.rs+++ b/src/commands/select.rs@@ -992,7 +992,7 @@ pub fn handle_select() -> anyhow::Result<()> {execute!(stderr(), crossterm::cursor::MoveTo(0, 0))?;// Show success message; emit cd directive if shell integration is active- handle_switch_output(&result, &branch_info, false)?;+ handle_switch_output(&result, &branch_info, None)?;}Ok(())diff --git a/src/main.rs b/src/main.rsindex 7d78219c3..0d4c61a01 100644--- a/src/main.rs+++ b/src/main.rs@@ -1172,7 +1172,7 @@ fn main() {)?;// Show success message (temporal locality: immediately after worktree operation)- handle_switch_output(&result, &branch_info, execute.is_some())?;+ handle_switch_output(&result, &branch_info, execute.as_deref())?;// Offer shell integration if not already installed/active// (only shows prompt/hint when shell integration isn't working)diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex a124587ce..f45cff48b 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -592,12 +592,17 @@ fn print_switch_message_if_changed(/// Handle output for a switch operation///-/// When shell integration is not active and no execute command is provided,-/// we show warnings for operations that can't complete without shell integration.+/// For existing worktrees: when shell integration is not active, we show+/// warnings that the shell's directory won't change. This includes when+/// `--execute` is used — the command runs in the target directory, but the+/// shell stays put.+///+/// For created worktrees: success message only (creation success trumps the+/// shell warning, and post-create hooks provide value even without cd).pub fn handle_switch_output(result: &SwitchResult,branch_info: &SwitchBranchInfo,- has_execute_command: bool,+ execute_command: Option<&str>,) -> anyhow::Result<()> {// Set target directory for command executionsuper::change_directory(result.path())?;@@ -627,29 +632,37 @@ pub fn handle_switch_output(}}SwitchResult::Existing(_) => {- if is_shell_integration_active || has_execute_command {+ if is_shell_integration_active {+ // Shell integration active — cd will happen automaticallysuper::print(info_message(format_switch_message(branch, path, false, None, None,)))?;if let Some(warning) = path_mismatch_warning {super::print(warning)?;}- } else if Shell::is_integration_configured(&crate::binary_name())- .ok()- .flatten()- .is_some()- {- // Shell wrapper is configured but user ran binary directly- super::print(warning_message(cformat!(- "Worktree for <bold>{branch}</> @ <bold>{path_display}</>, but cannot change directory — restart the shell to activate"- )))?;- if let Some(warning) = path_mismatch_warning {- super::print(warning)?;- }} else {- super::print(warning_message(cformat!(- "Worktree for <bold>{branch}</> @ <bold>{path_display}</>, but cannot change directory — shell integration not installed"- )))?;+ // Shell integration not active — warn that shell won't cd+ let reason = if Shell::is_integration_configured(&crate::binary_name())+ .ok()+ .flatten()+ .is_some()+ {+ "restart the shell to activate"+ } else {+ "shell integration not installed"+ };++ if let Some(cmd) = execute_command {+ // --execute: command will run in target dir, but shell stays put+ super::print(warning_message(cformat!(+ "Executing <bold>{cmd}</> @ <bold>{path_display}</>, but shell directory unchanged — {reason}"+ )))?;+ } else {+ // No --execute: user expected to cd but won't+ super::print(warning_message(cformat!(+ "Worktree for <bold>{branch}</> @ <bold>{path_display}</>, but cannot change directory — {reason}"+ )))?;+ }if let Some(warning) = path_mismatch_warning {super::print(warning)?;}diff --git a/tests/snapshots/integration__integration_tests__switch__switch_execute_existing.snap b/tests/snapshots/integration__integration_tests__switch__switch_execute_existing.snapindex 4125c2da4..17c8e76c8 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_execute_existing.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_execute_existing.snap@@ -33,7 +33,7 @@ exit_code: 0----- stdout ---------- stderr ------[2m○[22m Switched to worktree for [1mexisting-exec[22m @ [1m_REPO_.existing-exec[22m+[33m▲[39m [33mExecuting [1mecho 'existing worktree' > existing.txt[22m @ [1m_REPO_.existing-exec[22m, but shell directory unchanged — shell integration not installed[39m[2m↳[22m [2mTo enable automatic cd, run [90mwt config shell install[39m[22m[36m◎[39m [36mExecuting (--execute):[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'existing worktree'[0m[2m [0m[2m[36m>[0m[2m existing.txtdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_no_hooks_existing.snap b/tests/snapshots/integration__integration_tests__switch__switch_no_hooks_existing.snapindex 350fab691..988c5a771 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_no_hooks_existing.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_no_hooks_existing.snap@@ -35,7 +35,7 @@ exit_code: 0execute still runs----- stderr ------[2m○[22m Switched to worktree for [1mexisting-no-hooks[22m @ [1m_REPO_.existing-no-hooks[22m+[33m▲[39m [33mExecuting [1mecho 'execute still runs'[22m @ [1m_REPO_.existing-no-hooks[22m, but shell directory unchanged — shell integration not installed[39m[2m↳[22m [2mTo enable automatic cd, run [90mwt config shell install[39m[22m[36m◎[39m [36mExecuting (--execute):[39m[107m [0m [2m[0m[2m[34mecho[0m[2m [0m[2m[32m'execute still runs'[0m[2m