mirror of
https://github.com/stablyai/orca.git
synced 2026-10-08 08:02:32 +00:00
* fix(worktree): retry local main refresh through git lock contention and skip false alarms * fix(worktree): overlap the local main refresh with the checkout and run one refresh per repo at a time * fix(worktree): skip the local base refresh when the create makes that branch itself Creating a workspace named feature-x from origin/feature-x runs `worktree add -b feature-x`, which now overlaps the refresh. The refresh's drift probe could see refs/heads/feature-x missing and its presence probe then see it (the add just wrote it), which reported "not fast-forward" and showed a sticky "Local feature-x was not refreshed" warning. `-b` refuses an existing branch, so there is nothing to refresh in that case: skip it on the local, prepared-checkout and SSH create paths. The SSH overlap tests move to their own file so the existing suite stays under the line limit. * fix(worktree): say plainly what happens after the local base refresh queue wait expires * test(worktree): prove SSH local base refreshes of one repo run one at a time * test(worktree): drop type assertions from the SSH refresh overlap test mocks * fix(worktree): fast-forward local main with one host-owned merge --ff-only per branch Moves the whole local base refresh into one shared routine that runs on the execution host (main process for local and WSL repos, the relay for SSH), so the app no longer keeps a second copy of the checks, queue and retry. A checked-out branch now moves with merge --ff-only (hooks, auto-gc and autostash off) instead of status-then-reset --hard, which silently overwrote an untracked file the new commit adds and could discard an edit or a commit made after the check. A free branch moves with a compare-and-swap update-ref that writes a reflog message. Status reads no longer take index.lock. Creates of one branch share one run plus at most one trailing run; a create waits at most 30 s and never starts a competing mutation. The failure toast is keyed by repo and branch because every create that joined a run reports the same fact. * fix(worktree): fast-forward local main even when the repo requires signed merges With merge.verifySignatures=true, the owner-checkout fast-forward refused an unsigned origin/main tip, so every create warned "Local main was not refreshed" where the old reset moved main. The new workspace is already created from that same unsigned commit, and a branch that is not checked out moves without a signature check, so the refusal protected nothing. Turn the setting off for this one merge, like the hooks, gc and autostash overrides. * fix(worktree): clear git read caches when a shared local main update lands late The update of local main can finish after a create stopped waiting for it, so the shared run now invalidates git read caches itself. The index.lock real-git test also no longer reads the developer's global git config. * fix(worktree): never overwrite an ignored file when fast-forwarding local main A plain `git merge --ff-only` silently replaces an ignored file (for example a local `.env`) at a path the new commit starts tracking. Pass `--no-overwrite-ignore` so git refuses instead and the create reports the checkout as having local changes. Supported on the fast-forward path since well before Git 2.25. Also make the relay test for one-refresh-per-branch hold the first merge until the second request has reached the relay, so it fails without the coalescing. * fix(worktree): keep the local main update a plain fast-forward whatever the user's merge settings say A per-branch mergeOptions such as '-s ours' or '--squash', or pull.twohead=ours, made the update create a merge commit that dropped upstream, or stage upstream without moving main, while reporting success. The command now clears the branch's mergeOptions and passes the strategy and signature choice on the command line, which beats any config. After the move Orca confirms local main is exactly the target before reporting it updated. The exact command also runs in the Git 2.25 compatibility suite. * fix(worktree): make the Git 2.25 fast-forward contract pass in CI and rerun on every change to it The new real-Git contract for the local main fast-forward wrote a post-merge hook into .git/hooks, which does not exist when the repo is created by the uninstalled Git 2.25.5 build CI uses (no templates), so the Git compatibility check failed. Create the directory first. The Git compatibility check also did not run when only the fast-forward module changed, so a later edit to its merge arguments (for example a flag Git 2.25 lacks) would skip the one check that tests them. Add the module to the check's paths. * fix(worktree): answer every create from a local main update toward its own base Creates from different remotes' main (origin/main and upstream/main) shared one queued update per repo and branch, which ran only the latest caller's target: a create could get no result for its own base, or a false "not refreshed" warning computed for another remote's main. The per-branch runner now queues one run per distinct target, still one at a time per branch, and only callers toward the same target share a queued run. Applied in the app and the relay. * test(worktree): record the third create's result in the mixed-remote burst tests and update the toast id rationale * chore(worktree): correct the toast id rationale
168 lines
5.4 KiB
TypeScript
168 lines
5.4 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 }>
|
|
|
|
/** `cat-file --batch-check` reports every ref from one child, and reports a missing ref as data
|
|
* rather than a failed exit — so a batch stays as decidable as a per-ref `show-ref --verify`.
|
|
* A repo with many remotes otherwise pays one subprocess per remote on every conflict check. */
|
|
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 per line so a CRLF-translating host's `\r` does not 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 }
|
|
}
|