Skip to content

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.rs
index 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.rs
index 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.rs
index 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 execution
super::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 automatically
super::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.snap
index 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 -----
-○ Switched to worktree for existing-exec @ _REPO_.existing-exec
+▲ Executing echo 'existing worktree' > existing.txt @ _REPO_.existing-exec, but shell directory unchanged — shell integration not installed
↳ To enable automatic cd, run wt config shell install
◎ Executing (--execute):
  echo 'existing worktree' > existing.txt
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_no_hooks_existing.snap b/tests/snapshots/integration__integration_tests__switch__switch_no_hooks_existing.snap
index 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: 0
execute still runs
----- stderr -----
-○ Switched to worktree for existing-no-hooks @ _REPO_.existing-no-hooks
+▲ Executing echo 'execute still runs' @ _REPO_.existing-no-hooks, but shell directory unchanged — shell integration not installed
↳ To enable automatic cd, run wt config shell install
◎ Executing (--execute):
  echo 'execute still runs'