Skip to content

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

fix: improve --create conflict error for pr:/mr: syntax (#807)

Maximilian Roos19d82337bc4d
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rs
index cf5650708..3cd53fd82 100644
--- a/src/commands/worktree/switch.rs
+++ b/src/commands/worktree/switch.rs
@@ -58,14 +58,7 @@ fn resolve_pr_ref(
create: bool,
base: Option<&str>,
) -> anyhow::Result<ResolvedTarget> {
- // --create and --base are invalid with pr: syntax
- if create {
- return Err(GitError::RefCreateConflict {
- ref_type: RefType::Pr,
- number: pr_number,
- }
- .into());
- }
+ // --base is invalid with pr: syntax (check early, no network needed)
if base.is_some() {
return Err(GitError::RefBaseConflict {
ref_type: RefType::Pr,
@@ -89,6 +82,16 @@ fn resolve_pr_ref(
format_with_gutter(&format_ref_context(&pr_info), None)
);
+ // --create is invalid with pr: syntax (check after fetch to show branch name)
+ if create {
+ return Err(GitError::RefCreateConflict {
+ ref_type: RefType::Pr,
+ number: pr_number,
+ branch: pr_info.head_ref_name.clone(),
+ }
+ .into());
+ }
+
if pr_info.is_cross_repository {
// Fork PR: check if branch already exists and is tracking this PR
let local_branch = pr_ref::local_branch_name(&pr_info);
@@ -216,14 +219,7 @@ fn resolve_mr_ref(
create: bool,
base: Option<&str>,
) -> anyhow::Result<ResolvedTarget> {
- // --create and --base are invalid with mr: syntax
- if create {
- return Err(GitError::RefCreateConflict {
- ref_type: RefType::Mr,
- number: mr_number,
- }
- .into());
- }
+ // --base is invalid with mr: syntax (check early, no network needed)
if base.is_some() {
return Err(GitError::RefBaseConflict {
ref_type: RefType::Mr,
@@ -247,6 +243,16 @@ fn resolve_mr_ref(
format_with_gutter(&format_ref_context(&mr_info), None)
);
+ // --create is invalid with mr: syntax (check after fetch to show branch name)
+ if create {
+ return Err(GitError::RefCreateConflict {
+ ref_type: RefType::Mr,
+ number: mr_number,
+ branch: mr_info.source_branch.clone(),
+ }
+ .into());
+ }
+
if mr_info.is_cross_project {
// Fork MR: check if branch already exists and is tracking this MR
let local_branch = mr_ref::local_branch_name(&mr_info);
diff --git a/src/git/error.rs b/src/git/error.rs
index d47ff7640..b4fb6e0fb 100644
--- a/src/git/error.rs
+++ b/src/git/error.rs
@@ -221,6 +221,7 @@ pub enum GitError {
RefCreateConflict {
ref_type: RefType,
number: u32,
+ branch: String,
},
/// --base flag used with pr:/mr: syntax (conflict - base is predetermined)
RefBaseConflict {
@@ -689,17 +690,21 @@ impl std::fmt::Display for GitError {
)
}
- GitError::RefCreateConflict { ref_type, number } => {
+ GitError::RefCreateConflict {
+ ref_type,
+ number,
+ branch,
+ } => {
+ let name = ref_type.name();
let syntax = ref_type.syntax();
- let name_plural = ref_type.name_plural();
write!(
f,
"{}\n{}",
error_message(cformat!(
- "Cannot use <bold>--create</> with <bold>{syntax}{number}</>"
+ "Cannot create branch for <bold>{syntax}{number}</> — {name} already has branch <bold>{branch}</>"
)),
- hint_message(format!(
- "{name_plural} already have a branch; remove --create"
+ hint_message(cformat!(
+ "To switch to it: <bright-black>wt switch {syntax}{number}</>"
))
)
}
diff --git a/src/git/remote_ref.rs b/src/git/remote_ref.rs
index 553cdfee1..e679d7296 100644
--- a/src/git/remote_ref.rs
+++ b/src/git/remote_ref.rs
@@ -110,6 +110,6 @@
//! ## --create Conflict
//!
//! ```text
-//! ✗ Cannot use --create with pr:101
-//! ↳ PRs already have a branch; remove --create
+//! ✗ Cannot create branch for pr:101 — PR already has branch feature-auth
+//! ↳ To switch to it: wt switch pr:101
//! ```
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index d38f73073..99a9ec5b9 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -1416,10 +1416,38 @@ fn configure_mock_gh_env(cmd: &mut std::process::Command, mock_bin: &Path) {
/// Test that --create flag conflicts with pr: syntax
#[rstest]
-fn test_switch_pr_create_conflict(repo: TestRepo) {
+fn test_switch_pr_create_conflict(#[from(repo_with_remote)] repo: TestRepo) {
+ // Set origin URL to GitHub-style so PR resolution works
+ repo.run_git(&[
+ "remote",
+ "set-url",
+ "origin",
+ "https://github.com/owner/test-repo.git",
+ ]);
+
+ // Mock gh to return PR info (we fetch before checking --create to show branch name)
+ let gh_response = r#"{
+ "title": "Fix authentication bug in login flow",
+ "user": {"login": "alice"},
+ "state": "open",
+ "draft": false,
+ "head": {
+ "ref": "feature-auth",
+ "repo": {"name": "test-repo", "owner": {"login": "owner"}}
+ },
+ "base": {
+ "ref": "main",
+ "repo": {"name": "test-repo", "owner": {"login": "owner"}}
+ },
+ "html_url": "https://github.com/owner/test-repo/pull/101"
+ }"#;
+
+ let mock_bin = setup_mock_gh_for_pr(&repo, Some(gh_response));
+
let settings = setup_snapshot_settings(&repo);
settings.bind(|| {
let mut cmd = make_snapshot_cmd(&repo, "switch", &["--create", "pr:101"], None);
+ configure_mock_gh_env(&mut cmd, &mock_bin);
assert_cmd_snapshot!("switch_pr_create_conflict", cmd);
});
}
@@ -2320,10 +2348,25 @@ fn configure_mock_glab_env(cmd: &mut std::process::Command, mock_bin: &Path) {
/// Test that --create flag conflicts with mr: syntax
#[rstest]
-fn test_switch_mr_create_conflict(repo: TestRepo) {
+fn test_switch_mr_create_conflict(#[from(repo_with_remote)] repo: TestRepo) {
+ // Mock glab to return MR info (we fetch before checking --create to show branch name)
+ let glab_response = r#"{
+ "title": "Fix authentication bug in login flow",
+ "author": {"username": "alice"},
+ "state": "opened",
+ "draft": false,
+ "source_branch": "feature-auth",
+ "source_project_id": 123,
+ "target_project_id": 123,
+ "web_url": "https://gitlab.com/owner/test-repo/-/merge_requests/101"
+ }"#;
+
+ let mock_bin = setup_mock_glab_for_mr(&repo, Some(glab_response));
+
let settings = setup_snapshot_settings(&repo);
settings.bind(|| {
let mut cmd = make_snapshot_cmd(&repo, "switch", &["--create", "mr:101"], None);
+ configure_mock_glab_env(&mut cmd, &mock_bin);
assert_cmd_snapshot!("switch_mr_create_conflict", cmd);
});
}
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap b/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap
index 9d83fda73..a491d74fb 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_mr_create_conflict.snap
@@ -36,5 +36,8 @@ exit_code: 1
----- stdout -----
----- stderr -----
-✗ Cannot use --create with mr:101
-↳ MRs already have a branch; remove --create
+◎ Fetching MR !101...
+  Fix authentication bug in login flow (!101)
+  by @alice · opened · https://gitlab.com/owner/test-repo/-/merge_requests/101
+✗ Cannot create branch for mr:101 — MR already has branch feature-auth
+↳ To switch to it: wt switch mr:101
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap
index b47297004..2352adc09 100644
--- a/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap
+++ b/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap
@@ -1,5 +1,6 @@
---
source: tests/integration_tests/switch.rs
+assertion_line: 1451
info:
program: wt
args:
@@ -36,5 +37,8 @@ exit_code: 1
----- stdout -----
----- stderr -----
-✗ Cannot use --create with pr:101
-↳ PRs already have a branch; remove --create
+◎ Fetching PR #101...
+  Fix authentication bug in login flow (#101)
+  by @alice · open · https://github.com/owner/test-repo/pull/101
+✗ Cannot create branch for pr:101 — PR already has branch feature-auth
+↳ To switch to it: wt switch pr:101