Files
orca/src/shared/composer-branch-selection.ts
T
Brennan BensonandBrennan Benson 53a09afbef feat(mobile): match desktop's Smart workspace source picker exactly (#7985)
* feat(mobile): start a workspace from a branch, issue/PR, or Linear ticket

Unify mobile workspace creation with desktop. The "+" Create Workspace
modal now has a primary "Start from" field that opens a tabbed search
drawer (Branch · GitHub · GitLab · Linear), letting a user start a
workspace from an existing/new git branch, a GitHub issue/PR, a GitLab
issue/MR, or a Linear ticket — in addition to the default blank workspace.

No new backend is required: the search RPCs (github.listWorkItems,
gitlab.listWorkItems, linear.searchIssues/listIssues, repo.searchRefs) and
the worktree.create linked-item params were already used by the mobile
Tasks screen. This surfaces them in the create flow, reusing the existing
pure modules (buildTaskWorkspaceCreateParams, shouldResolveHostedReviewStartPoint,
filterAvailableTaskProviders).

Details:
- New pure modules: workspace-source-selection, use-workspace-source-search,
  source-workspace-create, worktree-create-retry, blank-workspace-create
  (the blank/retry path extracted from the modal for reuse + line budget).
- New UI: WorkspaceSourcePickerDrawer (+ row) and SetupHookTrustDrawer
  (extracted from the modal).
- Older paired desktops (missing the mobile.tasks.v1 capability) degrade to
  Branch + Blank only; GitLab/Linear tabs appear only when available.
- GitHub/GitLab sources pin their repo; switching repos resets the source.
  PR/MR sources resolve their base branch at create time; SSH repos gate
  search until connected (Linear search is repo/SSH-independent).

* fix(mobile): hydrate settings/trust before availability probes settle

Review fixes for #7985: setTrustedOrcaHooks/setRuntimeSettings no longer
wait on status.get/preflight.check/linear.status (a first-open
preflight.check can take seconds, widening the spurious setup-trust
re-prompt window). Also adds param-parity tests for createBlankWorkspace
and a GitLab MR base-resolve test.

* feat(mobile): match desktop's Smart source picker exactly

Rework the mobile create-workspace source picker to be a faithful port of
desktop's Smart picker instead of the earlier divergent "Start from" drawer.

The mobile field is now the workspace-name input AND the source search, with the
exact desktop tabs — Smart · GitHub · Linear · GitLab · Branch · Name. "Smart"
fans out across GitHub + GitLab + Linear + branches, prepends a "Use '<name>'"
row, and resolves pasted URLs / #123 / STA-42 to exact items (with a cross-repo
switch prompt). Selecting a source shows a pill and moves the editable name into
Advanced. The invented "Blank workspace" concept is removed — the neutral state
is just a typed/empty name (blank submit still yields a creature name).

DRY: the pure desktop logic (smart-workspace-source-results, -command-value,
github-links, gitlab-links, work-item-link-query-bounds, github-work-item-identity)
moves to src/shared/new-workspace/ with re-export shims at the old renderer paths,
so both renderer and mobile share one implementation. composer-branch-selection
and workspace-name were already shared and are reused directly.

Two read-only lookup RPCs are allowlisted for mobile so pasted GitLab URLs and
cross-repo GitHub URLs resolve to exact items (github.workItemByOwnerRepo,
gitlab.workItemByPath).

New mobile modules are split for max-lines: use-mobile-composer-source (selection
state + desktop-parity handlers, PR/MR base resolve), use-smart-workspace-source
+ smart-source-fan-out/-search-requests/-paste-intent (RPC orchestration),
composer-linked-work-item / work-item-lookup-text / mobile-smart-source-modes
(pure logic), and SmartWorkspaceSourceField/Drawer/Row + SmartWorkspaceAdvancedFields.
Replaces WorkspaceSourcePickerDrawer/Row, workspace-source-selection,
use-workspace-source-search, and MobileWorkspaceNameInput.

Reviewed by three adversarial agents + re-reviewed after fixes: GitHub search now
returns issues AND PRs (not issues-only), Linear defaults to assigned, create-branch
preserves slashy names, cross-repo PR base resolves against the item's own repo,
displayName is suppressed for user-edited names, and the smart-mode GitHub fan-out
respects availability. tsc/oxlint/max-lines-ratchet clean; 1328 mobile tests pass.

* fix(mobile): keep smart source drawer fully visible

* refactor: share workspace creation behavior across clients

* fix: address workspace creation review findings

---------

Co-authored-by: Brennan Benson <brennanbenson@Brennans-MacBook-Pro.local>
2026-07-13 15:38:02 -07:00

197 lines
6.4 KiB
TypeScript

import type { GitPushTarget } from './types'
export type ComposerBranchSelection = {
baseBranch: string
branchNameOverride: string | undefined
branchAutoName: string
name: string | undefined
lastAutoName: string | undefined
}
export function resolveComposerBranchSelection(args: {
refName: string
localBranchName: string
currentName: string
lastAutoName: string
}): ComposerBranchSelection {
const trimmedCurrentName = args.currentName.trim()
const shouldAutoName =
!trimmedCurrentName ||
args.currentName === args.lastAutoName ||
args.localBranchName.startsWith(trimmedCurrentName) ||
args.refName.startsWith(trimmedCurrentName)
if (!shouldAutoName) {
return {
baseBranch: args.refName,
branchNameOverride: undefined,
branchAutoName: '',
name: undefined,
lastAutoName: undefined
}
}
return {
baseBranch: args.refName,
branchNameOverride: args.localBranchName,
branchAutoName: args.localBranchName,
name: args.localBranchName,
lastAutoName: args.localBranchName
}
}
/**
* True when `branchName` is already checked out in one of the given worktree
* branch refs (which may be `refs/heads/foo` or short `foo`). Git refuses to
* check out a branch in two worktrees, so such a branch cannot be reused.
*/
export function isBranchCheckedOutInWorktrees(
branchName: string,
worktreeBranches: readonly string[]
): boolean {
return worktreeBranches.some((ref) => ref.replace(/^refs\/heads\//, '') === branchName)
}
export function getComposerRepoWorktreeBranches(
worktrees: readonly { repoId: string; branch: string }[],
repoId: string | null
): string[] {
return repoId
? worktrees.filter((worktree) => worktree.repoId === repoId).map((worktree) => worktree.branch)
: []
}
/**
* Issue #5181: decide whether a picked branch row is an existing LOCAL branch
* that can be reused (checked out) instead of branched off, and whether reuse
* should default ON.
*
* Reuse is only possible for a LOCAL branch (ref === local name; remote-only
* refs carry an `origin/`-style prefix) that is NOT already checked out in
* another worktree — git allows a branch in only one worktree at a time. Reuse
* defaults ON only when the worktree name was auto-derived from the branch (the
* selection produced a branch-name override); a user who typed a custom
* worktree name first is branching off the ref, so reuse stays OFF unless they
* opt in.
*/
export function resolveComposerBranchReuse(args: {
refName: string
localBranchName: string
selectionProducedOverride: boolean
branchCheckedOutElsewhere: boolean
}): { reuseEligibleBranch: string | null; defaultReuse: boolean } {
const reuseEligibleBranch =
args.refName === args.localBranchName && !args.branchCheckedOutElsewhere
? args.localBranchName
: null
return {
reuseEligibleBranch,
defaultReuse: reuseEligibleBranch !== null && args.selectionProducedOverride
}
}
/**
* Issue #5181: the branch-name override to apply for a picked branch. A local
* branch already checked out in another worktree can't be reused, so it must
* NOT be pinned as the override — pinning it would collide and silently produce
* a suffixed branch. In that case fall back to letting the worktree name derive
* a fresh branch from the selected ref as base; otherwise use the selection's
* override unchanged.
*/
export function resolveComposerReuseOverride(args: {
refName: string
localBranchName: string
branchNameOverride: string | undefined
branchCheckedOutElsewhere: boolean
}): string | undefined {
if (args.branchCheckedOutElsewhere && args.refName === args.localBranchName) {
return undefined
}
return args.branchNameOverride
}
export type ComposerBranchPick = ComposerBranchSelection & {
reuseEligibleBranch: string | null
defaultReuse: boolean
}
export function resolveComposerBranchPick(args: {
refName: string
localBranchName: string
currentName: string
lastAutoName: string
worktreeBranches: readonly string[]
}): ComposerBranchPick {
const selection = resolveComposerBranchSelection(args)
const branchCheckedOutElsewhere = isBranchCheckedOutInWorktrees(
args.localBranchName,
args.worktreeBranches
)
const reuse = resolveComposerBranchReuse({
refName: args.refName,
localBranchName: args.localBranchName,
selectionProducedOverride: selection.branchNameOverride !== undefined,
branchCheckedOutElsewhere
})
return {
...selection,
branchNameOverride: resolveComposerReuseOverride({
refName: args.refName,
localBranchName: args.localBranchName,
branchNameOverride: selection.branchNameOverride,
branchCheckedOutElsewhere
}),
...reuse
}
}
/**
* The branch-name override to apply when creating a worktree from the composer.
*
* With no resolver-provided override, branch mode (#6721) keeps a
* slash-containing typed name as the git branch — validated downstream by
* `git check-ref-format` — while the worktree folder name is sanitized
* separately; every other mode leaves the branch to be derived from the
* sanitized name. With an override, keep it verbatim when the workspace name is
* user-edited (`preserveWorkspaceNameEdits`) or still matches the auto-name.
*/
export function resolveComposerBranchNameOverrideForCreate(args: {
branchNameOverride: string | undefined
branchAutoName: string
workspaceName: string
preserveWorkspaceNameEdits: boolean
createBranchFromWorkspaceName?: boolean
}): string | undefined {
if (!args.branchNameOverride) {
return args.createBranchFromWorkspaceName && args.workspaceName.includes('/')
? args.workspaceName
: undefined
}
if (args.preserveWorkspaceNameEdits) {
return args.branchNameOverride
}
return args.workspaceName === args.branchAutoName ? args.branchNameOverride : undefined
}
export function resolveComposerManualBranchNameChange(args: {
value: string | undefined
pushTarget: GitPushTarget | undefined
forkPushWarning: string | null
}): {
branchNameOverride: string | undefined
pushTarget: GitPushTarget | undefined
forkPushWarning: string | null
} {
const branchNameOverride = args.value?.trim() || undefined
if (args.pushTarget && args.pushTarget.branchName !== branchNameOverride) {
return {
branchNameOverride,
pushTarget: undefined,
forkPushWarning: null
}
}
return {
branchNameOverride,
pushTarget: args.pushTarget,
forkPushWarning: args.forkPushWarning
}
}