Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(switch): trim redundant Repository rebuilds on the accept path (#3557)
Maximilian Roosf87e678aa1a7
diff --git a/src/commands/hook_plan.rs b/src/commands/hook_plan.rsindex 48042d7ef..5e6988960 100644--- a/src/commands/hook_plan.rs+++ b/src/commands/hook_plan.rs@@ -308,6 +308,14 @@ impl ApprovedHookPlan {}}+ /// True when the plan has no entries for any hook type or anchor — the+ /// common case (no project hooks configured). Lets a caller skip building+ /// a render context before finding out `lookup` would return `&[]`+ /// anyway.+ pub fn is_empty(&self) -> bool {+ self.entries.is_empty()+ }+/// The frozen selection for `(hook_type, anchor)`, by exact match.////// Every covered gate anchors at the identical path its executor passes:diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rsindex 62d99a30b..a11f5302c 100644--- a/src/commands/picker/mod.rs+++ b/src/commands/picker/mod.rs@@ -2162,12 +2162,7 @@ summary = truelet query = out.query.trim().to_string();let identifier = resolve_identifier(&action, query, selected_name)?;- // Load config — reuse the recovered repo if we recovered earlier.- let repo = if is_recovered {- repo.clone()- } else {- Repository::current().context("Failed to switch worktree")?- };+ let repo = switch_pipeline_repo(&repo, is_recovered)?;// Clone user config out — `SwitchPipeline` takes `&mut UserConfig` (the// bare-repo path-fix offer and the shell-integration offer record onto// it). Project config is loaded on demand inside the pipeline.@@ -2487,13 +2482,33 @@ fn resolve_identifier(}}+/// Select the `Repository` the accept path's `SwitchPipeline` runs against.+///+/// Extracted from the picker's accept handler for testability.+///+/// Rebuilds fresh rather than reuse `repo` (whose `OnceCell` worktree/branch+/// caches were primed at picker startup and never invalidated, same+/// discipline `spawn`'s `rebuild_repo` doc explains): an in-picker alt-x/alt-r+/// during this session can have removed or added worktrees/branches since.+/// `Repository::current()` re-discovers from cwd, which fails after a+/// deleted-CWD recovery, so the recovered arm rebuilds via `Repository::at`+/// on the already-recovered discovery path instead of reusing the stale+/// startup snapshot.+fn switch_pipeline_repo(repo: &Repository, is_recovered: bool) -> anyhow::Result<Repository> {+ if is_recovered {+ Repository::at(repo.discovery_path()).context("Failed to switch worktree")+ } else {+ Repository::current().context("Failed to switch worktree")+ }+}+#[cfg(test)]pub mod tests {use super::items::{LocalCheckout, LocalContent, PickerRow, worktree_output_token};use super::{AltXRemover, PickerAction, PickerRemovalTarget, RemovalEffect, drain_stashed_warnings,install_preview_tab_keybindings, install_shortcut_keybindings, picker_item_identifier,- resolve_identifier, resolve_shortcut_branch, resolve_shortcut_url,+ resolve_identifier, resolve_shortcut_branch, resolve_shortcut_url, switch_pipeline_repo,};use crate::commands::list::model::{BranchScope, ItemKind, ListItem, WorktreeData};use crate::commands::worktree::RemoveResult;@@ -2659,6 +2674,47 @@ pub mod tests {assert!(result.unwrap_err().to_string().contains("no branch name"));}+ /// `is_recovered=true` must rebuild rather than reuse `repo`: a worktree+ /// added after `repo`'s `OnceCell` list is primed must be visible through+ /// the returned `Repository`, and not through the stale one — proving the+ /// fix actually observes post-mutation state instead of just "not+ /// panicking".+ #[test]+ fn test_switch_pipeline_repo_recovered_rebuilds_fresh() {+ let test = worktrunk::testing::TestRepo::with_initial_commit();+ let stale_repo = worktrunk::git::Repository::at(test.path()).unwrap();+ // Prime the worktree-list cache before the mutation, same as the+ // picker priming its startup repo.+ assert!(stale_repo.worktree_for_branch("feature").unwrap().is_none());++ stale_repo+ .run_command(&[+ "worktree",+ "add",+ "-b",+ "feature",+ test.path()+ .parent()+ .unwrap()+ .join("feature")+ .to_str()+ .unwrap(),+ ])+ .unwrap();++ let fresh_repo = switch_pipeline_repo(&stale_repo, true).unwrap();+ assert!(+ fresh_repo.worktree_for_branch("feature").unwrap().is_some(),+ "recovered arm must rebuild fresh so a worktree added after the \+ startup snapshot was primed is visible"+ );+ assert!(+ stale_repo.worktree_for_branch("feature").unwrap().is_none(),+ "the startup repo's own cache stays stale, confirming the fix \+ rebuilds rather than mutates in place"+ );+ }+/// `from_signal` rejects tokens that carry no usable target: a blank or/// whitespace-only signal, and a bare `worktree-path:` prefix with no path/// after it. A non-empty branch token and a prefixed path both parse.diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rsindex b2e4688ae..3048cd73e 100644--- a/src/commands/worktree/switch.rs+++ b/src/commands/worktree/switch.rs@@ -1481,6 +1481,14 @@ fn spawn_switch_background_hooks(hooks_display_path: Option<&Path>,hook_plan: &ApprovedHookPlan,) -> anyhow::Result<()> {+ // The common case (no project hooks configured): nothing to render or+ // announce, so skip building the destination-rooted `Repository` — it+ // would only be discarded by `HookAnnouncer::flush`'s own no-op-when-empty+ // check below.+ if hook_plan.is_empty() {+ return Ok(());+ }+// Background hooks run in the new/destination worktree. `hook_repo` roots// the *render* context there; the command set is the frozen `hook_plan`// (selected at the gate from the invoking worktree's config), so no