openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
103 lines (102 loc) • 4.72 kB
JavaScript
import { r as gitMaintenanceEnvironment } from "./git-maintenance-env-l0LyckfF.mjs";
import { r as NODE_WORKER_WORKSPACE_STDIN_MAX_BYTES } from "./node-workspace-protocol-CnO26WQI.mjs";
import { t as GITHUB_PUBLICATION_CONFIG_GUARD } from "./github-publication-base-CnZoRiw5.mjs";
import { h as readGitHubPublicationTree, s as createGitHubPublicationCommandRunner } from "./github-publication-git-transport-eGUYv0Ac.mjs";
import { r as readGitHubRepositoryPublicationMetadata } from "./github-repository-publication-snapshot-CeAbMxp8.mjs";
//#region src/gateway/github-repository-publication-restore.ts
const RESTORE_PUBLICATION_INDEX_JS = String.raw`
const fs = require("node:fs");
const gitNullPath = process.platform === "win32" ? "NUL" : "/dev/null";
const { spawnSync } = require("node:child_process");
const paths = JSON.parse(fs.readFileSync(0, "utf8"));
const cwd = process.cwd();
const env = { ...process.env };
for (const key of Object.keys(env)) if (/^GIT_/i.test(key)) delete env[key];
Object.assign(env, { GIT_CONFIG_GLOBAL: gitNullPath, GIT_CONFIG_SYSTEM: gitNullPath,
${JSON.stringify(gitMaintenanceEnvironment()).slice(1, -1)}, GIT_ATTR_NOSYSTEM: "1", GIT_NO_REPLACE_OBJECTS: "1" });
${GITHUB_PUBLICATION_CONFIG_GUARD.script}
const action = process.argv[1];
if (action !== "add" && action !== "remove") throw new Error("Invalid publication restore operation");
const operation = action === "remove" ? ["update-index", "--force-remove", "-z", "--stdin"] :
["add", "--intent-to-add", "--force", "--pathspec-from-file=-", "--pathspec-file-nul"];
const result = spawnSync("git", ["--literal-pathspecs", "-c", "core.hooksPath=" + gitNullPath,
"-c", "core.fsmonitor=false", "-c", "core.attributesFile=" + gitNullPath,
...operation], {
env,
input: paths.join("\0") + "\0", timeout: 60000, maxBuffer: 128 * 1024,
});
if (result.error || result.status !== 0) {
throw new Error("Repository publication paths could not be restored; retry workspace preparation");
}
`;
/** Recover publication membership, not the prior staging contents or private recovery-only paths. */
async function prepareRepositoryPublicationRestore(e) {
if (!e.publicationStagingRoot || !e.publicationDigest) return [];
const { snapshot: t } = await readGitHubRepositoryPublicationMetadata(e.publicationStagingRoot, e.publicationDigest);
if (t.baseCommit !== e.current.baseCommit) throw new Error("Repository publication paths differ from the restored source baseline");
const n = new Map(e.current.entries.map((e) => [e.path, e]));
const r = new Map(t.entries.map((e) => [e.path, e]));
const i = new Map(r);
const a = new Set(t.entries.filter((e) => e.sha === null && e.mode !== "160000").map((e) => e.path));
let o;
if (e.indexBase) {
const { root: n, commit: s, assertCurrent: c } = e.indexBase;
const { run: l } = createGitHubPublicationCommandRunner(c);
const readTree = async (e) => {
const t = await readGitHubPublicationTree(n, e, l);
const r = t.toString("utf8");
if (!Buffer.from(r).equals(t)) throw new Error("Repository publication paths are not valid UTF-8");
return r.split("\0").filter(Boolean).map((e) => {
const t = e.indexOf(" ");
const [n, , r] = e.slice(0, t).split(" ");
if (t < 0 || !n || !r) throw new Error("Repository publication tree inventory is invalid");
return {
path: e.slice(t + 1),
mode: n,
sha: r
};
});
};
const [u, d] = await Promise.all([readTree(t.baseTree), readTree(s)]);
o = new Set(d.map((e) => e.path));
for (const e of u) if (!r.has(e.path) && (e.mode === "100644" || e.mode === "100755" || e.mode === "120000")) i.set(e.path, {
...e,
mode: e.mode,
sha: e.sha
});
for (const e of d) if (e.mode !== "160000" && !i.get(e.path)?.sha) a.add(e.path);
}
const s = [...i.values()].flatMap((e) => {
const t = n.get(e.path);
const i = (e.mode === "100644" || e.mode === "100755") && t?.type === "file";
const a = e.mode === "120000" && t?.type === "symlink";
return e.sha && !o?.has(e.path) && (i || a || !r.has(e.path)) ? [e.path] : [];
});
const c = [];
for (const [e, t] of [["remove", [...a]], ["add", s]]) {
let n = [];
let r = 2;
const flush = () => {
if (n.length) {
c.push({
action: e,
input: JSON.stringify(n)
});
n = [];
r = 2;
}
};
const i = NODE_WORKER_WORKSPACE_STDIN_MAX_BYTES / 2;
for (const e of t) {
const t = Buffer.byteLength(JSON.stringify(e)) + 1;
if (t + 2 > i) throw new Error("Repository publication path exceeds the restore command limit");
if (r + t > i) flush();
n.push(e);
r += t;
}
flush();
}
return c;
}
//#endregion
export { prepareRepositoryPublicationRestore as n, RESTORE_PUBLICATION_INDEX_JS as t };