70 lines
1.8 KiB
JavaScript
70 lines
1.8 KiB
JavaScript
import { spawnSync } from "node:child_process";
|
|
|
|
const RECORD_START = "\x01";
|
|
const FIELD_SEP = "\x1f";
|
|
|
|
/**
|
|
* @typedef {object} Commit
|
|
* @property {string} hash
|
|
* @property {string} authorName
|
|
* @property {string} authorEmail
|
|
* @property {Date} date
|
|
* @property {string[]} files
|
|
*/
|
|
|
|
/**
|
|
* Read the full commit history of a bare repo cloned by clone.mjs.
|
|
* Uses --name-only, which only needs tree metadata (which paths changed),
|
|
* not blob content - so it stays fast even on a --filter=blob:none clone.
|
|
*
|
|
* @param {string} gitDir path to the bare repo
|
|
* @returns {Commit[]}
|
|
*/
|
|
export function readCommits(gitDir) {
|
|
const result = spawnSync(
|
|
"git",
|
|
[
|
|
"--git-dir",
|
|
gitDir,
|
|
"log",
|
|
"--all",
|
|
"--no-merges",
|
|
`--pretty=format:${RECORD_START}%H${FIELD_SEP}%an${FIELD_SEP}%ae${FIELD_SEP}%aI`,
|
|
"--name-only",
|
|
],
|
|
{ encoding: "utf8", maxBuffer: 1024 * 1024 * 256 }
|
|
);
|
|
|
|
if (result.status !== 0) {
|
|
const stderr = (result.stderr || "").trim();
|
|
throw new Error(`git log failed${stderr ? `: ${stderr}` : ""}`);
|
|
}
|
|
|
|
const records = (result.stdout || "").split(RECORD_START).filter(Boolean);
|
|
const commits = [];
|
|
|
|
for (const record of records) {
|
|
const newlineIndex = record.indexOf("\n");
|
|
const header = newlineIndex === -1 ? record : record.slice(0, newlineIndex);
|
|
const body = newlineIndex === -1 ? "" : record.slice(newlineIndex + 1);
|
|
|
|
const [hash, authorName, authorEmail, isoDate] = header.split(FIELD_SEP);
|
|
if (!hash || !isoDate) continue;
|
|
|
|
const files = body
|
|
.split("\n")
|
|
.map((line) => line.trim())
|
|
.filter(Boolean);
|
|
|
|
commits.push({
|
|
hash,
|
|
authorName: authorName || "unknown",
|
|
authorEmail: authorEmail || "",
|
|
date: new Date(isoDate),
|
|
files,
|
|
});
|
|
}
|
|
|
|
return commits;
|
|
}
|