Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat(select): add Alt-C to create worktree from query (#933)
Maximilian Rooseb2f8021f60e
diff --git a/.claude-plugin/skills/worktrunk/reference/switch.md b/.claude-plugin/skills/worktrunk/reference/switch.mdindex 99b0cde59..4e2312469 100644--- a/.claude-plugin/skills/worktrunk/reference/switch.md+++ b/.claude-plugin/skills/worktrunk/reference/switch.md@@ -67,9 +67,10 @@ When called without arguments, `wt switch` opens an interactive picker to browse| Key | Action ||-----|--------|| `↑`/`↓` | Navigate worktree list |+| (type) | Filter worktrees || `Enter` | Switch to selected worktree |+| `Alt-c` | Create new worktree from query || `Esc` | Cancel |-| (type) | Filter worktrees || `1`/`2`/`3`/`4` | Switch preview tab || `Alt-p` | Toggle preview panel || `Ctrl-u`/`Ctrl-d` | Scroll preview up/down |diff --git a/docs/content/switch.md b/docs/content/switch.mdindex b51b9c6f2..e439db413 100644--- a/docs/content/switch.md+++ b/docs/content/switch.md@@ -89,9 +89,10 @@ When called without arguments, `wt switch` opens an interactive picker to browse| Key | Action ||-----|--------|| `↑`/`↓` | Navigate worktree list |+| (type) | Filter worktrees || `Enter` | Switch to selected worktree |+| `Alt-c` | Create new worktree from query || `Esc` | Cancel |-| (type) | Filter worktrees || `1`/`2`/`3`/`4` | Switch preview tab || `Alt-p` | Toggle preview panel || `Ctrl-u`/`Ctrl-d` | Scroll preview up/down |diff --git a/src/cli/mod.rs b/src/cli/mod.rsindex f3d94dafa..276c6d0ce 100644--- a/src/cli/mod.rs+++ b/src/cli/mod.rs@@ -318,9 +318,10 @@ When called without arguments, `wt switch` opens an interactive picker to browse| Key | Action ||-----|--------|| `↑`/`↓` | Navigate worktree list |+| (type) | Filter worktrees || `Enter` | Switch to selected worktree |+| `Alt-c` | Create new worktree from query || `Esc` | Cancel |-| (type) | Filter worktrees || `1`/`2`/`3`/`4` | Switch preview tab || `Alt-p` | Toggle preview panel || `Ctrl-u`/`Ctrl-d` | Scroll preview up/down |diff --git a/src/commands/select/items.rs b/src/commands/select/items.rsindex a9cbe8d24..b67bdedc3 100644--- a/src/commands/select/items.rs+++ b/src/commands/select/items.rs@@ -100,7 +100,7 @@ impl WorktreeSkimItem {// Controls use dim yellow to distinguish from dimmed (white) tabslet controls = cformat!(- "<dim,yellow>Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: toggle</>"+ "<dim,yellow>Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scroll | alt-p: toggle</>");// End each tab and controls with full reset to prevent style bleedingdiff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rsindex 0888b7b1a..4f0595595 100644--- a/src/commands/select/mod.rs+++ b/src/commands/select/mod.rs@@ -181,6 +181,8 @@ pub fn handle_select("4:execute-silent(echo 4 > {0})+refresh-preview",state_path_str),+ // Create new worktree with query as branch name (alt-c for "create")+ "alt-c:accept(create)".to_string(),// Preview toggle (alt-p shows/hides preview)// Note: skim doesn't support change-preview-window like fzf, only toggle"alt-p:toggle-preview".to_string(),@@ -207,18 +209,33 @@ pub fn handle_select(// Handle selectionif let Some(out) = output&& !out.is_abort- && let Some(selected) = out.selected_items.first(){- // Get branch name or worktree path from selected item- // (output() returns the worktree path for existing worktrees, branch name otherwise)- let identifier = selected.output().to_string();+ // Determine if user wants to create a new worktree (alt-n) or switch to existing (enter)+ let create_new =+ matches!(out.final_event, Event::EvActAccept(Some(ref label)) if label == "create");++ // Get branch name: from query if creating new, from selected item if switching+ let (identifier, should_create) = if create_new {+ let query = out.query.trim().to_string();+ if query.is_empty() {+ anyhow::bail!("Cannot create worktree: no branch name entered");+ }+ (query, true)+ } else {+ // Enter pressed: skim accept always includes a selection (abort handled above)+ let selected = out+ .selected_items+ .first()+ .expect("skim accept has selection");+ (selected.output().to_string(), false)+ };// Load configlet config = UserConfig::load().context("Failed to load config")?;let repo = Repository::current().context("Failed to switch worktree")?;- // Switch to the selected worktree (no creation, no approval prompts)- let plan = plan_switch(&repo, &identifier, false, None, false, &config)?;+ // Switch to existing worktree or create new one+ let plan = plan_switch(&repo, &identifier, should_create, None, false, &config)?;let (result, branch_info) = execute_switch(&repo, plan, &config, false, true)?;// Clear the terminal screen after skim exits to prevent artifactsdiff --git a/tests/integration_tests/select.rs b/tests/integration_tests/select.rsindex cffdfa017..b90e449cc 100644--- a/tests/integration_tests/select.rs+++ b/tests/integration_tests/select.rs@@ -649,3 +649,113 @@ branches = truescreen);}++#[rstest]+fn test_select_create_worktree_with_alt_c(mut repo: TestRepo) {+ repo.remove_fixture_worktrees();+ // Remove origin so there's no interference from remote branches+ repo.run_git(&["remote", "remove", "origin"]);++ let env_vars = repo.test_env_vars();++ // Type branch name "new-feature", then press Alt-C (escape + c) to create+ let (raw_output, exit_code) = exec_in_pty_with_input_expectations(+ wt_bin().to_str().unwrap(),+ &["select"],+ repo.root_path(),+ &env_vars,+ &[+ ("new-feature", None), // Type the branch name+ ("\x1bc", None), // Alt-C (escape + c) to create worktree+ ],+ );++ // Alt-C triggers accept which should exit normally+ assert_eq!(exit_code, 0, "Expected exit code 0 for successful create");++ let screen = render_terminal_screen(&raw_output);++ // Verify the success message shows the new branch+ assert!(+ screen.contains("new-feature") || screen.contains("Switched"),+ "Expected success message showing new-feature branch.\nScreen:\n{}",+ screen+ );++ // Verify the worktree was actually created by checking the branch exists+ let branch_output = repo+ .git_command()+ .args(["branch", "--list", "new-feature"])+ .output()+ .unwrap();+ assert!(+ String::from_utf8_lossy(&branch_output.stdout).contains("new-feature"),+ "Branch new-feature should have been created"+ );+}++#[rstest]+fn test_select_create_with_empty_query_fails(mut repo: TestRepo) {+ repo.remove_fixture_worktrees();+ // Remove origin so there's no interference from remote branches+ repo.run_git(&["remote", "remove", "origin"]);++ let env_vars = repo.test_env_vars();++ // Press Alt-C without typing a query - should error+ let (raw_output, exit_code) = exec_in_pty_with_input(+ wt_bin().to_str().unwrap(),+ &["select"],+ repo.root_path(),+ &env_vars,+ "\x1bc", // Alt-C (escape + c) without typing a branch name+ );++ // Should exit with error (non-zero)+ assert_ne!(exit_code, 0, "Expected non-zero exit for empty query");++ let screen = render_terminal_screen(&raw_output);++ // Verify the error message+ assert!(+ screen.contains("no branch name entered") || screen.contains("Cannot create"),+ "Expected error message about missing branch name.\nScreen:\n{}",+ screen+ );+}++#[rstest]+fn test_select_switch_to_existing_worktree(mut repo: TestRepo) {+ repo.remove_fixture_worktrees();+ // Remove origin so there's no interference from remote branches+ repo.run_git(&["remote", "remove", "origin"]);++ // Create a worktree to switch to+ repo.add_worktree("target-branch");++ let env_vars = repo.test_env_vars();++ // Navigate to target-branch and press Enter to switch+ let (raw_output, exit_code) = exec_in_pty_with_input_expectations(+ wt_bin().to_str().unwrap(),+ &["select"],+ repo.root_path(),+ &env_vars,+ &[+ ("target", None), // Filter to "target-branch"+ ("\r", None), // Enter to switch+ ],+ );++ // Should exit successfully+ assert_eq!(exit_code, 0, "Expected exit code 0 for successful switch");++ let screen = render_terminal_screen(&raw_output);++ // Verify the success message or cd directive+ assert!(+ screen.contains("target-branch") || screen.contains("Switched") || screen.contains("cd "),+ "Expected switch output showing target-branch.\nScreen:\n{}",+ screen+ );+}diff --git a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap b/tests/snapshots/integration__integration_tests__help__help_switch_long.snapindex ff9cdc1e5..6d1e5cc18 100644--- a/tests/snapshots/integration__integration_tests__help__help_switch_long.snap+++ b/tests/snapshots/integration__integration_tests__help__help_switch_long.snap@@ -153,15 +153,16 @@ When called without arguments, [2mwt switch[0m opens an interactive picker to[1mKeybindings:[0m- Key Action- ───────────── ───────────────────────────- ↑/↓ Navigate worktree list- Enter Switch to selected worktree- Esc Cancel- (type) Filter worktrees- 1/2/3/4 Switch preview tab- Alt-p Toggle preview panel- Ctrl-u/Ctrl-d Scroll preview up/down+ Key Action+ ───────────── ──────────────────────────────+ ↑/↓ Navigate worktree list+ (type) Filter worktrees+ Enter Switch to selected worktree+ Alt-c Create new worktree from query+ Esc Cancel+ 1/2/3/4 Switch preview tab+ Alt-p Toggle preview panel+ Ctrl-u/Ctrl-d Scroll preview up/down[1mPreview tabs[0m (toggle with number keys):diff --git a/tests/snapshots/integration__integration_tests__select__select_abort_escape.snap b/tests/snapshots/integration__integration_tests__select__select_abort_escape.snapindex 9d488b86b..570985381 100644--- a/tests/snapshots/integration__integration_tests__select__select_abort_escape.snap+++ b/tests/snapshots/integration__integration_tests__select__select_abort_escape.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/select.rsexpression: normalized---> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog+ Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr> @ main ^ 05a4a45d ││○ main has no uncommitted changes│diff --git a/tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snap b/tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snapindex a8ca07e94..3c4ab46de 100644--- a/tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snap+++ b/tests/snapshots/integration__integration_tests__select__select_multiple_worktrees.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/select.rsexpression: normalized---> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Age │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog+ Branch Status HEAD± main↕ Remote⇅ Age │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr> @ main ^ 16h │+ feature-one _ 16h │○ main has no uncommitted changes+ feature-two _ 16h │diff --git a/tests/snapshots/integration__integration_tests__select__select_preview_log.snap b/tests/snapshots/integration__integration_tests__select__select_preview_log.snapindex 05423fe9b..f7e18af0f 100644--- a/tests/snapshots/integration__integration_tests__select__select_preview_log.snap+++ b/tests/snapshots/integration__integration_tests__select__select_preview_log.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/select.rsexpression: normalized---> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog+ Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr> + feature ↑ ↑5 855962..││* 8559621 +1 1d (feature) Add file 5 with content│* b8208db +1 1d Add file 4 with contentdiff --git a/tests/snapshots/integration__integration_tests__select__select_preview_main_diff.snap b/tests/snapshots/integration__integration_tests__select__select_preview_main_diff.snapindex 922366cbc..f14a796e8 100644--- a/tests/snapshots/integration__integration_tests__select__select_preview_main_diff.snap+++ b/tests/snapshots/integration__integration_tests__select__select_preview_main_diff.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/select.rsexpression: normalized---> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog+ Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr> + feature ↑ ↑2 d1ceec..││ feature_code.rs | 6 ++++++│ tests.rs | 4 ++++diff --git a/tests/snapshots/integration__integration_tests__select__select_preview_uncommitted.snap b/tests/snapshots/integration__integration_tests__select__select_preview_uncommitted.snapindex 0159fbd00..8865a307d 100644--- a/tests/snapshots/integration__integration_tests__select__select_preview_uncommitted.snap+++ b/tests/snapshots/integration__integration_tests__select__select_preview_uncommitted.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/select.rsexpression: normalized---> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog+ Branch Status HEAD± main↕ Remote⇅ Commit │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr> + feature ! ↑ +3 -1 ↑1 c605f9..││ tracked.txt | 4 +++-│ 1 file changed, 3 insertions(+), 1 deletion(-)diff --git a/tests/snapshots/integration__integration_tests__select__select_with_branches.snap b/tests/snapshots/integration__integration_tests__select__select_with_branches.snapindex 22fcca199..cf5c60aec 100644--- a/tests/snapshots/integration__integration_tests__select__select_with_branches.snap+++ b/tests/snapshots/integration__integration_tests__select__select_with_branches.snap@@ -3,7 +3,7 @@ source: tests/integration_tests/select.rsexpression: normalized---> [QUERY] │[PREVIEW_HEADER]- Branch Status HEAD± main↕ Remote⇅ │Enter: switch | Esc: cancel | ctrl-u/d: scroll | alt-p: tog+ Branch Status HEAD± main↕ Remote⇅ │Enter: switch | alt-c: create | Esc: cancel | ctrl-u/d: scr> @ main ^ │+ active-worktree _ │○ main has no uncommitted changesorphan-branch /_ │