Skip to content

Commit 8f8b1c3

Browse files
TMHSDigitalclaude
andcommitted
build: cache the last-commit dates per HEAD inside .git (#262)
A no-change rebuild reads the dates from .git/fgda-commit-dates.json instead of walking the whole history; any new commit invalidates it. The sitemap is byte-identical with and without the cache. Closes #262 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpkeXQPaUhxjgqdqnk6BrJ
1 parent 9423fc8 commit 8f8b1c3

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

‎site/build.mjs‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,28 @@ function headMetaHtml(site, stats, generatedAt, hasCard) {
219219
* git is unavailable (a tarball); a shallow checkout dates every file to HEAD.
220220
*/
221221
function lastCommitDates() {
222+
const git = (args) =>
223+
execFileSync("git", args, { cwd: ROOT, encoding: "utf8", maxBuffer: 64 * 1024 * 1024, stdio: ["ignore", "pipe", "ignore"] });
222224
try {
223-
const out = execFileSync("git", ["log", "--format=%x00%cs", "--name-only", "--", ...DATED_PATHS], {
224-
cwd: ROOT,
225-
encoding: "utf8",
226-
maxBuffer: 64 * 1024 * 1024,
227-
stdio: ["ignore", "pipe", "ignore"],
228-
});
229-
return commitDatesFromLog(out);
225+
// The dates depend only on the commits, so they are cached per HEAD inside
226+
// .git (never published, never committed): a no-change rebuild skips the
227+
// full-history log, whose cost grows with every commit (#262).
228+
const [gitDir, head] = git(["rev-parse", "--git-dir", "HEAD"]).trim().split("\n");
229+
const cacheFile = path.resolve(ROOT, gitDir, "fgda-commit-dates.json");
230+
const key = `${head} ${DATED_PATHS.join(",")}`;
231+
try {
232+
const cached = JSON.parse(fs.readFileSync(cacheFile, "utf8"));
233+
if (cached.key === key) return new Map(Object.entries(cached.dates));
234+
} catch {
235+
// No cache yet, or an unreadable one: rebuild it below.
236+
}
237+
const dates = commitDatesFromLog(git(["log", "--format=%x00%cs", "--name-only", "--", ...DATED_PATHS]));
238+
try {
239+
fs.writeFileSync(cacheFile, JSON.stringify({ key, dates: Object.fromEntries(dates) }));
240+
} catch {
241+
// A read-only .git still builds; it just reads the log every time.
242+
}
243+
return dates;
230244
} catch {
231245
// No git: the sitemap falls back to the build date.
232246
return new Map();

0 commit comments

Comments
 (0)