-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-log-tool.ts
More file actions
362 lines (317 loc) · 11.8 KB
/
git-log-tool.ts
File metadata and controls
362 lines (317 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { basename } from "node:path";
import type { FastMCP } from "fastmcp";
import { z } from "zod";
import { asyncPool, GIT_SUBPROCESS_PARALLELISM, gitTopLevel, spawnGitAsync } from "./git.js";
import { jsonRespond, spreadDefined, spreadWhen } from "./json.js";
import { requireGitAndRoots } from "./roots.js";
import { WorkspacePickSchema } from "./schemas.js";
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const MAX_COMMITS_HARD_CAP = 500;
const DEFAULT_MAX_COMMITS = 50;
const DEFAULT_SINCE = "7.days";
// Field separator written by git into stdout — we use git's %x01 (SOH) escape.
// The format string itself is safe ASCII; git emits the byte.
const FIELD_SEP_OUT = "\x01"; // what git outputs (SOH)
const RECORD_SEP_OUT = "\x02"; // what git outputs (STX) — used as record-START marker
// git log --pretty tformat: shaFull, subject, author, email, ISO date.
// sha7 and ageRelative dropped in v3 (shaFull.slice(0,7) used for display; date is ISO).
// %x02 is placed at the START of each record (tformat adds \n as terminator after each).
// Splitting stdout on \x02 then gives empty-first-chunk + one chunk per commit,
// each structured as: <fields>\x01\n\n <shortstat text>\n
// Fields are separated by %x01; the trailing \x01 before \n leaves one empty last field (ignored).
const PRETTY_FORMAT = "%x02%H%x01%s%x01%aN%x01%aE%x01%aI%x01";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface CommitJson {
sha: string;
subject: string;
author: string;
email?: string;
date: string;
filesChanged?: number;
insertions?: number;
deletions?: number;
}
interface LogGroupJson {
workspaceRoot: string;
repo: string;
branch: string;
commits: CommitJson[];
truncated?: boolean;
omittedCount?: number;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Parse a shortstat line like "3 files changed, 12 insertions(+), 5 deletions(-)"
* Returns undefined when the line doesn't match (e.g. empty diff).
*/
function parseShortstat(
line: string,
): { filesChanged: number; insertions: number; deletions: number } | undefined {
const m = /(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/.exec(
line,
);
if (!m) return undefined;
return {
filesChanged: parseInt(m[1] ?? "0", 10),
insertions: parseInt(m[2] ?? "0", 10),
deletions: parseInt(m[3] ?? "0", 10),
};
}
/**
* Fetch the current branch name (or detached HEAD fallback).
*/
async function gitCurrentBranch(cwd: string, branchArg: string | undefined): Promise<string> {
if (branchArg?.trim()) return branchArg.trim();
const r = await spawnGitAsync(cwd, ["rev-parse", "--abbrev-ref", "HEAD"]);
if (r.ok) return r.stdout.trim();
return "HEAD";
}
interface LogResult {
workspaceRoot: string;
repo: string;
branch: string;
commits: CommitJson[];
truncated: boolean;
omittedCount: number;
}
/**
* Run git log for a single repo root and return structured data.
*/
async function runGitLog(opts: {
top: string;
since: string;
paths: string[];
grep: string | undefined;
author: string | undefined;
maxCommits: number;
branch: string | undefined;
}): Promise<LogResult | { error: string; path: string }> {
const { top, since, paths, grep, author, maxCommits, branch } = opts;
// Resolve branch first (needed for output metadata).
const resolvedBranch = await gitCurrentBranch(top, branch);
// Fetch one extra commit to detect truncation.
const fetchLimit = maxCommits + 1;
const logArgs: string[] = [
"log",
`--pretty=tformat:${PRETTY_FORMAT}`,
"--shortstat",
`-n`,
String(fetchLimit),
`--since=${since}`,
];
if (branch?.trim()) {
logArgs.push(branch.trim());
}
if (grep?.trim()) {
logArgs.push(`--grep=${grep.trim()}`, "-i");
}
if (author?.trim()) {
logArgs.push(`--author=${author.trim()}`);
}
if (paths.length > 0) {
logArgs.push("--", ...paths);
}
const r = await spawnGitAsync(top, logArgs);
if (!r.ok) {
return {
error: "git_log_failed",
path: top,
};
}
// Parse output.
// git log --pretty=tformat:%x02<fields>%x01 --shortstat emits, per commit:
// \x02<fields separated by SOH>\x01\n\n <shortstat line>\n
// The \x02 is a record-START marker. Splitting on \x02 gives an empty first chunk
// followed by one chunk per commit. Each chunk is:
// <fields>\x01\n\n <shortstat>\n
const raw = r.stdout;
const recordChunks = raw.split(RECORD_SEP_OUT).slice(1); // drop leading empty
const allCommits: CommitJson[] = [];
for (const chunk of recordChunks) {
if (!chunk.trim()) continue;
// Fields before the first newline; shortstat (if any) follows blank line.
const newlineIdx = chunk.indexOf("\n");
const fieldsPart = newlineIdx >= 0 ? chunk.slice(0, newlineIdx) : chunk;
const statPart = newlineIdx >= 0 ? chunk.slice(newlineIdx + 1) : "";
const fields = fieldsPart.split(FIELD_SEP_OUT);
const [shaFull, subject, authorName, email, date] = fields;
if (!shaFull) continue;
const stat = parseShortstat(statPart);
const commit: CommitJson = {
sha: shaFull.trim(),
subject: subject?.trim() ?? "",
author: authorName?.trim() ?? "",
...spreadDefined("email", email?.trim() || undefined),
date: date?.trim() ?? "",
...spreadDefined("filesChanged", stat?.filesChanged),
...spreadDefined("insertions", stat?.insertions),
...spreadDefined("deletions", stat?.deletions),
};
allCommits.push(commit);
}
const truncated = allCommits.length > maxCommits;
const commits = truncated ? allCommits.slice(0, maxCommits) : allCommits;
const omittedCount = truncated ? allCommits.length - maxCommits : 0;
return {
workspaceRoot: top,
repo: basename(top),
branch: resolvedBranch,
commits,
truncated,
omittedCount,
};
}
// ---------------------------------------------------------------------------
// Markdown rendering
// ---------------------------------------------------------------------------
function renderLogMarkdown(group: LogResult, filterSummary: string): string {
const lines: string[] = [];
lines.push(`### ${group.repo} (${group.branch})${filterSummary ? ` — ${filterSummary}` : ""}`);
lines.push(`_root: ${group.workspaceRoot}_`);
lines.push("");
if (group.commits.length === 0) {
lines.push("_(no commits match)_");
} else {
for (const c of group.commits) {
lines.push(
`- \`${c.sha.slice(0, 7)}\` ${c.date.slice(0, 10)} ${c.subject} — ${c.author}`,
);
}
}
if (group.truncated) {
lines.push("");
lines.push(
`_(truncated — ${group.omittedCount} more commit(s) not shown; lower \`since\` or \`maxCommits\`)_`,
);
}
return lines.join("\n");
}
// ---------------------------------------------------------------------------
// Tool registration
// ---------------------------------------------------------------------------
export function registerGitLogTool(server: FastMCP): void {
server.addTool({
name: "git_log",
description:
"Path-filtered, time-windowed read-only `git log` across one or more workspace roots. " +
"Returns structured commit history with author, date, subject, and optional diff stats.",
annotations: {
readOnlyHint: true,
},
parameters: WorkspacePickSchema.extend({
since: z
.string()
.optional()
.describe(
"Passed to `git log --since=`. Accepts ISO timestamps or git relative forms like " +
"`48.hours` or `2.weeks.ago`. Default: `7.days`.",
),
paths: z
.array(z.string())
.optional()
.describe("Limit to commits touching these paths (passed as `-- <paths>`)."),
grep: z
.string()
.optional()
.describe(
"Filter commits whose message matches this regex (git `--grep`, case-insensitive).",
),
author: z
.string()
.optional()
.describe("Filter by author name or email (passed as `--author=`)."),
maxCommits: z
.number()
.int()
.min(1)
.max(MAX_COMMITS_HARD_CAP)
.optional()
.default(DEFAULT_MAX_COMMITS)
.describe(
`Maximum commits to return per root (hard cap ${MAX_COMMITS_HARD_CAP}). Default ${DEFAULT_MAX_COMMITS}.`,
),
branch: z.string().optional().describe("Ref/branch to log from. Default: HEAD."),
}),
execute: async (args) => {
const pre = requireGitAndRoots(server, args, undefined);
if (!pre.ok) return jsonRespond(pre.error);
// Validate `since` — reject obvious injection attempts (newlines, semicolons, shell chars).
const rawSince = (args.since?.trim() ?? DEFAULT_SINCE) || DEFAULT_SINCE;
if (/[\n\r;|&`$<>]/.test(rawSince)) {
return jsonRespond({ error: "invalid_since", since: rawSince });
}
// Validate paths — reject anything with null bytes or shell meta.
// Use charCodeAt(0) === 0 for the null byte to avoid a biome lint on control chars in regex.
const rawPaths = args.paths ?? [];
for (const p of rawPaths) {
if (p.split("").some((c) => c.charCodeAt(0) === 0) || /[\n\r;|&`$<>]/.test(p)) {
return jsonRespond({ error: "invalid_paths", path: p });
}
}
const maxCommits = Math.min(args.maxCommits ?? DEFAULT_MAX_COMMITS, MAX_COMMITS_HARD_CAP);
// Fan out across roots.
const jobs = pre.roots.map((rootInput) => ({ rootInput }));
const results = await asyncPool(jobs, GIT_SUBPROCESS_PARALLELISM, async ({ rootInput }) => {
const top = gitTopLevel(rootInput);
if (!top) {
return { _error: true as const, workspaceRoot: rootInput, error: "not_a_git_repository" };
}
const r = await runGitLog({
top,
since: rawSince,
paths: rawPaths,
grep: args.grep,
author: args.author,
maxCommits,
branch: args.branch,
});
if ("error" in r) {
return { _error: true as const, workspaceRoot: rootInput, error: r.error };
}
return { _error: false as const, ...r };
});
// Build filter summary string for markdown.
const filterParts: string[] = [`since: ${rawSince}`];
if (rawPaths.length > 0) filterParts.push(`paths: ${rawPaths.join(", ")}`);
if (args.grep) filterParts.push(`grep: ${args.grep}`);
if (args.author) filterParts.push(`author: ${args.author}`);
const filterSummary = filterParts.join(" | ");
if (args.format === "json") {
const groups: LogGroupJson[] = results.map((r) => {
if (r._error) {
return {
workspaceRoot: r.workspaceRoot,
repo: basename(r.workspaceRoot ?? ""),
branch: "",
commits: [],
...spreadWhen(true, { error: r.error }),
} as unknown as LogGroupJson;
}
const { _error: _e, ...rest } = r;
return {
...rest,
...spreadWhen(r.truncated, { truncated: true, omittedCount: r.omittedCount }),
} as LogGroupJson;
});
return jsonRespond({ groups } as unknown as Record<string, unknown>);
}
// Markdown
const mdChunks: string[] = ["# Git log"];
for (const r of results) {
if (r._error) {
mdChunks.push(`### ${r.workspaceRoot}\n_error: ${r.error}_`);
continue;
}
const { _error: _e, ...group } = r;
mdChunks.push(renderLogMarkdown(group, filterSummary));
}
return mdChunks.join("\n\n");
},
});
}