Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: show "detached worktree" instead of redundant path in switch output (#1685)
Maximilian Roos3f7c6e0ad521
diff --git a/src/commands/handle_switch.rs b/src/commands/handle_switch.rsindex 2b7158e60..9ad124bef 100644--- a/src/commands/handle_switch.rs+++ b/src/commands/handle_switch.rs@@ -132,7 +132,7 @@ pub(crate) fn approve_switch_hooks(return Ok(false);}- let ctx = CommandContext::new(repo, config, Some(plan.branch()), plan.worktree_path(), yes);+ let ctx = CommandContext::new(repo, config, plan.branch(), plan.worktree_path(), yes);let approved = approve_hooks(&ctx, switch_post_hook_types(plan.is_create()))?;if !approved {@@ -176,12 +176,12 @@ pub(crate) fn spawn_switch_background_hooks(repo: &Repository,config: &UserConfig,result: &SwitchResult,- branch: &str,+ branch: Option<&str>,yes: bool,extra_vars: &[(&str, &str)],hooks_display_path: Option<&Path>,) -> anyhow::Result<()> {- let ctx = CommandContext::new(repo, config, Some(branch), result.path(), yes);+ let ctx = CommandContext::new(repo, config, branch, result.path(), yes);let mut hooks = super::hooks::prepare_background_hooks(&ctx,@@ -296,10 +296,14 @@ pub fn handle_switch(return Ok(());}- // Compute path mismatch lazily (deferred from plan_switch for existing worktrees)+ // Compute path mismatch lazily (deferred from plan_switch for existing worktrees).+ // Skip for detached HEAD worktrees (branch is None) — no branch to compute expected path from.let branch_info = match &result {SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {- let expected_path = path_mismatch(&repo, &branch_info.branch, path, config);+ let expected_path = branch_info+ .branch+ .as_deref()+ .and_then(|b| path_mismatch(&repo, b, path, config));SwitchBranchInfo {expected_path,..branch_info@@ -356,7 +360,7 @@ pub fn handle_switch(&repo,config,&result,- &branch_info.branch,+ branch_info.branch.as_deref(),yes,&extra_vars,hooks_display_path.as_deref(),@@ -367,7 +371,13 @@ pub fn handle_switch(// Note: execute_args requires execute via clap's `requires` attributeif let Some(cmd) = execute {// Build template context for expansion (includes base vars when creating)- let ctx = CommandContext::new(&repo, config, Some(&branch_info.branch), result.path(), yes);+ let ctx = CommandContext::new(+ &repo,+ config,+ branch_info.branch.as_deref(),+ result.path(),+ yes,+ );let template_vars = build_hook_context(&ctx, &extra_vars)?;let vars: HashMap<&str, &str> = template_vars.iter()diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex 04664929c..8b164387f 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -408,11 +408,14 @@ pub fn handle_picker(let (result, branch_info) =execute_switch(&repo, plan, &config, false, hooks_approved)?;- // Compute path mismatch lazily (deferred from plan_switch for existing worktrees)+ // Compute path mismatch lazily (deferred from plan_switch for existing worktrees).+ // Skip for detached HEAD worktrees (branch is None).let branch_info = match &result {SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {- let expected_path =- path_mismatch(&repo, &branch_info.branch, path, &config);+ let expected_path = branch_info+ .branch+ .as_deref()+ .and_then(|b| path_mismatch(&repo, b, path, &config));SwitchBranchInfo {expected_path,..branch_info@@ -441,7 +444,7 @@ pub fn handle_picker(&repo,&config,&result,- &branch_info.branch,+ branch_info.branch.as_deref(),false,&extra_vars,hooks_display_path.as_deref(),diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rsindex 95debcccf..9597329fe 100644--- a/src/commands/worktree/switch.rs+++ b/src/commands/worktree/switch.rs@@ -600,7 +600,7 @@ pub fn plan_switch(Some(existing_path) if existing_path.exists() => {return Ok(SwitchPlan::Existing {path: canonicalize(&existing_path).unwrap_or(existing_path),- branch: target.branch,+ branch: Some(target.branch),new_previous,});}@@ -630,16 +630,9 @@ pub fn plan_switch(&& let Some((path, wt_branch)) = repo.worktree_at_path(&abs_path)?{let canonical = canonicalize(&path).unwrap_or_else(|_| path.clone());- // For detached worktrees, use the directory name as the branch identifier- let branch = wt_branch.unwrap_or_else(|| {- canonical- .file_name()- .map(|n| n.to_string_lossy().into_owned())- .unwrap_or_else(|| "(detached)".to_string())- });return Ok(SwitchPlan::Existing {path: canonical,- branch,+ branch: wt_branch,new_previous,});}@@ -938,7 +931,7 @@ pub fn execute_switch(from_remote,},SwitchBranchInfo {- branch,+ branch: Some(branch),expected_path: None,},))diff --git a/src/commands/worktree/types.rs b/src/commands/worktree/types.rsindex e1a5dc5e4..f078d26c0 100644--- a/src/commands/worktree/types.rs+++ b/src/commands/worktree/types.rs@@ -48,8 +48,8 @@ impl SwitchResult {/// Branch state for a switch operation.#[derive(Debug, Clone)]pub struct SwitchBranchInfo {- /// The branch being switched to- pub branch: String,+ /// The branch being switched to. `None` for detached HEAD worktrees.+ pub branch: Option<String>,/// Expected path when there's a branch-worktree mismatch (None = path matches template)pub expected_path: Option<PathBuf>,}@@ -96,7 +96,8 @@ pub enum SwitchPlan {/// Branch already has a worktree - just switch to it (no git commands needed)Existing {path: PathBuf,- branch: String,+ /// The branch at this worktree. `None` for detached HEAD.+ branch: Option<String>,/// Branch to record as "previous" for `wt switch -`new_previous: Option<String>,},@@ -122,10 +123,11 @@ impl SwitchPlan {}}- /// Get the branch name for this plan.- pub fn branch(&self) -> &str {+ /// Get the branch name for this plan. `None` for detached HEAD worktrees.+ pub fn branch(&self) -> Option<&str> {match self {- SwitchPlan::Existing { branch, .. } | SwitchPlan::Create { branch, .. } => branch,+ SwitchPlan::Existing { branch, .. } => branch.as_deref(),+ SwitchPlan::Create { branch, .. } => Some(branch),}}diff --git a/src/output/handlers.rs b/src/output/handlers.rsindex f62b0325a..20649dee0 100644--- a/src/output/handlers.rs+++ b/src/output/handlers.rs@@ -469,7 +469,11 @@ pub fn handle_switch_output(// The cd directive (above) handles its own translation internally.let path = super::to_logical_path(result.path());let path_display = format_path_for_display(&path);- let branch = &branch_info.branch;+ // For detached HEAD worktrees, use a static label since the path already appears after @.+ let branch: &str = match &branch_info.branch {+ Some(b) => b,+ None => "detached worktree",+ };// Check if shell integration is active (directive file set)let is_shell_integration_active = super::is_shell_integration_active();@@ -494,7 +498,7 @@ pub fn handle_switch_output(let branch_worktree_mismatch_warning = branch_info.expected_path.as_ref()- .map(|expected| format_path_mismatch_warning(&branch_info.branch, &path, expected));+ .map(|expected| format_path_mismatch_warning(branch, &path, expected));let display_path_for_hooks = match result {SwitchResult::AlreadyAt(_) => {diff --git a/tests/snapshots/integration__integration_tests__switch__switch_detached_worktree_by_relative_path.snap b/tests/snapshots/integration__integration_tests__switch__switch_detached_worktree_by_relative_path.snapindex 83ee289c4..0711da752 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_detached_worktree_by_relative_path.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_detached_worktree_by_relative_path.snap@@ -43,5 +43,4 @@ exit_code: 0----- stdout ---------- stderr ------[33m▲[39m [33mBranch-worktree mismatch: [1mrepo.feature-detached[22m @ [1m_REPO_.feature-detached[22m, expected @ [1m_REPO_.repo.feature-detached[22m [31m⚑[39m[39m-[2m○[22m Switched to worktree for [1mrepo.feature-detached[22m @ [1m_REPO_.feature-detached[22m+[2m○[22m Switched to worktree for [1mdetached worktree[22m @ [1m_REPO_.feature-detached[22m