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.rsindex 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 PRlet 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 MRlet local_branch = mr_ref::local_branch_name(&mr_info);diff --git a/src/git/error.rs b/src/git/error.rsindex 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.rsindex 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.rsindex 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.snapindex 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 ------[31m✗[39m [31mCannot use [1m--create[22m with [1mmr:101[22m[39m-[2m↳[22m [2mMRs already have a branch; remove --create[22m+[36m◎[39m [36mFetching MR !101...[39m+[107m [0m [1mFix authentication bug in login flow[22m (!101)+[107m [0m by @alice · opened · [90mhttps://gitlab.com/owner/test-repo/-/merge_requests/101[39m+[31m✗[39m [31mCannot create branch for [1mmr:101[22m — MR already has branch [1mfeature-auth[22m[39m+[2m↳[22m [2mTo switch to it: [90mwt switch mr:101[39m[22mdiff --git a/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snap b/tests/snapshots/integration__integration_tests__switch__switch_pr_create_conflict.snapindex 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: 1451info:program: wtargs:@@ -36,5 +37,8 @@ exit_code: 1----- stdout ---------- stderr ------[31m✗[39m [31mCannot use [1m--create[22m with [1mpr:101[22m[39m-[2m↳[22m [2mPRs already have a branch; remove --create[22m+[36m◎[39m [36mFetching PR #101...[39m+[107m [0m [1mFix authentication bug in login flow[22m (#101)+[107m [0m by @alice · open · [90mhttps://github.com/owner/test-repo/pull/101[39m+[31m✗[39m [31mCannot create branch for [1mpr:101[22m — PR already has branch [1mfeature-auth[22m[39m+[2m↳[22m [2mTo switch to it: [90mwt switch pr:101[39m[22m