Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(switch): branch --create from a remote-tracking default branch (#4482)
Worktrunk Bot8de96f711cd6
diff --git a/src/commands/worktree/switch.rs b/src/commands/worktree/switch.rsindex 1f6931c40..74ef2c2ef 100644--- a/src/commands/worktree/switch.rs+++ b/src/commands/worktree/switch.rs@@ -699,14 +699,16 @@ fn resolve_switch_target(}}- // Compute base branch for creation. When the cached default branch- // no longer resolves locally, return None and let the downstream- // StaleDefaultBranch error emerge at the actual use site.+ // Compute base branch for creation. The default branch is a base like any+ // `--base` value, so any ref that resolves to a commit qualifies — a+ // remote-tracking default such as git-p4's `p4/master` has no local+ // branch (#4481). When the cached default no longer resolves, return None+ // and create from HEAD.let base_branch = if create {resolved_base.or_else(|| {repo.resolve_target_branch(None).ok()- .filter(|b| repo.branch(b).exists_locally().unwrap_or(false))+ .filter(|b| repo.ref_exists(b).unwrap_or(false))})} else {Nonediff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rsindex 3a3909ea6..848aea65f 100644--- a/tests/integration_tests/switch.rs+++ b/tests/integration_tests/switch.rs@@ -384,6 +384,40 @@ fn test_switch_create_with_remote_only_base(#[from(repo_with_remote)] repo: Test);}+/// A default branch that is a remote-tracking ref with no local branch of the+/// same name (git-p4 keeps its mainline only as `p4/master`) is the base for+/// `--create`, not the current HEAD. Regression test for GitHub issue #4481.+#[rstest]+fn test_switch_create_from_remote_tracking_default_branch(+ #[from(repo_with_remote)] repo: TestRepo,+) {+ // `origin/release` exists only as a remote-tracking ref.+ repo.run_git(&["branch", "release"]);+ repo.run_git(&["push", "origin", "release"]);+ repo.run_git(&["branch", "-D", "release"]);+ repo.run_git(&["config", "worktrunk.default-branch", "origin/release"]);++ // Move HEAD past the default branch so the two bases differ.+ repo.run_git(&["commit", "--allow-empty", "-m", "unrelated work"]);++ let output = repo+ .wt_command()+ .args(["switch", "--create", "probe", "--no-cd"])+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "switch should succeed; stderr: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ assert_eq!(+ repo.git_output(&["rev-parse", "probe"]),+ repo.git_output(&["rev-parse", "origin/release"]),+ "probe should be created from the default branch, not HEAD"+ );+}+/// `--create` from a remote tracking branch sets the new branch's upstream only/// when the two share a name. Otherwise the branch created from `origin/release`/// tracks `origin/release`, and a bare `git push` under `push.default = upstream`