Skip to content

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

perf(switch): defer path computation for existing worktree switches (#1031)

Maximilian Roos848e599f7442
diff --git a/src/commands/handle_switch.rs b/src/commands/handle_switch.rs
index bff5a682d..24de2c91e 100644
--- a/src/commands/handle_switch.rs
+++ b/src/commands/handle_switch.rs
@@ -13,7 +13,7 @@ use worktrunk::workspace::build_worktree_map;
use super::command_approval::approve_hooks;
use super::command_executor::{CommandContext, build_hook_context};
use super::worktree::{
- SwitchBranchInfo, SwitchPlan, SwitchResult, execute_switch, get_path_mismatch, plan_switch,
+ SwitchPlan, SwitchResult, execute_switch, plan_switch, resolve_path_mismatch,
};
use crate::output::{
execute_user_command, handle_switch_output, is_shell_integration_active,
@@ -175,16 +175,7 @@ pub fn handle_switch(
}
// Compute path mismatch lazily (deferred from plan_switch for existing worktrees)
- let branch_info = match &result {
- SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {
- let expected_path = get_path_mismatch(repo, &branch_info.branch, path, config);
- SwitchBranchInfo {
- expected_path,
- ..branch_info
- }
- }
- _ => branch_info,
- };
+ let branch_info = resolve_path_mismatch(branch_info, &result, repo, config);
// Show success message (temporal locality: immediately after worktree operation)
// Returns path to display in hooks when user's shell won't be in the worktree
diff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rs
index 73b50b5bf..08af017f9 100644
--- a/src/commands/select/mod.rs
+++ b/src/commands/select/mod.rs
@@ -19,9 +19,7 @@ use super::handle_switch::{
approve_switch_hooks, spawn_switch_background_hooks, switch_extra_vars,
};
use super::list::collect;
-use super::worktree::{
- SwitchBranchInfo, SwitchResult, execute_switch, get_path_mismatch, plan_switch,
-};
+use super::worktree::{execute_switch, plan_switch, resolve_path_mismatch};
use crate::output::handle_switch_output;
use items::{HeaderSkimItem, PreviewCache, WorktreeSkimItem};
@@ -288,16 +286,7 @@ pub fn handle_select(branches: bool, remotes: bool, config: &UserConfig) -> anyh
let (result, branch_info) = execute_switch(repo, plan, &config, false, skip_hooks)?;
// Compute path mismatch lazily (deferred from plan_switch for existing worktrees)
- let branch_info = match &result {
- SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {
- let expected_path = get_path_mismatch(repo, &branch_info.branch, path, &config);
- SwitchBranchInfo {
- expected_path,
- ..branch_info
- }
- }
- _ => branch_info,
- };
+ let branch_info = resolve_path_mismatch(branch_info, &result, repo, &config);
// Show success message; emit cd directive if shell integration is active
// Interactive picker always performs cd (change_dir: true)
diff --git a/src/commands/worktree/mod.rs b/src/commands/worktree/mod.rs
index ea335a9f5..6ef86e4bc 100644
--- a/src/commands/worktree/mod.rs
+++ b/src/commands/worktree/mod.rs
@@ -91,7 +91,7 @@ pub use resolve::{
compute_worktree_path, get_path_mismatch, is_worktree_at_expected_path, resolve_worktree_arg,
worktree_display_name,
};
-pub use switch::{execute_switch, plan_switch};
+pub use switch::{execute_switch, plan_switch, resolve_path_mismatch};
pub use types::{
BranchDeletionMode, OperationMode, RemoveResult, SwitchBranchInfo, SwitchPlan, SwitchResult,
};
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index fd8e36e35..b6a35f2f4 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -17,7 +17,7 @@ use worktrunk::styling::{
warning_message,
};
-use super::resolve::{compute_clobber_backup, compute_worktree_path};
+use super::resolve::{compute_clobber_backup, compute_worktree_path, get_path_mismatch};
use super::types::{CreationMethod, SwitchBranchInfo, SwitchPlan, SwitchResult};
use crate::commands::command_executor::CommandContext;
@@ -627,7 +627,9 @@ pub fn plan_switch(
/// Execute a validated switch plan.
///
/// Takes a `SwitchPlan` from `plan_switch()` and executes it.
-/// For `SwitchPlan::Existing`, just records history.
+/// For `SwitchPlan::Existing`, just records history. The returned
+/// `SwitchBranchInfo` has `expected_path: None` — callers must use
+/// [`resolve_path_mismatch`] after first output to fill in mismatch warnings.
/// For `SwitchPlan::Create`, creates the worktree and runs hooks.
pub fn execute_switch(
repo: &Repository,
@@ -903,6 +905,29 @@ pub fn execute_switch(
}
}
+/// Resolve the deferred path mismatch for existing worktree switches.
+///
+/// `execute_switch` returns `expected_path: None` for existing worktrees to avoid
+/// ~7 git commands on the hot path. Call this after first output to fill in the
+/// mismatch warning.
+pub fn resolve_path_mismatch(
+ branch_info: SwitchBranchInfo,
+ result: &SwitchResult,
+ repo: &Repository,
+ config: &UserConfig,
+) -> SwitchBranchInfo {
+ match result {
+ SwitchResult::Existing { path } | SwitchResult::AlreadyAt(path) => {
+ let expected_path = get_path_mismatch(repo, &branch_info.branch, path, config);
+ SwitchBranchInfo {
+ expected_path,
+ ..branch_info
+ }
+ }
+ _ => branch_info,
+ }
+}
+
fn worktree_creation_error(
err: &anyhow::Error,
branch: String,