Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(switch): accept remote-only base in --create (#2411)
Worktrunk Bot18bea3a32f4b
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rsindex ad58c2a36..f06220529 100644--- a/src/commands/worktree/switch.rs+++ b/src/commands/worktree/switch.rs@@ -290,6 +290,13 @@ fn fetch_same_repo_branch(repo: &Repository, info: &RemoteRefInfo) -> anyhow::Re/// Resolve a `--base` value, expanding `pr:`/`mr:` shortcuts. Non-shortcut/// inputs go through [`Repository::resolve_worktree_name`] (handles `@`/`-`/`^`).+///+/// When the bare name doesn't exist locally but a single remote has it,+/// returns the remote-qualified form so the validation in+/// [`resolve_switch_target`] doesn't reject `wt switch -c new --base+/// remote-only-branch`. Git's rev-parse doesn't auto-expand `foo` to+/// `refs/remotes/origin/foo`. The new branch's upstream is unset downstream+/// to keep `git push` from targeting the base.fn resolve_base_ref(repo: &Repository, base: &str) -> anyhow::Result<String> {if let Some(suffix) = base.strip_prefix("pr:")&& let Ok(number) = suffix.parse::<u32>()@@ -303,7 +310,16 @@ fn resolve_base_ref(repo: &Repository, base: &str) -> anyhow::Result<String> {return resolve_remote_ref_as_base(repo, &GitLabProvider, number);}- repo.resolve_worktree_name(base)+ let resolved = repo.resolve_worktree_name(base)?;++ if !repo.ref_exists(&resolved)? {+ let remotes = repo.branch(&resolved).remotes()?;+ if remotes.len() == 1 {+ return Ok(format!("{}/{}", remotes[0], resolved));+ }+ }++ Ok(resolved)}/// Resolve `pr:{N}` / `mr:{N}` for `--base`. Same-repo returns the sourcediff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rsindex 22b5f1eba..38616ea7b 100644--- a/tests/integration_tests/switch.rs+++ b/tests/integration_tests/switch.rs@@ -205,6 +205,45 @@ fn test_switch_dwim_ambiguous_remotes(#[from(repo_with_remote)] mut repo: TestResnapshot_switch("switch_dwim_ambiguous_remotes", &repo, &["shared-feature"]);}+/// `--base <branch>` should accept a branch that exists only as a remote-tracking ref+/// (e.g., `origin/releases/4.x.x`) when the user passes the bare name. Without this,+/// `wt switch -c new --base releases/4.x.x` fails the pre-validation in+/// `resolve_switch_target` even though `git worktree add` would have DWIM'd it.+/// Regression test for GitHub issue #2410.+#[rstest]+fn test_switch_create_with_remote_only_base(#[from(repo_with_remote)] repo: TestRepo) {+ // Create a branch with slashes/dots on the remote only (matches the issue repro).+ repo.run_git(&["branch", "releases/4.x.x"]);+ repo.run_git(&["push", "origin", "releases/4.x.x"]);+ repo.run_git(&["branch", "-D", "releases/4.x.x"]);++ let output = repo+ .wt_command()+ .args(["switch", "--create", "new-wt", "--base", "releases/4.x.x"])+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "switch should succeed; stderr: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ // The new branch must exist and must NOT track the remote base+ // (same safety property as test_switch_create_from_remote_base_no_upstream).+ let branch_output = repo.git_output(&["branch", "--list", "new-wt"]);+ assert!(branch_output.contains("new-wt"), "branch should be created");++ let upstream_check = repo+ .git_command()+ .args(["rev-parse", "--abbrev-ref", "new-wt@{upstream}"])+ .run()+ .unwrap();+ assert!(+ !upstream_check.status.success(),+ "new branch should NOT track origin/releases/4.x.x"+ );+}+/// When creating a new branch from a remote tracking branch (e.g., origin/main),/// the new branch should NOT track the remote base branch./// This prevents accidental `git push` to the base branch (e.g., pushing to main).