Versions 127 to 318
new file, 90 lines
1+// Where a push came from in git, when it was imported (abso-core issue 21). 2+// A version imported by client/scripts/import-git.ts ends its message with 3+// a line naming the commit, its author and its time, and, when the 4+// repository's issues are on the web, a line saying where: 6+// git 4e1a7c2b9f03, Ada Lovelace, 2026-10-06T20:36:43-05:00 7+// Issues: https://github.com/owner/repo/issues 9+// A commit pushed with plain git ends with "Git-Commit: <sha>" and 10+// "Author: <name>". A host may also name the commit beside the version 11+// (`commit`). Issue references like "#39" in an imported message, or in a 12+// line it wrote, mean that repository's issues, not the project's. 14+export interface GitOrigin { 15+ readonly commit: string; 16+ readonly author?: string; 17+ readonly time?: string; 18+ /** The repository's issues, as a web address, when known. */ 19+ readonly issues?: string; 20+ /** The message with these lines taken off. */ 21+ readonly message: string; 24+const IMPORTED = /^git ([0-9a-f]{7,64}), (.+), (\d{4}-\d\d-\d\dT[^,\s]+)$/; 25+const ISSUES = /^Issues: (https?:\/\/\S+)$/; 26+const PUSHED = /^Git-Commit: ([0-9a-f]{7,64})$/; 27+const PUSHED_BY = /^Author: (.+)$/; 30+ * A push's git origin, from its message's last lines or the commit its host 31+ * names, or undefined when it didn't come from git. `issues` is where "#N" 32+ * points when the message doesn't say (the project's "gitIssues"). 34+export function gitOrigin( 35+ p: { readonly message: string; readonly commit?: string; readonly author?: string }, 37+): GitOrigin | undefined { 38+ const lines = p.message.trimEnd().split("\n"); 39+ let end = lines.length; 40+ let found: Omit<GitOrigin, "message"> | undefined; 41+ const tracker = ISSUES.exec(lines[end - 1] ?? ""); 43+ const imported = IMPORTED.exec(lines[end - 1] ?? ""); 46+ found = { commit: imported[1]!, author: imported[2]!, time: imported[3]! }; 48+ const by = PUSHED_BY.exec(lines[end - 1] ?? ""); 49+ const sha = PUSHED.exec(lines[end - (by ? 2 : 1)] ?? ""); 52+ found = { commit: sha[1]!, ...(by ? { author: by[1]! } : {}) }; 55+ if (!found && p.commit !== undefined) found = { commit: p.commit, ...(p.author ? { author: p.author } : {}) }; 56+ if (!found) return undefined; 57+ const where = tracker?.[1] ?? issues; 60+ ...(where ? { issues: where.replace(/\/+$/, "") } : {}), 61+ message: lines.slice(0, end).join("\n").trimEnd(), 65+/** A git remote's issues on the web: GitHub's or GitLab's, by https or ssh; else undefined. */ 66+export function issuesOf(remote: string): string | undefined { 70+ .replace(/\.git$/, ""); 71+ const m = /^(?:https:\/\/|ssh:\/\/git@|git@)(github\.com|gitlab\.com)[:/]([\w.-]+(?:\/[\w.-]+)+)$/.exec(plain); 72+ if (!m) return undefined; 73+ return m[1] === "gitlab.com" ? `https://gitlab.com/${m[2]}/-/issues` : `https://github.com/${m[2]}/issues`; 76+/** The issues "#N" names in text, each once, in order: not "owner/repo#N", which names its own. */ 77+export function issueNumbers(text: string): number[] { 78+ const out: number[] = []; 79+ for (const m of text.matchAll(/(^|[^\w/])#(\d+)\b/g)) { 80+ const n = Number(m[2]); 81+ if (!out.includes(n)) out.push(n); 86+/** One line per "#N" in text, saying which tracker it's in. Empty without a tracker or a reference. */ 87+export function issueLinks(text: string, issues: string | undefined): string[] { 88+ if (!issues) return []; 89+ return issueNumbers(text).map((n) => `#${n} is ${issues}/${n}`);