-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-fetch-tool.ts
More file actions
339 lines (296 loc) · 10.7 KB
/
git-fetch-tool.ts
File metadata and controls
339 lines (296 loc) · 10.7 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
import type { FastMCP } from "fastmcp";
import { z } from "zod";
import { ERROR_CODES } from "./error-codes.js";
import { isSafeGitUpstreamToken, spawnGitAsync } from "./git.js";
import { jsonRespond, spreadWhen } from "./json.js";
import { requireSingleRepo } from "./roots.js";
import { WorkspacePickSchema } from "./schemas.js";
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface UpdatedRefDelta {
ref: string;
oldSha: string;
newSha: string;
flag: string;
}
interface CreatedRefDelta {
ref: string;
newSha: string;
flag: string;
}
interface PrunedRefDelta {
ref: string;
}
interface GitFetchResult {
ok: boolean;
remote: string;
updatedRefs: string[];
newRefs: string[];
output: string;
// Structured deltas — present only when non-empty (v3 "omit when empty" convention)
updated?: UpdatedRefDelta[];
created?: CreatedRefDelta[];
pruned?: PrunedRefDelta[];
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Parse `git fetch` output to extract updated and new refs.
* Lines containing "[new" indicate new refs (new branch, new tag, new ref).
* Lines with " -> " but not containing "[new" indicate updated refs.
*/
function parseGitFetchOutput(output: string): { updatedRefs: string[]; newRefs: string[] } {
const lines = output.split("\n");
const updatedRefs: string[] = [];
const newRefs: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) continue;
// Lines containing "[new" indicate new refs (e.g. "[new branch]", "[new tag]", "[new ref]")
if (trimmed.includes("[new")) {
newRefs.push(trimmed);
}
// Lines with " -> " that don't contain "[new" indicate ref updates
else if (trimmed.includes(" -> ")) {
updatedRefs.push(trimmed);
}
}
return { updatedRefs, newRefs };
}
const ZEROS_SHA = "0000000000000000000000000000000000000000";
/**
* Parse `git fetch --porcelain` stdout.
*
* Machine-readable lines have the form:
* <flag><SP><old-sha><SP><new-sha><SP><local-ref>
*
* Flags:
* ' ' = fast-forward update
* '+' = forced update
* '*' = new ref
* '-' = pruned (deleted)
* '!' = rejected
* '=' = up-to-date (no change)
* 't' = tag update
*/
function parsePorcelainOutput(stdout: string): {
updated: UpdatedRefDelta[];
created: CreatedRefDelta[];
pruned: PrunedRefDelta[];
} {
const updated: UpdatedRefDelta[] = [];
const created: CreatedRefDelta[] = [];
const pruned: PrunedRefDelta[] = [];
for (const line of stdout.split("\n")) {
if (!line) continue;
// Porcelain lines: flag(1) + space(1) + old-sha + space + new-sha + space + ref
// Minimum viable line: 1 flag + 1 space + at least some content
if (line.length < 3) continue;
const flag = line[0];
const rest = line.slice(2); // skip "flag " prefix
const parts = rest.split(" ");
// Expected: [old-sha, new-sha, ref...]
if (parts.length < 3) continue;
const oldSha = parts[0] ?? "";
const newSha = parts[1] ?? "";
const ref = parts.slice(2).join(" ");
if (!ref) continue;
if (flag === "-") {
pruned.push({ ref });
} else if (flag === "*" || oldSha === ZEROS_SHA) {
// New ref: flag is '*' OR old-sha is all-zeros
created.push({ ref, newSha, flag: flag ?? "*" });
} else if (flag === " " || flag === "+" || flag === "t") {
// Update: old and new differ
if (oldSha !== newSha) {
updated.push({ ref, oldSha, newSha, flag: flag ?? " " });
}
}
// '!' (rejected) and '=' (up-to-date) are intentionally ignored
}
return { updated, created, pruned };
}
// ---------------------------------------------------------------------------
// Tool registration
// ---------------------------------------------------------------------------
export function registerGitFetchTool(server: FastMCP): void {
server.addTool({
name: "git_fetch",
description:
"Fetch updates from a remote repository without modifying the working tree. " +
"Returns structured output distinguishing updated refs from new refs.",
annotations: {
readOnlyHint: false, // Fetch modifies refs but not working tree; not strictly read-only but safe
},
parameters: WorkspacePickSchema.omit({ absoluteGitRoots: true, allWorkspaceRoots: true })
.pick({
workspaceRoot: true,
rootIndex: true,
format: true,
})
.extend({
remote: z
.string()
.optional()
.default("origin")
.describe("Remote to fetch from (default: origin)."),
branch: z
.string()
.optional()
.describe("If specified: fetch only this branch (e.g. 'main')."),
prune: z
.boolean()
.optional()
.default(false)
.describe("Pass --prune to remove deleted remote branches (default: false)."),
tags: z
.boolean()
.optional()
.default(false)
.describe("Pass --tags to also fetch all tags (default: false)."),
}),
execute: async (args) => {
const pre = requireSingleRepo(server, args);
if (!pre.ok) {
return jsonRespond(pre.error);
}
const gitTop = pre.gitTop;
const remote = (args.remote ?? "origin").trim();
const branch = args.branch?.trim();
const prune = args.prune === true;
const tags = args.tags === true;
// Validate remote and branch to prevent argument injection.
if (!isSafeGitUpstreamToken(remote)) {
return jsonRespond({ error: ERROR_CODES.UNSAFE_REMOTE_TOKEN, remote });
}
if (branch && !isSafeGitUpstreamToken(branch)) {
return jsonRespond({ error: ERROR_CODES.UNSAFE_REF_TOKEN, branch });
}
// Build base fetch args (without --porcelain for now)
const baseArgs: string[] = ["fetch"];
if (prune) {
baseArgs.push("--prune");
}
if (tags) {
baseArgs.push("--tags");
}
baseArgs.push(remote);
if (branch) {
baseArgs.push(branch);
}
// --- Attempt structured fetch with --porcelain (requires git >= 2.41) ---
let structured: {
updated: UpdatedRefDelta[];
created: CreatedRefDelta[];
pruned: PrunedRefDelta[];
} | null = null;
let result: { ok: boolean; stdout: string; stderr: string };
const porcelainArgs = ["fetch", "--porcelain"];
if (prune) porcelainArgs.push("--prune");
if (tags) porcelainArgs.push("--tags");
porcelainArgs.push(remote);
if (branch) porcelainArgs.push(branch);
const porcelainResult = await spawnGitAsync(gitTop, porcelainArgs);
if (
!porcelainResult.ok &&
(porcelainResult.stderr.includes("unknown option") ||
porcelainResult.stderr.includes("unknown switch") ||
porcelainResult.stderr.includes("invalid option"))
) {
// --porcelain not supported: fall back to plain fetch
result = await spawnGitAsync(gitTop, baseArgs);
structured = null;
} else {
// Use porcelain result (ok or actual fetch error)
result = porcelainResult;
if (porcelainResult.ok || !porcelainResult.stderr.includes("unknown option")) {
structured = parsePorcelainOutput(porcelainResult.stdout);
}
}
// Legacy string-line parse: use combined output when porcelain is unavailable.
// When porcelain succeeded, derive legacy fields from structured data so
// that callers who rely on updatedRefs/newRefs still get useful values.
let updatedRefs: string[];
let newRefs: string[];
if (structured !== null) {
// Derive legacy string arrays from structured deltas
updatedRefs = structured.updated.map(
(d) => `${d.oldSha.slice(0, 7)}..${d.newSha.slice(0, 7)} ${d.ref}`,
);
newRefs = structured.created.map((d) => `[new ref] ${d.ref} -> ${d.newSha.slice(0, 7)}`);
} else {
const parsed = parseGitFetchOutput(result.stdout + result.stderr);
updatedRefs = parsed.updatedRefs;
newRefs = parsed.newRefs;
}
const fetchResult: GitFetchResult = {
ok: result.ok,
remote,
updatedRefs,
newRefs,
output: (result.stdout + result.stderr).trim(),
...spreadWhen(structured !== null && structured.updated.length > 0, {
updated: structured?.updated ?? [],
}),
...spreadWhen(structured !== null && structured.created.length > 0, {
created: structured?.created ?? [],
}),
...spreadWhen(structured !== null && structured.pruned.length > 0, {
pruned: structured?.pruned ?? [],
}),
};
if (args.format === "json") {
return jsonRespond(fetchResult as unknown as Record<string, unknown>);
}
// Markdown output
const lines: string[] = [`# Git fetch from '${remote}'`];
if (!result.ok) {
lines.push("", "**Status**: Failed", "");
lines.push("```", result.stdout || result.stderr || "(no output)", "```");
return lines.join("\n");
}
lines.push("", "**Status**: Success", "");
// Prefer structured deltas in markdown when available
if (structured !== null && structured.updated.length > 0) {
lines.push("## Updated refs", "");
for (const d of structured.updated) {
lines.push(`- \`${d.ref}\` ${d.oldSha.slice(0, 7)}→${d.newSha.slice(0, 7)}`);
}
} else if (updatedRefs.length > 0) {
lines.push("## Updated refs", "");
for (const ref of updatedRefs) {
lines.push(`- ${ref}`);
}
}
if (structured !== null && structured.created.length > 0) {
lines.push("", "## New refs", "");
for (const d of structured.created) {
lines.push(`- \`${d.ref}\` (new, ${d.newSha.slice(0, 7)})`);
}
} else if (newRefs.length > 0) {
lines.push("", "## New refs", "");
for (const ref of newRefs) {
lines.push(`- ${ref}`);
}
}
if (structured !== null && structured.pruned.length > 0) {
lines.push("", "## Pruned refs", "");
for (const d of structured.pruned) {
lines.push(`- \`${d.ref}\` (deleted)`);
}
}
if (
updatedRefs.length === 0 &&
newRefs.length === 0 &&
(structured === null ||
(structured.updated.length === 0 && structured.created.length === 0)) &&
result.stdout.trim()
) {
lines.push("", "## Output", "", "```", result.stdout.trim(), "```");
}
return lines.join("\n");
},
});
}