Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/server/test/git-disk-clone.test.ts?version=181&format=json
243 linescodex-capacity-chair 100%Last change codex-capacity-chair in version 180, 2 versions changed it

You're reading version 181. The newest is 318, and 1 version since then changed this file. Go to the newestCompare with 318

1import assert from "node:assert/strict";
2import { randomBytes } from "node:crypto";
3import { test } from "node:test";
4import git, { type HttpClient } from "isomorphic-git";
5import { GitDiskIntake } from "../src/git-disk-intake.ts";
6import { GitFileContents } from "../src/git-file-contents.ts";
7import { GitIntakeTrees } from "../src/git-intake-trees.ts";
8import { pushedSince } from "../src/ingest.ts";
9import { MemoryFS } from "../src/memory-fs.ts";
10import { commitShared } from "../src/push.ts";
11import { memoryStorage } from "./helpers.ts";
13const packet = (text: string | Uint8Array) => {
14 const body = Buffer.from(text);
15 return Buffer.concat([Buffer.from((body.length + 4).toString(16).padStart(4, "0")), body]);
16};
18for (const mode of ["success", "limit", "fatal", "server-intake", "text-intake"])
19 test(`actual disk clone handles ${mode}`, async (t) => {
20 const rejectPack = mode === "limit";
21 const source = new MemoryFS(),
22 dir = "/repo";
23 await git.init({ fs: source, dir, defaultBranch: "main" });
24 const blob = mode === "text-intake" ? Buffer.from("x".repeat(16383) + "🚀é中".repeat(20000)) : randomBytes(400000);
25 const blobOid = await git.writeBlob({ fs: source, dir, blob });
26 const tree = await git.writeTree({
27 fs: source,
28 dir,
29 tree: [{ mode: "100644", path: "large.bin", oid: blobOid, type: "blob" }],
30 });
31 const author = {
32 name: "Capacity fixture",
33 email: "fixture@example.invalid",
34 timestamp: 1,
35 timezoneOffset: 0,
36 };
37 const base = await git.writeCommit({
38 fs: source,
39 dir,
40 commit: {
41 tree,
42 parent: [],
43 author,
44 committer: author,
45 message: "Base\n\nabso-version: 0\n",
46 },
47 });
48 const head = await git.writeCommit({
49 fs: source,
50 dir,
51 commit: {
52 tree,
53 parent: [base],
54 author,
55 committer: author,
56 message: "Fixture\n",
57 },
58 });
59 const packed = (
60 await git.packObjects({
61 fs: source,
62 dir,
63 oids: [head, base, tree, blobOid],
64 write: false,
65 })
66 ).packfile!;
67 const advertisement = Buffer.concat([
68 packet("# service=git-upload-pack\n"),
69 Buffer.from("0000"),
70 packet(`${head} HEAD\0symref=HEAD:refs/heads/main side-band-64k shallow\n`),
71 packet(`${head} refs/heads/main\n`),
72 Buffer.from("0000"),
73 ]);
74 let uploads = 0;
75 let uploadChunksRead = 0;
76 let uploadClosed = false;
77 const uploadParts = [packet("NAK\n")];
78 for (let at = 0; at < packed.length; at += 16000) {
79 uploadParts.push(packet(Buffer.concat([Buffer.from([1]), packed.subarray(at, at + 16000)])));
80 uploadParts.push(
81 packet(Buffer.concat([Buffer.from([2]), Buffer.from("Counting objects: 50% (1/2)\n".repeat(200))])),
82 );
83 if (mode === "fatal" && at === 16000) {
84 uploadParts.push(packet(Buffer.concat([Buffer.from([3]), Buffer.from("fixture remote failure")])));
85 break;
86 }
87 }
88 uploadParts.push(Buffer.from("0000"));
89 const http: HttpClient = {
90 request: async ({ url, method }) => {
91 const body = method === "POST" ? Buffer.concat(uploadParts) : advertisement;
92 if (method === "POST") uploads++;
93 return {
94 url,
95 method: method ?? "GET",
96 statusCode: 200,
97 statusMessage: "OK",
98 headers: {},
99 body: (async function* () {
100 try {
101 for (let at = 0; at < body.length; at += 997) {
102 if (method === "POST") uploadChunksRead++;
103 yield body.subarray(at, at + 997);
104 }
105 } finally {
106 if (method === "POST") uploadClosed = true;
107 }
108 })(),
109 };
110 },
111 };
112 const storage = memoryStorage();
113 const intake = new GitDiskIntake(storage, rejectPack ? 70000 : 2000000, 1000000);
114 const ingest = intake.ingest.bind(intake);
115 intake.ingest = async (...args) => {
116 await new Promise((resolve) => setTimeout(resolve, 25));
117 assert.ok(uploadChunksRead < 64, `pack source read ahead ${uploadChunksRead} chunks before disk consumption`);
118 return ingest(...args);
119 };
120 const cache = {};
121 intake.attach(cache);
122 const contents = new GitFileContents(storage, 1000000, 2000000);
123 const fs = new MemoryFS(contents);
124 const write = fs.promises.writeFile;
125 fs.promises.writeFile = async (path, ...args) => {
126 assert.ok(!/\.(pack|idx)$/.test(path), "clone must not persist a collected pack or index");
127 return write(path, ...args);
128 };
129 try {
130 if (mode === "server-intake" || mode === "text-intake") {
131 const trees = new GitIntakeTrees(storage);
132 if (mode === "text-intake") {
133 const original = GitDiskIntake.prototype.read;
134 t.mock.method(GitDiskIntake.prototype, "read", function (this: GitDiskIntake, oid: string) {
135 if (oid === blobOid) throw Error("whole text blob read forbidden");
136 return original.call(this, oid);
137 });
138 }
139 const artifacts = {
140 get: async () => ({
141 info: async () => ({ remote: "https://fixture.invalid/repo" }),
142 createToken: async () => ({ plaintext: "invented-test-token" }),
143 [Symbol.dispose]() {},
144 }),
145 };
146 const found = await pushedSince(
147 artifacts as never,
148 "fixture",
149 undefined,
150 async (bytes) => {
151 assert.deepEqual(Buffer.from(bytes), blob);
152 return "fixture-blob";
153 },
154 undefined,
155 undefined,
156 {
157 storage,
158 maxPackBytes: 2000000,
159 maxObjectBytes: 1000000,
160 http,
161 retainFileChunks: (files, path, chunks, oid) => trees.retainChunks(files, path, chunks, oid),
162 },
163 );
164 assert.equal(found!.base, 0);
165 assert.equal(found!.commits.length, 1);
166 if (mode === "text-intake") assert.equal(found!.commits[0]!.files["large.bin"], blob.toString("utf8"));
167 else assert.equal(found!.commits[0]!.blobs["large.bin"], "fixture-blob");
168 trees.dispose();
169 for (const table of ["git_pack_spool", "git_pack_resolve", "git_object_meta", "git_object_bytes"])
170 assert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0);
171 return;
172 }
173 const clone = git.clone({
174 fs,
175 dir,
176 http,
177 cache,
178 url: "https://fixture.invalid/repo",
179 ref: "main",
180 singleBranch: true,
181 noCheckout: true,
182 });
183 if (mode !== "success") {
184 await assert.rejects(clone, rejectPack ? /pack exceeds byte limit/ : /fixture remote failure/);
185 assert.equal(uploadClosed, true);
186 for (const table of ["git_pack_spool", "git_pack_resolve", "git_object_meta", "git_object_bytes"])
187 assert.equal(storage.db.prepare(`SELECT COUNT(*) AS n FROM ${table}`).get()!.n, 0);
188 return;
189 }
190 await clone;
191 intake.assertReady();
192 assert.equal((await git.log({ fs, dir, cache, ref: "main" })).length, 2);
193 let walked = 0;
194 await git.walk({
195 fs,
196 dir,
197 cache,
198 trees: [git.TREE({ ref: head })],
199 map: async (path, [entry]) => {
200 if (path === "large.bin") {
201 assert.deepEqual(Buffer.from((await entry!.content())!), blob);
202 walked++;
203 }
204 return true;
205 },
206 });
207 assert.equal(walked, 1);
208 assert.equal(uploads, 1);
209 assert.equal(await git.resolveRef({ fs, dir, ref: "main" }), head);
210 assert.equal((await git.readCommit({ fs, dir, cache, oid: head })).commit.tree, tree);
211 assert.deepEqual(Buffer.from((await git.readBlob({ fs, dir, cache, oid: blobOid })).blob), blob);
212 const exported = await commitShared(
213 fs,
214 dir,
215 head,
216 {
217 version: 1,
218 files: { "new.txt": "Disk export fixture" },
219 blobs: { "large.bin": "fixture-blob" },
220 },
221 () => blob,
222 undefined,
223 cache,
224 );
225 const next = await git.readCommit({ fs, dir, cache, oid: exported.commit });
226 assert.deepEqual(next.commit.parent, [head]);
227 assert.deepEqual(
228 Buffer.from((await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: "large.bin" })).blob),
229 blob,
230 );
231 assert.equal(
232 Buffer.from(
233 (await git.readBlob({ fs, dir, cache, oid: exported.commit, filepath: "new.txt" })).blob,
234 ).toString(),
235 "Disk export fixture",
236 );
237 for (const entry of fs.entries.values()) if (entry.kind === "file") assert.equal(entry.data.byteLength, 0);
238 } finally {
239 contents.dispose();
240 intake.dispose();
241 storage.db.close();
242 }
243 });