Skip to content

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

Simplify switch branch info to remove mismatch tracking

Maximilian Roosc51a8c9e3990
diff --git a/.claude/rules/cli-output-formatting.md b/.claude/rules/cli-output-formatting.md
index 96fdfee0a..3f801c9a4 100644
--- a/.claude/rules/cli-output-formatting.md
+++ b/.claude/rules/cli-output-formatting.md
@@ -99,6 +99,20 @@ behavior:
"Removing feature worktree in background; retaining branch (--no-delete-branch)" // User flag (user keeps)
```
+**Comma + "but" + em-dash for limitations:** When stating an outcome with a
+limitation and its reason:
+
+```rust
+// Outcome, but limitation — reason
+"Worktree for feature @ ~/repo.feature, but cannot change directory — shell integration not installed"
+"Worktree for feature @ ~/repo.feature, but cannot change directory — shell integration not active"
+```
+
+This pattern:
+- States what succeeded (worktree exists at path)
+- Uses "but" to introduce what didn't work (cannot cd)
+- Uses em-dash to explain why (shell integration status)
+
**Compute decisions once:** For background operations, check conditions upfront,
show the message, then pass the decision explicitly rather than re-checking in
background scripts:
diff --git a/docs/content/list.md b/docs/content/list.md
index cb33505b0..0c332bc5f 100644
--- a/docs/content/list.md
+++ b/docs/content/list.md
@@ -114,7 +114,7 @@ The Status column has multiple subcolumns. Within each, only the first matching
| | `⤴` | Rebase in progress |
| | `⤵` | Merge in progress |
| | `/` | Branch without worktree |
-| | `⚑` | Path doesn't match template |
+| | `⚑` | Worktree path doesn't match branch name |
| | `⊟` | Prunable (directory missing) |
| | `⊞` | Locked worktree |
| Main | `^` | Is the default branch |
diff --git a/src/cli.rs b/src/cli.rs
index 47948211a..d78899f36 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1905,7 +1905,7 @@ The Status column has multiple subcolumns. Within each, only the first matching
| | `⤴` | Rebase in progress |
| | `⤵` | Merge in progress |
| | `/` | Branch without worktree |
-| | `⚑` | Path doesn't match template |
+| | `⚑` | Worktree path doesn't match branch name |
| | `⊟` | Prunable (directory missing) |
| | `⊞` | Locked worktree |
| Main | `^` | Is the default branch |
diff --git a/src/commands/list/model.rs b/src/commands/list/model.rs
index f02fcc421..c2d0de457 100644
--- a/src/commands/list/model.rs
+++ b/src/commands/list/model.rs
@@ -1021,7 +1021,7 @@ impl PositionMask {
/// - ✘: Actual conflicts (must resolve)
/// - ⤴: Rebase in progress
/// - ⤵: Merge in progress
-/// - ⚑: Path doesn't match template
+/// - ⚑: Worktree path doesn't match branch name
/// - ⊟: Prunable (directory missing)
/// - ⊞: Locked worktree
/// - /: Branch without worktree
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index f81b45b67..63cf47ec7 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -334,26 +334,19 @@ impl SwitchResult {
}
}
-/// Branch state for a switch operation, tracking expected vs current branch.
+/// Branch state for a switch operation.
#[derive(Debug, Clone)]
pub struct SwitchBranchInfo {
- /// The branch user expected to switch to (resolved from input)
- pub expected: String,
- /// The branch currently checked out (None = detached HEAD)
- pub current: Option<String>,
+ /// The branch being switched to
+ pub branch: String,
/// Expected path when there's a path mismatch (None = path matches template)
pub expected_path: Option<PathBuf>,
}
impl SwitchBranchInfo {
- /// Check if there's a mismatch between expected and current branch
- pub fn is_branch_mismatch(&self) -> bool {
- self.current.as_deref() != Some(self.expected.as_str())
- }
-
- /// The branch name (for no-mismatch case where expected == current)
+ /// The branch name
pub fn branch(&self) -> &str {
- self.current.as_deref().unwrap_or(&self.expected)
+ &self.branch
}
}
@@ -475,48 +468,41 @@ pub fn handle_switch(
let expected_path = compute_worktree_path(&repo, &resolved_branch, config)?;
// Helper to build switch result for an existing worktree
- // `actual_branch` is None for detached HEAD
- let switch_to_existing =
- |path: PathBuf, actual_branch: Option<String>| -> (SwitchResult, SwitchBranchInfo) {
- let canonical_path = canonicalize(&path).unwrap_or(path.clone());
- let current_dir = std::env::current_dir()
- .ok()
- .and_then(|p| canonicalize(&p).ok());
- let already_at_worktree = current_dir
- .as_ref()
- .map(|cur| cur == &canonical_path)
- .unwrap_or(false);
-
- // Check if the actual path matches the expected path
- let canonical_expected = canonicalize(&expected_path).unwrap_or(expected_path.clone());
- let path_mismatch = if canonical_path != canonical_expected {
- Some(expected_path.clone())
- } else {
- None
- };
+ let switch_to_existing = |path: PathBuf| -> (SwitchResult, SwitchBranchInfo) {
+ let canonical_path = canonicalize(&path).unwrap_or(path.clone());
+ let current_dir = std::env::current_dir()
+ .ok()
+ .and_then(|p| canonicalize(&p).ok());
+ let already_at_worktree = current_dir
+ .as_ref()
+ .map(|cur| cur == &canonical_path)
+ .unwrap_or(false);
+
+ // Check if the actual path matches the expected path
+ let canonical_expected = canonicalize(&expected_path).unwrap_or(expected_path.clone());
+ let path_mismatch = if canonical_path != canonical_expected {
+ Some(expected_path.clone())
+ } else {
+ None
+ };
- let result = if already_at_worktree {
- SwitchResult::AlreadyAt(canonical_path)
- } else {
- SwitchResult::Existing(canonical_path)
- };
- let branch_info = SwitchBranchInfo {
- expected: resolved_branch.clone(),
- current: actual_branch,
- expected_path: path_mismatch,
- };
- (result, branch_info)
+ let result = if already_at_worktree {
+ SwitchResult::AlreadyAt(canonical_path)
+ } else {
+ SwitchResult::Existing(canonical_path)
+ };
+ let branch_info = SwitchBranchInfo {
+ branch: resolved_branch.clone(),
+ expected_path: path_mismatch,
};
+ (result, branch_info)
+ };
// Branch-first lookup: check if branch has a worktree anywhere
match repo.worktree_for_branch(&resolved_branch)? {
Some(existing_path) if existing_path.exists() => {
let _ = repo.record_switch_previous(new_previous.as_deref());
- // Branch matches (found by branch lookup), so actual = requested
- return Ok(switch_to_existing(
- existing_path,
- Some(resolved_branch.clone()),
- ));
+ return Ok(switch_to_existing(existing_path));
}
Some(_) => {
return Err(GitError::WorktreeMissing {
@@ -658,8 +644,7 @@ pub fn handle_switch(
from_remote,
},
SwitchBranchInfo {
- expected: resolved_branch.clone(),
- current: Some(resolved_branch),
+ branch: resolved_branch,
expected_path: None, // Created at expected path by definition
},
))
@@ -1073,69 +1058,6 @@ mod tests {
assert_eq!(result.path(), &path);
}
- #[test]
- fn test_switch_branch_info_is_branch_mismatch_true() {
- let info = SwitchBranchInfo {
- expected: "feature".to_string(),
- current: Some("main".to_string()),
- expected_path: None,
- };
- assert!(info.is_branch_mismatch());
- }
-
- #[test]
- fn test_switch_branch_info_is_branch_mismatch_false() {
- let info = SwitchBranchInfo {
- expected: "feature".to_string(),
- current: Some("feature".to_string()),
- expected_path: None,
- };
- assert!(!info.is_branch_mismatch());
- }
-
- #[test]
- fn test_switch_branch_info_is_branch_mismatch_detached() {
- let info = SwitchBranchInfo {
- expected: "feature".to_string(),
- current: None, // Detached HEAD
- expected_path: None,
- };
- assert!(info.is_branch_mismatch());
- }
-
- #[test]
- fn test_switch_branch_info_branch_with_current() {
- let info = SwitchBranchInfo {
- expected: "expected".to_string(),
- current: Some("current".to_string()),
- expected_path: None,
- };
- assert_eq!(info.branch(), "current");
- }
-
- #[test]
- fn test_switch_branch_info_branch_without_current() {
- let info = SwitchBranchInfo {
- expected: "expected".to_string(),
- current: None,
- expected_path: None,
- };
- assert_eq!(info.branch(), "expected");
- }
-
- #[test]
- fn test_switch_branch_info_with_expected_path() {
- let info = SwitchBranchInfo {
- expected: "feature".to_string(),
- current: Some("feature".to_string()),
- expected_path: Some(PathBuf::from("/expected/path")),
- };
- assert_eq!(
- info.expected_path.as_ref().unwrap().to_str().unwrap(),
- "/expected/path"
- );
- }
-
#[test]
fn test_merge_operations_struct() {
let ops = MergeOperations {
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 6e483cd37..b68ca8d8d 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -287,57 +287,13 @@ pub fn handle_switch_output(
let path = result.path();
let path_display = format_path_for_display(path);
- let expected_branch = &branch_info.expected;
-
- // Handle branch mismatch cases (worktree checked out to wrong branch or detached)
- if branch_info.is_branch_mismatch() {
- let mismatch_info = match &branch_info.current {
- Some(current) => {
- cformat!("expecting <bold>{expected_branch}</>, got <bold>{current}</>")
- }
- None => cformat!("expecting <bold>{expected_branch}</>, got detached HEAD"),
- };
-
- match result {
- SwitchResult::AlreadyAt(_) => {
- super::print(warning_message(cformat!(
- "Already at <bold>{path_display}</>. Branch doesn't match path: {mismatch_info}"
- )))?;
- }
- SwitchResult::Existing(_) => {
- if is_directive_mode || has_execute_command {
- super::print(warning_message(cformat!(
- "Switched to <bold>{path_display}</>. Branch doesn't match path: {mismatch_info}"
- )))?;
- } else if Shell::is_integration_configured().ok().flatten().is_some() {
- super::print(warning_message(cformat!(
- "Worktree at <bold>{path_display}</>. Branch doesn't match path: {mismatch_info}; cannot cd, binary invoked directly"
- )))?;
- } else {
- super::print(warning_message(cformat!(
- "Worktree at <bold>{path_display}</>. Branch doesn't match path: {mismatch_info}; cannot cd, no shell integration"
- )))?;
- super::shell_integration_hint(shell_integration_hint())?;
- }
- }
- SwitchResult::Created { .. } => {
- // Created case should never have mismatch - we just created it
- unreachable!("Created worktree should not have branch mismatch");
- }
- }
-
- super::flush()?;
- return Ok(());
- }
-
- // Normal case - branch matches
let branch = branch_info.branch();
// Show path mismatch warning after the main message
let path_mismatch_warning = branch_info.expected_path.as_ref().map(|expected| {
let expected_display = format_path_for_display(expected);
- warning_message(format!(
- "Worktree path doesn't match naming template; expected {expected_display}"
+ warning_message(cformat!(
+ "Worktree path doesn't match branch name; expected <bold>{expected_display}</> <red>⚑</>"
))
});
@@ -359,15 +315,16 @@ pub fn handle_switch_output(
super::print(warning)?;
}
} else if Shell::is_integration_configured().ok().flatten().is_some() {
+ // Shell wrapper is configured but user ran binary directly
super::print(warning_message(cformat!(
- "Worktree for <bold>{branch}</> @ <bold>{path_display}</>; cannot cd (binary invoked directly)"
+ "Worktree for <bold>{branch}</> @ <bold>{path_display}</>, but cannot change directory — shell integration not active"
)))?;
if let Some(warning) = path_mismatch_warning {
super::print(warning)?;
}
} else {
super::print(warning_message(cformat!(
- "Worktree for <bold>{branch}</> @ <bold>{path_display}</>; cannot cd (no shell integration)"
+ "Worktree for <bold>{branch}</> @ <bold>{path_display}</>, but cannot change directory — shell integration not installed"
)))?;
if let Some(warning) = path_mismatch_warning {
super::print(warning)?;
diff --git a/tests/snapshots/integration__integration_tests__help__help_list_long.snap b/tests/snapshots/integration__integration_tests__help__help_list_long.snap
index e16fc8817..3ca5a81bc 100644
--- a/tests/snapshots/integration__integration_tests__help__help_list_long.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_list_long.snap
@@ -128,7 +128,7 @@ The Status column has multiple subcolumns. Within each, only the first matching
⤴ Rebase in progress
⤵ Merge in progress
/ Branch without worktree
- ⚑ Path doesn't match template
+ ⚑ Worktree path doesn't match branch name
⊟ Prunable (directory missing)
⊞ Locked worktree
Main ^ Is the default branch
diff --git a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_skip_existing.snap b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_skip_existing.snap
index 76f2b4055..b37321512 100644
--- a/tests/snapshots/integration__integration_tests__post_start_commands__post_start_skip_existing.snap
+++ b/tests/snapshots/integration__integration_tests__post_start_commands__post_start_skip_existing.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for feature @ [REPO].feature; cannot cd (no shell integration)
+🟡 Worktree for feature @ [REPO].feature, but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd
diff --git a/tests/snapshots/integration__integration_tests__switch__ping_pong_2_feature_a_to_main.snap b/tests/snapshots/integration__integration_tests__switch__ping_pong_2_feature_a_to_main.snap
index 046ddf3c1..243852daf 100644
--- a/tests/snapshots/integration__integration_tests__switch__ping_pong_2_feature_a_to_main.snap
+++ b/tests/snapshots/integration__integration_tests__switch__ping_pong_2_feature_a_to_main.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for main @ [REPO]; cannot cd (no shell integration)
+🟡 Worktree for main @ [REPO], but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd
diff --git a/tests/snapshots/integration__integration_tests__switch__ping_pong_3_dash_to_feature_a.snap b/tests/snapshots/integration__integration_tests__switch__ping_pong_3_dash_to_feature_a.snap
index cdacba8f9..48f06a14c 100644
--- a/tests/snapshots/integration__integration_tests__switch__ping_pong_3_dash_to_feature_a.snap
+++ b/tests/snapshots/integration__integration_tests__switch__ping_pong_3_dash_to_feature_a.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for feature-a @ [REPO].feature-a; cannot cd (no shell integration)
+🟡 Worktree for feature-a @ [REPO].feature-a, but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd
diff --git a/tests/snapshots/integration__integration_tests__switch__ping_pong_4_dash_to_main.snap b/tests/snapshots/integration__integration_tests__switch__ping_pong_4_dash_to_main.snap
index 6172119a6..0c29ba248 100644
--- a/tests/snapshots/integration__integration_tests__switch__ping_pong_4_dash_to_main.snap
+++ b/tests/snapshots/integration__integration_tests__switch__ping_pong_4_dash_to_main.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for main @ [REPO]; cannot cd (no shell integration)
+🟡 Worktree for main @ [REPO], but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd
diff --git a/tests/snapshots/integration__integration_tests__switch__ping_pong_5_dash_to_feature_a_again.snap b/tests/snapshots/integration__integration_tests__switch__ping_pong_5_dash_to_feature_a_again.snap
index cdacba8f9..48f06a14c 100644
--- a/tests/snapshots/integration__integration_tests__switch__ping_pong_5_dash_to_feature_a_again.snap
+++ b/tests/snapshots/integration__integration_tests__switch__ping_pong_5_dash_to_feature_a_again.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for feature-a @ [REPO].feature-a; cannot cd (no shell integration)
+🟡 Worktree for feature-a @ [REPO].feature-a, but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_existing_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_existing_branch.snap
index 2b5630e5c..d6f2177c7 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_existing_branch.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_existing_branch.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for feature-z @ [REPO].feature-z; cannot cd (no shell integration)
+🟡 Worktree for feature-z @ [REPO].feature-z, but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_existing_with_shell_configured.snap b/tests/snapshots/integration__integration_tests__switch__switch_existing_with_shell_configured.snap
index e50b24122..0abf4e8c5 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_existing_with_shell_configured.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_existing_with_shell_configured.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,4 +29,4 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for shell-configured @ [REPO].shell-configured; cannot cd (binary invoked directly)
+🟡 Worktree for shell-configured @ [REPO].shell-configured, but cannot change directory — shell integration not active
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_path_mismatch_shows_hint.snap b/tests/snapshots/integration__integration_tests__switch__switch_path_mismatch_shows_hint.snap
index 640371c52..f542fa926 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_path_mismatch_shows_hint.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_path_mismatch_shows_hint.snap
@@ -32,4 +32,4 @@ cd '[PROJECT_ID]'
----- stderr -----
✅ Switched to worktree for feature @ [PROJECT_ID]
-🟡 Worktree path doesn't match naming template; expected [REPO].feature
+🟡 Worktree path doesn't match branch name; expected [REPO].feature ⚑
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_stale_history_first_dash.snap b/tests/snapshots/integration__integration_tests__switch__switch_stale_history_first_dash.snap
index 83638d67b..8f02818cb 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_stale_history_first_dash.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_stale_history_first_dash.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for branch-a @ [REPO].branch-a; cannot cd (no shell integration)
+🟡 Worktree for branch-a @ [REPO].branch-a, but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_to_existing_primary_on_different_branch.snap b/tests/snapshots/integration__integration_tests__switch__switch_to_existing_primary_on_different_branch.snap
index b4d77ac10..435899b13 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_to_existing_primary_on_different_branch.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_to_existing_primary_on_different_branch.snap
@@ -14,6 +14,7 @@ info:
GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
GIT_CONFIG_SYSTEM: /dev/null
GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
HOME: "[TEST_HOME]"
LANG: C
LC_ALL: C
@@ -28,5 +29,5 @@ exit_code: 0
----- stdout -----
----- stderr -----
-🟡 Worktree for existing-branch @ [REPO].existing-branch; cannot cd (no shell integration)
+🟡 Worktree for existing-branch @ [REPO].existing-branch, but cannot change directory — shell integration not installed
💡 Run wt config shell install to enable automatic cd