Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
71 linesCodeBlameRaw
1/**
2 * A commit message's parts: its subject, the prose of its body, and the
3 * trailers git and tools add at the end (`Co-Authored-By: …`), which read
4 * better as metadata than as prose.
5 */
6export type CommitMessage = {
7 subject: string;
8 body: string;
9 /** The names in `Co-Authored-By` trailers, without their addresses. */
10 coAuthors: string[];
11 /** Every other trailer, as written but for any email address in it. */
12 trailers: { key: string; value: string }[];
13};
14
15const TRAILER = /^([A-Za-z][A-Za-z0-9-]*):\s+(.+)$/;
16
17export function parseCommitMessage(message: string): CommitMessage {
18 const [subject = "", ...rest] = message.split("\n");
19 const lines = rest.join("\n").trim().split("\n");
20 // Trailers are the last paragraph, when every line of it is one.
21 const lastBreak = lines.lastIndexOf("");
22 const last = lines.slice(lastBreak + 1);
23 const isTrailers = last.length > 0 && last.every((line) => TRAILER.test(line.trim()));
24 const prose = isTrailers ? lines.slice(0, Math.max(lastBreak, 0)) : lines;
25 const coAuthors: string[] = [];
26 const trailers: { key: string; value: string }[] = [];
27 if (isTrailers) {
28 for (const line of last) {
29 const [, key, value] = TRAILER.exec(line.trim())!;
30 if (key.toLowerCase() === "co-authored-by") coAuthors.push(value.replace(/\s*<[^>]*>\s*$/, "").trim());
31 else {
32 // `Signed-off-by: Ada <ada@example.com>` shows Ada: g1t never shows an address.
33 const shown = value.replace(/\s*<[^<>]*@[^<>]*>/g, "").trim();
34 if (shown) trailers.push({ key, value: shown });
35 }
36 }
37 }
38 return { subject, body: prose.join("\n").trim(), coAuthors, trailers };
39}
40
41/**
42 * A run of a commit message, or of a note on an issue's timeline: plain
43 * text, a reference to an issue or pull request (`#12` in the repository
44 * it is in, `acme/web#12` in another), a commit by its full hash, or a
45 * session by its id (`ses_…`, as in "closed this in … via session ses_…").
46 */
47export type MessagePart =
48 | { kind: "text"; text: string }
49 | { kind: "ref"; text: string; repo: string | null; number: number }
50 | { kind: "commit"; text: string; sha: string }
51 | { kind: "session"; text: string; id: string };
52
53// `#12` and `acme/web#12`, but not `a#12` or a link's `/page#12`; a full hash on its own; a session's id.
54const REFERENCE =
55 /(?<![\w/#.-])(?:([A-Za-z0-9][A-Za-z0-9-]*\/[A-Za-z0-9._-]+))?#(\d+)\b|\b([0-9a-f]{40})\b|\b(ses_[0-9a-z]{10,60})\b/g;
56
57/** Splits text into its references and the text between them; `Closes #12` keeps `Closes ` as text. */
58export function messageParts(text: string): MessagePart[] {
59 const parts: MessagePart[] = [];
60 let at = 0;
61 for (const match of text.matchAll(REFERENCE)) {
62 const start = match.index;
63 if (start > at) parts.push({ kind: "text", text: text.slice(at, start) });
64 if (match[3]) parts.push({ kind: "commit", text: match[0], sha: match[3] });
65 else if (match[4]) parts.push({ kind: "session", text: match[0], id: match[4] });
66 else parts.push({ kind: "ref", text: match[0], repo: match[1] ?? null, number: Number(match[2]) });
67 at = start + match[0].length;
68 }
69 if (at < text.length) parts.push({ kind: "text", text: text.slice(at) });
70 return parts;
71}