Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(errors): improve "not found" error messages (#774)
Maximilian Roos85c4fc18469f
diff --git a/src/commands/config/state.rs b/src/commands/config/state.rsindex acdf54ca5..c296787fb 100644--- a/src/commands/config/state.rs+++ b/src/commands/config/state.rs@@ -205,8 +205,9 @@ pub fn handle_state_get(key: &str, branch: Option<String>) -> anyhow::Result<()>.unwrap_or_default();if head.is_empty() {- return Err(worktrunk::git::GitError::InvalidReference {- reference: branch_name,+ return Err(worktrunk::git::GitError::BranchNotFound {+ branch: branch_name,+ show_create_hint: true,}.into());}diff --git a/src/commands/repository_ext.rs b/src/commands/repository_ext.rsindex 624c31f48..f4786383e 100644--- a/src/commands/repository_ext.rs+++ b/src/commands/repository_ext.rs@@ -129,8 +129,9 @@ impl RepositoryCliExt for Repository {}.into());}- return Err(GitError::NoWorktreeFound {+ return Err(GitError::BranchNotFound {branch: branch.into(),+ show_create_hint: false,}.into());}diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rsindex 2bd76c3a3..e42e35855 100644--- a/src/commands/worktree/switch.rs+++ b/src/commands/worktree/switch.rs@@ -246,7 +246,7 @@ fn resolve_switch_target())?;None} else if !repo.ref_exists(&resolved)? {- return Err(GitError::InvalidReference {+ return Err(GitError::ReferenceNotFound {reference: resolved,}.into());@@ -360,8 +360,9 @@ fn validate_worktree_creation(} = method&& !repo.branch(branch).exists()?{- return Err(GitError::InvalidReference {- reference: branch.to_string(),+ return Err(GitError::BranchNotFound {+ branch: branch.to_string(),+ show_create_hint: true,}.into());}diff --git a/src/git/error.rs b/src/git/error.rsindex a15cad417..4eb4f6839 100644--- a/src/git/error.rs+++ b/src/git/error.rs@@ -102,7 +102,14 @@ pub enum GitError {BranchAlreadyExists {branch: String,},- InvalidReference {+ BranchNotFound {+ branch: String,+ /// Show hint about creating the branch. Set to false for remove operations+ /// where suggesting creation doesn't make sense.+ show_create_hint: bool,+ },+ /// Reference (branch, tag, commit) not found - used when any commit-ish is accepted+ ReferenceNotFound {reference: String,},@@ -114,9 +121,6 @@ pub enum GitError {WorktreeMissing {branch: String,},- NoWorktreeFound {- branch: String,- },RemoteOnlyBranch {branch: String,remote: String,@@ -293,15 +297,33 @@ impl std::fmt::Display for GitError {)}- GitError::InvalidReference { reference } => {- let create_cmd = suggest_command("switch", &[reference], &["--create"]);+ GitError::BranchNotFound {+ branch,+ show_create_hint,+ } => {let list_cmd = suggest_command("list", &[], &["--branches", "--remotes"]);+ let hint = if *show_create_hint {+ let create_cmd = suggest_command("switch", &[branch], &["--create"]);+ cformat!(+ "To create a new branch, run <bright-black>{create_cmd}</>; to list branches, run <bright-black>{list_cmd}</>"+ )+ } else {+ cformat!("To list branches, run <bright-black>{list_cmd}</>")+ };write!(f,"{}\n{}",- error_message(cformat!("Branch <bold>{reference}</> not found")),- hint_message(cformat!(- "To create a new branch, run <bright-black>{create_cmd}</>; to list branches, run <bright-black>{list_cmd}</>"+ error_message(cformat!("No branch named <bold>{branch}</>")),+ hint_message(hint)+ )+ }++ GitError::ReferenceNotFound { reference } => {+ write!(+ f,+ "{}",+ error_message(cformat!(+ "No branch, tag, or commit named <bold>{reference}</>")))}@@ -332,14 +354,6 @@ impl std::fmt::Display for GitError {)}- GitError::NoWorktreeFound { branch } => {- write!(- f,- "{}",- error_message(cformat!("No worktree found for branch <bold>{branch}</>"))- )- }-GitError::RemoteOnlyBranch { branch, remote } => {let cmd = suggest_command("switch", &[branch], &[]);cwrite!(@@ -646,10 +660,14 @@ impl std::fmt::Display for GitError {}GitError::WorktreeNotFound { branch } => {+ let switch_cmd = suggest_command("switch", &[branch], &[]);write!(f,- "{}",- error_message(cformat!("No worktree found for branch <bold>{branch}</>"))+ "{}\n{}",+ error_message(cformat!("Branch <bold>{branch}</> has no worktree")),+ hint_message(cformat!(+ "To create a worktree, run <bright-black>{switch_cmd}</>"+ )))}diff --git a/src/git/repository/config.rs b/src/git/repository/config.rsindex ebeed4f0d..8b1557fee 100644--- a/src/git/repository/config.rs+++ b/src/git/repository/config.rs@@ -246,7 +246,11 @@ impl Repository {pub fn require_target_branch(&self, target: Option<&str>) -> anyhow::Result<String> {let branch = self.resolve_target_branch(target)?;if !self.branch(&branch).exists()? {- return Err(GitError::InvalidReference { reference: branch }.into());+ return Err(GitError::BranchNotFound {+ branch,+ show_create_hint: true,+ }+ .into());}Ok(branch)}@@ -258,7 +262,7 @@ impl Repository {pub fn require_target_ref(&self, target: Option<&str>) -> anyhow::Result<String> {let reference = self.resolve_target_branch(target)?;if !self.ref_exists(&reference)? {- return Err(GitError::InvalidReference { reference }.into());+ return Err(GitError::ReferenceNotFound { reference }.into());}Ok(reference)}diff --git a/tests/integration_tests/git_error_display.rs b/tests/integration_tests/git_error_display.rsindex cb945cc7d..7d405caea 100644--- a/tests/integration_tests/git_error_display.rs+++ b/tests/integration_tests/git_error_display.rs@@ -38,12 +38,23 @@ fn display_worktree_missing() {}#[test]-fn display_no_worktree_found() {- let err = GitError::NoWorktreeFound {+fn branch_not_found() {+ let err = GitError::BranchNotFound {branch: "nonexistent".into(),+ show_create_hint: true,};- assert_snapshot!("no_worktree_found", err.to_string());+ assert_snapshot!("branch_not_found", err.to_string());+}++#[test]+fn branch_not_found_no_create_hint() {+ let err = GitError::BranchNotFound {+ branch: "nonexistent".into(),+ show_create_hint: false,+ };++ assert_snapshot!("branch_not_found_no_create_hint", err.to_string());}#[test]@@ -137,15 +148,6 @@ fn display_branch_already_exists() {assert_snapshot!("branch_already_exists", err.to_string());}-#[test]-fn display_invalid_reference() {- let err = GitError::InvalidReference {- reference: "nonexistent-branch".into(),- };-- assert_snapshot!("invalid_reference", err.to_string());-}-// ============================================================================// Merge/push errors// ============================================================================diff --git a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__branch_not_found.snap b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__branch_not_found.snapnew file mode 100644index 000000000..9987ec725--- /dev/null+++ b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__branch_not_found.snap@@ -0,0 +1,6 @@+---+source: tests/integration_tests/git_error_display.rs+expression: err.to_string()+---+[31m✗[39m [31mNo branch named [1mnonexistent[22m[39m+[2m↳[22m [2mTo create a new branch, run [90mwt switch nonexistent --create[39m; to list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__branch_not_found_no_create_hint.snap b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__branch_not_found_no_create_hint.snapnew file mode 100644index 000000000..03f96bdab--- /dev/null+++ b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__branch_not_found_no_create_hint.snap@@ -0,0 +1,6 @@+---+source: tests/integration_tests/git_error_display.rs+expression: err.to_string()+---+[31m✗[39m [31mNo branch named [1mnonexistent[22m[39m+[2m↳[22m [2mTo list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__invalid_reference.snap b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__invalid_reference.snapdeleted file mode 100644index ab420d92a..000000000--- a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__invalid_reference.snap+++ /dev/null@@ -1,6 +0,0 @@-----source: tests/integration_tests/git_error_display.rs-expression: err.to_string()-----[31m✗[39m [31mBranch [1mnonexistent-branch[22m not found[39m-[2m↳[22m [2mTo create a new branch, run [90mwt switch nonexistent-branch --create[39m; to list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__no_worktree_found.snap b/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__no_worktree_found.snapdeleted file mode 100644index 04c645c93..000000000--- a/tests/integration_tests/snapshots/integration__integration_tests__git_error_display__no_worktree_found.snap+++ /dev/null@@ -1,5 +0,0 @@-----source: tests/integration_tests/git_error_display.rs-expression: err.to_string()-----[31m✗[39m [31mNo worktree found for branch [1mnonexistent[22m[39mdiff --git a/tests/snapshots/integration__integration_tests__directives__switch_without_directive_file.snap b/tests/snapshots/integration__integration_tests__directives__switch_without_directive_file.snapindex efa73b226..98d4f4ea6 100644--- a/tests/snapshots/integration__integration_tests__directives__switch_without_directive_file.snap+++ b/tests/snapshots/integration__integration_tests__directives__switch_without_directive_file.snap@@ -35,5 +35,5 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mBranch [1mmy-feature[22m not found[39m+[31m✗[39m [31mNo branch named [1mmy-feature[22m[39m[2m↳[22m [2mTo create a new branch, run [90mwt switch my-feature --create[39m; to list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__merge__merge_invalid_target.snap b/tests/snapshots/integration__integration_tests__merge__merge_invalid_target.snapindex 3aa39c826..90f7c2763 100644--- a/tests/snapshots/integration__integration_tests__merge__merge_invalid_target.snap+++ b/tests/snapshots/integration__integration_tests__merge__merge_invalid_target.snap@@ -35,5 +35,5 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mBranch [1mnonexistent-branch[22m not found[39m+[31m✗[39m [31mNo branch named [1mnonexistent-branch[22m[39m[2m↳[22m [2mTo create a new branch, run [90mwt switch nonexistent-branch --create[39m; to list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__merge__step_rebase_invalid_target.snap b/tests/snapshots/integration__integration_tests__merge__step_rebase_invalid_target.snapindex 6bc6ed1bf..e25ca9020 100644--- a/tests/snapshots/integration__integration_tests__merge__step_rebase_invalid_target.snap+++ b/tests/snapshots/integration__integration_tests__merge__step_rebase_invalid_target.snap@@ -36,5 +36,4 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mBranch [1mnonexistent-ref[22m not found[39m-[2m↳[22m [2mTo create a new branch, run [90mwt switch nonexistent-ref --create[39m; to list branches, run [90mwt list --branches --remotes[39m[22m+[31m✗[39m [31mNo branch, tag, or commit named [1mnonexistent-ref[22m[39mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_nonexistent_force.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_nonexistent_force.snapindex 924d0858e..09f41e2fe 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_multiple_nonexistent_force.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_multiple_nonexistent_force.snap@@ -38,6 +38,9 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mNo worktree found for branch [1mfoo[22m[39m-[31m✗[39m [31mNo worktree found for branch [1mbar[22m[39m-[31m✗[39m [31mNo worktree found for branch [1mbaz[22m[39m+[31m✗[39m [31mNo branch named [1mfoo[22m[39m+[2m↳[22m [2mTo list branches, run [90mwt list --branches --remotes[39m[22m+[31m✗[39m [31mNo branch named [1mbar[22m[39m+[2m↳[22m [2mTo list branches, run [90mwt list --branches --remotes[39m[22m+[31m✗[39m [31mNo branch named [1mbaz[22m[39m+[2m↳[22m [2mTo list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_branch.snap b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_branch.snapindex ed603afeb..8a352b1cd 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_branch.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_branch.snap@@ -35,4 +35,5 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mNo worktree found for branch [1mnonexistent[22m[39m+[31m✗[39m [31mNo branch named [1mnonexistent[22m[39m+[2m↳[22m [2mTo list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snapindex ed603afeb..8a352b1cd 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap@@ -35,4 +35,5 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mNo worktree found for branch [1mnonexistent[22m[39m+[31m✗[39m [31mNo branch named [1mnonexistent[22m[39m+[2m↳[22m [2mTo list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__remove__remove_partial_success.snap b/tests/snapshots/integration__integration_tests__remove__remove_partial_success.snapindex 8b3f45c95..e68e06b15 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_partial_success.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_partial_success.snap@@ -36,5 +36,6 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mNo worktree found for branch [1mnonexistent[22m[39m+[31m✗[39m [31mNo branch named [1mnonexistent[22m[39m+[2m↳[22m [2mTo list branches, run [90mwt list --branches --remotes[39m[22m[36m◎ Removing [1mfeature[22m worktree & branch in background (same commit as [1mmain[22m,[39m [2m_[22m[36m)[39mdiff --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 54ea05731..0fb1c9399 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@@ -36,5 +36,5 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mBranch [1m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[22m not found[39m+[31m✗[39m [31mNo branch named [1m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[22m[39m[2m↳[22m [2mTo create a new branch, run [90mwt switch '__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6' --create[39m; to list branches, run [90mwt list --branches --remotes[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_from_nonexistent_worktree.snap b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_from_nonexistent_worktree.snapindex 0438b3230..9e2db360e 100644--- a/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_from_nonexistent_worktree.snap+++ b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_from_nonexistent_worktree.snap@@ -37,4 +37,5 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mNo worktree found for branch [1morphan-branch[22m[39m+[31m✗[39m [31mBranch [1morphan-branch[22m has no worktree[39m+[2m↳[22m [2mTo create a worktree, run [90mwt switch orphan-branch[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_to_nonexistent_worktree.snap b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_to_nonexistent_worktree.snapindex bce9ca7c1..31e608946 100644--- a/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_to_nonexistent_worktree.snap+++ b/tests/snapshots/integration__integration_tests__step_copy_ignored__copy_ignored_to_nonexistent_worktree.snap@@ -37,4 +37,5 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mNo worktree found for branch [1morphan-branch[22m[39m+[31m✗[39m [31mBranch [1morphan-branch[22m has no worktree[39m+[2m↳[22m [2mTo create a worktree, run [90mwt switch orphan-branch[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_create_invalid_base.snap b/tests/snapshots/integration__integration_tests__switch__switch_create_invalid_base.snapindex 380ae3036..16b66c03f 100644--- a/tests/snapshots/integration__integration_tests__switch__switch_create_invalid_base.snap+++ b/tests/snapshots/integration__integration_tests__switch__switch_create_invalid_base.snap@@ -38,5 +38,4 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mBranch [1mnonexistent-base[22m not found[39m-[2m↳[22m [2mTo create a new branch, run [90mwt switch nonexistent-base --create[39m; to list branches, run [90mwt list --branches --remotes[39m[22m+[31m✗[39m [31mNo branch, tag, or commit named [1mnonexistent-base[22m[39m