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.rsindex 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 contextreturn Err(GitError::WorktreeCreationFailed {branch: resolved_branch.clone(),diff --git a/src/git/error.rs b/src/git/error.rsindex 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 errorsWorktreeMissing {@@ -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.rsindex 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 ------ β [31mFailed to create worktree for [1mmy-feature[22m[39m- [107m [0m fatal: invalid reference: my-feature+ β [31mBranch [1mmy-feature[22m not found[39m++ π‘ [2mUse --create to create it, or 'wt list --branches --remotes' for available branches[22m");});}@@ -57,8 +58,9 @@ fn test_switch_without_internal() {----- stdout ---------- stderr ------ β [31mFailed to create worktree for [1mmy-feature[22m[39m- [107m [0m fatal: invalid reference: my-feature+ β [31mBranch [1mmy-feature[22m not found[39m++ π‘ [2mUse --create to create it, or 'wt list --branches --remotes' for available branches[22m");});}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.snapindex 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 ------β [31mFailed to create worktree for [1m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[22m[39m-[107m [0m fatal: invalid reference: __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6+β [31mBranch [1m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[22m not found[39m++π‘ [2mUse --create to create it, or 'wt list --branches --remotes' for available branches[22m