Skip to content

fix(switch): handle fork branch conflicts on GitLab and Azure DevOps - #4542

Merged
max-sixty merged 7 commits into
mainfrom
nightly/clean-39556777026-docs
Nov 5, 2026
Merged

max-sixty merged 7 commits into
mainfrom
nightly/clean-39556777026-docs

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

wt switch now handles a fork PR or MR whose branch name is already used locally on GitLab and Azure DevOps, matching the existing GitHub and Gitea behavior. GitLab uses the source project's namespace as a prefix, including nested groups; Azure DevOps uses the fork repository name. The prefixed branch tracks the PR or MR ref and remains fetch-only, so it cannot push to the fork.

The change also updates the wt switch help and its generated documentation to describe the shared behavior. Integration tests cover GitLab conflicts with and without existing tracking, Azure DevOps conflicts, and the no-conflict Azure path. The prefixed-name unit tests, docs sync test, and help snapshots pass locally.

The `wt switch` help said a local branch of the same name tracking
something else "requires renaming first". That holds only for GitLab
and Azure DevOps MRs. For GitHub and Gitea PRs, `resolve_fork_ref`
falls back to an `<owner>/<branch>` local branch, which is fetch-only
and can't push to the fork (it warns "Push to fork is not supported
with prefixed branches"). Correct the sentence in place and regenerate
the docs mirrors and help snapshot.
@worktrunk-bot worktrunk-bot added the nightly-cleanup Issues found by nightly code quality sweep label Oct 21, 2026

@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.

Azure DevOps fork changes are pull requests, not MRs: ForgeKind::ref_type maps Self::GitHub | Self::Gitea | Self::AzureDevOps => RefType::Pr, so wt shows PR #N and takes pr:<number>. Calling one an "Azure DevOps MR" points readers at the wrong shortcut. I'll push this wording to the source line and its four mirrors.

Comment thread src/cli/mod.rs Resolved
@max-sixty

Copy link
Copy Markdown
Owner

why do we have different behavior?? should we fix that

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

It's history, not a deliberate split. #779 added the <owner>/<branch> fallback for GitHub only (closing #714), and #1320 copied it into the Gitea backend because Gitea's PR payload has the same head_owner field. The GitLab and Azure DevOps fork paths were written without it, so prefixed_local_branch_name returns None for them and resolve_fork_ref errors with "Branch X exists but doesn't track …".

Neither forge is missing the information:

  • GitLab: the source project's namespace is already parsed for display. source_ref runs extract_namespace_from_url on the fork URL. The conflict path would just need to call fetch_gitlab_project_urls before choosing a name, which the no-conflict path already does. The prefixed name would be group/subgroup/branch for nested groups, which is still a valid ref.
  • Azure DevOps: forkSource.repository gives us the fork's URL but no owner, and Azure forks belong to a project rather than a user. The natural prefix is the fork repo name, which isn't the same <owner>/ shape. This one needs a small naming decision.

I'd fix it: GitLab with the same <namespace>/<branch> rule, and Azure prefixed by fork repo name unless you'd prefer it keep erroring. Both are testable with the existing mocked glab/az fixtures (test_switch_mr_fork_existing_branch_tracks_different, test_switch_pr_azure_fork). If you want that, I'll do it in this PR and reduce the help sentence to the forge-agnostic version ("checked out as <owner>/<branch>, fetch-only"). That would replace the current per-forge wording.

@max-sixty

Copy link
Copy Markdown
Owner

yes, please fix

@worktrunk-bot worktrunk-bot changed the title docs(switch): say what happens when a fork PR's branch name is taken fix(switch): handle fork branch conflicts on GitLab and Azure DevOps Oct 26, 2026

@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.

GitLab's prefixed checkout still fails for valid SSH clone URLs with an explicit port; the inline change uses the existing URL parser.

Comment thread src/git/remote_ref/info.rs Resolved
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Fixed in this PR. GitLab now prefixes a conflicting fork branch with the source project namespace (including nested groups), and Azure DevOps uses the fork repository name. These branches track the PR or MR ref and remain fetch-only. I updated the wt switch help and added checkout tests, including a missing fork URL case.

All checks passed on ea4cdd9a9d98f0ba9ab15e2c40b337176509968a, including codecov/patch and the Linux, macOS, and Windows tests. The PR is ready for your merge decision.

Comment thread tests/integration_tests/switch.rs Outdated
let mut cmd = make_snapshot_cmd(&repo, "switch", &["pr:42"], None);
configure_mock_cli_env(&mut cmd, &mock_bin);
assert_cmd_snapshot!("switch_pr_azure_fork", cmd);
if missing_fork_url {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this does not look like a canonical way to handle tests??

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.

I changed the Azure fixture to build JSON values directly in 6fcd4dfd63afcf00256a8cc4d4461b7a9c19734e. The missing-URL case now sets both URL fields to null through Option values, without string replacement. All three Azure fork cases pass locally.

@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot my comment on if missing_fork_url { is still open: the fix changed the fixture, not the branching. Give each case its own test function, as the GitLab tests in this PR do, or take the snapshot name from the #[case].

This was written by Claude Code on behalf of max-sixty

Each case gets its own test function over a shared setup helper, so no
assertion branches on the case.
@max-sixty
max-sixty merged commit 0081536 into main Nov 5, 2026
36 checks passed
@max-sixty
max-sixty deleted the nightly/clean-39556777026-docs branch November 5, 2026 06:44
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Split into three test functions in f66ec11: test_switch_pr_azure_fork, test_switch_pr_azure_fork_conflict, and test_switch_pr_azure_fork_missing_url. They share a setup_azure_fork_pr(repo, fork_urls) helper that pushes the PR ref and mocks az. Each test now has a single snapshot assertion, and only the conflict test checks the tracking config. The snapshot names haven't changed. All checks, including codecov/patch, are green at that commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nightly-cleanup Issues found by nightly code quality sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants