Skip to content

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

feat(switch): make --no-cd print-only in picker mode (#1445)

worktrunk-bot54f34bb19c0d
diff --git a/docs/content/switch.md b/docs/content/switch.md
index c747ec1f6..b5272a690 100644
--- a/docs/content/switch.md
+++ b/docs/content/switch.md
@@ -217,6 +217,9 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
Hooks still run normally. Useful when hooks handle navigation (e.g.,
tmux workflows) or for CI/automation.
+ In picker mode (no branch argument), prints the selected branch name
+ and exits without switching. Useful for scripting.
+
<b><span class=c>-h</span></b>, <b><span class=c>--help</span></b>
Print help (see a summary with &#39;-h&#39;)
diff --git a/skills/worktrunk/reference/switch.md b/skills/worktrunk/reference/switch.md
index 9a7aaea31..9fccc1388 100644
--- a/skills/worktrunk/reference/switch.md
+++ b/skills/worktrunk/reference/switch.md
@@ -187,6 +187,9 @@ Usage: <b><span class=c>wt switch</span></b> <span class=c>[OPTIONS]</span> <spa
Hooks still run normally. Useful when hooks handle navigation (e.g.,
tmux workflows) or for CI/automation.
+ In picker mode (no branch argument), prints the selected branch name
+ and exits without switching. Useful for scripting.
+
<b><span class=c>-h</span></b>, <b><span class=c>--help</span></b>
Print help (see a summary with &#39;-h&#39;)
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index 68d98f833..32838315b 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -450,6 +450,9 @@ To change which branch a worktree is on, use `git switch` inside that worktree.
///
/// Hooks still run normally. Useful when hooks handle navigation
/// (e.g., tmux workflows) or for CI/automation.
+ ///
+ /// In picker mode (no branch argument), prints the selected branch
+ /// name and exits without switching. Useful for scripting.
#[arg(long)]
no_cd: bool,
diff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rs
index d7d5d717d..d652b84e8 100644
--- a/src/commands/select/mod.rs
+++ b/src/commands/select/mod.rs
@@ -311,6 +311,18 @@ pub fn handle_select(
_ => PickerAction::Switch,
};
+ // --no-cd: just output the selected branch name and exit (read-only, no side effects)
+ if !change_dir {
+ let selected_name = out
+ .selected_items
+ .first()
+ .map(|item| item.output().to_string());
+ let query = out.query.trim().to_string();
+ let identifier = resolve_print_identifier(&action, query, selected_name)?;
+ println!("{identifier}");
+ return Ok(());
+ }
+
match action {
PickerAction::Remove => {
// Get the selected worktree's branch name
@@ -420,9 +432,32 @@ pub fn handle_select(
Ok(())
}
+/// Resolve the identifier to print for `--no-cd` print mode.
+///
+/// Extracted from the picker callback for testability.
+fn resolve_print_identifier(
+ action: &PickerAction,
+ query: String,
+ selected_name: Option<String>,
+) -> anyhow::Result<String> {
+ match action {
+ PickerAction::Create => {
+ if query.is_empty() {
+ anyhow::bail!("Cannot create worktree: no branch name entered");
+ }
+ Ok(query)
+ }
+ PickerAction::Switch => selected_name.context("skim accept has no selection"),
+ PickerAction::Remove => {
+ anyhow::bail!("--no-cd is read-only and cannot be combined with remove (alt-r)")
+ }
+ }
+}
+
#[cfg(test)]
pub mod tests {
use super::preview::{PreviewLayout, PreviewMode, PreviewStateData};
+ use super::{PickerAction, resolve_print_identifier};
use std::fs;
#[test]
@@ -459,4 +494,32 @@ pub mod tests {
let spec = PreviewLayout::Down.to_preview_window_spec(5);
assert!(spec.starts_with("down:"));
}
+
+ #[test]
+ fn test_resolve_print_identifier() {
+ // Switch returns the selected name
+ let result = resolve_print_identifier(
+ &PickerAction::Switch,
+ String::new(),
+ Some("feature/foo".into()),
+ );
+ assert_eq!(result.unwrap(), "feature/foo");
+
+ // Switch with no selection is an error
+ let result = resolve_print_identifier(&PickerAction::Switch, String::new(), None);
+ assert!(result.is_err());
+
+ // Create returns the query
+ let result = resolve_print_identifier(&PickerAction::Create, "new-branch".into(), None);
+ assert_eq!(result.unwrap(), "new-branch");
+
+ // Create with empty query is an error
+ let result = resolve_print_identifier(&PickerAction::Create, String::new(), None);
+ assert!(result.unwrap_err().to_string().contains("no branch name"));
+
+ // Remove is always an error
+ let result =
+ resolve_print_identifier(&PickerAction::Remove, String::new(), Some("main".into()));
+ assert!(result.unwrap_err().to_string().contains("read-only"));
+ }
}
diff --git a/tests/integration_tests/output_system_guard.rs b/tests/integration_tests/output_system_guard.rs
index e931f2a52..53f5d880c 100644
--- a/tests/integration_tests/output_system_guard.rs
+++ b/tests/integration_tests/output_system_guard.rs
@@ -36,6 +36,8 @@ const STDOUT_ALLOWED_PATHS: &[&str] = &[
"config/hints.rs",
// LLM prompt output for wt step commit --show-prompt
"step_commands.rs",
+ // --no-cd flag: branch name output for scripting
+ "select/mod.rs",
];
/// Substrings that indicate the line is a special case (e.g., in a comment or test reference)
diff --git a/tests/integration_tests/switch_picker.rs b/tests/integration_tests/switch_picker.rs
index 7c0072b64..e9edc1638 100644
--- a/tests/integration_tests/switch_picker.rs
+++ b/tests/integration_tests/switch_picker.rs
@@ -1087,3 +1087,54 @@ fn test_switch_picker_emits_cd_directive_by_default(mut repo: TestRepo) {
directives
);
}
+
+#[rstest]
+fn test_switch_picker_no_cd_prints_branch_without_switching(mut repo: TestRepo) {
+ repo.remove_fixture_worktrees();
+ repo.run_git(&["remote", "remove", "origin"]);
+
+ // Create a worktree to select
+ 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`, filter to "target", press Enter to select
+ 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 select
+ ],
+ );
+
+ assert_eq!(
+ result.exit_code, 0,
+ "Expected exit code 0 for --no-cd selection"
+ );
+
+ let screen = result.screen();
+
+ // --no-cd should output the branch name
+ assert!(
+ screen.contains("target-branch"),
+ "Expected branch name in output with --no-cd.\nScreen:\n{}",
+ screen
+ );
+
+ // --no-cd should NOT emit a cd directive (read-only operation)
+ 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, got: {}",
+ directives
+ );
+}
diff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
index d334af32e..4d890dc40 100644
--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap
@@ -78,6 +78,8 @@ Usage: wt switch [OPTIONS] [BRANCH] [--
Skip directory change after switching
Hooks still run normally. Useful when hooks handle navigation (e.g., tmux workflows) or for CI/automation.
+
+ In picker mode (no branch argument), prints the selected branch name and exits without switching. Useful for scripting.
-h, --help
Print help (see a summary with '-h')