@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 145 lines 5.03 KB
Raw
1 /**
2 * Release-note generation: `git log` → changelogen's conventional-commit
3 * parser → `render.ts`.
4 *
5 * This used to be changelogithub, but only its commit-range read, parser and
6 * author dedupe were used (no GitHub token was ever passed, so authors were
7 * never resolved to logins), and it pins an old changelogen that drags in
8 * deprecated `tar@6`. What's left of it is the defaults below.
9 *
10 * Runs under node through Effect's `ChildProcessSpawner`. The log is read
11 * through a pipe rather than a fixed buffer: `git log …
12 * --name-status` prints every changed path of every commit in the range, so a
13 * release spanning one repo-wide refactor overflows `execSync`'s 1 MB default
14 * and the release dies at the tagging step having already bumped versions.
15 */
16 import { parseCommits, type GitCommit, type RawGitCommit } from "changelogen";
17 import * as Data from "effect/Data";
18 import * as Effect from "effect/Effect";
19 import { ChildProcess } from "effect/process";
20 import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner";
21 import * as Stream from "effect/Stream";
22 import { renderMarkdown, type RenderConfig } from "./render.ts";
23
24 export interface Commit extends GitCommit {
25 /** Commit authors (incl. co-authors), deduped by email, bots dropped. */
26 resolvedAuthors: Array<{ name: string; email: string }>;
27 }
28
29 export interface ChangelogOptions {
30 /** Start of the range. Defaults to the closest tag before `to`. */
31 from?: string;
32 to: string;
33 /** `<owner>/<repo>`. */
34 repo: string;
35 }
36
37 const defaults = {
38 types: {
39 feat: { title: "🚀 Features" },
40 fix: { title: "🐞 Bug Fixes" },
41 perf: { title: "🏎 Performance" },
42 },
43 titles: { breakingChanges: "🚨 Breaking Changes" },
44 emoji: true,
45 capitalize: true,
46 baseUrl: "github.com",
47 scopeMap: {} as Record<string, string>,
48 };
49
50 const excludeAuthors = [/\[bot\]/i, /dependabot/i, /\(bot\)/i];
51
52 export class GitFailed extends Data.TaggedError("GitFailed")<{
53 readonly message: string;
54 readonly args: ReadonlyArray<string>;
55 readonly exitCode: number;
56 }> {}
57
58 /** Runs git, collecting stdout and stderr through pipes. */
59 const git = Effect.fn(function* (args: ReadonlyArray<string>) {
60 const spawner = yield* ChildProcessSpawner;
61 return yield* Effect.scoped(
62 Effect.gen(function* () {
63 const handle = yield* spawner.spawn(
64 ChildProcess.make("git", [...args], { stdout: "pipe", stderr: "pipe" }),
65 );
66 const [stdout, stderr, exitCode] = yield* Effect.all(
67 [
68 handle.stdout.pipe(Stream.decodeText(), Stream.mkString),
69 handle.stderr.pipe(Stream.decodeText(), Stream.mkString),
70 handle.exitCode,
71 ],
72 { concurrency: "unbounded" },
73 );
74 return { stdout, stderr, exitCode: Number(exitCode) };
75 }),
76 );
77 });
78
79 /** The tag closest to `to`, excluding a tag on `to` itself. */
80 const previousTag = Effect.fn(function* (to: string) {
81 const { stdout, exitCode } = yield* git(["describe", "--tags", "--abbrev=0", `${to}^`]);
82 return exitCode === 0 ? stdout.trim() : undefined;
83 });
84
85 /**
86 * Same command and record format as changelogen's `getGitDiff` (the `----`
87 * separator and `|`-delimited header are what `parseCommits` expects).
88 */
89 export const getGitDiff = Effect.fn(function* (from: string | undefined, to = "HEAD") {
90 const args = [
91 "--no-pager",
92 "log",
93 `${from ? `${from}...` : ""}${to}`,
94 "--pretty=----%n%s|%h|%an|%ae%n%b",
95 "--name-status",
96 ];
97 const { stdout, stderr, exitCode } = yield* git(args);
98 if (exitCode !== 0) {
99 return yield* new GitFailed({
100 message: `git log ${from ?? ""}...${to} failed: ${stderr.trim()}`,
101 args,
102 exitCode,
103 });
104 }
105 return stdout
106 .trim()
107 .split("----\n")
108 .splice(1)
109 .map((line): RawGitCommit => {
110 const [firstLine, ..._body] = line.split("\n");
111 const [message, shortHash, authorName, authorEmail] = (firstLine ?? "").split("|");
112 return {
113 message: message ?? "",
114 shortHash: shortHash ?? "",
115 author: { name: authorName ?? "", email: authorEmail ?? "" },
116 body: _body.join("\n"),
117 };
118 });
119 });
120
121 /** Attaches `resolvedAuthors`, keeping the first name seen per email. */
122 const resolveAuthors = (commits: GitCommit[]): Commit[] => {
123 const byEmail = new Map<string, { name: string; email: string }>();
124 return commits.map((commit) => ({
125 ...commit,
126 resolvedAuthors: commit.authors.flatMap((a) => {
127 if (!a.email || !a.name || excludeAuthors.some((re) => re.test(a.name))) return [];
128 if (!byEmail.has(a.email)) byEmail.set(a.email, { name: a.name, email: a.email });
129 return [byEmail.get(a.email)!];
130 }),
131 }));
132 };
133
134 export const generate = Effect.fn(function* (options: ChangelogOptions) {
135 const from = options.from ?? (yield* previousTag(options.to));
136 const config: RenderConfig = {
137 ...defaults,
138 repo: options.repo,
139 from: from ?? "",
140 to: options.to,
141 };
142 const raw = yield* getGitDiff(from, options.to);
143 const commits = resolveAuthors(parseCommits(raw, config as never));
144 return { config, commits, md: renderMarkdown(commits, config) };
145 });