Repository navigation
Expand file tree
/
Copy pathgithub-extract.ts
More file actions
1063 lines (927 loc) · 32.7 KB
/
Copy pathgithub-extract.ts
File metadata and controls
1063 lines (927 loc) · 32.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { chmodSync, closeSync, constants as fsConstants, existsSync, fstatSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readSync, readdirSync, realpathSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
import { lstat as lstatAsync, open as openAsync, opendir as opendirAsync, readFile as readFileAsync, realpath as realpathAsync, rm as rmAsync, type FileHandle } from "node:fs/promises";
import { execFile, spawn, type ChildProcess } from "node:child_process";
import { createHash } from "node:crypto";
import { basename, dirname, extname, join, resolve as resolvePath, sep as pathSep } from "node:path";
import { activityMonitor } from "./activity.ts";
import type { ExtractedContent } from "./extract.ts";
import { checkGhAvailable, checkRepoSize, fetchViaApi, showGhHint } from "./github-api.ts";
import { getWebSearchConfigPath } from "./utils.ts";
const CONFIG_PATH = getWebSearchConfigPath();
const BINARY_EXTENSIONS = new Set([
".png", ".jpg", ".jpeg", ".gif", ".bmp", ".ico", ".webp", ".svg", ".tiff", ".tif",
".mp3", ".mp4", ".avi", ".mov", ".mkv", ".flv", ".wmv", ".wav", ".ogg", ".webm", ".flac", ".aac",
".zip", ".tar", ".gz", ".bz2", ".xz", ".7z", ".rar", ".zst",
".exe", ".dll", ".so", ".dylib", ".bin", ".o", ".a", ".lib",
".woff", ".woff2", ".ttf", ".otf", ".eot",
".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx",
".sqlite", ".db", ".sqlite3",
".pyc", ".pyo", ".class", ".jar", ".war",
".iso", ".img", ".dmg",
]);
const NOISE_DIRS = new Set([
"node_modules", "vendor", ".next", "dist", "build", "__pycache__",
".venv", "venv", ".tox", ".mypy_cache", ".pytest_cache",
"target", ".gradle", ".idea", ".vscode",
]);
const MAX_INLINE_FILE_CHARS = 100_000;
const MAX_TREE_ENTRIES = 200;
const CLONE_RUNTIME_OWNER_FILENAME = ".owner.json";
const CLONE_RUNTIME_OWNER_VERSION = 1;
const MAX_CLONE_RUNTIME_OWNER_BYTES = 1024;
export interface GitHubUrlInfo {
owner: string;
repo: string;
ref?: string;
refIsFullSha: boolean;
path?: string;
type: "root" | "blob" | "tree";
}
interface CachedClone {
destination: CloneDestination;
clonePromise: Promise<string | null>;
/** When a fetch last returned this clone's path. */
lastUsedAt: number;
settled: boolean;
}
interface CloneDestination {
rootPath: string;
localPath: string;
}
interface CloneRuntimeOwner {
version: 1;
pid: number;
platform: string;
bootId?: string;
startTime?: string;
}
interface GitHubCloneConfig {
enabled: boolean;
maxRepoSizeMB: number;
cloneTimeoutSeconds: number;
clonePath: string;
}
const cloneCache = new Map<string, CachedClone>();
const startedCloneRuntimeCleanups = new Set<string>();
let cachedConfig: GitHubCloneConfig | null = null;
let cloneRuntime: { parentPath: string; rootPath: string } | null = null;
function normalizeEnabled(value: unknown, fallback: boolean): boolean {
return typeof value === "boolean" ? value : fallback;
}
function normalizePositiveNumber(value: unknown, fallback: number): number {
if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
return value > 0 ? value : fallback;
}
function expandPath(value: string): string {
let expanded = value;
// Expand ~ at the start of the path
if (expanded.startsWith("~/") || expanded === "~") {
expanded = expanded.replace(/^~/, process.env.HOME || process.env.USERPROFILE || "");
}
// Expand environment variables like $HOME, $USER, etc.
expanded = expanded.replace(/\$([A-Z_][A-Z0-9_]*)/gi, (match, varName) => {
return process.env[varName] ?? match;
});
return expanded;
}
function normalizeClonePath(value: unknown, fallback: string): string {
if (typeof value !== "string") return fallback;
const normalized = value.trim();
if (normalized.length === 0) return fallback;
return expandPath(normalized);
}
function loadGitHubConfig(): GitHubCloneConfig {
if (cachedConfig) return cachedConfig;
const defaults: GitHubCloneConfig = {
enabled: true,
maxRepoSizeMB: 350,
cloneTimeoutSeconds: 30,
clonePath: "/tmp/pi-github-repos",
};
if (!existsSync(CONFIG_PATH)) {
cachedConfig = defaults;
return cachedConfig;
}
const rawText = readFileSync(CONFIG_PATH, "utf-8");
let raw: { githubClone?: { enabled?: unknown; maxRepoSizeMB?: unknown; cloneTimeoutSeconds?: unknown; clonePath?: unknown } };
try {
raw = JSON.parse(rawText) as { githubClone?: { enabled?: unknown; maxRepoSizeMB?: unknown; cloneTimeoutSeconds?: unknown; clonePath?: unknown } };
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(`Failed to parse ${CONFIG_PATH}: ${message}`);
}
const gc = raw.githubClone ?? {};
cachedConfig = {
enabled: normalizeEnabled(gc.enabled, defaults.enabled),
maxRepoSizeMB: normalizePositiveNumber(gc.maxRepoSizeMB, defaults.maxRepoSizeMB),
cloneTimeoutSeconds: normalizePositiveNumber(gc.cloneTimeoutSeconds, defaults.cloneTimeoutSeconds),
clonePath: normalizeClonePath(gc.clonePath, defaults.clonePath),
};
return cachedConfig;
}
const NON_CODE_SEGMENTS = new Set([
"issues", "pull", "pulls", "discussions", "releases", "wiki",
"actions", "settings", "security", "projects", "graphs",
"compare", "commits", "tags", "branches", "stargazers",
"watchers", "network", "forks", "milestone", "labels",
"packages", "codespaces", "contribute", "community",
"sponsors", "invitations", "notifications", "insights",
]);
export function parseGitHubUrl(url: string): GitHubUrlInfo | null {
let parsed: URL;
try {
parsed = new URL(url);
} catch {
return null;
}
const host = parsed.hostname.toLowerCase();
if (host !== "github.com" && host !== "www.github.com") return null;
const segments: string[] = [];
for (const segment of parsed.pathname.split("/").filter(Boolean)) {
try {
segments.push(decodeURIComponent(segment));
} catch {
return null;
}
}
if (segments.length < 2) return null;
const owner = segments[0];
const repo = segments[1].replace(/\.git$/, "");
if (!/^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/.test(owner)) return null;
if (owner.includes("--")) return null;
if (!/^[A-Za-z0-9._-]{1,100}$/.test(repo) || repo === "." || repo === "..") return null;
if (NON_CODE_SEGMENTS.has(segments[2]?.toLowerCase())) return null;
if (segments.length === 2) {
return { owner, repo, refIsFullSha: false, type: "root" };
}
const action = segments[2];
if (action !== "blob" && action !== "tree") return null;
if (segments.length < 4) return null;
const ref = segments[3];
if (ref.length === 0 || ref.length > 1024 || /[\0-\x1f\x7f]/.test(ref)) return null;
const refIsFullSha = /^[0-9a-f]{40}$/.test(ref);
const pathParts = segments.slice(4);
const path = pathParts.length > 0 ? pathParts.join("/") : "";
return {
owner,
repo,
ref,
refIsFullSha,
path,
type: action as "blob" | "tree",
};
}
function cacheKey(owner: string, repo: string, ref?: string): string {
return ref ? `${owner}/${repo}@${ref}` : `${owner}/${repo}`;
}
function readLinuxBootId(): string | null {
try {
const bootId = readFileSync("/proc/sys/kernel/random/boot_id", "utf-8").trim();
return bootId.length > 0 ? bootId : null;
} catch {
return null;
}
}
async function readLinuxBootIdAsync(): Promise<string | null> {
try {
const bootId = (await readFileAsync("/proc/sys/kernel/random/boot_id", "utf-8")).trim();
return bootId.length > 0 ? bootId : null;
} catch {
return null;
}
}
interface LinuxProcessInfo {
state: string;
startTime: string | null;
dead: boolean;
}
function parseLinuxProcessInfo(stat: string): LinuxProcessInfo | null {
const closingParen = stat.lastIndexOf(") ");
if (closingParen < 0) return null;
const fields = stat.slice(closingParen + 2).trim().split(/\s+/);
const state = fields[0];
const startTime = fields[19];
if (!state || !startTime || !/^\d+$/.test(startTime)) return null;
return { state, startTime, dead: false };
}
function readLinuxProcessInfo(pid: number): LinuxProcessInfo | null {
try {
return parseLinuxProcessInfo(readFileSync(`/proc/${pid}/stat`, "utf-8"));
} catch (err) {
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") {
return { state: "", startTime: null, dead: true };
}
return null;
}
}
async function readLinuxProcessInfoAsync(pid: number): Promise<LinuxProcessInfo | null> {
try {
return parseLinuxProcessInfo(await readFileAsync(`/proc/${pid}/stat`, "utf-8"));
} catch (err) {
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") {
return { state: "", startTime: null, dead: true };
}
return null;
}
}
function currentCloneRuntimeOwner(): CloneRuntimeOwner {
const owner: CloneRuntimeOwner = {
version: CLONE_RUNTIME_OWNER_VERSION,
pid: process.pid,
platform: process.platform,
};
if (process.platform === "linux") {
const bootId = readLinuxBootId();
const processInfo = readLinuxProcessInfo(process.pid);
if (bootId && processInfo?.startTime) {
owner.bootId = bootId;
owner.startTime = processInfo.startTime;
}
}
return owner;
}
function writeCloneRuntimeOwner(runtimePath: string): void {
const ownerPath = join(runtimePath, CLONE_RUNTIME_OWNER_FILENAME);
writeFileSync(ownerPath, JSON.stringify(currentCloneRuntimeOwner()), {
encoding: "utf-8",
flag: "wx",
mode: 0o600,
});
}
function parseCloneRuntimeOwner(text: string): CloneRuntimeOwner | null {
let parsed: unknown;
try {
parsed = JSON.parse(text);
} catch {
return null;
}
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
const record = parsed as Record<string, unknown>;
if (
record.version !== CLONE_RUNTIME_OWNER_VERSION ||
typeof record.pid !== "number" ||
!Number.isSafeInteger(record.pid) ||
record.pid <= 0 ||
typeof record.platform !== "string" ||
record.platform.length === 0 ||
record.platform.length > 32
) {
return null;
}
const owner: CloneRuntimeOwner = {
version: CLONE_RUNTIME_OWNER_VERSION,
pid: record.pid,
platform: record.platform,
};
if (record.bootId !== undefined) {
if (typeof record.bootId !== "string" || record.bootId.length === 0 || record.bootId.length > 256) return null;
owner.bootId = record.bootId;
}
if (record.startTime !== undefined) {
if (typeof record.startTime !== "string" || !/^\d+$/.test(record.startTime)) return null;
owner.startTime = record.startTime;
}
return owner;
}
async function readCloneRuntimeOwner(runtimePath: string): Promise<CloneRuntimeOwner | null> {
const ownerPath = join(runtimePath, CLONE_RUNTIME_OWNER_FILENAME);
let file: FileHandle | undefined;
try {
const entry = await lstatAsync(ownerPath);
if (!entry.isFile() || entry.size > MAX_CLONE_RUNTIME_OWNER_BYTES) return null;
const noFollow = fsConstants.O_NOFOLLOW ?? 0;
file = await openAsync(ownerPath, fsConstants.O_RDONLY | noFollow);
const opened = await file.stat();
if (!opened.isFile() || opened.size > MAX_CLONE_RUNTIME_OWNER_BYTES) return null;
return parseCloneRuntimeOwner(await file.readFile("utf-8"));
} catch {
return null;
} finally {
if (file !== undefined) {
try {
await file.close();
} catch {
// The handle may already have been closed externally.
}
}
}
}
function processNonexistenceProven(pid: number): boolean {
try {
process.kill(pid, 0);
return false;
} catch (err) {
return Boolean(err && typeof err === "object" && "code" in err && err.code === "ESRCH");
}
}
async function cloneRuntimeOwnerIsDead(owner: CloneRuntimeOwner, bootId: string | null): Promise<boolean> {
if (owner.platform !== process.platform) return false;
if (process.platform !== "linux") {
return processNonexistenceProven(owner.pid);
}
// Linux PID values can be reused. Require both boot ID and proc start time
// before treating a runtime as owned, and preserve it when either check is
// unavailable.
if (!owner.bootId || !owner.startTime || !bootId) return false;
const processInfo = await readLinuxProcessInfoAsync(owner.pid);
if (!processInfo) return false;
if (processInfo.dead || processInfo.state === "Z" || processInfo.state === "X") return true;
return processInfo.startTime !== owner.startTime || bootId !== owner.bootId;
}
async function removeCloneRuntimeAsync(parentPath: string, runtimePath: string): Promise<void> {
const normalizedParentPath = resolvePath(parentPath);
const normalizedRuntimePath = resolvePath(runtimePath);
if (dirname(normalizedRuntimePath) !== normalizedParentPath || !basename(normalizedRuntimePath).startsWith("runtime-")) return;
try {
const realRuntimePath = await realpathAsync(normalizedRuntimePath);
if (dirname(realRuntimePath) !== normalizedParentPath || basename(realRuntimePath) !== basename(normalizedRuntimePath)) return;
const entry = await lstatAsync(normalizedRuntimePath);
if (entry.isSymbolicLink() || !entry.isDirectory()) return;
await rmAsync(normalizedRuntimePath, { recursive: true, force: true });
} catch {
// The runtime directory may already have been removed externally.
}
}
async function sweepStaleCloneRuntimes(parentPath: string): Promise<void> {
const normalizedParentPath = resolvePath(parentPath);
const bootId = process.platform === "linux" ? await readLinuxBootIdAsync() : null;
try {
const directory = await opendirAsync(normalizedParentPath);
for await (const entry of directory) {
if (!entry.name.startsWith("runtime-")) continue;
try {
if (!entry.isDirectory() || entry.isSymbolicLink()) continue;
const runtimePath = resolvePath(normalizedParentPath, entry.name);
if (dirname(runtimePath) !== normalizedParentPath) continue;
const runtimeEntry = await lstatAsync(runtimePath);
if (!runtimeEntry.isDirectory() || runtimeEntry.isSymbolicLink()) continue;
const realRuntimePath = await realpathAsync(runtimePath);
if (dirname(realRuntimePath) !== normalizedParentPath || basename(realRuntimePath) !== basename(runtimePath)) continue;
const owner = await readCloneRuntimeOwner(runtimePath);
if (!owner || !(await cloneRuntimeOwnerIsDead(owner, bootId))) continue;
await removeCloneRuntimeAsync(normalizedParentPath, runtimePath);
} catch {
// A changing or unreadable runtime must not stop the rest of the sweep.
}
}
} catch {
// Cleanup is opportunistic. A changing or unreadable cache must not stop
// GitHub extraction from falling back to the API.
}
}
function startStaleCloneRuntimeCleanup(parentPath: string): void {
const normalizedParentPath = resolvePath(parentPath);
if (startedCloneRuntimeCleanups.has(normalizedParentPath)) return;
startedCloneRuntimeCleanups.add(normalizedParentPath);
// Keep runtime creation synchronous and let the complete sweep run in the background.
void sweepStaleCloneRuntimes(normalizedParentPath).catch(() => {
// Cleanup is best effort and must never affect extraction.
});
}
function removeCloneRuntime(parentPath: string, runtimePath: string): void {
const normalizedParentPath = resolvePath(parentPath);
const normalizedRuntimePath = resolvePath(runtimePath);
if (dirname(normalizedRuntimePath) !== normalizedParentPath || !basename(normalizedRuntimePath).startsWith("runtime-")) return;
try {
const entry = lstatSync(normalizedRuntimePath);
if (entry.isSymbolicLink()) unlinkSync(normalizedRuntimePath);
else rmSync(normalizedRuntimePath, { recursive: true, force: true });
} catch {
// The runtime directory may already have been removed externally.
}
}
function getCloneRuntimeRoot(config: GitHubCloneConfig): string | null {
if (cloneRuntime) return cloneRuntime.rootPath;
let parentPath: string | null = null;
let runtimePath: string | null = null;
try {
const configuredPath = resolvePath(config.clonePath);
mkdirSync(configuredPath, { recursive: true });
parentPath = realpathSync(configuredPath);
startStaleCloneRuntimeCleanup(parentPath);
runtimePath = mkdtempSync(join(parentPath, "runtime-"));
chmodSync(runtimePath, 0o700);
const rootPath = realpathSync(runtimePath);
if (dirname(rootPath) !== parentPath) {
removeCloneRuntime(parentPath, runtimePath);
return null;
}
writeCloneRuntimeOwner(rootPath);
cloneRuntime = { parentPath, rootPath };
return rootPath;
} catch {
if (parentPath && runtimePath) removeCloneRuntime(parentPath, runtimePath);
return null;
}
}
function cloneDestination(config: GitHubCloneConfig, owner: string, repo: string, ref?: string): CloneDestination | null {
try {
const rootPath = getCloneRuntimeRoot(config);
if (!rootPath) return null;
const digest = createHash("sha256").update(JSON.stringify([owner, repo, ref ?? null])).digest("hex");
const localPath = resolvePath(rootPath, digest);
if (dirname(localPath) !== rootPath) return null;
return { rootPath, localPath };
} catch {
return null;
}
}
function removeCloneDestination(destination: CloneDestination): boolean {
const rootPath = resolvePath(destination.rootPath);
const localPath = resolvePath(destination.localPath);
if (dirname(localPath) !== rootPath || !/^[0-9a-f]{64}$/.test(basename(localPath))) return false;
try {
const entry = lstatSync(localPath);
if (entry.isSymbolicLink()) unlinkSync(localPath);
else rmSync(localPath, { recursive: true, force: true });
return true;
} catch (err) {
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") return true;
return false;
}
}
const PROCESS_KILL_GRACE_MS = 3000;
function terminateProcessTree(child: ChildProcess): void {
const pid = child.pid;
if (!pid) return;
if (process.platform === "win32") {
const killer = execFile(
"taskkill",
["/pid", String(pid), "/T", "/F"],
{ windowsHide: true },
(err) => {
if (err) child.kill();
},
);
killer.unref();
return;
}
try {
// Clone commands run in their own process group so git/gh helpers cannot
// survive a timeout or cancellation and keep reading from the host TTY.
process.kill(-pid, "SIGTERM");
} catch {
child.kill();
}
// A credential helper may handle or ignore SIGTERM. Escalate against the
// entire process group so neither git nor any descendant can block forever.
const forceKill = setTimeout(() => {
try {
process.kill(-pid, "SIGKILL");
} catch {
child.kill("SIGKILL");
}
}, PROCESS_KILL_GRACE_MS);
forceKill.unref();
}
function execClone(args: string[], destination: CloneDestination, timeoutMs: number, signal?: AbortSignal): Promise<string | null> {
return new Promise((resolve) => {
const { localPath } = destination;
let settled = false;
let timeout: ReturnType<typeof setTimeout> | undefined;
let onAbort: (() => void) | undefined;
const finish = (success: boolean) => {
if (settled) return;
settled = true;
if (timeout) clearTimeout(timeout);
if (signal && onAbort) signal.removeEventListener("abort", onAbort);
if (!success) {
removeCloneDestination(destination);
resolve(null);
return;
}
resolve(localPath);
};
const child = spawn(args[0], args.slice(1), {
detached: process.platform !== "win32",
env: {
...process.env,
GIT_TERMINAL_PROMPT: "0",
GCM_INTERACTIVE: "Never",
GH_PROMPT_DISABLED: "1",
},
stdio: "ignore",
windowsHide: true,
});
child.once("error", () => finish(false));
child.once("close", (code) => finish(code === 0));
timeout = setTimeout(() => terminateProcessTree(child), timeoutMs);
timeout.unref();
if (signal) {
onAbort = () => {
if (timeout) clearTimeout(timeout);
terminateProcessTree(child);
};
if (signal.aborted) onAbort();
else signal.addEventListener("abort", onAbort, { once: true });
}
});
}
async function cloneRepo(
owner: string,
repo: string,
ref: string | undefined,
config: GitHubCloneConfig,
destination: CloneDestination,
signal?: AbortSignal,
): Promise<string | null> {
const { localPath } = destination;
if (!removeCloneDestination(destination)) return null;
const timeoutMs = config.cloneTimeoutSeconds * 1000;
const hasGh = await checkGhAvailable();
if (hasGh) {
const args = ["gh", "repo", "clone", `${owner}/${repo}`, localPath, "--", "--depth", "1", "--single-branch"];
if (ref) args.push("--branch", ref);
return execClone(args, destination, timeoutMs, signal);
}
showGhHint();
const gitUrl = `https://github.com/${owner}/${repo}.git`;
const args = ["git", "clone", "--depth", "1", "--single-branch"];
if (ref) args.push("--branch", ref);
args.push(gitUrl, localPath);
return execClone(args, destination, timeoutMs, signal);
}
function isBinaryFile(filePath: string): boolean {
const ext = extname(filePath).toLowerCase();
if (BINARY_EXTENSIONS.has(ext)) return true;
let fd: number;
try {
fd = openSync(filePath, "r");
} catch {
return false;
}
try {
const buf = Buffer.alloc(512);
const bytesRead = readSync(fd, buf, 0, 512, 0);
for (let i = 0; i < bytesRead; i++) {
if (buf[i] === 0) return true;
}
} catch {
return false;
} finally {
closeSync(fd);
}
return false;
}
function formatFileSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
function resolveWithinRepo(rootPath: string, relativePath: string): string | null {
const normalizedRoot = resolvePath(rootPath);
const candidate = resolvePath(normalizedRoot, relativePath);
if (candidate !== normalizedRoot) {
const rootPrefix = normalizedRoot.endsWith(pathSep) ? normalizedRoot : normalizedRoot + pathSep;
if (!candidate.startsWith(rootPrefix)) return null;
}
if (!existsSync(candidate)) return candidate;
try {
const realRoot = realpathSync(normalizedRoot);
const realCandidate = realpathSync(candidate);
if (realCandidate === realRoot) return candidate;
const realRootPrefix = realRoot.endsWith(pathSep) ? realRoot : realRoot + pathSep;
return realCandidate.startsWith(realRootPrefix) ? candidate : null;
} catch {
return null;
}
}
function readTextFile(path: string): string | null {
try {
return readFileSync(path, "utf-8");
} catch {
return null;
}
}
function buildTree(rootPath: string): string {
const entries: string[] = [];
function walk(dir: string, relPath: string): void {
if (entries.length >= MAX_TREE_ENTRIES) return;
let items: string[];
try {
items = readdirSync(dir).sort();
} catch {
return;
}
for (const item of items) {
if (entries.length >= MAX_TREE_ENTRIES) return;
if (item === ".git") continue;
const rel = relPath ? `${relPath}/${item}` : item;
const safePath = resolveWithinRepo(rootPath, rel);
if (!safePath) {
entries.push(`${rel} [outside repo skipped]`);
continue;
}
let stat;
try {
stat = statSync(safePath);
} catch {
continue;
}
if (stat.isDirectory()) {
if (NOISE_DIRS.has(item)) {
entries.push(`${rel}/ [skipped]`);
continue;
}
entries.push(`${rel}/`);
walk(safePath, rel);
} else {
entries.push(rel);
}
}
}
walk(rootPath, "");
if (entries.length >= MAX_TREE_ENTRIES) {
entries.push(`... (truncated at ${MAX_TREE_ENTRIES} entries)`);
}
return entries.join("\n");
}
function buildDirListing(rootPath: string, subPath: string): string {
const targetPath = resolveWithinRepo(rootPath, subPath);
if (!targetPath) return "(path escapes repository root)";
const lines: string[] = [];
let items: string[];
try {
items = readdirSync(targetPath).sort();
} catch {
return "(directory not readable)";
}
for (const item of items) {
if (item === ".git") continue;
const rel = subPath ? `${subPath}/${item}` : item;
const safePath = resolveWithinRepo(rootPath, rel);
if (!safePath) {
lines.push(` ${item} (outside repo)`);
continue;
}
try {
const stat = statSync(safePath);
if (stat.isDirectory()) {
lines.push(` ${item}/`);
} else {
lines.push(` ${item} (${formatFileSize(stat.size)})`);
}
} catch {
lines.push(` ${item} (unreadable)`);
}
}
return lines.join("\n");
}
function readReadme(localPath: string): string | null {
const candidates = ["README.md", "readme.md", "README", "README.txt", "README.rst"];
for (const name of candidates) {
const readmePath = join(localPath, name);
if (existsSync(readmePath)) {
try {
const content = readFileSync(readmePath, "utf-8");
return content.length > 8192 ? content.slice(0, 8192) + "\n\n[README truncated at 8K chars]" : content;
} catch {
continue;
}
}
}
return null;
}
function generateContent(localPath: string, info: GitHubUrlInfo): string {
const lines: string[] = [];
lines.push(`Repository cloned to: ${localPath}`);
lines.push("");
if (info.type === "root") {
lines.push("## Structure");
lines.push(buildTree(localPath));
lines.push("");
const readme = readReadme(localPath);
if (readme) {
lines.push("## README.md");
lines.push(readme);
lines.push("");
}
lines.push("Use `read` and `bash` tools at the path above to explore further.");
return lines.join("\n");
}
if (info.type === "tree") {
const dirPath = info.path || "";
const fullDirPath = resolveWithinRepo(localPath, dirPath);
if (!fullDirPath || !existsSync(fullDirPath)) {
lines.push(`Path \`${dirPath}\` not found in clone. Showing repository root instead.`);
lines.push("");
lines.push("## Structure");
lines.push(buildTree(localPath));
} else {
lines.push(`## ${dirPath || "/"}`);
lines.push(buildDirListing(localPath, dirPath));
}
lines.push("");
lines.push("Use `read` and `bash` tools at the path above to explore further.");
return lines.join("\n");
}
if (info.type === "blob") {
const filePath = info.path || "";
const fullFilePath = resolveWithinRepo(localPath, filePath);
if (!fullFilePath || !existsSync(fullFilePath)) {
lines.push(`Path \`${filePath}\` not found in clone. Showing repository root instead.`);
lines.push("");
lines.push("## Structure");
lines.push(buildTree(localPath));
lines.push("");
lines.push("Use `read` and `bash` tools at the path above to explore further.");
return lines.join("\n");
}
let stat: ReturnType<typeof statSync>;
try {
stat = statSync(fullFilePath);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
lines.push(`Could not inspect \`${filePath}\`: ${message}`);
lines.push("");
lines.push("Use `read` and `bash` tools at the path above to explore further.");
return lines.join("\n");
}
if (stat.isDirectory()) {
lines.push(`## ${filePath || "/"}`);
lines.push(buildDirListing(localPath, filePath));
lines.push("");
lines.push("Use `read` and `bash` tools at the path above to explore further.");
return lines.join("\n");
}
if (isBinaryFile(fullFilePath)) {
const ext = extname(filePath).replace(".", "");
lines.push(`## ${filePath}`);
lines.push(`Binary file (${ext}, ${formatFileSize(stat.size)}). Use \`read\` or \`bash\` tools at the path above to inspect.`);
return lines.join("\n");
}
const content = readTextFile(fullFilePath);
if (content === null) {
lines.push(`Could not read \`${filePath}\` as UTF-8 text.`);
lines.push("");
lines.push("Use `read` and `bash` tools at the path above to explore further.");
return lines.join("\n");
}
lines.push(`## ${filePath}`);
if (content.length > MAX_INLINE_FILE_CHARS) {
lines.push(content.slice(0, MAX_INLINE_FILE_CHARS));
lines.push("");
lines.push(`[File truncated at 100K chars. Full file: ${fullFilePath}]`);
} else {
lines.push(content);
}
lines.push("");
lines.push("Use `read` and `bash` tools at the path above to explore further.");
return lines.join("\n");
}
return lines.join("\n");
}
async function awaitCachedClone(
cached: CachedClone,
url: string,
owner: string,
repo: string,
info: GitHubUrlInfo,
signal?: AbortSignal,
): Promise<ExtractedContent | null> {
if (signal?.aborted) return null;
const result = await cached.clonePromise;
if (signal?.aborted) return null;
if (result) {
const content = generateContent(result, info);
const title = info.path ? `${owner}/${repo} - ${info.path}` : `${owner}/${repo}`;
return { url, title, content, error: null };
}
return fetchViaApi(url, owner, repo, info);
}
export async function extractGitHub(
url: string,
signal?: AbortSignal,
forceClone?: boolean,
): Promise<ExtractedContent | null> {
const info = parseGitHubUrl(url);
if (!info) return null;
if (signal?.aborted) return null;
const config = loadGitHubConfig();
if (!config.enabled) return null;
const { owner, repo } = info;
const key = cacheKey(owner, repo, info.ref);
const cached = cloneCache.get(key);
if (cached) {
cached.lastUsedAt = Date.now();
return awaitCachedClone(cached, url, owner, repo, info, signal);
}
if (info.refIsFullSha) {
if (signal?.aborted) return null;
const sizeNote = `Note: Commit SHA URLs use the GitHub API instead of cloning.`;
return fetchViaApi(url, owner, repo, info, sizeNote);
}
const activityId = activityMonitor.logStart({ type: "fetch", url: `github.com/${owner}/${repo}` });
if (!forceClone) {
const sizeKB = await checkRepoSize(owner, repo);
if (signal?.aborted) {
activityMonitor.logComplete(activityId, 0);
return null;
}
if (sizeKB !== null) {
const sizeMB = sizeKB / 1024;
if (sizeMB > config.maxRepoSizeMB) {
if (signal?.aborted) {
activityMonitor.logComplete(activityId, 0);
return null;
}
const sizeNote =
`Note: Repository is ${Math.round(sizeMB)}MB (threshold: ${config.maxRepoSizeMB}MB). ` +
`Showing API-fetched content instead of full clone. Ask the user if they'd like to clone the full repo -- ` +
`if yes, call fetch_content again with the same URL and add forceClone: true to the params.`;
const apiView = await fetchViaApi(url, owner, repo, info, sizeNote);
if (apiView) {
activityMonitor.logComplete(activityId, 200);
return apiView;
}
activityMonitor.logError(activityId, "api fallback unavailable for oversized repository");
return null;
}
}
}
if (signal?.aborted) {
activityMonitor.logComplete(activityId, 0);
return null;
}
// Re-check: another concurrent caller may have started a clone while we awaited the size check
const cachedAfterSizeCheck = cloneCache.get(key);
if (cachedAfterSizeCheck) {
cachedAfterSizeCheck.lastUsedAt = Date.now();
const cachedResult = await awaitCachedClone(cachedAfterSizeCheck, url, owner, repo, info, signal);
if (signal?.aborted) {
activityMonitor.logComplete(activityId, 0);
} else if (cachedResult) {
activityMonitor.logComplete(activityId, 200);
} else {
activityMonitor.logError(activityId, "clone failed");
}
return cachedResult;
}
const destination = cloneDestination(config, owner, repo, info.ref);
if (!destination) {
const apiFallback = await fetchViaApi(url, owner, repo, info);
if (apiFallback) activityMonitor.logComplete(activityId, 200);
else activityMonitor.logError(activityId, "invalid clone destination");
return apiFallback;
}
const clonePromise = cloneRepo(owner, repo, info.ref, config, destination, signal);
const entry: CachedClone = { destination, clonePromise, lastUsedAt: Date.now(), settled: false };
cloneCache.set(key, entry);
void clonePromise.finally(() => { entry.settled = true; });
const result = await clonePromise;
if (signal?.aborted) {