-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-inventory-tool.ts
More file actions
189 lines (172 loc) · 6.71 KB
/
git-inventory-tool.ts
File metadata and controls
189 lines (172 loc) · 6.71 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
import type { FastMCP } from "fastmcp";
import { z } from "zod";
import {
asyncPool,
GIT_SUBPROCESS_PARALLELISM,
gitRevParseGitDir,
gitTopLevel,
isSafeGitUpstreamToken,
} from "./git.js";
import {
buildInventorySectionMarkdown,
collectInventoryEntry,
type InventoryEntryJson,
makeSkipEntry,
validateRepoPath,
} from "./inventory.js";
import { jsonRespond, spreadDefined, spreadWhen } from "./json.js";
import { applyPresetNestedRoots } from "./presets.js";
import { requireGitAndRoots } from "./roots.js";
import { MAX_INVENTORY_ROOTS_DEFAULT, WorkspacePickSchema } from "./schemas.js";
export function registerGitInventoryTool(server: FastMCP): void {
server.addTool({
name: "git_inventory",
description: "Read-only status + ahead/behind per root.",
annotations: {
readOnlyHint: true,
},
parameters: WorkspacePickSchema.extend({
nestedRoots: z.array(z.string()).optional(),
preset: z.string().optional(),
presetMerge: z
.boolean()
.optional()
.default(false)
.describe("Merge with preset instead of replacing."),
remote: z.string().optional().describe("Pair with `branch`."),
branch: z.string().optional().describe("Pair with `remote`."),
maxRoots: z.number().int().min(1).max(256).optional().default(MAX_INVENTORY_ROOTS_DEFAULT),
}),
execute: async (args) => {
if (args.absoluteGitRoots != null && args.absoluteGitRoots.length > 0) {
if (args.preset || (args.nestedRoots?.length ?? 0) > 0) {
return jsonRespond({ error: "absolute_git_roots_nested_or_preset_conflict" });
}
}
const pre = requireGitAndRoots(server, args, args.preset);
if (!pre.ok) {
return jsonRespond(pre.error);
}
const rawRemote = args.remote?.trim();
const rawBranch = args.branch?.trim();
const hasRemote = rawRemote !== undefined && rawRemote !== "";
const hasBranch = rawBranch !== undefined && rawBranch !== "";
if (hasRemote !== hasBranch) {
return jsonRespond({ error: "remote_branch_mismatch" });
}
type Upstream =
| { mode: "fixed"; remote: string; branch: string }
| { mode: "auto"; remote?: undefined; branch?: undefined };
let upstream: Upstream = { mode: "auto" };
if (hasRemote && hasBranch && rawRemote && rawBranch) {
if (!isSafeGitUpstreamToken(rawRemote) || !isSafeGitUpstreamToken(rawBranch)) {
return jsonRespond({ error: "invalid_remote_or_branch" });
}
upstream = { mode: "fixed", remote: rawRemote, branch: rawBranch };
}
const useFixed = upstream.mode === "fixed";
const allJson: {
workspace_root: string;
presetSchemaVersion?: string;
upstream?: { mode: "fixed"; remote: string; branch: string };
entries: InventoryEntryJson[];
}[] = [];
const mdChunks: string[] = [];
for (const workspaceRoot of pre.roots) {
const top = gitTopLevel(workspaceRoot);
if (!top) {
const err = { error: "not_a_git_repository", path: workspaceRoot };
if (args.format === "json") {
allJson.push({
workspace_root: workspaceRoot,
...(upstream.mode === "fixed" ? { upstream } : {}),
entries: [
makeSkipEntry(workspaceRoot, workspaceRoot, upstream.mode, JSON.stringify(err)),
],
});
} else {
mdChunks.push(`### ${workspaceRoot}\n${jsonRespond(err)}`);
}
continue;
}
let nestedRoots: string[] | undefined = args.nestedRoots;
let presetSchemaVersion: string | undefined;
if (args.preset) {
const applied = applyPresetNestedRoots(top, args.preset, args.presetMerge, nestedRoots);
if (!applied.ok) {
return jsonRespond(applied.error);
}
nestedRoots = applied.nestedRoots;
presetSchemaVersion = applied.presetSchemaVersion;
}
const maxRoots = args.maxRoots ?? MAX_INVENTORY_ROOTS_DEFAULT;
let nestedRootsTruncated = false;
let nestedRootsOmittedCount = 0;
if (nestedRoots && nestedRoots.length > maxRoots) {
nestedRootsOmittedCount = nestedRoots.length - maxRoots;
nestedRoots = nestedRoots.slice(0, maxRoots);
nestedRootsTruncated = true;
}
const headerNote = useFixed
? `upstream (fixed): ${upstream.remote}/${upstream.branch}`
: "upstream: @{u}";
const entries: InventoryEntryJson[] = [];
if (nestedRoots?.length) {
const jobs: { label: string; abs: string }[] = [];
for (const rel of nestedRoots) {
const { abs, underTop } = validateRepoPath(rel, top);
if (!underTop) {
entries.push(
makeSkipEntry(rel, abs, upstream.mode, "(path escapes git toplevel — rejected)"),
);
continue;
}
if (!gitRevParseGitDir(abs)) {
entries.push(makeSkipEntry(rel, abs, upstream.mode, "(not a git work tree — skip)"));
continue;
}
jobs.push({ label: rel, abs });
}
const computed = await asyncPool(jobs, GIT_SUBPROCESS_PARALLELISM, (j) =>
collectInventoryEntry(j.label, j.abs, upstream.remote, upstream.branch),
);
entries.push(...computed);
} else if (!gitRevParseGitDir(top)) {
entries.push(
makeSkipEntry(".", top, upstream.mode, "(not a git work tree — unexpected)"),
);
} else {
const one = await collectInventoryEntry(".", top, upstream.remote, upstream.branch);
entries.push(one);
}
if (args.format === "json") {
allJson.push({
workspace_root: top,
...spreadDefined("presetSchemaVersion", presetSchemaVersion),
...spreadWhen(nestedRootsTruncated, {
nestedRootsTruncated: true,
nestedRootsOmittedCount,
}),
...(upstream.mode === "fixed" ? { upstream } : {}),
entries,
});
} else {
const sections: string[] = [`### ${top}`, headerNote];
if (nestedRootsTruncated) {
sections.push(
`nested_roots_truncated: ${nestedRootsOmittedCount} path(s) not listed (maxRoots=${maxRoots})`,
);
}
for (const e of entries) {
sections.push(...buildInventorySectionMarkdown(e));
}
mdChunks.push(sections.join("\n"));
}
}
if (args.format === "json") {
return jsonRespond({ inventories: allJson });
}
return ["# Git inventory", ...mdChunks].join("\n\n");
},
});
}