- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/src/gitview/trailers.ts?format=json
95 linesFirst import 99% · pinch 1%Last change pinch in version 78, 2 versions changed it
1// Commit message trailers: "Key: value" lines in the last paragraph of a
2// message, the way git writes Signed-off-by. View commits carry Atom, Deps,
3// Resolves, Reason and View-Heads; ingested tie atoms carry Git-Commit.
5export interface Trailer {
6 readonly key: string;
7 readonly value: string;
8}
10/** Trailer keys the git view writes. A last paragraph with View-Heads is a view's own block. */
11export const VIEW_HEADS = "View-Heads";
12/** The trailer an ingested commit's tie atom carries: the full sha of the git commit. */
13export const GIT_COMMIT = "Git-Commit";
15const TRAILER_LINE = /^([A-Za-z0-9][A-Za-z0-9-]*):[ \t]*(.*)$/;
17/** Line endings to "\n", no blank lines at the start, no trailing whitespace at the end. */
18export function cleanMessage(message: string): string {
19 return message
20 .replace(/\r\n?/g, "\n")
21 .replace(/^(?:[ \t]*\n)+/, "")
22 .trimEnd();
23}
25/** A trailer value on one line: line breaks become spaces. */
26export function oneLine(text: string): string {
27 return text.replace(/\s*\n\s*/g, " ").trim();
28}
30/**
31 * The message, a blank line, then one "Key: value" line per trailer. The
32 * result ends with a newline. An empty message gets `fallback` as its subject
33 * so the trailers never become the subject line.
34 */
35export function withTrailers(message: string, trailers: readonly Trailer[], fallback: string): string {
36 const body = cleanMessage(message) || fallback;
37 if (trailers.length === 0) return `${body}\n`;
38 const block = trailers.map((t) => `${t.key}: ${oneLine(t.value)}`).join("\n");
39 return `${body}\n\n${block}\n`;
40}
42/** Split a cleaned message into its paragraphs (separated by blank lines). */
43function paragraphs(message: string): string[] {
44 return cleanMessage(message)
45 .split(/\n[ \t]*\n/)
46 .filter((p) => p.trim() !== "");
47}
49/**
50 * The trailers in the last paragraph, when every line of it is a trailer (a
51 * line starting with whitespace continues the one above). Otherwise none.
52 * A one-paragraph message has no trailers: it is all subject.
53 */
54export function parseTrailers(message: string): Trailer[] {
55 const paras = paragraphs(message);
56 if (paras.length < 2) return [];
57 const out: { key: string; value: string }[] = [];
58 for (const line of paras[paras.length - 1]!.split("\n")) {
59 const last = out[out.length - 1];
60 if (/^[ \t]/.test(line) && last) {
61 last.value = `${last.value} ${line.trim()}`;
62 continue;
63 }
64 const m = TRAILER_LINE.exec(line);
65 if (!m) return [];
66 out.push({ key: m[1]!, value: m[2]!.trim() });
67 }
68 return out;
69}
71/** The value of the first trailer with this key, if any. */
72export function trailerValue(trailers: readonly Trailer[], key: string): string | undefined {
73 return trailers.find((t) => t.key === key)?.value;
74}
76/**
77 * The message without a view's trailer block (a last paragraph carrying
78 * View-Heads). Used when a person reworded or amended a view commit and it is
79 * ingested as their own: its old trailers describe atoms, not this change.
80 */
81export function stripViewTrailers(message: string): string {
82 const paras = paragraphs(message);
83 if (paras.length >= 2 && trailerValue(parseTrailers(message), VIEW_HEADS) !== undefined) {
84 return paras.slice(0, -1).join("\n\n");
85 }
86 return cleanMessage(message);
87}
89/** First line of a message, and the rest (without the blank line between). */
90export function splitSubject(message: string): { subject: string; body: string } {
91 const clean = cleanMessage(message);
92 const nl = clean.indexOf("\n");
93 if (nl < 0) return { subject: clean, body: "" };
94 return { subject: clean.slice(0, nl).trim(), body: clean.slice(nl + 1).replace(/^(?:[ \t]*\n)+/, "") };
95}