Files
archived-orca/src/main/git/command-runner/missing-git-spawn-cwd.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

70 lines
2.5 KiB
TypeScript

import { stat } from 'node:fs/promises'
import { isDefinitiveAbsence } from '../../../shared/definitive-filesystem-absence'
import { waitForPromiseWithSignal } from '../../../shared/abort-signal-reason'
import { isMissingCommandBinaryError } from '../exec-error'
const MAX_KNOWN_MISSING_CWDS = 256
// Remember missing folders in memory to avoid a failing Git spawn on every sweep.
const knownMissingCwds = new Set<string>()
type GitCwdOptions = { cwd?: string; wslDistro?: string; signal?: AbortSignal }
// Cache native absence only; unreachable WSL/UNC paths do not prove deletion.
function recordableCwd({ cwd, wslDistro }: GitCwdOptions): string | null {
const routesThroughWsl = process.platform === 'win32' && Boolean(wslDistro)
return cwd && !routesThroughWsl && !/^[\\/]{2}/.test(cwd) ? cwd : null
}
// Match spawn chdir semantics: stat follows symlinked roots.
function isCwdAbsent(cwd: string): Promise<boolean> {
return stat(cwd).then(() => false, isDefinitiveAbsence)
}
async function noteMissingCwd(error: unknown, cwd: string, signal?: AbortSignal): Promise<void> {
if (!isMissingCommandBinaryError(error) || signal?.aborted || knownMissingCwds.has(cwd)) {
return
}
// Recheck after stat so concurrent failures record and warn once.
if (!(await isCwdAbsent(cwd)) || knownMissingCwds.has(cwd)) {
return
}
knownMissingCwds.add(cwd)
if (knownMissingCwds.size > MAX_KNOWN_MISSING_CWDS) {
knownMissingCwds.delete(knownMissingCwds.values().next().value ?? cwd)
}
console.warn('[git] repository folder missing; skipping Git there until it returns:', cwd)
}
/** Answers a known-missing native cwd without starting Git; healthy repos pay one Set lookup. */
export function runUnlessGitCwdKnownMissing<T>(
options: GitCwdOptions,
run: () => Promise<T>
): Promise<T> {
const cwd = recordableCwd(options)
if (!cwd) {
return run()
}
const runAndNote = (): Promise<T> =>
run().catch(async (error: unknown) => {
await noteMissingCwd(error, cwd, options.signal)
throw error
})
if (!knownMissingCwds.has(cwd)) {
return runAndNote()
}
return waitForPromiseWithSignal(isCwdAbsent(cwd), options.signal).then((absent) => {
if (!absent) {
knownMissingCwds.delete(cwd)
return runAndNote()
}
// Preserve the spawn failure shape expected by existing callers.
throw Object.assign(new Error('spawn git ENOENT'), {
code: 'ENOENT',
syscall: 'spawn git',
path: 'git',
stdout: '',
stderr: ''
})
})
}