Files
orca/src/shared/git-default-base-ref.ts
T
Neil 328caa2160 fix(git): reduce queries and preserve data across execution hosts (#24602)
* fix(git): reduce queries and preserve data across execution hosts

* fix(ci): exercise pinned Git and serialize mobile dependency entrypoints

* fix(relay): preserve fresh diff retries after hung shared reads

* test(git): wait for fetch barrier before canceling preparation

* fix(i18n): describe index-preserving discard in every locale

* fix(git): retain clone diagnostics and allow WSL policy startup

* test(git): refresh default-base and branch-safety fixtures
2026-10-02 18:05:37 -07:00

38 lines
1.6 KiB
TypeScript

import { iterateProcessOutputLines } from './process-output-field-scanner'
export const DEFAULT_BASE_REF_PROBES: readonly { ref: string; returnAs: string }[] = [
{ ref: 'refs/remotes/origin/main', returnAs: 'origin/main' },
{ ref: 'refs/remotes/origin/master', returnAs: 'origin/master' },
{ ref: 'refs/heads/main', returnAs: 'main' },
{ ref: 'refs/heads/master', returnAs: 'master' }
]
export type GitExec = (argv: string[]) => Promise<{ stdout: string }>
/** Resolve the same default-base ordering through a host-owned Git executor. */
export async function resolveDefaultBaseRefViaExec(exec: GitExec): Promise<string | null> {
const originHeadRef = 'refs/remotes/origin/HEAD'
const refs = [originHeadRef, ...DEFAULT_BASE_REF_PROBES.map(({ ref }) => ref)]
// A character class forces exact matching instead of including descendants such as main/topic.
const patterns = refs.map((ref) => `${ref.slice(0, -1)}[${ref.slice(-1)}]`)
try {
const { stdout } = await exec(['for-each-ref', '--format=%(refname)%00%(symref)', ...patterns])
const presentRefs = new Set<string>()
for (const line of iterateProcessOutputLines(stdout)) {
const separator = line.indexOf('\0')
if (separator === -1) {
continue
}
const ref = line.slice(0, separator)
const target = line.slice(separator + 1)
if (ref === originHeadRef && target) {
return target.replace(/^refs\/remotes\//, '')
}
presentRefs.add(ref)
}
return DEFAULT_BASE_REF_PROBES.find(({ ref }) => presentRefs.has(ref))?.returnAs ?? null
} catch {
return null
}
}