Files
orca/src/main/git/source-control/worktree-diff-stamp.ts
T
Neil 6d122f1eb5 perf(git): use git's untracked cache and reuse unchanged line stats in status reads (#39721)
* refactor(git): move porcelain status streaming into its own module

* perf(git): let status use the untracked cache and expand collapsed directories

-uall never matches the untracked cache's stored mode, so every poll rescanned the whole tree. Scan in the configured mode and list only the collapsed '? dir/' rows with ls-files, reapplying the entry cap so results match -uall exactly. Ignored reads and status.showUntrackedFiles=no keep -uall.

* refactor(git): let the settled diff cache and stamp serve multi-path reads

* perf(git): reuse status numstat passes while their stamped inputs are unchanged

Each poll re-ran up to two whole-worktree numstat diffs even when nothing changed. Cache each pass in a SettledDiffCache validated by the worktree diff stamp (HEAD, index, .gitmodules, plus every unstaged working file for the unstaged pass); racy writes, mutations and submodule rows still recompute.

* perf(git): defer status while index.lock is held

A status read during an agent's commit sees a half-written state and is immediately redone. Wait (up to 1s, abortable, inside the shared lease) for the lock in the worktree's own gitdir to go away; a lock that survives a full wait is remembered as stale so later reads skip waiting.

* perf(git): pick the untracked scan from config and fall back to -uall when expansion cannot win

Read status.showUntrackedFiles and core.untrackedCache in one config call, remember worktrees that need -uall so polls do not scan twice, and rescan with -uall when rows sit outside cwd, need more than one ls-files chunk, or list empty.

* refactor(git): keep numstat cache limits beside the file diff limits

* refactor(git): stamp host settings files and resolve the common gitdir

* perf(git): read untracked-scan config from a stamped snapshot, with a rollback switch

Cache 'git config --list --show-origin' per worktree while every config file it named (plus repo, worktree and global candidates) keeps its stamp, so steady-state polls spawn no config read. WSL and UNC checkouts keep -uall: their config cannot be stamped from the host and network mtimes cannot vouch for the untracked cache. ORCA_GIT_STATUS_UNTRACKED_EXPANSION=0 restores the plain -uall scan.

* fix(git): stamp config and attributes before reusing status numstat

Numstat output also depends on Git config (including includes) and on .gitattributes, info/attributes and core.attributesFile. Stamp all of them, plus every .gitattributes that can apply to a changed path, and skip reuse under WSL. ORCA_GIT_STATUS_NUMSTAT_REUSE=0 recomputes every pass.

* fix(git): stamp include targets and onbranch HEAD in the config snapshot, and share one read

Include and includeIf targets (and HEAD for onbranch conditions) are now stamped, so a branch switch or a filled-in include re-reads config. Concurrent cold reads coalesce, both numstat passes share one config read, folders without their own gitdir skip the snapshot, aborted reads keep the cached entry, and Windows HOME follows Git's HOMEDRIVE/HOMEPATH order.

* perf(git): expand untracked directories only when the untracked cache is opted in

Git's default core.untrackedCache=keep only reuses a cache someone already built, and fresh repos have none, so status plus ls-files cost more than one -uall scan. Expand only for core.untrackedCache=true or feature.manyFiles=true without an override.

* fix(git): bound config stamps on dead mounts and share commondir resolution

Config file stats run under the same 2s deadline as local-git-config-signature, coalesced per file set, so a hung home or network mount skips reuse instead of stalling status. readHeadComponent now uses resolveGitCommonDir.

* test(git): pin the -uall truncation contract of untracked directory expansion

* fix(git): treat an unreadable commondir as unstampable config

main's resolveGitCommonDir rethrows non-missing errors; the config snapshot must fall back to -uall rather than reject the status read.
2026-11-07 20:33:31 -08:00

226 lines
8.3 KiB
TypeScript

import { readFile, stat } from 'node:fs/promises'
import * as path from 'node:path'
import { resolveWorktreeHostPath } from '../../../shared/git/metadata/git-metadata-path'
import type { GitRuntimeOptions } from '../git-runtime-options'
import { resolveGitCommonDir, resolveGitDir } from './resolve-git-dir'
/** Stamp HEAD, index, routing metadata and working-file inputs without Git; null forbids caching. */
export type WorktreeDiffStamp = {
/** Opaque; compare for equality only. */
value: string
/** Newest mtime any component reported, or -Infinity when every one was absent. */
newestMtimeMs: number
capturedAtMs: number
}
// Wait beyond FAT/exFAT's 2s mtime bucket so another write cannot share a recent timestamp.
export const DIFF_STAMP_RACY_WRITE_MARGIN_MS = 2_000
const MISSING = '-'
const ABSENT: StampComponent = { text: MISSING, mtimeMs: Number.NEGATIVE_INFINITY }
type StampComponent = { text: string; mtimeMs: number }
/** Refuse stamps within the mtime bucket; separately count future mtimes to expose host/guest clock skew. */
export function canProveUnchangedByStamp(stamp: WorktreeDiffStamp): boolean {
return stamp.capturedAtMs - stamp.newestMtimeMs >= DIFF_STAMP_RACY_WRITE_MARGIN_MS
}
/** Future component mtimes cannot satisfy the cache's freshness margin. */
export function isDiffStampClockSkewed(stamp: WorktreeDiffStamp): boolean {
return Number.isFinite(stamp.newestMtimeMs) && stamp.newestMtimeMs > stamp.capturedAtMs
}
/** Staged diffs compare HEAD/index only; omit working-tree inputs for those reads. */
export async function readWorktreeDiffStamp(
worktreePath: string,
filePath: string,
includeWorkingTree: boolean,
options: Pick<GitRuntimeOptions, 'wslDistro'> = {}
): Promise<WorktreeDiffStamp | null> {
return readStamp(worktreePath, filePath, includeWorkingTree ? [filePath] : [], options)
}
/** Stamp one read spanning several working files, such as a whole-worktree numstat; host paths stamp settings files. */
export async function readWorktreePathsDiffStamp(
worktreePath: string,
filePaths: readonly string[],
options: Pick<GitRuntimeOptions, 'wslDistro'> = {},
hostFilePaths: readonly string[] = []
): Promise<WorktreeDiffStamp | null> {
return readStamp(worktreePath, [filePaths, hostFilePaths], filePaths, options, hostFilePaths)
}
/** Stamp files by absolute host path (for example Git config); null when any cannot be stamped. */
export async function readHostFilesStamp(
filePaths: readonly string[]
): Promise<WorktreeDiffStamp | null> {
const capturedAtMs = Date.now()
try {
const components = await Promise.all(filePaths.map(readWorkingTreeComponent))
return {
value: JSON.stringify([filePaths, ...components.map((component) => component.text)]),
newestMtimeMs: Math.max(
Number.NEGATIVE_INFINITY,
...components.map((component) => component.mtimeMs)
),
capturedAtMs
}
} catch {
return null
}
}
async function readStamp(
worktreePath: string,
readIdentity: unknown,
workingTreePaths: readonly string[],
options: Pick<GitRuntimeOptions, 'wslDistro'>,
hostFilePaths: readonly string[] = []
): Promise<WorktreeDiffStamp | null> {
const capturedAtMs = Date.now()
try {
// Git can run in the distro against a raw Linux worktree path while Node stats it through Win32.
const hostWorktreePath = resolveWorktreeHostPath(worktreePath, options)
if (!hostWorktreePath) {
return null
}
// Retain distro options for non-drvfs gitdir pointers even after the worktree becomes a drive path.
const gitDir = await resolveGitDir(hostWorktreePath, options)
const [head, index, gitmodules, ...workingTree] = await Promise.all([
readHeadComponent(gitDir),
// Keep index stamps even when only stat metadata changed: dropping them would make staging invisible.
readFileStampComponent(path.join(gitDir, 'index')),
readFileStampComponent(path.join(hostWorktreePath, '.gitmodules')),
...(workingTreePaths.length > 0
? workingTreePaths.map((filePath) =>
readWorkingTreeComponent(path.join(hostWorktreePath, filePath))
)
: [Promise.resolve(ABSENT)]),
...hostFilePaths.map(readWorkingTreeComponent)
])
if (!head) {
return null
}
const components = [head, index, gitmodules, ...workingTree]
return {
// Structural encoding avoids collisions when paths or refs contain separators.
value: JSON.stringify([
worktreePath,
readIdentity,
...components.map((component) => component.text)
]),
newestMtimeMs: Math.max(...components.map((component) => component.mtimeMs)),
capturedAtMs
}
} catch {
return null
}
}
/** Read loose ref content to detect commits without HEAD changes; packed/reftable/unborn tips use file stamps. */
async function readHeadComponent(gitDir: string): Promise<StampComponent | null> {
const [head, commonDir] = await Promise.all([
readTrimmedFile(path.join(gitDir, 'HEAD')),
resolveGitCommonDir(gitDir)
])
if (!head) {
return null
}
const refName = head.match(/^ref:\s*(.+?)\s*$/)?.[1]
if (!refName || !isSafeRefName(refName)) {
// Detached HEAD already holds the object id; an unrecognized HEAD is covered by its own text.
return { text: head, mtimeMs: Number.NEGATIVE_INFINITY }
}
// Per-worktree refs (`refs/bisect`, `refs/worktree`) live beside the checkout; branches are shared.
const [perWorktreeTip, sharedTip] = await Promise.all([
readTrimmedFile(path.join(gitDir, refName)),
commonDir === gitDir ? Promise.resolve(null) : readTrimmedFile(path.join(commonDir, refName))
])
const looseTip = perWorktreeTip ?? sharedTip
if (looseTip) {
// A loose ref shadows any packed entry, so its bytes settle the tip on their own.
return {
text: JSON.stringify([head, 'loose', perWorktreeTip ? 'worktree' : 'common', looseTip]),
mtimeMs: Number.NEGATIVE_INFINITY
}
}
const [packedRefs, reftable] = await Promise.all([
readFileStampComponent(path.join(commonDir, 'packed-refs')),
readFileStampComponent(path.join(commonDir, 'reftable'))
])
return {
text: JSON.stringify([head, 'packed', packedRefs.text, reftable.text]),
mtimeMs: Math.max(packedRefs.mtimeMs, reftable.mtimeMs)
}
}
/** Keep a hand-edited HEAD from steering the stamp outside the repo's ref store. */
function isSafeRefName(refName: string): boolean {
const segments = refName.split(/[\\/]/)
return (
segments[0] === 'refs' &&
segments.length > 1 &&
segments.every((segment) => segment.length > 0 && segment !== '.' && segment !== '..') &&
!path.isAbsolute(refName)
)
}
async function readTrimmedFile(filePath: string): Promise<string | null> {
try {
const trimmed = (await readFile(filePath, 'utf-8')).trim()
return trimmed.length > 0 ? trimmed : null
} catch (error) {
if (isMissingEntryError(error)) {
return null
}
throw error
}
}
export async function readFileStampComponent(filePath: string): Promise<StampComponent> {
try {
const stats = await stat(filePath)
return { text: `${requireMtimeMs(stats.mtimeMs)}:${stats.size}`, mtimeMs: stats.mtimeMs }
} catch (error) {
if (isMissingEntryError(error)) {
return ABSENT
}
throw error
}
}
async function readWorkingTreeComponent(filePath: string): Promise<StampComponent> {
try {
const stats = await stat(filePath)
// Inodes detect atomic replacement, but zero/unstable network-redirector values cannot improve the stamp.
const inode = isUsableInode(stats.ino) ? String(stats.ino) : MISSING
return {
text: `${requireMtimeMs(stats.mtimeMs)}:${stats.size}:${inode}`,
mtimeMs: stats.mtimeMs
}
} catch (error) {
if (isMissingEntryError(error)) {
return ABSENT
}
throw error
}
}
function isUsableInode(ino: unknown): boolean {
return typeof ino === 'number' && Number.isFinite(ino) && ino !== 0
}
/** A filesystem (or a stub) that reports no usable mtime cannot prove anything unchanged. */
function requireMtimeMs(mtimeMs: unknown): number {
if (typeof mtimeMs !== 'number' || !Number.isFinite(mtimeMs)) {
throw new Error('stat reported no usable mtime')
}
return mtimeMs
}
function isMissingEntryError(error: unknown): boolean {
const code = (error as NodeJS.ErrnoException | null)?.code
return code === 'ENOENT' || code === 'ENOTDIR'
}