{"draft":null,"from":143,"to":318,"files":[{"path":"server/scripts/capacity-patch-git-push.mjs","added":94,"removed":0,"diff":"--- /dev/null\n+++ b/server/scripts/capacity-patch-git-push.mjs\n@@ -0,0 +1,94 @@\n+import { createHash } from \"node:crypto\";\n+import { readFileSync, writeFileSync } from \"node:fs\";\n+import { join } from \"node:path\";\n+\n+const root = \"capacity-git-dependency-sandbox/node_modules/isomorphic-git\";\n+const hashes = {\n+\t\"index.js\": \"66c20b7d890a6d48819f0be5f7bb1c5aab69937e6318ec912295c501028a5084\",\n+\t\"index.cjs\": \"b2142bba9e651fce83d6c1d266d32fc5eefa748544c452f9f22fada4e33dc367\",\n+};\n+const patched = {\n+\t\"index.js\": \"0bd051b1a38bf8d593bf37b5acf9ee453ddab33d48950c84e2c2c3f3ec449fc8\",\n+\t\"index.cjs\": \"6c9f6eab37a10c9311f2b7aa8d24b0772d4ba2703d848525a0519e1454d016e7\",\n+};\n+if (JSON.parse(readFileSync(join(root, \"package.json\"), \"utf8\")).version !== \"1.44.2\")\n+\tthrow Error(\"Unsupported Git version\");\n+const installed = Object.fromEntries(\n+\tObject.keys(hashes).map((file) => [\n+\t\tfile,\n+\t\tcreateHash(\"sha256\")\n+\t\t\t.update(readFileSync(join(root, file)))\n+\t\t\t.digest(\"hex\"),\n+\t]),\n+);\n+if (Object.keys(patched).every((file) => installed[file] === patched[file])) {\n+\tconsole.log(JSON.stringify({ verified: true, version: \"1.44.2\", bundles: installed }));\n+\tprocess.exit(0);\n+}\n+if (process.argv.includes(\"--check\")) throw Error(\"Git runtime patch is absent or modified\");\n+function replaceOnce(source, before, after) {\n+\tif (source.split(before).length !== 2) throw Error(\"Patch anchor mismatch\");\n+\treturn source.replace(before, after);\n+}\n+const outputs = [];\n+for (const [file, expected] of Object.entries(hashes)) {\n+\tconst original = readFileSync(join(root, file), \"utf8\");\n+\tif (createHash(\"sha256\").update(original).digest(\"hex\") !== expected) throw Error(`Unrecognized source: ${file}`);\n+\tconst start = original.indexOf(\"async function _pack({\");\n+\tconst end = original.indexOf(\"\\n// @ts-check\", start);\n+\tif (start < 0 || end < 0) throw Error(\"Missing pack implementation\");\n+\tconst old = original.slice(start, end);\n+\tlet streamed = replaceOnce(old, \"async function _pack({\", \"async function* _packStream({\");\n+\tstreamed = replaceOnce(\n+\t\tstreamed,\n+\t\t\"  for (const oid of oids) {\",\n+\t\t\"  yield* outputStream;\\n  outputStream.length = 0;\\n  for (const oid of oids) {\",\n+\t);\n+\tstreamed = replaceOnce(\n+\t\tstreamed,\n+\t\t\"    await writeObject({ write, object, stype: type });\",\n+\t\t\"    await writeObject({ write, object, stype: type });\\n    yield* outputStream;\\n    outputStream.length = 0;\",\n+\t);\n+\tstreamed = replaceOnce(streamed, \"  outputStream.push(digest);\\n  return outputStream\", \"  yield digest;\");\n+\tlet result =\n+\t\toriginal.slice(0, start) +\n+\t\tstreamed +\n+\t\t\"\\nasync function _pack(options) {\\n  const output = [];\\n  for await (const chunk of _packStream(options)) output.push(chunk);\\n  return output;\\n}\\n\" +\n+\t\toriginal.slice(end);\n+\tconst begin = result.indexOf(\"  const packstream2 = _delete\");\n+\tconst finish = result.indexOf(\"  const res = await GitRemoteHTTP.connect({\", begin);\n+\tif (begin < 0 || finish < 0) throw Error(\"Missing push implementation\");\n+\tresult =\n+\t\tresult.slice(0, begin) +\n+\t\t`  const packoids = [...objects];\n+  // Recreate the stream for every authentication retry; never retain a whole pack.\n+  const packbody = {\n+    async *[Symbol.asyncIterator]() {\n+      yield* packstream1;\n+      if (!_delete) yield* _packStream({ fs, cache, gitdir, oids: packoids });\n+    }\n+  };\n+` +\n+\t\tresult.slice(finish);\n+\tresult = replaceOnce(result, \"    body: [...packstream1, ...packstream2],\", \"    body: packbody,\");\n+\tconst sha256 = createHash(\"sha256\").update(result).digest(\"hex\");\n+\tif (sha256 !== patched[file]) throw Error(`Unexpected patch output: ${file}`);\n+\toutputs.push({ file, result, sha256 });\n+}\n+// Validate every input before changing either runtime bundle.\n+for (const output of outputs) writeFileSync(join(root, output.file), output.result);\n+writeFileSync(\n+\t\"capacity-git-push-patch-manifest.json\",\n+\tJSON.stringify(\n+\t\toutputs.map(({ file, sha256 }) => ({ file, sha256 })),\n+\t\tnull,\n+\t\t2,\n+\t),\n+);\n+console.log(\n+\tJSON.stringify({\n+\t\tversion: \"1.44.2\",\n+\t\tpatched: outputs.map(({ file, sha256 }) => ({ file, sha256 })),\n+\t\tscope: \"Isolated push patch; fetch and install integration incomplete\",\n+\t}),\n+);"}],"unmatched":[]}