Skip to content

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

Fix WorktreePathOccupied hint: tilde expansion and formatting (#363)

Maximilian Roos3de56afafe14
diff --git a/.claude/rules/cli-output-formatting.md b/.claude/rules/cli-output-formatting.md
index b6d459a29..ef3a788d3 100644
--- a/.claude/rules/cli-output-formatting.md
+++ b/.claude/rules/cli-output-formatting.md
@@ -334,8 +334,9 @@ cformat!("{ERROR_SYMBOL} <red>Branch <bold>{branch}</> not found</>")
Never quote commands or branch names. Use styling to make them stand out:
- **In normal font context**: Use `<bold>` for commands and branches
-- **In hints**: Use `<bright-black>` for commands (hint() already applies
- dimming — no explicit `<dim>` needed)
+- **In hints**: Use `<bright-black>` for commands and data values (paths,
+ branches). Avoid `<bold>` inside hints — the closing `[22m` resets both bold
+ AND dim, so text after `</bold>` loses dim styling.
```rust
// GOOD - bold in normal context
@@ -376,7 +377,8 @@ See `src/commands/list/render.rs` for advanced usage.
## Gutter Formatting
-Use gutter for **quoted content** (git output, commit messages, shell commands):
+Use gutter for **quoted content** (git output, commit messages, config to copy,
+hook commands being displayed):
- `format_bash_with_gutter()` — shell commands (dimmed + syntax highlighting)
- `format_with_gutter()` — other content
@@ -384,6 +386,10 @@ Use gutter for **quoted content** (git output, commit messages, shell commands):
**Gutter vs Table:** Tables for structured app data; gutter for quoting external
content.
+**Gutter vs Hints:** Command suggestions in hints use inline `<bright-black>`,
+not gutter. Gutter is for displaying content (what will execute, config to
+copy); hints suggest what the user should run.
+
## Newline Convention
**Core principle:** All formatting functions return content WITHOUT trailing
diff --git a/src/git/error.rs b/src/git/error.rs
index a87653288..263f1a165 100644
--- a/src/git/error.rs
+++ b/src/git/error.rs
@@ -18,8 +18,8 @@ use shell_escape::escape;
use super::HookType;
use crate::path::format_path_for_display;
use crate::styling::{
- ERROR_SYMBOL, HINT_SYMBOL, error_message, format_bash_with_gutter, format_with_gutter,
- hint_message, info_message, suggest_command,
+ ERROR_SYMBOL, HINT_SYMBOL, error_message, format_with_gutter, hint_message, info_message,
+ suggest_command,
};
/// Domain errors for git and worktree operations.
@@ -244,18 +244,17 @@ impl std::fmt::Display for GitError {
"there's a detached worktree at the expected path <bold>{path_display}</>"
)
};
- // Hint leads into the command in the gutter block
- let hint = hint_message(cformat!(
- "To switch the worktree at <bold>{path_display}</> to branch <bold>{branch}</>:"
- ));
- let path_escaped = escape(Cow::Borrowed(path_display.as_ref()));
+ // Use actual path for command (not display path with ~, which won't expand in single quotes)
+ let path_str = path.to_string_lossy();
+ let path_escaped = escape(Cow::Borrowed(path_str.as_ref()));
let command = format!("cd {path_escaped} && git switch {branch}");
write!(
f,
- "{}\n{}\n{}",
+ "{}\n{}",
error_message(cformat!("Cannot switch to <bold>{branch}</> — {reason}")),
- hint,
- format_bash_with_gutter(&command)
+ hint_message(cformat!(
+ "To switch the worktree at <bright-black>{path_display}</> to <bright-black>{branch}</>, run <bright-black>{command}</>"
+ ))
)
}
@@ -919,7 +918,8 @@ mod tests {
assert!(display.contains("on branch"));
assert!(display.contains("main"));
assert!(display.contains("To switch the worktree at"));
- assert!(display.contains("to branch"));
+ assert!(display.contains(", run ")); // ANSI codes follow, then command
+ assert!(display.contains("cd /tmp/repo && git switch feature"));
// Without occupant (detached)
let err = GitError::WorktreePathOccupied {
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__worktree_path_occupied.snap b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__worktree_path_occupied.snap
index e68f39f65..562e7c022 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__worktree_path_occupied.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__worktree_path_occupied.snap
@@ -3,5 +3,4 @@ source: tests/integration_tests/git_error_display.rs
expression: err.to_string()
---
✗ Cannot switch to feature-z — there's a worktree at the expected path /tmp/repo.feature-z on branch other-branch
-↳ To switch the worktree at /tmp/repo.feature-z to branch feature-z:
-  cd /tmp/repo.feature-z && git switch feature-z
+↳ To switch the worktree at /tmp/repo.feature-z to feature-z, run cd /tmp/repo.feature-z && git switch feature-z
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_default_branch_from_feature_worktree.snap b/tests/snapshots/integration__integration_tests__switch__switch_default_branch_from_feature_worktree.snap
index 459f00a01..42a9a796c 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_default_branch_from_feature_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_default_branch_from_feature_worktree.snap
@@ -23,6 +23,7 @@ info:
SOURCE_DATE_EPOCH: "1735776000"
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -31,5 +32,4 @@ exit_code: 1
----- stderr -----
✗ Cannot switch to main — there's a worktree at the expected path _REPO_ on branch feature-rpa
-↳ To switch the worktree at _REPO_ to branch main:
-  cd _REPO_ && git switch main
+↳ To switch the worktree at _REPO_ to main, run cd _REPO_ && git switch main
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_detached.snap b/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_detached.snap
index 23b047a7d..952f9647f 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_detached.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_detached.snap
@@ -24,6 +24,7 @@ info:
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_DIRECTIVE_FILE: "[DIRECTIVE_FILE]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -32,5 +33,4 @@ exit_code: 1
----- stderr -----
✗ Cannot switch to feature — there's a detached worktree at the expected path _REPO_.feature
-↳ To switch the worktree at _REPO_.feature to branch feature:
-  cd _REPO_.feature && git switch feature
+↳ To switch the worktree at _REPO_.feature to feature, run cd _REPO_.feature && git switch feature
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_different_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_different_branch.snap
index ff6edb831..9580fe134 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_different_branch.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_error_path_occupied_different_branch.snap
@@ -24,6 +24,7 @@ info:
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_DIRECTIVE_FILE: "[DIRECTIVE_FILE]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -32,5 +33,4 @@ exit_code: 1
----- stderr -----
✗ Cannot switch to feature — there's a worktree at the expected path _REPO_.feature on branch bugfix
-↳ To switch the worktree at _REPO_.feature to branch feature:
-  cd _REPO_.feature && git switch feature
+↳ To switch the worktree at _REPO_.feature to feature, run cd _REPO_.feature && git switch feature
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_main_worktree_on_different_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_main_worktree_on_different_branch.snap
index 564cd899f..51968365f 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_main_worktree_on_different_branch.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_main_worktree_on_different_branch.snap
@@ -24,6 +24,7 @@ info:
USERPROFILE: "[TEST_HOME]"
WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
WORKTRUNK_DIRECTIVE_FILE: "[DIRECTIVE_FILE]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
---
success: false
@@ -32,5 +33,4 @@ exit_code: 1
----- stderr -----
✗ Cannot switch to main — there's a worktree at the expected path _REPO_ on branch feature
-↳ To switch the worktree at _REPO_ to branch main:
-  cd _REPO_ && git switch main
+↳ To switch the worktree at _REPO_ to main, run cd _REPO_ && git switch main