Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031import { execFile } from "node:child_process";import { promisify } from "node:util";import { parseBundle, type Bundle } from "../shared/bundle";
const run = promisify(execFile);const REF = /^[A-Za-z0-9][A-Za-z0-9._\/-]{0,100}$/;const LIMIT = 8 * 1024 * 1024;
/** * The tests of one commit of the test repository, as a bundle: every `scripts/**.aite` and `scripts/**.lock.json` at that commit, read from git's object store (never * from a working folder, so uncommitted edits cannot leak in and the same commit always gives the same bundle). The repository is the one the service was configured * with, never a path a request names. `git` is run without a shell and with the ref passed as one argument after `--end-of-options`-style checks. */export async function bundleFromGit(repo: string, ref: string): Promise<{ commit: string; bundle: Bundle }> { if (!REF.test(ref) || ref.includes("..") || ref.startsWith("-")) throw new Error(`${JSON.stringify(ref).slice(0, 60)} is not a usable commit or branch name`); const git = (args: string[]) => run("git", ["-C", repo, ...args], { maxBuffer: LIMIT, encoding: "utf8" }).then((r) => r.stdout); let commit: string; try { commit = (await git(["rev-parse", "--verify", "--quiet", `${ref}^{commit}`])).trim(); } catch { throw new Error(`the test repository has no commit ${ref}`); } const names = (await git(["ls-tree", "-r", "-z", "--name-only", commit, "--", "scripts/"])).split("\0").filter((n) => /^scripts\/(?:[A-Za-z0-9._-]+\/)*[A-Za-z0-9._-]+\.(?:aite|lock\.json)$/.test(n)); if (names.length === 0) throw new Error(`commit ${commit.slice(0, 12)} has no scripts under scripts/`); const files: Record<string, string> = {}; for (const name of names.sort()) files[name] = await git(["show", `${commit}:${name}`]); const parsed = parseBundle({ version: 1, files }); if (!parsed.ok) throw new Error(`commit ${commit.slice(0, 12)} cannot be bundled: ${parsed.message}`); return { commit, bundle: parsed.bundle };}