| 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 | */ |
| 6 | export 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 | |
| 15 | const TRAILER = /^([A-Za-z][A-Za-z0-9-]*):\s+(.+)$/; |
| 16 | |
| 17 | export 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), or a commit by its full hash. |
| 45 | */ |
| 46 | export type MessagePart = |
| 47 | | { kind: "text"; text: string } |
| 48 | | { kind: "ref"; text: string; repo: string | null; number: number } |
| 49 | | { kind: "commit"; text: string; sha: string }; |
| 50 | |
| 51 | // `#12` and `acme/web#12`, but not `a#12` or a link's `/page#12`; a full hash on its own. |
| 52 | const REFERENCE = /(?<![\w/#.-])(?:([A-Za-z0-9][A-Za-z0-9-]*\/[A-Za-z0-9._-]+))?#(\d+)\b|\b([0-9a-f]{40})\b/g; |
| 53 | |
| 54 | /** Splits text into its references and the text between them; `Closes #12` keeps `Closes ` as text. */ |
| 55 | export function messageParts(text: string): MessagePart[] { |
| 56 | const parts: MessagePart[] = []; |
| 57 | let at = 0; |
| 58 | for (const match of text.matchAll(REFERENCE)) { |
| 59 | const start = match.index; |
| 60 | if (start > at) parts.push({ kind: "text", text: text.slice(at, start) }); |
| 61 | if (match[3]) parts.push({ kind: "commit", text: match[0], sha: match[3] }); |
| 62 | else parts.push({ kind: "ref", text: match[0], repo: match[1] ?? null, number: Number(match[2]) }); |
| 63 | at = start + match[0].length; |
| 64 | } |
| 65 | if (at < text.length) parts.push({ kind: "text", text: text.slice(at) }); |
| 66 | return parts; |
| 67 | } |