import { existsSync } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; /** The checkout a directory is in: the nearest ancestor of the path as given that holds `.git` (a directory, or a * worktree's file), or undefined outside every checkout. It walks the spelling it is handed; a caller whose base may * be a symlink passes its physical path. What a checkout tracks, and what a failed Git query means, is each caller's. */ export function checkoutOf(dir: string): string | undefined { for (let d = resolve(dir); ; d = dirname(d)) { if (existsSync(join(d, '.git'))) return d; if (dirname(d) === d) return undefined; } }