Skip to content

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.rs
index 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.rs
index 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.rs
index 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.rs
index 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.snap
index 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
fatal: 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>...]'
-✗ Failed in main (exit code 128)
+✗ Failed in main (exit code 128)
◎ Running in feature...
fatal: 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>...]'
-✗ Failed in feature (exit code 128)
+✗ Failed in feature (exit code 128)
◎ Running in feature-a...
fatal: 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>...]'
-✗ Failed in feature-a (exit code 128)
+✗ Failed in feature-a (exit code 128)
◎ Running in feature-b...
fatal: 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>...]'
-✗ Failed in feature-b (exit code 128)
+✗ Failed in feature-b (exit code 128)
◎ Running in feature-c...
fatal: 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>...]'
-✗ Failed in feature-c (exit code 128)
+✗ Failed in feature-c (exit code 128)
▲ 5 of 5 worktrees failed
  main
diff --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.snap
index 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 -----
◎ Running in main...
Branch: [main]
-◎ Running in repo.detached-test (detached)...
+◎ Running in _REPO_.detached-test (detached)...
Branch: []
◎ Running in feature-a...
Branch: [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.snap
index 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 -----
◎ Running in main...
Branch: main
-◎ Running in repo.detached-test (detached)...
+◎ Running in _REPO_.detached-test (detached)...
✗ Failed to expand for-each argument: undefined variable branch @ line 1
  Branch: {{ branch }}
↳ Available variables: commit, cwd, default_branch, primary_worktree_path, remote, remote_url, repo, repo_path, short_commit, worktree_name, worktree_path
diff --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.snap
index 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 -----
◎ Running in main...
-✗ Failed in main (spawn failed)
+✗ Failed in main (spawn failed)
  Failed to execute nonexistent-command-12345 @ ~/repo: [SPAWN_FAIL]
◎ Running in feature...
-✗ Failed in feature (spawn failed)
+✗ Failed in feature (spawn failed)
  Failed to execute nonexistent-command-12345 @ ~/repo.feature: [SPAWN_FAIL]
◎ Running in feature-a...
-✗ Failed in feature-a (spawn failed)
+✗ Failed in feature-a (spawn failed)
  Failed to execute nonexistent-command-12345 @ ~/repo.feature-a: [SPAWN_FAIL]
◎ Running in feature-b...
-✗ Failed in feature-b (spawn failed)
+✗ Failed in feature-b (spawn failed)
  Failed to execute nonexistent-command-12345 @ ~/repo.feature-b: [SPAWN_FAIL]
◎ Running in feature-c...
-✗ Failed in feature-c (spawn failed)
+✗ Failed in feature-c (spawn failed)
  Failed to execute nonexistent-command-12345 @ ~/repo.feature-c: [SPAWN_FAIL]
▲ 5 of 5 worktrees failed
diff --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.snap
index 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 -----
◎ Running in main...
-◎ Running in repo.detached-test (detached)...
+◎ Running in _PARENT_/a/repo (detached)...
+◎ Running in on-branch @ _PARENT_/on-branch/repo...
◎ Running in feature-a...
◎ Running in feature-b...
◎ Running in feature-c...
+◎ Running in _PARENT_/solo/worktrunk (detached)...

-✓ Completed in 5 worktrees
+✓ Completed in 7 worktrees