// REFS AS WORLD STATE: a repository's refs live in the string world store under // `/refs/` (one sha per file) and `/HEAD` (a symref), the bare layout — // state a snapshot carries, a projection can read, and (through the action log the plane keeps // beside it) a push policy can gate. No packed-refs: the store lists directories. import type { WorldStore } from '../world-store.ts'; import { refuseReadOnlyWrite } from '../request-scope.ts'; export class GitRefs { constructor(private readonly store: WorldStore, private readonly prefix: string) {} private path(name: string): string { return `${this.prefix}/${name}`; } get(name: string): string | null { const v = this.store.read(this.path(name)); return v === null ? null : v.trim(); } // a read-only request moves no ref (request-scope.ts): refused before the store is touched set(name: string, sha: string): void { refuseReadOnlyWrite('git'); this.store.write(this.path(name), `${sha}\n`); } delete(name: string): void { refuseReadOnlyWrite('git'); if (this.store.exists(this.path(name))) this.store.remove(this.path(name)); } head(): string { const v = this.store.read(this.path('HEAD')); return v === null ? 'refs/heads/main' : v.trim().replace(/^ref: /, ''); } setHead(target: string): void { refuseReadOnlyWrite('git'); this.store.write(this.path('HEAD'), `ref: ${target}\n`); } /** Run `fn` holding the repository's ref lock (the world store's exclusive lock). */ withLock(fn: () => T): T { return this.store.withLock(this.path('refs.lock'), fn); } /** Every `refs/...` name with its sha, sorted by name. */ list(): Array<{ name: string; sha: string }> { const out: Array<{ name: string; sha: string }> = []; // stat before read: on the fs store a directory read throws (EISDIR); the memory store answers null const walk = (dir: string, rel: string): void => { for (const child of this.store.list(dir)) { const p = `${dir}/${child}`; const r = `${rel}/${child}`; if (this.store.stat(p)?.isDirectory) { walk(p, r); continue; } const sha = this.store.read(p); if (sha !== null) out.push({ name: r, sha: sha.trim() }); } }; walk(this.path('refs'), 'refs'); return out.sort((a, b) => (a.name < b.name ? -1 : 1)); } }