Skip to content

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

Fix error message for invalid base branch in switch --create (#569)

Maximilian Roos00756c3c0646
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index f9d3a5d48..ac7920ab1 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -521,6 +521,18 @@ pub fn handle_switch(
None => {}
}
+ // No worktree for branch - validate branch exists before proceeding
+ // When not creating, the branch must exist (locally or on remote)
+ // Note: This check could be moved to the error recovery path (after git worktree add
+ // fails) to avoid the ~10ms overhead on the happy path. We'd detect "invalid reference"
+ // in the error, then call branch_exists() to determine which branch is invalid.
+ if !create && !repo.branch_exists(&resolved_branch)? {
+ return Err(GitError::InvalidReference {
+ reference: resolved_branch.clone(),
+ }
+ .into());
+ }
+
// No worktree for branch - check if expected path is occupied by a different branch's worktree
if let Some((existing_path, path_branch)) = repo.worktree_at_path(&expected_path)? {
if !existing_path.exists() {
@@ -603,7 +615,17 @@ pub fn handle_switch(
// For bare repos with no branches yet (bootstrap case), allow None to create orphan branch.
let base_for_creation = if create {
match resolved_base {
- Some(b) => Some(b),
+ Some(ref b) => {
+ // Validate that the explicitly specified base branch exists
+ // (Same note as above re: moving to error recovery path for performance)
+ if !repo.branch_exists(b)? {
+ return Err(GitError::InvalidReference {
+ reference: b.clone(),
+ }
+ .into());
+ }
+ Some(b.clone())
+ }
None => {
// Try to use default branch as base, but only if it actually exists
// (has commits). For empty repos, the default branch is unborn and
@@ -651,15 +673,8 @@ 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
+ // Note: "invalid reference" errors are caught by upfront validation (lines 529, 621)
return Err(GitError::WorktreeCreationFailed {
branch: resolved_branch.clone(),
base_branch: base_for_creation.clone(),
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 7d7b65361..c49d3b772 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -176,6 +176,18 @@ fn test_switch_base_without_create_warning(repo: TestRepo) {
&["--base", "main", "main"],
);
}
+
+#[rstest]
+fn test_switch_create_with_invalid_base(repo: TestRepo) {
+ // Issue #562: Error message should identify the invalid base branch,
+ // not the target branch being created
+ snapshot_switch(
+ "switch_create_invalid_base",
+ &repo,
+ &["--create", "new-feature", "--base", "nonexistent-base"],
+ );
+}
+
// Internal mode tests
#[rstest]
fn test_switch_internal_mode(repo: TestRepo) {
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
new file mode 100644
index 000000000..c51af2196
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_create_invalid_base.snap
@@ -0,0 +1,40 @@
+---
+source: tests/integration_tests/switch.rs
+assertion_line: 54
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - new-feature
+ - "--base"
+ - nonexistent-base
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ 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
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: false
+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