Skip to content

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

fix: pass --no-cd flag through to interactive picker (#1331)

worktrunk-botdb14500de1bb
diff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rs
index 67ccf2353..545898f17 100644
--- a/src/commands/select/mod.rs
+++ b/src/commands/select/mod.rs
@@ -38,7 +38,11 @@ enum PickerAction {
Remove,
}
-pub fn handle_select(cli_branches: bool, cli_remotes: bool) -> anyhow::Result<()> {
+pub fn handle_select(
+ cli_branches: bool,
+ cli_remotes: bool,
+ change_dir: bool,
+) -> anyhow::Result<()> {
// Interactive picker requires a terminal for the TUI
if !std::io::stdin().is_terminal() {
anyhow::bail!("Interactive picker requires an interactive terminal");
@@ -383,13 +387,17 @@ pub fn handle_select(cli_branches: bool, cli_remotes: bool) -> anyhow::Result<()
};
// Show success message; emit cd directive if shell integration is active
- // Interactive picker always performs cd (change_dir: true)
// When recovered from a deleted worktree, fall back to repo_path().
let fallback_path = repo.repo_path()?.to_path_buf();
let cwd = std::env::current_dir().unwrap_or(fallback_path.clone());
let source_root = repo.current_worktree().root().unwrap_or(fallback_path);
- let hooks_display_path =
- handle_switch_output(&result, &branch_info, true, Some(&source_root), &cwd)?;
+ let hooks_display_path = handle_switch_output(
+ &result,
+ &branch_info,
+ change_dir,
+ Some(&source_root),
+ &cwd,
+ )?;
// Spawn background hooks after success message
if !skip_hooks {
diff --git a/src/main.rs b/src/main.rs
index 753b3da04..db63cef6c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -718,7 +718,7 @@ fn main() {
// Deprecated: show warning and delegate to handle_select
warn_select_deprecated();
- handle_select(branches, remotes)
+ handle_select(branches, remotes, true)
}
#[cfg(not(unix))]
Commands::Select { .. } => {
@@ -779,7 +779,7 @@ fn main() {
let Some(branch) = branch else {
#[cfg(unix)]
{
- return handle_select(branches, remotes);
+ return handle_select(branches, remotes, !no_cd);
}
#[cfg(not(unix))]
diff --git a/tests/integration_tests/switch_picker.rs b/tests/integration_tests/switch_picker.rs
index a4972c231..7c0072b64 100644
--- a/tests/integration_tests/switch_picker.rs
+++ b/tests/integration_tests/switch_picker.rs
@@ -29,7 +29,7 @@ use insta::assert_snapshot;
use portable_pty::CommandBuilder;
use rstest::rstest;
use std::io::{Read, Write};
-use std::path::Path;
+use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::{Duration, Instant};
@@ -994,3 +994,96 @@ fn test_switch_picker_switch_to_existing_worktree(mut repo: TestRepo) {
screen
);
}
+
+/// Helper to create a temporary directive file for PTY tests.
+/// Returns (path, guard) — the guard keeps the temp file alive until dropped.
+fn directive_file_for_pty() -> (PathBuf, tempfile::TempPath) {
+ let file = tempfile::NamedTempFile::new().expect("failed to create temp file");
+ let path = file.path().to_path_buf();
+ let guard = file.into_temp_path();
+ (path, guard)
+}
+
+#[rstest]
+fn test_switch_picker_no_cd_suppresses_directive(mut repo: TestRepo) {
+ repo.remove_fixture_worktrees();
+ repo.run_git(&["remote", "remove", "origin"]);
+
+ // Create a worktree to switch to
+ repo.add_worktree("target-branch");
+
+ let (directive_path, _guard) = directive_file_for_pty();
+
+ let mut env_vars = repo.test_env_vars();
+ env_vars.push((
+ "WORKTRUNK_DIRECTIVE_FILE".to_string(),
+ directive_path.display().to_string(),
+ ));
+
+ // Run `wt switch --no-cd`, select "target-branch" via picker, press Enter
+ let result = exec_in_pty_with_input_expectations(
+ wt_bin().to_str().unwrap(),
+ &["switch", "--no-cd"],
+ repo.root_path(),
+ &env_vars,
+ &[
+ ("target", None), // Filter to "target-branch"
+ ("\r", None), // Enter to switch
+ ],
+ );
+
+ assert_eq!(
+ result.exit_code, 0,
+ "Expected exit code 0 for successful switch"
+ );
+
+ // Verify directive file does NOT contain cd command
+ let directives = std::fs::read_to_string(&directive_path).unwrap_or_default();
+ assert!(
+ !directives.contains("cd '"),
+ "Directive file should NOT contain cd command with --no-cd via picker, got: {}",
+ directives
+ );
+}
+
+#[rstest]
+fn test_switch_picker_emits_cd_directive_by_default(mut repo: TestRepo) {
+ repo.remove_fixture_worktrees();
+ repo.run_git(&["remote", "remove", "origin"]);
+
+ // Create a worktree to switch to
+ repo.add_worktree("target-branch");
+
+ let (directive_path, _guard) = directive_file_for_pty();
+
+ let mut env_vars = repo.test_env_vars();
+ env_vars.push((
+ "WORKTRUNK_DIRECTIVE_FILE".to_string(),
+ directive_path.display().to_string(),
+ ));
+
+ // Run `wt switch` (without --no-cd), select "target-branch" via picker
+ let result = exec_in_pty_with_input_expectations(
+ wt_bin().to_str().unwrap(),
+ &["switch"],
+ repo.root_path(),
+ &env_vars,
+ &[
+ ("target", None), // Filter to "target-branch"
+ ("\r", None), // Enter to switch
+ ],
+ );
+
+ assert_eq!(
+ result.exit_code, 0,
+ "Expected exit code 0 for successful switch"
+ );
+
+ // Verify directive file DOES contain cd command (default behavior)
+ let directives = std::fs::read_to_string(&directive_path).unwrap_or_default();
+ assert!(
+ directives.contains("cd '"),
+ "Directive file should contain cd command without --no-cd, got: {}",
+ directives
+ );
+}