Files
orca/src/relay/git-handler-push-target.ts
Neil 77f23b013f refactor(shared): drop the shared/types barrel and import from the real modules (#14447)
#14397 split `shared/types.ts` into 46 per-domain modules but kept the path as
a re-export barrel so the import sites did not have to change. This removes
the barrel: every consumer now imports from the module that actually declares
the type, and `src/shared/types.ts` is deleted.

Barrels hide where a type lives, make every consumer look like it depends on
the whole domain, and let an unrelated edit invalidate a module that ~2,000
files transitively import.

2,323 import declarations across 2,321 files. Rewritten mechanically: each
specifier was resolved to an absolute path via the TypeScript AST and
recomputed, rather than string-substituted, so alias forms (`@/../../shared/
types`) and per-specifier `type` modifiers survive.

Four cases the mechanical pass had to handle, each found by a gate rather than
by reading the diff:

- Modules inside `src/shared` import the barrel as `./types`, not
  `shared/types`. A pre-filter on the latter string skipped 176 of them and
  left imports dangling at a deleted file, which surfaced as confusing
  `Property 'x' is optional in type 'Repo' but required in Pick<Repo, ...>`
  errors rather than "module not found".
- The barrel RENAMED one type on the way through
  (`WorkspaceSource as WorkspaceCreateTelemetrySource`), so the original name
  in the owning module has to be re-aliased at each consumer.
- Three test files put `;(globalThis as ...)` on the line after the import.
  TypeScript parses that `;` as the import statement's terminator, so
  replacing through `statement.getEnd()` deletes it and breaks ASI. The
  rewrite now stops at the module specifier.
- A file that already imported directly from a module got a SECOND import
  from it, because the barrel re-exported those same names — which trips
  `import/no-duplicates` under `--deny-warnings`. A post-pass merges
  declarations sharing a specifier and type-only-ness; the `import type` plus
  `import` pair from one module is left alone, since that form is allowed.

Splitting one barrel import into several genuinely adds lines, which pushed
`terminal-layout-pty-ownership.ts` to 301 counted lines: its 107-character
import must wrap, and neither local type collapses onto one line (101 and 116
characters). Rather than contort a type declaration to fit a line budget,
`collectLeafIds` and `pruneLeaves` move to `terminal-pane-layout-tree.ts` —
they are pure structural operations on the layout tree and independent of PTY
ownership. `visible-worktrees.ts` similarly loses its own mini-barrel
re-export of `isDefaultBranchWorkspace`, with the four real consumers
repointed at the declaring module. No `max-lines` bypass added.

Verified: cold `tsc --noEmit` green on node, cli, and web (buildinfo deleted
first — these projects are `composite: true` and reuse stale caches); the full
`pnpm lint` green, not just bare oxlint — the narrower local check is what let
the duplicate imports reach CI; max-lines ratchet OK at 344.
2026-08-13 22:48:24 -07:00

175 lines
4.9 KiB
TypeScript

import { assertGitPushTargetShape } from '../shared/git-push-target-validation'
import { gitRefTargetsBranchOnRemote } from '../shared/git-remote-branch-name'
import type { GitPushTarget } from '../shared/worktree/types'
type RelayGit = (args: string[], cwd: string) => Promise<{ stdout: string; stderr: string }>
export type ResolvedPushTarget = {
remote: string
refspec: string
}
async function getConfiguredPushTarget(
git: RelayGit,
worktreePath: string
): Promise<ResolvedPushTarget | null> {
try {
const { stdout: branchStdout } = await git(
['symbolic-ref', '--quiet', '--short', 'HEAD'],
worktreePath
)
const branch = branchStdout.trim()
if (!branch) {
return null
}
const [pushRemote, { stdout: mergeStdout }] = await Promise.all([
getConfiguredPushRemote(git, worktreePath, branch),
git(['config', '--get', `branch.${branch}.merge`], worktreePath)
])
const remote = pushRemote?.remote
const mergeRef = mergeStdout.trim()
const branchRef = mergeRef.replace(/^refs\/heads\//, '')
if (!remote || !branchRef || remote === '.' || branchRef === mergeRef) {
return null
}
if (await branchMergeTargetsConfiguredBase(git, worktreePath, branch, remote, branchRef)) {
return null
}
if (!canPushConfiguredMergeBranch(pushRemote, branch, branchRef)) {
return null
}
return { remote, refspec: `HEAD:${branchRef}` }
} catch {
return null
}
}
async function getConfigValue(
git: RelayGit,
worktreePath: string,
key: string
): Promise<string | null> {
try {
const { stdout } = await git(['config', '--get', key], worktreePath)
const value = stdout.trim()
return value || null
} catch {
return null
}
}
function isUrlValuedRemote(remote: string): boolean {
return /^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(remote) || /^[^@/:]+@[^:]+:.+/.test(remote)
}
type ConfiguredPushRemote = {
remote: string
branchRemote: string | null
}
async function findRemoteNameForUrl(
git: RelayGit,
worktreePath: string,
remoteUrl: string
): Promise<string | null> {
try {
const { stdout } = await git(['remote'], worktreePath)
const remotes = stdout
.split(/\r?\n/)
.map((line) => line.trim())
.filter(Boolean)
for (const remoteName of remotes) {
try {
const { stdout: urlStdout } = await git(['remote', 'get-url', remoteName], worktreePath)
if (urlStdout.trim() === remoteUrl) {
return remoteName
}
} catch {
// Ignore a remote that disappeared or has no fetch URL.
}
}
} catch {
return null
}
return null
}
async function normalizePushRemote(
git: RelayGit,
worktreePath: string,
remote: string
): Promise<string> {
if (!isUrlValuedRemote(remote)) {
return remote
}
return (await findRemoteNameForUrl(git, worktreePath, remote)) ?? remote
}
async function getConfiguredPushRemote(
git: RelayGit,
worktreePath: string,
branch: string
): Promise<ConfiguredPushRemote | null> {
// Why: mirror the local gitPush resolver so SSH worktrees do not drift to a
// different target when branch.pushRemote or remote.pushDefault is present.
const branchRemote = await getConfigValue(git, worktreePath, `branch.${branch}.remote`)
const remote =
(await getConfigValue(git, worktreePath, `branch.${branch}.pushRemote`)) ??
(await getConfigValue(git, worktreePath, 'remote.pushDefault')) ??
branchRemote
if (!remote) {
return null
}
return {
remote: await normalizePushRemote(git, worktreePath, remote),
branchRemote: branchRemote ? await normalizePushRemote(git, worktreePath, branchRemote) : null
}
}
async function branchMergeTargetsConfiguredBase(
git: RelayGit,
worktreePath: string,
branch: string,
remote: string,
branchRef: string
): Promise<boolean> {
return gitRefTargetsBranchOnRemote(
await getConfigValue(git, worktreePath, `branch.${branch}.base`),
remote,
branchRef
)
}
function canPushConfiguredMergeBranch(
pushRemote: ConfiguredPushRemote | null,
branch: string,
branchRef: string
): boolean {
if (!pushRemote) {
return false
}
if (branchRef === branch) {
return true
}
// Why: branch.merge belongs to branch.remote. A pushDefault fork must not
// inherit origin/main as its destination branch.
return pushRemote.remote !== 'origin' && pushRemote.branchRemote === pushRemote.remote
}
export async function resolveRelayPushTarget(
git: RelayGit,
worktreePath: string,
pushTarget: unknown
): Promise<ResolvedPushTarget | null> {
if (pushTarget === undefined) {
return getConfiguredPushTarget(git, worktreePath)
}
assertGitPushTargetShape(pushTarget)
const explicitTarget: GitPushTarget = pushTarget
await git(['check-ref-format', '--branch', explicitTarget.branchName], worktreePath)
return {
remote: explicitTarget.remoteName,
refspec: `HEAD:${explicitTarget.branchName}`
}
}