fix(workspaces): base a PR workspace on the branch the PR merges into

MJ
1mo ago
+99-8

A workspace opened from a linked PR now compares against the branch that PR merges into, not the project's default branch. Before this, a stacked PR opened showing its parent's commits as its own changes.

Only the v1 desktop path needed the change. The v2 host-service path already records branch.<name>.base from prMetadata.baseRefName.

What changed

  • utils/git.ts: getPrInfo asks gh for baseRefName, and PullRequestInfo carries it.
  • procedures/create.ts (handleNewWorktree): passes the PR's base as explicitBaseBranch, so it lands in the worktree row's baseBranch and in branch.<name>.base via setBranchBaseConfig.
  • utils/base-branch.ts: new resolvePrBaseBranch drops a base the repo does not have, so a missing branch falls back to the project default instead of breaking every diff in the workspace. When branch listing fails (offline), the PR base is kept.
  • base-branch.test.ts: four cases covering the above.

Testing

  • bun run --cwd apps/desktop typecheck passes.
  • bun test apps/desktop/src/lib/trpc/routers/workspaces/utils/base-branch.test.ts — 10 pass.

Reopening a workspace whose worktree already exists keeps its stored base. Overwriting it would clobber a base someone set by hand.

🤖 Generated with Claude Code


Summary by cubic

Fixes PR workspaces so they compare against the branch the PR merges into instead of the project's default branch. A stacked PR previously showed its parent's commits as its own changes.

Bug Fixes

  • getPrInfo now reads baseRefName from gh, and handleNewWorktree passes it as the workspace's explicit base.
  • resolvePrBaseBranch keeps the PR base only when the repo has a matching remote-tracking ref; a base that exists only locally is dropped so the workspace falls back to the project's configured base branch (then the repo default) instead of showing an empty diff.
  • When branch listing fails (offline), the PR base is kept.
  • Reopening an existing workspace keeps its stored base rather than overwriting it.
  • Only the v1 desktop path needed the change; the v2 host-service path already records branch.<name>.base from prMetadata.baseRefName.

Summary by CodeRabbit

  • Bug Fixes

    • Workspace comparisons created from pull requests now use the pull request’s target branch as the comparison base when available.
    • Improved handling of local, remote-tracking, missing, blank, and unavailable branch information to prevent incorrect base-branch selection.
    • Workspace imports now consistently use known branches when determining comparison bases.
  • Tests

    • Added coverage for pull request base-branch resolution across matching, local-only, missing, blank, and unavailable branch scenarios.