Skip to content

fix(switch): set upstream when --base pr:N pairs with --create - #2498

Merged
max-sixty merged 2 commits into
mainfrom
fix/issue-2497
Apr 30, 2026
Merged

max-sixty merged 2 commits into
mainfrom
fix/issue-2497

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

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 — so git push from 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:N resolves to a same-repo PR, resolve_remote_ref_as_base returns the bare source-branch name (e.g., feature-this-is-a-very-long-name). git worktree add -b custom-name feature-this-... then creates custom-name from 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 via pr:/mr: and thread it through CreationMethod::Regular as base_pr_upstream. After git worktree add succeeds, configure branch.<new>.remote and branch.<new>.merge so git push knows 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

  • New test_switch_base_pr_sets_upstream in tests/integration_tests/switch.rs reproduces the bug: it sets up a remote with a long-named PR source branch, runs wt switch --create swa-65 --base pr:2648, and asserts branch.swa-65.remote == origin and branch.swa-65.merge == refs/heads/<source>. Fails before the fix, passes after.
  • All 204 cargo test --test integration switch tests pass.
  • All 10 cargo test --test integration switch_base tests pass — the existing test_switch_base_pr_same_repo snapshot is unchanged because it only captures stdout/stderr.
  • Manually verified end-to-end: after the fix, git push -n from the new worktree shows git's expected push.default = simple warning about the name mismatch and offers git push origin HEAD:feature-this-is-a-very-long-pr-source-branch-name — exactly the command the issue requested.

Closes #2497 — automated triage

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 worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/commands/worktree/switch.rs Resolved
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]>
@max-sixty
max-sixty merged commit 90d5cfc into main Apr 30, 2026
25 checks passed
@max-sixty
max-sixty deleted the fix/issue-2497 branch April 30, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unset git push upstream, when wt switch -b pr:999 -c custom-local-branch

2 participants