Files
orca/src/main/git/exact-ref-probe.ts
T
OrcaWinandOrcaWin 4687e90a16 [comment audit] Continue main-process comment cleanup (#32946)
* [comment audit] Shorten daemon, hook, and emulator comments [skip ci]

* [comment audit] Shorten VM, conversation, SQLite, and theme-import comments [skip ci]

* [comment audit] Tighten Git execution and source-control comment contracts [skip ci]

* [comment audit] Clarify repository probes and maintenance comments [skip ci]

* [comment audit] Shorten status, diff-cache, and checkout comments [skip ci]

* [comment audit] Clarify worktree lifecycle and WSL routing comments [skip ci]

* [comment audit] Shorten forge identity and review-refresh comments [skip ci]

* [comment audit] Tighten hosted-review client comment contracts [skip ci]

* [comment audit] Clarify GitLab compatibility and review comments [skip ci]

* [comment audit] Tighten hook ownership and watchdog rationale [skip ci]

* [comment audit] Condense host and integration compatibility comments [skip ci]

* [comment audit] Clarify IPC authority and remote-routing rationale [skip ci]

* [comment audit] Finalize file-import and containment comment cleanup

* [comment audit] Fix stale Jira type imports blocking full CI

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-23 16:27:37 -07:00

166 lines
5.2 KiB
TypeScript

import { isSafeGitRefName } from '../../shared/git-status-upstream-ref'
import { isShowRefNoMatchError } from '../../shared/git-show-ref-no-match'
export { isShowRefNoMatchError }
export type ExactRefProbeExecOptions = {
maxBuffer?: number
timeoutMs?: number
}
export type ExactRefProbeExec = (
argv: string[],
options?: ExactRefProbeExecOptions
) => Promise<{ stdout: string }>
export type ExactRefProbeSetResult = {
presentRefs: string[]
absentRefs: string[]
unknownRefs: string[]
}
type ExactRefPresence = 'present' | 'absent' | 'unknown'
const EXACT_REF_PROBE_CONCURRENCY = 8
// SHA-1 and SHA-256 repositories both report a full object id here.
const OBJECT_ID_PATTERN = /^[0-9a-f]{40}(?:[0-9a-f]{24})?$/
function commandOptions(options: ExactRefProbeExecOptions): ExactRefProbeExecOptions | undefined {
if (options.maxBuffer === undefined && options.timeoutMs === undefined) {
return undefined
}
return { ...options }
}
async function probeExactRef(
runGit: ExactRefProbeExec,
ref: string,
options: ExactRefProbeExecOptions
): Promise<ExactRefPresence> {
if (!isSafeGitRefName(ref)) {
return 'unknown'
}
try {
const argv = ['show-ref', '--verify', '--quiet', '--', ref]
const forwardedOptions = commandOptions(options)
await (forwardedOptions ? runGit(argv, forwardedOptions) : runGit(argv))
return 'present'
} catch (error) {
return isShowRefNoMatchError(error) ? 'absent' : 'unknown'
}
}
/** Probe full ref names with bounded subprocess concurrency and exact lookups. */
export async function probeExactRefs(
runGit: ExactRefProbeExec,
refs: readonly string[],
options: ExactRefProbeExecOptions = {}
): Promise<ExactRefProbeSetResult> {
const uniqueRefs = [...new Set(refs)]
const states: (ExactRefPresence | undefined)[] = Array.from(
{ length: uniqueRefs.length },
() => undefined
)
let nextIndex = 0
async function probeNext(): Promise<void> {
while (true) {
const index = nextIndex++
if (index >= uniqueRefs.length) {
return
}
const ref = uniqueRefs[index]
states[index] = await probeExactRef(runGit, ref, options)
}
}
const workerCount = Math.min(EXACT_REF_PROBE_CONCURRENCY, uniqueRefs.length)
await Promise.all(Array.from({ length: workerCount }, () => probeNext()))
for (let index = 0; index < states.length; index += 1) {
states[index] ??= 'unknown'
}
return {
presentRefs: uniqueRefs.filter((_, index) => states[index] === 'present'),
absentRefs: uniqueRefs.filter((_, index) => states[index] === 'absent'),
unknownRefs: uniqueRefs.filter((_, index) => states[index] === 'unknown')
}
}
/** Stop scheduling exact lookups once any requested ref is present. */
export async function probeAnyExactRef(
runGit: ExactRefProbeExec,
refs: readonly string[],
options: ExactRefProbeExecOptions = {}
): Promise<{ found: boolean; unknown: boolean }> {
const uniqueRefs = [...new Set(refs)]
let nextIndex = 0
let found = false
let unknown = false
async function probeNext(): Promise<void> {
while (!found) {
const index = nextIndex++
if (index >= uniqueRefs.length) {
return
}
const ref = uniqueRefs[index]
const state = await probeExactRef(runGit, ref, options)
found ||= state === 'present'
unknown ||= state === 'unknown'
}
}
const workerCount = Math.min(EXACT_REF_PROBE_CONCURRENCY, uniqueRefs.length)
await Promise.all(Array.from({ length: workerCount }, () => probeNext()))
return { found, unknown }
}
/** Runs Git with a stdin payload. Only hosts that can feed a child's stdin supply one. */
export type ExactRefProbeStdinExec = (
argv: string[],
options: ExactRefProbeExecOptions & { stdin: string }
) => Promise<{ stdout: string }>
/** Batch exact probes in one child; missing refs remain data rather than aborting the batch. */
export async function probeAnyExactRefBatched(
runGit: ExactRefProbeStdinExec,
refs: readonly string[],
options: ExactRefProbeExecOptions = {}
): Promise<{ found: boolean; unknown: boolean }> {
const uniqueRefs = [...new Set(refs)]
const safeRefs = uniqueRefs.filter((ref) => isSafeGitRefName(ref))
if (safeRefs.length === 0) {
return { found: false, unknown: uniqueRefs.length > 0 }
}
let stdout: string
try {
;({ stdout } = await runGit(['cat-file', '--batch-check'], {
...options,
stdin: `${safeRefs.join('\n')}\n`
}))
} catch {
return { found: false, unknown: true }
}
// Trim CRLF so carriage returns cannot become part of the type.
const lines = stdout
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0)
// One line per input, in order; a short read means the batch never answered for the rest.
if (lines.length !== safeRefs.length) {
return { found: false, unknown: true }
}
let unknown = safeRefs.length !== uniqueRefs.length
for (const line of lines) {
const [head, type] = line.split(' ')
if (OBJECT_ID_PATTERN.test(head) && type !== undefined && type !== 'missing') {
return { found: true, unknown: false }
}
if (type !== 'missing') {
// `ambiguous`, or a spelling this Git reports differently; neither proves absence.
unknown = true
}
}
return { found: false, unknown }
}