-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit.ts
More file actions
359 lines (315 loc) · 11.9 KB
/
git.ts
File metadata and controls
359 lines (315 loc) · 11.9 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
/**
* Git utilities for cloning and updating repositories
*/
import { simpleGit, SimpleGit } from "simple-git";
import { existsSync, mkdirSync, rmSync, renameSync } from "fs";
import { join } from "path";
import { homedir } from "os";
import { RepoConfig } from "../repos/config.js";
export type Logger = (message: string, level?: "info" | "debug" | "warning" | "error") => void;
/**
* Get the alternate v-prefix variant of a tag.
* "v1.0.0" → "1.0.0", "1.0.0" → "v1.0.0"
*/
function alternateTagName(tag: string): string {
return tag.startsWith("v") ? tag.slice(1) : `v${tag}`;
}
/**
* Fetch a tag from origin, trying the alternate v-prefix variant on failure.
* Returns the resolved tag name that was successfully fetched.
*/
async function fetchTag(
repoGit: SimpleGit,
tag: string,
log?: Logger,
repoName?: string,
): Promise<string> {
const fetchArgs = (t: string): string[] => ["--depth=1", "origin", `refs/tags/${t}:refs/tags/${t}`];
try {
log?.(`${repoName}: Fetching tag ${tag}`, "info");
await repoGit.fetch(fetchArgs(tag));
return tag;
} catch {
const alt = alternateTagName(tag);
log?.(`${repoName}: Tag "${tag}" not found, trying "${alt}"`, "info");
await repoGit.fetch(fetchArgs(alt));
return alt;
}
}
/** Base directory for cloned repos */
export const REPOS_DIR = join(
process.env.AZTEC_MCP_REPOS_DIR || join(homedir(), ".aztec-mcp"),
"repos"
);
/**
* Ensure the repos directory exists
*/
export function ensureReposDir(): void {
mkdirSync(REPOS_DIR, { recursive: true });
}
/**
* Get the local path for a repository
*/
export function getRepoPath(repoName: string): string {
return join(REPOS_DIR, repoName);
}
/**
* Check if a repository is already cloned
*/
export function isRepoCloned(repoName: string): boolean {
const repoPath = getRepoPath(repoName);
return existsSync(join(repoPath, ".git"));
}
/**
* Clone a repository with optional sparse checkout and tag support
*/
export async function cloneRepo(
config: RepoConfig,
force: boolean = false,
log?: Logger
): Promise<string> {
ensureReposDir();
const repoPath = getRepoPath(config.name);
// Check if we need to re-clone due to version mismatch
const versionMismatch = await needsReclone(config);
const needsForceReclone = (force || versionMismatch) && existsSync(repoPath);
// If already cloned and version matches, skip or update
if (!needsForceReclone && isRepoCloned(config.name)) {
if (config.tag || config.commit) {
log?.(`${config.name}: Already cloned at correct ${config.tag ? "tag" : "commit"}, skipping`, "debug");
return `${config.name} already at ${config.commit || config.tag}`;
}
log?.(`${config.name}: Already cloned, updating`, "debug");
return await updateRepo(config.name, log);
}
// Clone to a temp dir when replacing an existing repo, so failure leaves the old repo intact
const clonePath = needsForceReclone ? repoPath + ".tmp" : repoPath;
if (needsForceReclone) {
log?.(`${config.name}: Safe re-clone (force=${force}, versionMismatch=${versionMismatch})`, "debug");
// Clean up stale temp dir from any previous failed attempt
rmSync(clonePath, { recursive: true, force: true });
}
// Determine ref to checkout: commit > tag > branch
const ref = config.commit || config.tag || config.branch || "default";
const refType = config.commit ? "commit" : config.tag ? "tag" : "branch";
const isSparse = config.sparse && config.sparse.length > 0;
log?.(`${config.name}: Cloning @ ${ref} (${refType}${isSparse ? ", sparse" : ""})`, "info");
const progressHandler = log
? (data: { method: string; stage: string; progress: number }) => {
log(`${config.name}: ${data.method} ${data.stage} ${data.progress}%`, "debug");
}
: undefined;
const git: SimpleGit = simpleGit({ progress: progressHandler });
try {
if (isSparse) {
// Clone with sparse checkout for large repos
if (config.commit) {
// For commits, we need full history to fetch the commit
await git.clone(config.url, clonePath, [
"--filter=blob:none",
"--sparse",
"--no-checkout",
]);
const repoGit = simpleGit({ baseDir: clonePath, progress: progressHandler });
await repoGit.raw(["config", "gc.auto", "0"]);
log?.(`${config.name}: Setting sparse checkout paths: ${config.sparse!.join(", ")}`, "debug");
await repoGit.raw(["sparse-checkout", "set", "--skip-checks", ...config.sparse!]);
log?.(`${config.name}: Fetching commit ${config.commit.substring(0, 7)}`, "info");
await repoGit.fetch(["origin", config.commit]);
log?.(`${config.name}: Checking out commit`, "debug");
await repoGit.checkout(config.commit);
} else if (config.tag) {
await git.clone(config.url, clonePath, [
"--filter=blob:none",
"--sparse",
"--no-checkout",
]);
const repoGit = simpleGit({ baseDir: clonePath, progress: progressHandler });
await repoGit.raw(["config", "gc.auto", "0"]);
log?.(`${config.name}: Setting sparse checkout paths: ${config.sparse!.join(", ")}`, "debug");
await repoGit.raw(["sparse-checkout", "set", "--skip-checks", ...config.sparse!]);
const resolvedTag = await fetchTag(repoGit, config.tag, log, config.name);
log?.(`${config.name}: Checking out tag`, "debug");
await repoGit.checkout(resolvedTag);
} else {
await git.clone(config.url, clonePath, [
"--filter=blob:none",
"--sparse",
"--depth=1",
...(config.branch ? ["-b", config.branch] : []),
]);
const repoGit = simpleGit({ baseDir: clonePath, progress: progressHandler });
await repoGit.raw(["config", "gc.auto", "0"]);
log?.(`${config.name}: Setting sparse checkout paths: ${config.sparse!.join(", ")}`, "debug");
await repoGit.raw(["sparse-checkout", "set", "--skip-checks", ...config.sparse!]);
}
} else {
// Clone for smaller repos
if (config.commit) {
// For commits, clone and checkout specific commit
await git.clone(config.url, clonePath, ["--no-checkout"]);
const repoGit = simpleGit({ baseDir: clonePath, progress: progressHandler });
log?.(`${config.name}: Fetching commit ${config.commit.substring(0, 7)}`, "info");
await repoGit.fetch(["origin", config.commit]);
log?.(`${config.name}: Checking out commit`, "debug");
await repoGit.checkout(config.commit);
} else if (config.tag) {
// Clone and checkout tag
await git.clone(config.url, clonePath, ["--no-checkout"]);
const repoGit = simpleGit({ baseDir: clonePath, progress: progressHandler });
const resolvedTag = await fetchTag(repoGit, config.tag, log, config.name);
log?.(`${config.name}: Checking out tag`, "debug");
await repoGit.checkout(resolvedTag);
} else {
await git.clone(config.url, clonePath, [
"--depth=1",
...(config.branch ? ["-b", config.branch] : []),
]);
}
}
} catch (error) {
// On failure: clean up temp dir, leave original repo intact
if (needsForceReclone) {
rmSync(clonePath, { recursive: true, force: true });
}
throw error;
}
// On success: atomic swap — move old out, move new in, then delete old.
// If the new rename fails, restore old from backup so the repo stays available.
if (needsForceReclone) {
const backupPath = repoPath + ".old";
rmSync(backupPath, { recursive: true, force: true });
if (existsSync(repoPath)) {
renameSync(repoPath, backupPath);
}
try {
renameSync(clonePath, repoPath);
} catch (swapError) {
// Restore old checkout so the repo isn't left unavailable
if (existsSync(backupPath)) {
try { renameSync(backupPath, repoPath); } catch { /* best-effort restore */ }
}
rmSync(clonePath, { recursive: true, force: true });
throw swapError;
}
rmSync(backupPath, { recursive: true, force: true });
}
log?.(`${config.name}: Clone complete`, "info");
const sparseLabel = isSparse ? `, sparse: ${config.sparse!.join(", ")}` : "";
return `Cloned ${config.name} @ ${ref} (${refType}${sparseLabel})`;
}
/**
* Update an existing repository
*/
export async function updateRepo(repoName: string, log?: Logger): Promise<string> {
const repoPath = getRepoPath(repoName);
if (!isRepoCloned(repoName)) {
throw new Error(`Repository ${repoName} is not cloned`);
}
log?.(`${repoName}: Updating`, "info");
const git = simpleGit(repoPath);
try {
await git.fetch(["--depth=1"]);
await git.reset(["--hard", "origin/HEAD"]);
log?.(`${repoName}: Update complete`, "info");
return `Updated ${repoName}`;
} catch (error) {
log?.(`${repoName}: Fetch failed, trying pull`, "warning");
// If fetch fails, try a simple pull
try {
await git.pull();
log?.(`${repoName}: Pull complete`, "info");
return `Updated ${repoName}`;
} catch (pullError) {
log?.(`${repoName}: Update failed: ${pullError}`, "error");
return `Failed to update ${repoName}: ${pullError}`;
}
}
}
/**
* Get the current commit hash for a repo
*/
export async function getRepoCommit(repoName: string, full: boolean = false): Promise<string | null> {
if (!isRepoCloned(repoName)) {
return null;
}
const git = simpleGit(getRepoPath(repoName));
const log = await git.log(["-1"]);
const hash = log.latest?.hash;
if (!hash) return null;
return full ? hash : hash.substring(0, 7);
}
/**
* Get the current tag for a repo (if HEAD points to a tag)
*/
export async function getRepoTag(repoName: string): Promise<string | null> {
const repoPath = getRepoPath(repoName);
if (!isRepoCloned(repoName)) {
return null;
}
const git = simpleGit(repoPath);
try {
// Get tag pointing to HEAD
const result = await git.raw(["describe", "--tags", "--exact-match", "HEAD"]);
return result.trim() || null;
} catch {
// HEAD is not at a tag
return null;
}
}
/**
* Check if the cloned repo matches the requested config
* Returns true if re-clone is needed
*/
export async function needsReclone(config: RepoConfig): Promise<boolean> {
if (!isRepoCloned(config.name)) {
return true; // Not cloned, need to clone
}
// If a specific commit is requested, check if we're at that commit
if (config.commit) {
const currentCommit = await getRepoCommit(config.name, true);
return !currentCommit?.startsWith(config.commit.substring(0, 7));
}
// If a tag is requested, check if we're at that tag (v-prefix insensitive)
if (config.tag) {
const currentTag = await getRepoTag(config.name);
if (currentTag === null) return true;
return currentTag !== config.tag && currentTag !== alternateTagName(config.tag);
}
// For branches, we don't force re-clone (just update)
return false;
}
/**
* Get status of all repos
*/
export async function getReposStatus(
configs: RepoConfig[]
): Promise<Map<string, { cloned: boolean; commit?: string }>> {
const status = new Map<string, { cloned: boolean; commit?: string }>();
for (const config of configs) {
const cloned = isRepoCloned(config.name);
const commit = cloned ? (await getRepoCommit(config.name)) || undefined : undefined;
status.set(config.name, { cloned, commit });
}
return status;
}
/**
* Get the Noir submodule commit from aztec-packages
* Returns the commit hash that aztec-packages uses for noir
*/
export async function getNoirCommitFromAztec(): Promise<string | null> {
const aztecPath = getRepoPath("aztec-packages");
if (!isRepoCloned("aztec-packages")) {
return null;
}
const git = simpleGit(aztecPath);
try {
// Get the submodule commit from the git tree
const result = await git.raw(["ls-tree", "HEAD", "noir/noir-repo"]);
// Output format: "160000 commit <hash>\tnoir/noir-repo"
const match = result.match(/^160000\s+commit\s+([a-f0-9]+)/);
return match ? match[1] : null;
} catch {
return null;
}
}