Skip to content

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

feat: Handle "invalid reference" error for `switch`

Maximilian Roos01523a70018c
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index c69167702..c8f4f17ab 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -370,6 +370,14 @@ pub fn handle_switch(
.into());
}
}
+ // Check if error is about invalid reference (branch not found)
+ // Format: "fatal: invalid reference: branch-name"
+ if msg.contains("invalid reference:") {
+ return Err(GitError::InvalidReference {
+ reference: resolved_branch.clone(),
+ }
+ .into());
+ }
// Fall back to generic error with context
return Err(GitError::WorktreeCreationFailed {
branch: resolved_branch.clone(),
diff --git a/src/git/error.rs b/src/git/error.rs
index b247d8cdc..26fda08d3 100644
--- a/src/git/error.rs
+++ b/src/git/error.rs
@@ -46,6 +46,9 @@ pub enum GitError {
BranchAlreadyExists {
branch: String,
},
+ InvalidReference {
+ reference: String,
+ },
// Worktree errors
WorktreeMissing {
@@ -145,6 +148,13 @@ impl std::fmt::Display for GitError {
)
}
+ GitError::InvalidReference { reference } => {
+ cwrite!(
+ f,
+ "{ERROR_EMOJI} <red>Branch <bold>{reference}</> not found</>\n\n{HINT_EMOJI} <dim>Use --create to create it, or 'wt list --branches --remotes' for available branches</>"
+ )
+ }
+
GitError::WorktreeMissing { branch } => {
cwrite!(
f,
diff --git a/tests/integration_tests/directives.rs b/tests/integration_tests/directives.rs
index d541fbdd0..b57e778f2 100644
--- a/tests/integration_tests/directives.rs
+++ b/tests/integration_tests/directives.rs
@@ -29,8 +29,9 @@ fn test_switch_internal_directive() {
----- stdout -----
----- stderr -----
- ❌ Failed to create worktree for my-feature
-   fatal: invalid reference: my-feature
+ ❌ Branch my-feature not found
+
+ πŸ’‘ Use --create to create it, or 'wt list --branches --remotes' for available branches
");
});
}
@@ -57,8 +58,9 @@ fn test_switch_without_internal() {
----- stdout -----
----- stderr -----
- ❌ Failed to create worktree for my-feature
-   fatal: invalid reference: my-feature
+ ❌ Branch my-feature not found
+
+ πŸ’‘ Use --create to create it, or 'wt list --branches --remotes' for available branches
");
});
}
diff --git a/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap b/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap
index 9fc102253..ac8d93fab 100644
--- a/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap
+++ b/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap
@@ -25,5 +25,6 @@ exit_code: 1
----- stdout -----
----- stderr -----
-❌ Failed to create worktree for __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6
-  fatal: invalid reference: __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6
+❌ Branch __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6 not found
+
+πŸ’‘ Use --create to create it, or 'wt list --branches --remotes' for available branches