Repository navigation
Expand file tree
/
Copy pathcommitRecords.js
More file actions
129 lines (121 loc) · 4.16 KB
/
Copy pathcommitRecords.js
File metadata and controls
129 lines (121 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
// @ts-check
/* Draws commit records as a Semantic Diff page: one card per commit, each
* the commit's identity and its message drawn. A record is a commit message
* in the format of the `records` skill (.claude/skills/records/SKILL.md).
* This file reads git; scripts/recordRender.js draws.
*
* node scripts/commitRecords.js --out review.html <rev>...
* node scripts/commitRecords.js --out review.html <base>..<tip>...
* --pr <number> and --title <text> head the page with the pull request.
* --markdown <file> also draws the records as the pull request's comment
* (scripts/recordMarkdown.js), linking the page at --url <url>. With
* --since <rev>, the comment draws only the commits <rev> lacks, and is
* not written when there are none.
*/
import { execFileSync } from "node:child_process";
import { writeFileSync } from "node:fs";
import { commentMarkdown } from "./recordMarkdown.js";
import { CSS, esc, pageBody, pageTitle, parseRecord } from "./recordRender.js";
/** @import { CommitRecord, Group } from "./recordRender.js" */
/** @param {string[]} args */
const git = args =>
execFileSync("git", args, {
encoding: "utf8",
maxBuffer: 1 << 28,
stdio: ["ignore", "pipe", "ignore"],
});
/**
* The record of one commit: its short hash, author, date and message.
* @param {string} rev
* @returns {CommitRecord}
*/
function readRecord(rev) {
const show = (/** @type {string[]} */ ...format) =>
git(["show", "-s", ...format, rev]);
return parseRecord({
sha: show("--format=%h", "--abbrev=9").trim(),
author: show("--format=%an").trim(),
date: show("--format=%ad", "--date=format:%b %d, %Y").trim(),
message: show("--format=%B"),
});
}
/**
* A revision is one card; a range `base..tip` is its commits, oldest first.
* @param {string} arg
* @returns {Group}
*/
function group(arg) {
if (!arg.includes("..")) return { records: [readRecord(arg)] };
const revs = git(["rev-list", "--reverse", "--no-merges", arg])
.split("\n")
.filter(Boolean);
return { range: arg, records: revs.map(readRecord) };
}
const args = process.argv.slice(2);
/** @type {Record<string, string>} */
const options = {};
/** @type {string[]} */
const targets = [];
for (let i = 0; i < args.length; i++) {
if (
["--out", "--pr", "--title", "--markdown", "--url", "--since"].includes(
args[i],
) &&
i + 1 < args.length
)
options[args[i].slice(2)] = args[++i];
else targets.push(args[i]);
}
const outFile = options.out;
if (!outFile) {
console.error(
"usage: commitRecords.js --out <page.html> [--pr <number>] [--title <text>] [--markdown <comment.md> [--url <page url>] [--since <rev>]] <rev | base..tip>...",
);
process.exit(1);
}
const heading = {
pr: (options.pr ?? "").replace(/\D/g, ""),
title: options.title ?? "",
};
const groups = targets.map(group);
const body = pageBody(groups, heading);
if (options.markdown) {
const since = options.since;
// The commits of the targets that `since` does not reach.
const fresh = new Set(
since
? targets.flatMap(t =>
git(["rev-list", "--abbrev-commit", "--abbrev=9", t, `^${since}`])
.split("\n")
.filter(Boolean),
)
: [],
);
const shown = since
? groups.map(g => ({
...g,
records: g.records.filter(r => fresh.has(r.sha)),
}))
: groups;
if (shown.some(g => g.records.length)) {
writeFileSync(
options.markdown,
commentMarkdown(shown, {
url: options.url,
total: since
? groups.reduce((n, g) => n + g.records.length, 0)
: undefined,
}),
);
console.log(options.markdown);
}
}
const head = `<title>${esc(pageTitle(heading))}</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,600&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500&family=STIX+Two+Text:ital,wght@0,400;0,500;1,400&display=swap">
<style>${CSS}</style>`;
writeFileSync(
outFile,
`<!doctype html>\n<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">\n${head}</head><body>${body}</body></html>\n`,
);
console.log(outFile);