Repository navigation
fix(switch): set upstream when --base pr:N pairs with --create - #2498
Merged
Merged
Conversation
When `wt switch --base pr:N --create custom-name` resolves a same-repo PR, the new branch is created from the PR's source branch but no tracking is configured, so `git push` from the new worktree fails with "no upstream branch". `git worktree add -b custom-name <bare-source>` doesn't auto-set tracking when the base is a local branch, and DWIM-tracking via `<remote>/<source>` would trip the issue-#713 safety unset that protects against accidental pushes when branching off `origin/main`. Thread the (remote, branch) the user implicitly chose via `pr:`/`mr:` through `CreationMethod::Regular` and configure `branch.<new>.remote` and `branch.<new>.merge` after the worktree is created. The same-repo MR path benefits via the same code path; fork PRs/MRs still resolve to a SHA and remain untracked, since the user hasn't asked for the contributor's branch to live as a local upstream target. Closes #2497 Co-Authored-By: Claude <[email protected]>
worktrunk-bot
commented
Apr 30, 2026
worktrunk-bot
left a comment
Collaborator
Author
There was a problem hiding this comment.
codecov/patch is failing at 94.44% (34/36 lines). The two missed lines are the .with_context(...) closures on set_config calls that fire only when git config itself fails — see the inline suggestions for a one-line fix.
These match the existing pattern in setup_fork_branch (switch.rs:589–592), so leaving them as-is is consistent with the file. But CLAUDE.md says: "If code is unused, remove it. This includes specialized error handlers for rare cases when falling through to a more general handler is sufficient." — and set_config's underlying error already names the failing git config <key> <value> invocation, so the wrapped context is redundant.
The .with_context calls only fire when `git config` itself fails, so they're impractical to test, and `set_config`'s underlying error already names the failing `git config <key> <value>` invocation — the wrapped message is redundant and adds nothing on the error path. Co-Authored-By: Claude <[email protected]>
This was referenced May 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
wt switch --base pr:N --create custom-name(same-repo PR) creates the worktree and new branch successfully, but the new branch has no upstream tracking — sogit pushfrom the new worktree fails with "fatal: The current branch X has no upstream branch."Reported in #2497: the user expected the local short-name branch to push back to the PR's long-name source branch on the remote.
The root cause: when
--base pr:Nresolves to a same-repo PR,resolve_remote_ref_as_basereturns the bare source-branch name (e.g.,feature-this-is-a-very-long-name).git worktree add -b custom-name feature-this-...then createscustom-namefrom a local branch, which doesn't auto-set tracking. The issue-#713 safety unset only fires when the base is a remote-tracking ref (origin/foo), so it doesn't apply here either — leaving the new branch with no upstream.Solution
Capture the
(remote, branch)the user implicitly chose viapr:/mr:and thread it throughCreationMethod::Regularasbase_pr_upstream. Aftergit worktree addsucceeds, configurebranch.<new>.remoteandbranch.<new>.mergesogit pushknows where to push.Same-repo MR resolution benefits via the same code path. Fork PRs/MRs still resolve to a SHA and remain untracked — the user hasn't asked for the contributor's branch to live as a local upstream target.
Testing
test_switch_base_pr_sets_upstreamintests/integration_tests/switch.rsreproduces the bug: it sets up a remote with a long-named PR source branch, runswt switch --create swa-65 --base pr:2648, and assertsbranch.swa-65.remote == originandbranch.swa-65.merge == refs/heads/<source>. Fails before the fix, passes after.cargo test --test integration switchtests pass.cargo test --test integration switch_basetests pass — the existingtest_switch_base_pr_same_reposnapshot is unchanged because it only captures stdout/stderr.git push -nfrom the new worktree shows git's expectedpush.default = simplewarning about the name mismatch and offersgit push origin HEAD:feature-this-is-a-very-long-pr-source-branch-name— exactly the command the issue requested.Closes #2497 — automated triage