- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/src/git-intake-trees.ts?format=json
77 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 178, 1 version changed it
1import { StoredText } from "../../src/engine/stored-text.ts";
2import type { SqliteStorage } from "./engine-project.ts";
3import type { PushedCommits } from "./ingest.ts";
4import { SqliteWriterState } from "./writer-host.ts";
6type Tree = Pick<PushedCommits["commits"][number], "files" | "modes" | "blobs">;
8/** Keeps earlier Git trees on disk while later commits are decoded. */
9export class GitIntakeTrees {
10 /** Only at a cold Durable Object start, before any intake can be active. */
11 static recover(storage: SqliteStorage): void {
12 SqliteWriterState.initialize(storage);
13 storage.sql.exec("DELETE FROM writer_state WHERE namespace GLOB 'git-intake:*'");
14 }
15 readonly namespace = `git-intake:${crypto.randomUUID()}`;
16 readonly storage: SqliteStorage;
17 readonly text: StoredText;
18 private next = 0;
19 private active = true;
20 constructor(storage: SqliteStorage) {
21 this.storage = storage;
22 SqliteWriterState.initialize(storage);
23 this.text = new StoredText(new SqliteWriterState(storage, this.namespace), "files");
24 }
25 /** Created outside any scope containing decoded file text or a whole tree. */
26 private getter(key: string): () => string {
27 return () => {
28 if (!this.active) throw Error("Git intake trees are closed");
29 return this.text.read(key);
30 };
31 }
32 retainFile(files: Record<string, string>, path: string, body: string, oid?: string): void {
33 this.retainChunks(
34 files,
35 path,
36 (function* () {
37 for (let at = 0; at < body.length; at += StoredText.chunkCharacters)
38 yield body.slice(at, at + StoredText.chunkCharacters);
39 })(),
40 oid,
41 );
42 }
43 retainChunks(files: Record<string, string>, path: string, chunks: Iterable<string>, oid?: string): void {
44 if (!this.active) throw Error("Git intake trees are closed");
45 if (oid !== undefined && !/^[0-9a-f]{40}$/.test(oid)) throw Error("Invalid Git blob id");
46 const key = oid === undefined ? String(this.next++) : `blob:${oid}`;
47 this.storage.transactionSync(() => {
48 if (this.text.storage.read(this.text.collection, key) === undefined) this.text.writeChunks(key, chunks);
49 });
50 Object.defineProperty(files, path, {
51 enumerable: true,
52 get: this.getter(key),
53 });
54 }
55 retain(tree: Tree): Tree {
56 if (!this.active) throw Error("Git intake trees are closed");
57 const files: Record<string, string> = Object.create(null);
58 this.storage.transactionSync(() => {
59 for (const path of Object.keys(tree.files)) {
60 const key = String(this.next++);
61 this.text.write(key, tree.files[path]!);
62 Object.defineProperty(files, path, {
63 enumerable: true,
64 get: this.getter(key),
65 });
66 }
67 });
68 return { files, modes: tree.modes, blobs: tree.blobs };
69 }
70 [Symbol.dispose](): void {
71 this.dispose();
72 }
73 dispose(): void {
74 this.active = false;
75 this.storage.sql.exec("DELETE FROM writer_state WHERE namespace = ?", this.namespace);
76 }
77}