Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix: identify detached worktrees by their paths (#4498)
Maximilian Roos5e5c65b2bba5
diff --git a/src/commands/for_each.rs b/src/commands/for_each.rsindex 7b8dc1f0d..8a6b77c09 100644--- a/src/commands/for_each.rs+++ b/src/commands/for_each.rs@@ -28,7 +28,6 @@ use std::io::Write as _;use std::process::Stdio;use anyhow::Context;-use color_print::cformat;use worktrunk::config::{UserConfig, VarScope};use worktrunk::git::{ErrorExt, Repository, WorktreeInfo, WorktrunkError};use worktrunk::shell_exec::{Cmd, ShellEscapeMode};@@ -130,7 +129,7 @@ pub fn step_for_each(args: Vec<String>, format: crate::cli::SwitchFormat) -> any};eprintln!("{}",- error_message(cformat!("Failed in <bold>{display_name}</>{exit_info}"))+ error_message(format!("Failed in {display_name}{exit_info}")));if let Some(detail) = &stderr_detail {eprintln!("{}", format_with_gutter(detail, None));diff --git a/src/commands/remove.rs b/src/commands/remove.rsindex 4ca80bd37..dea1d9075 100644--- a/src/commands/remove.rs+++ b/src/commands/remove.rs@@ -74,7 +74,7 @@ impl RemovePlans {/// weaker association than the rest of the module draws — the template records/// where a worktree *would* go, not that this one belongs to that branch, and/// [`is_worktree_at_expected_path`] returns false for a detached worktree while-/// [`worktree_display_name`] renders one as `dir_name (detached)` without+/// [`worktree_display_name`] renders one as `path (detached)` without/// consulting the template at all. That weakness is why this only ever adds a/// line: a mention that occasionally points at a coincidence costs nothing,/// where a refusal built on the same inference would block work.diff --git a/src/commands/worktree/resolve.rs b/src/commands/worktree/resolve.rsindex 9b5bd9e53..312313cf9 100644--- a/src/commands/worktree/resolve.rs+++ b/src/commands/worktree/resolve.rs@@ -87,8 +87,10 @@ pub fn is_worktree_at_expected_path(////// Returns styled content with branch names bolded:/// - If branch is consistent with worktree location: just the branch name (bolded)-/// - If branch differs from expected location: `dir_name (on **branch**)` (both bolded)-/// - If detached HEAD: `dir_name (detached)` (dir_name bolded)+/// - If branch differs from expected location: `branch @ path` (both bolded)+/// - If detached HEAD: `path (detached)` (path bolded)+///+/// Paths use the full display path, including home-directory abbreviation.////// "Consistent" means the worktree path matches `compute_worktree_path(branch)`,/// which returns repo root for default branch and templated path for others.@@ -97,17 +99,17 @@ pub fn worktree_display_name(repo: &Repository,config: &UserConfig,) -> String {- let dir_name = wt.dir_name();+ let path = format_path_for_display(&wt.path);match &wt.branch {Some(branch) => {if is_worktree_at_expected_path(wt, repo, config) {cformat!("<bold>{branch}</>")} else {- cformat!("<bold>{dir_name}</> (on <bold>{branch}</>)")+ cformat!("<bold>{branch}</> @ <bold>{path}</>")}}- None => cformat!("<bold>{dir_name}</> (detached)"),+ None => cformat!("<bold>{path}</> (detached)"),}}diff --git a/tests/integration_tests/for_each.rs b/tests/integration_tests/for_each.rsindex f6cc8fcc7..92a6bccbe 100644--- a/tests/integration_tests/for_each.rs+++ b/tests/integration_tests/for_each.rs@@ -46,9 +46,25 @@ fn test_for_each_no_args_error(repo: TestRepo) {#[rstest]fn test_for_each_with_detached_head(mut repo: TestRepo) {- repo.add_worktree("detached-test");+ // A generic directory name needs its location even without a collision.+ let lone_path = repo+ .root_path()+ .parent()+ .unwrap()+ .join("solo")+ .join("worktrunk");+ repo.add_worktree_at_path("detached-test", &lone_path);repo.detach_head_in_worktree("detached-test");+ // A detached checkout and an off-template branch can share a directory name.+ for name in ["a", "on-branch"] {+ let path = repo.root_path().parent().unwrap().join(name).join("repo");+ repo.add_worktree_at_path(name, &path);+ if name != "on-branch" {+ repo.detach_head_in_worktree(name);+ }+ }+assert_cmd_snapshot!(make_snapshot_cmd(&repo,"step",diff --git a/tests/snapshots/integration__integration_tests__for_each__for_each_command_fails_in_one.snap b/tests/snapshots/integration__integration_tests__for_each__for_each_command_fails_in_one.snapindex c8f173810..847bdf449 100644--- a/tests/snapshots/integration__integration_tests__for_each__for_each_command_fails_in_one.snap+++ b/tests/snapshots/integration__integration_tests__for_each__for_each_command_fails_in_one.snap@@ -67,27 +67,27 @@ exit_code: 1[0mfatal: ambiguous argument 'nonexistent-ref': unknown revision or path not in the working tree.Use '--' to separate paths from revisions, like this:'git <command> [<revision>...] -- [<file>...]'-[31m✗[39m [31mFailed in [1m[1mmain[22m[22m (exit code 128)[39m+[31m✗[39m [31mFailed in [1mmain[22m (exit code 128)[39m[36m◎[39m [36mRunning in [1mfeature[22m...[39m[0mfatal: ambiguous argument 'nonexistent-ref': unknown revision or path not in the working tree.Use '--' to separate paths from revisions, like this:'git <command> [<revision>...] -- [<file>...]'-[31m✗[39m [31mFailed in [1m[1mfeature[22m[22m (exit code 128)[39m+[31m✗[39m [31mFailed in [1mfeature[22m (exit code 128)[39m[36m◎[39m [36mRunning in [1mfeature-a[22m...[39m[0mfatal: ambiguous argument 'nonexistent-ref': unknown revision or path not in the working tree.Use '--' to separate paths from revisions, like this:'git <command> [<revision>...] -- [<file>...]'-[31m✗[39m [31mFailed in [1m[1mfeature-a[22m[22m (exit code 128)[39m+[31m✗[39m [31mFailed in [1mfeature-a[22m (exit code 128)[39m[36m◎[39m [36mRunning in [1mfeature-b[22m...[39m[0mfatal: ambiguous argument 'nonexistent-ref': unknown revision or path not in the working tree.Use '--' to separate paths from revisions, like this:'git <command> [<revision>...] -- [<file>...]'-[31m✗[39m [31mFailed in [1m[1mfeature-b[22m[22m (exit code 128)[39m+[31m✗[39m [31mFailed in [1mfeature-b[22m (exit code 128)[39m[36m◎[39m [36mRunning in [1mfeature-c[22m...[39m[0mfatal: ambiguous argument 'nonexistent-ref': unknown revision or path not in the working tree.Use '--' to separate paths from revisions, like this:'git <command> [<revision>...] -- [<file>...]'-[31m✗[39m [31mFailed in [1m[1mfeature-c[22m[22m (exit code 128)[39m+[31m✗[39m [31mFailed in [1mfeature-c[22m (exit code 128)[39m[33m▲[39m [33m5 of 5 worktrees failed[39m[107m [0m [1mmain[22mdiff --git a/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable.snap b/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable.snapindex b10e0c3d0..b5203e480 100644--- a/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable.snap+++ b/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable.snap@@ -64,7 +64,7 @@ exit_code: 0----- stderr -----[36m◎[39m [36mRunning in [1mmain[22m...[39m[0mBranch: [main]-[36m◎[39m [36mRunning in [1mrepo.detached-test[22m (detached)...[39m+[36m◎[39m [36mRunning in [1m_REPO_.detached-test[22m (detached)...[39m[0mBranch: [][36m◎[39m [36mRunning in [1mfeature-a[22m...[39m[0mBranch: [feature-a]diff --git a/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable_unguarded.snap b/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable_unguarded.snapindex 4ac3aa58c..248571bf1 100644--- a/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable_unguarded.snap+++ b/tests/snapshots/integration__integration_tests__for_each__for_each_detached_branch_variable_unguarded.snap@@ -64,7 +64,7 @@ exit_code: 1----- stderr -----[36m◎[39m [36mRunning in [1mmain[22m...[39m[0mBranch: main-[36m◎[39m [36mRunning in [1mrepo.detached-test[22m (detached)...[39m+[36m◎[39m [36mRunning in [1m_REPO_.detached-test[22m (detached)...[39m[31m✗[39m [31mFailed to expand for-each argument: undefined variable branch @ line 1[39m[107m [0m Branch: {{ branch }}[2m↳[22m [2mAvailable variables: [4mcommit[24m, [4mcwd[24m, [4mdefault_branch[24m, [4mprimary_worktree_path[24m, [4mremote[24m, [4mremote_url[24m, [4mrepo[24m, [4mrepo_path[24m, [4mshort_commit[24m, [4mworktree_name[24m, [4mworktree_path[24m[22mdiff --git a/tests/snapshots/integration__integration_tests__for_each__for_each_spawn_fails.snap b/tests/snapshots/integration__integration_tests__for_each__for_each_spawn_fails.snapindex d1632070c..f8590f2ec 100644--- a/tests/snapshots/integration__integration_tests__for_each__for_each_spawn_fails.snap+++ b/tests/snapshots/integration__integration_tests__for_each__for_each_spawn_fails.snap@@ -63,19 +63,19 @@ exit_code: 1----- stderr -----[36m◎[39m [36mRunning in [1mmain[22m...[39m-[0m[31m✗[39m [31mFailed in [1m[1mmain[22m[22m (spawn failed)[39m+[0m[31m✗[39m [31mFailed in [1mmain[22m (spawn failed)[39m[107m [0m Failed to execute nonexistent-command-12345 @ ~/repo: [SPAWN_FAIL][36m◎[39m [36mRunning in [1mfeature[22m...[39m-[0m[31m✗[39m [31mFailed in [1m[1mfeature[22m[22m (spawn failed)[39m+[0m[31m✗[39m [31mFailed in [1mfeature[22m (spawn failed)[39m[107m [0m Failed to execute nonexistent-command-12345 @ ~/repo.feature: [SPAWN_FAIL][36m◎[39m [36mRunning in [1mfeature-a[22m...[39m-[0m[31m✗[39m [31mFailed in [1m[1mfeature-a[22m[22m (spawn failed)[39m+[0m[31m✗[39m [31mFailed in [1mfeature-a[22m (spawn failed)[39m[107m [0m Failed to execute nonexistent-command-12345 @ ~/repo.feature-a: [SPAWN_FAIL][36m◎[39m [36mRunning in [1mfeature-b[22m...[39m-[0m[31m✗[39m [31mFailed in [1m[1mfeature-b[22m[22m (spawn failed)[39m+[0m[31m✗[39m [31mFailed in [1mfeature-b[22m (spawn failed)[39m[107m [0m Failed to execute nonexistent-command-12345 @ ~/repo.feature-b: [SPAWN_FAIL][36m◎[39m [36mRunning in [1mfeature-c[22m...[39m-[0m[31m✗[39m [31mFailed in [1m[1mfeature-c[22m[22m (spawn failed)[39m+[0m[31m✗[39m [31mFailed in [1mfeature-c[22m (spawn failed)[39m[107m [0m Failed to execute nonexistent-command-12345 @ ~/repo.feature-c: [SPAWN_FAIL][33m▲[39m [33m5 of 5 worktrees failed[39mdiff --git a/tests/snapshots/integration__integration_tests__for_each__for_each_with_detached_head.snap b/tests/snapshots/integration__integration_tests__for_each__for_each_with_detached_head.snapindex 3ba313c5e..de387d49f 100644--- a/tests/snapshots/integration__integration_tests__for_each__for_each_with_detached_head.snap+++ b/tests/snapshots/integration__integration_tests__for_each__for_each_with_detached_head.snap@@ -64,9 +64,11 @@ exit_code: 0----- stderr -----[36m◎[39m [36mRunning in [1mmain[22m...[39m-[0m[36m◎[39m [36mRunning in [1mrepo.detached-test[22m (detached)...[39m+[0m[36m◎[39m [36mRunning in [1m_PARENT_/a/repo[22m (detached)...[39m+[0m[36m◎[39m [36mRunning in [1mon-branch[22m @ [1m_PARENT_/on-branch/repo[22m...[39m[0m[36m◎[39m [36mRunning in [1mfeature-a[22m...[39m[0m[36m◎[39m [36mRunning in [1mfeature-b[22m...[39m[0m[36m◎[39m [36mRunning in [1mfeature-c[22m...[39m+[0m[36m◎[39m [36mRunning in [1m_PARENT_/solo/worktrunk[22m (detached)...[39m[0m-[32m✓[39m [32mCompleted in 5 worktrees[39m+[32m✓[39m [32mCompleted in 7 worktrees[39m