Skip to content

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.rs
index 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.rs
index 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.rs
index 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.rs
index 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.rs
index 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.rs
index 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.snap
new file mode 100644
index 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()
+---
+✗ No branch named nonexistent
+↳ To create a new branch, run wt switch nonexistent --create; to list branches, run wt list --branches --remotes
diff --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.snap
new file mode 100644
index 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()
+---
+✗ No branch named nonexistent
+↳ To list branches, run wt list --branches --remotes
diff --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.snap
deleted file mode 100644
index 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()
----
-✗ Branch nonexistent-branch not found
-↳ To create a new branch, run wt switch nonexistent-branch --create; to list branches, run wt list --branches --remotes
diff --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.snap
deleted file mode 100644
index 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()
----
-✗ No worktree found for branch nonexistent
diff --git a/tests/snapshots/integration__integration_tests__directives__switch_without_directive_file.snap b/tests/snapshots/integration__integration_tests__directives__switch_without_directive_file.snap
index 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 -----
-✗ Branch my-feature not found
+✗ No branch named my-feature
↳ To create a new branch, run wt switch my-feature --create; to list branches, run wt list --branches --remotes
diff --git a/tests/snapshots/integration__integration_tests__merge__merge_invalid_target.snap b/tests/snapshots/integration__integration_tests__merge__merge_invalid_target.snap
index 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 -----
-✗ Branch nonexistent-branch not found
+✗ No branch named nonexistent-branch
↳ To create a new branch, run wt switch nonexistent-branch --create; to list branches, run wt list --branches --remotes
diff --git a/tests/snapshots/integration__integration_tests__merge__step_rebase_invalid_target.snap b/tests/snapshots/integration__integration_tests__merge__step_rebase_invalid_target.snap
index 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 -----
-✗ Branch nonexistent-ref not found
-↳ To create a new branch, run wt switch nonexistent-ref --create; to list branches, run wt list --branches --remotes
+✗ No branch, tag, or commit named nonexistent-ref
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_nonexistent_force.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_nonexistent_force.snap
index 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 -----
-✗ No worktree found for branch foo
-✗ No worktree found for branch bar
-✗ No worktree found for branch baz
+✗ No branch named foo
+↳ To list branches, run wt list --branches --remotes
+✗ No branch named bar
+↳ To list branches, run wt list --branches --remotes
+✗ No branch named baz
+↳ To list branches, run wt list --branches --remotes
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_branch.snap b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_branch.snap
index 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 -----
-✗ No worktree found for branch nonexistent
+✗ No branch named nonexistent
+↳ To list branches, run wt list --branches --remotes
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap
index 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 -----
-✗ No worktree found for branch nonexistent
+✗ No branch named nonexistent
+↳ To list branches, run wt list --branches --remotes
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_partial_success.snap b/tests/snapshots/integration__integration_tests__remove__remove_partial_success.snap
index 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 -----
-✗ No worktree found for branch nonexistent
+✗ No branch named nonexistent
+↳ To list branches, run wt list --branches --remotes
◎ Removing feature worktree & branch in background (same commit as main, _)
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 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 -----
-✗ Branch __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6 not found
+✗ No branch named __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6
↳ To create a new branch, run wt switch '__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6' --create; to list branches, run wt list --branches --remotes
diff --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.snap
index 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 -----
-✗ No worktree found for branch orphan-branch
+✗ Branch orphan-branch has no worktree
+↳ To create a worktree, run wt switch orphan-branch
diff --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.snap
index 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 -----
-✗ No worktree found for branch orphan-branch
+✗ Branch orphan-branch has no worktree
+↳ To create a worktree, run wt switch orphan-branch
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_create_invalid_base.snap b/tests/snapshots/integration__integration_tests__switch__switch_create_invalid_base.snap
index 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 -----
-✗ Branch nonexistent-base not found
-↳ To create a new branch, run wt switch nonexistent-base --create; to list branches, run wt list --branches --remotes
+✗ No branch, tag, or commit named nonexistent-base