fix: solve #4364 — case-insensitive branch lookup for workspace creation

Draft
GI
4mo ago
+316-2

Summary

When the user creates a workspace targeting an existing remote branch and the typed casing doesn't match the canonical casing in git (e.g. picker shows claude/auto-balance-logo-sizing-1UFT7, but the value reaches the server as …-1uft7), planBranchSource silently forks a fresh branch from baseBranch instead of checking out the remote tip — and the next push opens a brand-new PR instead of continuing the existing one.

Root cause

resolveRef probes refs/heads/<input> and refs/remotes/origin/<input> via git rev-parse --verify, which is case-sensitive on case-sensitive filesystems. Anything that flattens the user's casing on the way in (cmdk's lowercased selected value, OS autocaps, retyping from memory) misses the existing ref, so planBranchSource returns usedExistingBranch: false and the worktree is created from main instead of the remote branch.

The fix

Added resolveRefCaseInsensitive in packages/host-service/src/runtime/git/refs.ts. It runs git for-each-ref over refs/heads/ and refs/remotes/origin/ and returns the canonical-cased ref when an input matches case-insensitively (local match wins over remote-tracking). planBranchSource calls it after the exact-case probe fails, before falling through to fresh-branch creation. The plan's branch is set to the canonical case so the resulting local branch and the workspace row both store the same casing as the remote.

Tests proving it

  • packages/host-service/test/integration/workspace-create-delete.integration.test.ts — two real-bare-remote scenarios:
    • exact-case typed branch tracks the existing remote tip (smoke check that the happy path still works)
    • lowercase-typed branch on a mixed-case remote — the regression. Before the fix, the worktree's HEAD equaled main. After the fix, it equals the remote tip.
  • packages/host-service/src/runtime/git/refs.test.ts — 7 unit tests for resolveRefCaseInsensitive (canonical case, local-wins precedence, origin/foo short-form, HEAD skipped, empty input, no-match → null).

Test plan

  • bun test packages/host-service/test/integration/workspace-create-delete.integration.test.ts — 12 pass
  • bun test packages/host-service/src/runtime/git/refs.test.ts — 19 pass
  • bun test packages/host-service/test/ packages/host-service/src/ — 583 pass, 0 fail
  • bun run --filter @superset/host-service typecheck — clean
  • bun run lint — clean

Closes #4364


Summary by cubic

Fixes incorrect workspace creation when branch casing differs from the remote, so we continue the existing remote branch instead of forking from base. Addresses #4364.

  • Bug Fixes
    • Added resolveRefCaseInsensitive to scan refs with git for-each-ref and return the canonical-cased match (local wins over remote; skips origin/HEAD; supports origin/foo input).
    • Updated planBranchSource to use this fallback after resolveRef fails and set the plan’s branch to the canonical case, ensuring the worktree tracks the remote tip.
    • Added integration and unit tests covering remote-only and mixed-case inputs to prevent regressions.