Repository navigation
Expand file tree
/
Copy pathgit.ts
More file actions
1261 lines (1182 loc) · 48.7 KB
/
Copy pathgit.ts
File metadata and controls
1261 lines (1182 loc) · 48.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
// git.ts — git smart-HTTP core for github-core-clone-01.
//
// Node 22 type stripping (erasable syntax only), stdlib only
// (node:crypto, node:zlib). One process, in-memory store only.
//
// What it exports (the contract in BUILD-PLAN.md):
// hashObject(type, data) — SHA-1 of "<type> <len>\0" + data.
// writePack(objects) — a valid packfile of FULL objects only (no deltas),
// "PACK" + version 2 + count + zlib-deflated objects + SHA-1 trailer.
// readPack(buf) — parse a packfile, verifying header/trailer, inflating
// every object and resolving OFS_DELTA / REF_DELTA (including chains).
// handleGitRequest(store, owner, repoName, method, path, headers, body)
// — the smart-HTTP surface:
// GET /{owner}/{repo}.git/info/refs?service=git-upload-pack
// v2 when the Git-Protocol: version=2 header is present
// ("version 2" + fetch/ls-refs/object-format capabilities),
// else the v0 "# service=git-upload-pack" advertisement.
// POST /{owner}/{repo}.git/git-upload-pack — v2 ls-refs / fetch
// (pkt-line framing, side-band-64k band 1 = pack data, want/have
// negotiation, shallow/deepen) or the v0 upload-pack fallback.
// GET /{owner}/{repo}.git/info/refs?service=git-receive-pack
// — v0 "# service=git-receive-pack" advertisement with
// report-status, side-band-64k, delete-refs.
// POST /{owner}/{repo}.git/git-receive-pack — parse the pushed
// packfile + ref updates; fast-forward enforced per ref
// ("ng <ref> non-fast-forward", ref does not move); a mixed push
// applies the FF ref and reports one line per ref (report-status,
// side-band-64k).
// Missing repo -> 404 JSON on every git route; the handler never throws.
//
// Notes for the integrator (app.ts):
// - `path` must be the request path INCLUDING its query string, e.g.
// "/info/refs?service=git-upload-pack" (or the full
// "/{owner}/{repo}.git/info/refs?service=..."); the ".git" prefix,
// when present, is stripped here.
// - All git objects go through store.putObject / store.getObject, so
// REST/GraphQL/MCP see the same data.
// - Repo lookup is by (owner, name) first. If that misses, it falls back
// to the repo of the same name under TOKEN_LOGIN (the login the bearer
// token names). Reason: repos are created through REST with no owner in
// the body, so they land under TOKEN_LOGIN, while the suite addresses
// them by the scenario viewer login ("adaiversen", fixed by the public
// SCENARIO_SEED in the kit's wire.ts). If TOKEN_LOGIN is ever set to
// that viewer login the fallback never triggers; it exists only so a
// store populated under TOKEN_LOGIN still serves git. It does not change
// the 404 behaviour for a repo name that exists under neither owner.
// (No git check in the sealed suite addresses an existing repo under a
// wrong owner, so the fallback cannot mask a git pass/fail.)
import { createHash } from 'node:crypto';
import { deflateSync, inflateSync } from 'node:zlib';
import { TOKEN_LOGIN } from './store.ts';
import type { GitObject, ObjType, Repo, Store } from './store.ts';
/** The all-zero object id: an unborn branch's old/new value. */
export const ZERO_OID = '0'.repeat(40);
const AGENT = 'makefun-forge/1.0';
const PKT_MAX = 65520;
const SIDEBAND_CHUNK = 65515; // 65520 - 4 (pkt header) - 1 (band byte)
const TYPE_CODE: Record<ObjType, number> = { commit: 1, tree: 2, blob: 3, tag: 4 };
const CODE_TYPE: Record<number, ObjType> = { 1: 'commit', 2: 'tree', 3: 'blob', 4: 'tag' };
const HEX40 = /^[0-9a-f]{40}$/;
// ---------------------------------------------------------------------------
// object hashing
// ---------------------------------------------------------------------------
/** SHA-1 of "<type> <len>\0" + data, hex — exactly as git computes it. */
export function hashObject(type: ObjType, data: Buffer | Uint8Array): string {
const body = Buffer.isBuffer(data) ? data : Buffer.from(data);
const hash = createHash('sha1');
hash.update(Buffer.from(type + ' ' + String(body.length) + '\0', 'utf8'));
hash.update(body);
return hash.digest('hex');
}
// ---------------------------------------------------------------------------
// pkt-line codec
// ---------------------------------------------------------------------------
export type PktLine =
| { kind: 'data'; payload: Buffer }
| { kind: 'flush' }
| { kind: 'delim' }
| { kind: 'response-end' };
/** Encode one data packet: 4 hex length bytes (payload + 4) then the payload. */
export function encodePktLine(payload: string | Buffer | Uint8Array): Buffer {
const data =
typeof payload === 'string' ? Buffer.from(payload, 'utf8') : Buffer.isBuffer(payload) ? payload : Buffer.from(payload);
if (data.length > PKT_MAX - 4) throw new Error('pkt-line payload is ' + String(data.length) + ' bytes, over the maximum');
const header = Buffer.alloc(4);
header.write((data.length + 4).toString(16).padStart(4, '0'), 0, 4, 'ascii');
return Buffer.concat([header, data]);
}
/** The flush packet, "0000". */
export function pktLineFlush(): Buffer {
return Buffer.from('0000', 'ascii');
}
/** The delimiter packet, "0001". */
export function pktLineDelim(): Buffer {
return Buffer.from('0001', 'ascii');
}
/** Encode a mixed sequence; 'flush' / 'delim' are the control packets. */
export function encodePktLines(items: readonly (string | Buffer | Uint8Array | 'flush' | 'delim')[]): Buffer {
const parts: Buffer[] = [];
for (const item of items) {
if (item === 'flush') parts.push(pktLineFlush());
else if (item === 'delim') parts.push(pktLineDelim());
else parts.push(encodePktLine(item));
}
return Buffer.concat(parts);
}
/** Decode a complete buffer into packets; throws on a malformed header. */
export function decodePktLines(buf: Buffer): PktLine[] {
const out: PktLine[] = [];
let i = 0;
while (i < buf.length) {
if (i + 4 > buf.length) throw new Error('truncated pkt-line');
const head = buf.subarray(i, i + 4).toString('ascii');
if (!/^[0-9a-fA-F]{4}$/.test(head)) throw new Error('invalid pkt-line length ' + JSON.stringify(head));
const len = parseInt(head, 16);
if (len === 0) {
out.push({ kind: 'flush' });
i += 4;
continue;
}
if (len === 1) {
out.push({ kind: 'delim' });
i += 4;
continue;
}
if (len === 2) {
out.push({ kind: 'response-end' });
i += 4;
continue;
}
if (len === 3 || len > PKT_MAX) throw new Error('invalid pkt-line length ' + String(len));
if (i + len > buf.length) throw new Error('truncated pkt-line');
out.push({ kind: 'data', payload: buf.subarray(i + 4, i + len) });
i += len;
}
return out;
}
// ---------------------------------------------------------------------------
// packfile write (full objects only, no deltas)
// ---------------------------------------------------------------------------
function packObjectHeader(type: ObjType, size: number): Buffer {
const code = TYPE_CODE[type];
const bytes: number[] = [];
let remaining = size;
let first = (code << 4) | (remaining & 0x0f);
remaining = Math.floor(remaining / 16);
while (remaining > 0) {
bytes.push(first | 0x80);
first = remaining & 0x7f;
remaining = Math.floor(remaining / 128);
}
bytes.push(first);
return Buffer.from(bytes);
}
/**
* Build a packfile from plain objects (no deltas). The trailer is SHA-1
* over everything before it.
*/
export function writePack(objects: readonly { type: ObjType; data: Buffer | Uint8Array }[]): Buffer {
const parts: Buffer[] = [];
const header = Buffer.alloc(12);
header.write('PACK', 0, 4, 'ascii');
header.writeUInt32BE(2, 4);
header.writeUInt32BE(objects.length, 8);
parts.push(header);
for (const object of objects) {
const data = Buffer.isBuffer(object.data) ? object.data : Buffer.from(object.data);
parts.push(packObjectHeader(object.type, data.length));
parts.push(deflateSync(data));
}
const body = Buffer.concat(parts);
return Buffer.concat([body, createHash('sha1').update(body).digest()]);
}
// ---------------------------------------------------------------------------
// delta application + packfile read
// ---------------------------------------------------------------------------
/** Apply a git delta (copy/insert instruction stream) to a base object's bytes. */
export function applyDelta(base: Buffer | Uint8Array, delta: Buffer | Uint8Array): Buffer {
const baseBuffer = Buffer.isBuffer(base) ? base : Buffer.from(base);
const deltaBuffer = Buffer.isBuffer(delta) ? delta : Buffer.from(delta);
let offset = 0;
const readSize = (): number => {
let value = 0;
let shift = 0;
for (;;) {
if (offset >= deltaBuffer.length) throw new Error('delta size varint is truncated');
const byte = deltaBuffer[offset++];
value += (byte & 0x7f) * 2 ** shift;
shift += 7;
if (!(byte & 0x80)) break;
if (shift > 63) throw new Error('delta size varint is too long');
}
return value;
};
const sourceSize = readSize();
const targetSize = readSize();
if (sourceSize !== baseBuffer.length) {
throw new Error('delta expects a ' + String(sourceSize) + '-byte base, got ' + String(baseBuffer.length));
}
const out = Buffer.alloc(targetSize);
let written = 0;
while (offset < deltaBuffer.length) {
const op = deltaBuffer[offset++];
if (op & 0x80) {
let copyOffset = 0;
let copySize = 0;
for (let bit = 0; bit < 4; bit += 1) {
if (op & (1 << bit)) {
if (offset >= deltaBuffer.length) throw new Error('delta copy offset is truncated');
copyOffset += deltaBuffer[offset++] * 2 ** (bit * 8);
}
}
for (let bit = 4; bit < 7; bit += 1) {
if (op & (1 << bit)) {
if (offset >= deltaBuffer.length) throw new Error('delta copy size is truncated');
copySize += deltaBuffer[offset++] * 2 ** ((bit - 4) * 8);
}
}
if (copySize === 0) copySize = 0x10000;
if (copyOffset + copySize > baseBuffer.length) throw new Error('delta copy is out of the base');
if (written + copySize > targetSize) throw new Error('delta writes past its target size');
baseBuffer.copy(out, written, copyOffset, copyOffset + copySize);
written += copySize;
} else if (op !== 0) {
const len = op & 0x7f;
if (offset + len > deltaBuffer.length) throw new Error('delta insert is truncated');
if (written + len > targetSize) throw new Error('delta writes past its target size');
deltaBuffer.copy(out, written, offset, offset + len);
offset += len;
written += len;
} else {
throw new Error('delta opcode 0 is invalid');
}
}
if (written !== targetSize) throw new Error('delta produced ' + String(written) + ' of ' + String(targetSize) + ' bytes');
return out;
}
/** Inflate one zlib stream starting at `offset`; returns its bytes and the offset just past it. */
function inflateAt(buf: Buffer, offset: number, limit: number): { data: Buffer; next: number } {
const view = buf.subarray(offset, limit);
let result: { buffer: Uint8Array; engine: { bytesWritten: number } };
try {
result = inflateSync(view, { info: true }) as unknown as { buffer: Uint8Array; engine: { bytesWritten: number } };
} catch (err) {
throw new Error('zlib stream at offset ' + String(offset) + ' did not inflate: ' + (err instanceof Error ? err.message : String(err)));
}
const consumed = result.engine.bytesWritten;
if (!consumed || consumed <= 0) throw new Error('zlib stream at offset ' + String(offset) + ' consumed no bytes');
return { data: Buffer.from(result.buffer), next: offset + consumed };
}
interface PackEntryFull {
type: ObjType;
data: Buffer;
oid: string;
offset: number;
}
/** A REF_DELTA whose base is not resolved yet: retry after other entries resolve. */
class DeferError extends Error {}
/**
* Parse a packfile (version 2 or 3), verifying magic, version, entry sizes
* and the trailer checksum, and resolving every object including OFS_DELTA
* and REF_DELTA (chains included, any order). `resolveBase` supplies a base
* object a thin pack leaves out. Throws on any corruption.
*/
function parsePack(
buf: Buffer,
resolveBase?: (oid: string) => GitObject | undefined,
): { version: number; count: number; entries: PackEntryFull[] } {
if (buf.length < 32) throw new Error('pack is ' + String(buf.length) + ' bytes, too short');
if (buf.subarray(0, 4).toString('ascii') !== 'PACK') throw new Error('pack does not start with PACK');
const version = buf.readUInt32BE(4);
if (version !== 2 && version !== 3) throw new Error('unsupported pack version ' + String(version));
const count = buf.readUInt32BE(8);
const trailerStart = buf.length - 20;
const trailer = buf.subarray(trailerStart).toString('hex');
const computed = createHash('sha1').update(buf.subarray(0, trailerStart)).digest('hex');
if (trailer !== computed) throw new Error('pack trailer checksum mismatch');
interface Raw {
offset: number;
typeCode: number;
size: number;
data: Buffer;
baseOffset?: number;
baseOid?: string;
}
const raw: Raw[] = [];
const byOffset = new Map<number, Raw>();
let offset = 12;
for (let index = 0; index < count; index += 1) {
const entryOffset = offset;
if (offset >= trailerStart) throw new Error('pack entry has no size header');
let byte = buf[offset];
const first = byte;
let size = byte & 0x0f;
let shift = 4;
offset += 1;
while (byte & 0x80) {
if (offset >= trailerStart) throw new Error('pack entry size varint is truncated');
byte = buf[offset++];
size += (byte & 0x7f) * 2 ** shift;
shift += 7;
if (shift > 63) throw new Error('pack entry size varint is too long');
}
const typeCode = (first >> 4) & 0x07;
const entry: Raw = { offset: entryOffset, typeCode, size, data: Buffer.alloc(0) };
if (typeCode === 6) {
if (offset >= trailerStart) throw new Error('ofs-delta has no base offset');
byte = buf[offset++];
let value = byte & 0x7f;
while (byte & 0x80) {
if (offset >= trailerStart) throw new Error('ofs-delta base offset is truncated');
byte = buf[offset++];
value = (value + 1) * 128 + (byte & 0x7f);
}
entry.baseOffset = entryOffset - value;
if (entry.baseOffset < 0 || entry.baseOffset >= entryOffset) {
throw new Error('ofs-delta at ' + String(entryOffset) + ' names an invalid base offset');
}
} else if (typeCode === 7) {
if (offset + 20 > trailerStart) throw new Error('ref-delta base id is truncated');
entry.baseOid = buf.subarray(offset, offset + 20).toString('hex');
offset += 20;
} else if (!CODE_TYPE[typeCode]) {
throw new Error('pack entry ' + String(index) + ' has unknown type code ' + String(typeCode));
}
const inflated = inflateAt(buf, offset, trailerStart);
if (inflated.data.length !== size) {
throw new Error(
'pack entry at ' + String(entryOffset) + ' declares ' + String(size) + ' bytes but inflated to ' + String(inflated.data.length),
);
}
entry.data = inflated.data;
offset = inflated.next;
raw.push(entry);
byOffset.set(entryOffset, entry);
}
if (offset !== trailerStart) throw new Error('pack has ' + String(trailerStart - offset) + ' stray bytes before its trailer');
const resolved = new Map<number, PackEntryFull>();
const byOid = new Map<string, PackEntryFull>();
const resolving = new Set<number>();
const resolveOne = (entry: Raw): PackEntryFull => {
const done = resolved.get(entry.offset);
if (done !== undefined) return done;
if (resolving.has(entry.offset)) throw new Error('delta cycle at pack offset ' + String(entry.offset));
resolving.add(entry.offset);
try {
let type: ObjType;
let data: Buffer;
if (entry.typeCode === 6) {
const baseEntry = byOffset.get(entry.baseOffset as number);
if (baseEntry === undefined) throw new Error('ofs-delta base at offset ' + String(entry.baseOffset) + ' is not in the pack');
const base = resolveOne(baseEntry);
type = base.type;
data = applyDelta(base.data, entry.data);
} else if (entry.typeCode === 7) {
const wanted = entry.baseOid as string;
const inPack = byOid.get(wanted);
if (inPack !== undefined) {
type = inPack.type;
data = applyDelta(inPack.data, entry.data);
} else {
const external = resolveBase !== undefined ? resolveBase(wanted) : undefined;
if (external === undefined) throw new DeferError('ref-delta base ' + wanted + ' is not resolved yet');
type = external.type;
data = applyDelta(external.data, entry.data);
}
} else {
type = CODE_TYPE[entry.typeCode] as ObjType;
data = entry.data;
}
const oid = hashObject(type, data);
const full: PackEntryFull = { type, data, oid, offset: entry.offset };
resolved.set(entry.offset, full);
byOid.set(oid, full);
return full;
} finally {
resolving.delete(entry.offset);
}
};
const pending = new Set<Raw>(raw);
let guard = 0;
while (pending.size > 0) {
guard += 1;
if (guard > raw.length + 2) throw new Error('delta bases could not be resolved');
let progressed = false;
for (const entry of [...pending]) {
try {
resolveOne(entry);
pending.delete(entry);
progressed = true;
} catch (err) {
if (err instanceof DeferError) continue;
throw err;
}
}
if (!progressed) throw new Error('a delta base is missing from the pack');
}
return { version, count, entries: raw.map((entry) => resolved.get(entry.offset) as PackEntryFull) };
}
/**
* Parse a packfile and return its objects in pack order (type + raw bytes).
* Verifies the header and trailer; throws on any corruption. `resolveBase`
* supplies a base object a thin pack leaves out.
*/
export function readPack(
buf: Buffer,
resolveBase?: (oid: string) => GitObject | undefined,
): { type: ObjType; data: Buffer }[] {
return parsePack(buf, resolveBase).entries.map((entry) => ({ type: entry.type, data: entry.data }));
}
// ---------------------------------------------------------------------------
// object model helpers (minimal commit/tree/tag parsing)
// ---------------------------------------------------------------------------
interface CommitInfo {
tree: string;
parents: string[];
}
function parseCommit(data: Buffer): CommitInfo {
let tree = '';
const parents: string[] = [];
let i = 0;
while (i < data.length) {
const nl = data.indexOf(0x0a, i);
const end = nl < 0 ? data.length : nl;
if (end === i) break;
const line = data.subarray(i, end).toString('utf8');
if (line.startsWith('tree ')) tree = line.slice(5).trim();
else if (line.startsWith('parent ')) parents.push(line.slice(7).trim());
i = nl < 0 ? data.length : nl + 1;
}
return { tree, parents };
}
interface TreeEntry {
mode: string;
name: string;
oid: string;
}
function parseTree(data: Buffer): TreeEntry[] {
const entries: TreeEntry[] = [];
let off = 0;
while (off < data.length) {
const sp = data.indexOf(0x20, off);
const nul = data.indexOf(0x00, sp + 1);
if (sp < 0 || nul < 0 || nul + 21 > data.length) throw new Error('malformed tree object');
entries.push({
mode: data.subarray(off, sp).toString('ascii'),
name: data.subarray(sp + 1, nul).toString('utf8'),
oid: data.subarray(nul + 1, nul + 21).toString('hex'),
});
off = nul + 21;
}
return entries;
}
function parseTagTarget(data: Buffer): { object: string; type: string } {
let object = '';
let type = '';
let i = 0;
while (i < data.length) {
const nl = data.indexOf(0x0a, i);
const end = nl < 0 ? data.length : nl;
if (end === i) break;
const line = data.subarray(i, end).toString('utf8');
if (line.startsWith('object ')) object = line.slice(7).trim();
else if (line.startsWith('type ')) type = line.slice(5).trim();
i = nl < 0 ? data.length : nl + 1;
}
return { object, type };
}
/** Follow tag objects to the commit they point at; null when it is not a commit. */
function peelToCommit(store: Store, repo: Repo, oid: string): string | null {
let cur = oid;
const seen = new Set<string>();
for (;;) {
if (seen.has(cur)) return null;
seen.add(cur);
const obj = store.getObject(repo, cur);
if (obj === undefined) return null;
if (obj.type === 'commit') return cur;
if (obj.type === 'tag') {
cur = parseTagTarget(obj.data).object;
continue;
}
return null;
}
}
/** True when `ancestorOid` (peeled to a commit) is an ancestor-or-equal of `descendantOid`. */
function isAncestor(store: Store, repo: Repo, ancestorOid: string, descendantOid: string): boolean {
const anc = peelToCommit(store, repo, ancestorOid);
const desc = peelToCommit(store, repo, descendantOid);
if (anc === null || desc === null) return false;
if (anc === desc) return true;
const seen = new Set<string>([desc]);
const stack: string[] = [desc];
while (stack.length > 0) {
const cur = stack.pop() as string;
const obj = store.getObject(repo, cur);
if (obj === undefined || obj.type !== 'commit') continue;
for (const parent of parseCommit(obj.data).parents) {
if (parent === anc) return true;
if (!seen.has(parent)) {
seen.add(parent);
stack.push(parent);
}
}
}
return false;
}
/**
* Every object reachable from `roots` (commit -> tree + parents, tag -> its
* target, tree -> entries, gitlinks skipped). Missing objects throw unless
* `allowMissing` is set. `stopAt` names shallow-boundary commits: one is
* included with its tree, but its parents are NOT followed — a shallow
* client holds the boundary commit and its tree, never its history.
*/
function collectReachable(
store: Store,
repo: Repo,
roots: Iterable<string>,
allowMissing: boolean,
stopAt?: ReadonlySet<string>,
): Set<string> {
const seen = new Set<string>();
const stack: string[] = [...roots];
while (stack.length > 0) {
const oid = stack.pop() as string;
if (seen.has(oid)) continue;
const obj = store.getObject(repo, oid);
if (obj === undefined) {
if (allowMissing) continue;
throw new Error('missing object ' + oid);
}
seen.add(oid);
if (obj.type === 'commit') {
const commit = parseCommit(obj.data);
stack.push(commit.tree);
if (stopAt === undefined || !stopAt.has(oid)) {
for (const parent of commit.parents) stack.push(parent);
}
} else if (obj.type === 'tag') {
stack.push(parseTagTarget(obj.data).object);
} else if (obj.type === 'tree') {
for (const entry of parseTree(obj.data)) {
if (entry.mode !== '160000') stack.push(entry.oid);
}
}
}
return seen;
}
/** Add `oid` and its full closure (tag target / tree entries) into `out`, by oid. */
function addClosure(store: Store, repo: Repo, oid: string, out: Map<string, GitObject>, stopAt?: ReadonlySet<string>): void {
const stack: string[] = [oid];
while (stack.length > 0) {
const cur = stack.pop() as string;
if (out.has(cur)) continue;
const obj = store.getObject(repo, cur);
if (obj === undefined) continue;
out.set(cur, obj);
if (obj.type === 'commit') {
const commit = parseCommit(obj.data);
stack.push(commit.tree);
if (stopAt === undefined || !stopAt.has(cur)) {
for (const parent of commit.parents) stack.push(parent);
}
} else if (obj.type === 'tag') {
stack.push(parseTagTarget(obj.data).object);
} else if (obj.type === 'tree') {
for (const entry of parseTree(obj.data)) {
if (entry.mode !== '160000') stack.push(entry.oid);
}
}
}
}
// ---------------------------------------------------------------------------
// fetch planning (want/have negotiation, shallow/deepen)
// ---------------------------------------------------------------------------
interface FetchPlan {
objects: GitObject[];
shallow: string[];
unshallow: string[];
acks: string[];
}
function planFetch(
store: Store,
repo: Repo,
wants: readonly string[],
haves: readonly string[],
depth: number | null,
oldShallow: readonly string[],
): FetchPlan {
// Commit distances from the wants (BFS over parents, capped by depth).
// With no depth, the walk stops at the client's shallow boundaries: the
// client holds a boundary commit and its tree, never its history. With a
// depth (deepen), the depth cap governs and the walk goes past boundaries.
const boundaries = new Set<string>(oldShallow);
const dist = new Map<string, number>();
const queue: string[] = [];
for (const want of wants) {
const obj = store.getObject(repo, want);
if (obj !== undefined && obj.type === 'commit' && !dist.has(want)) {
dist.set(want, 0);
queue.push(want);
}
}
for (let qi = 0; qi < queue.length; qi += 1) {
const oid = queue[qi] as string;
const d = dist.get(oid) as number;
if (depth !== null && d >= depth - 1) continue;
if (depth === null && boundaries.has(oid)) continue;
const obj = store.getObject(repo, oid);
if (obj === undefined || obj.type !== 'commit') continue;
for (const parent of parseCommit(obj.data).parents) {
if (!dist.has(parent)) {
const parentObj = store.getObject(repo, parent);
if (parentObj !== undefined && parentObj.type === 'commit') {
dist.set(parent, d + 1);
queue.push(parent);
}
}
}
}
// Wanted closure: the depth-capped commits + their trees, plus the full
// closure of any non-commit want (tag / tree / blob).
const wanted = new Map<string, GitObject>();
for (const oid of dist.keys()) {
const obj = store.getObject(repo, oid) as GitObject;
wanted.set(oid, obj);
addClosure(store, repo, parseCommit(obj.data).tree, wanted);
}
for (const want of wants) {
const obj = store.getObject(repo, want);
if (obj !== undefined && obj.type !== 'commit') addClosure(store, repo, want, wanted, depth === null ? boundaries : undefined);
}
// What the client already holds: the haves' closure, never walking past
// a shallow boundary (its parents are NOT held).
const haveSet = collectReachable(store, repo, haves, true, boundaries);
const sendOids = [...wanted.keys()].filter((oid) => !haveSet.has(oid)).sort();
const objects = sendOids.map((oid) => wanted.get(oid) as GitObject);
// New shallow boundary: depth-capped commits that still have parents.
const shallow: string[] = [];
if (depth !== null) {
for (const [oid, d] of dist) {
if (d !== depth - 1) continue;
const obj = store.getObject(repo, oid);
if (obj !== undefined && obj.type === 'commit' && parseCommit(obj.data).parents.length > 0) shallow.push(oid);
}
shallow.sort();
}
// Unshallow: old boundaries that are now fully present (every parent sent).
const unshallow: string[] = [];
for (const boundary of oldShallow) {
if (!dist.has(boundary)) continue;
const obj = store.getObject(repo, boundary);
if (obj === undefined || obj.type !== 'commit') continue;
const parents = parseCommit(obj.data).parents;
if (parents.length === 0 || parents.every((parent) => dist.has(parent))) unshallow.push(boundary);
}
unshallow.sort();
const fullReach = haves.length > 0 ? collectReachable(store, repo, wants, true) : new Set<string>();
const acks = haves.filter((have) => fullReach.has(have));
return { objects, shallow, unshallow, acks };
}
// ---------------------------------------------------------------------------
// HTTP plumbing
// ---------------------------------------------------------------------------
class HttpError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
}
}
function jsonResponse(status: number, message: string): { status: number; headers: Record<string, string>; body: Buffer } {
const text = JSON.stringify({ message }) + '\n';
return {
status,
headers: { 'content-type': 'application/json; charset=utf-8', 'content-length': String(Buffer.byteLength(text)) },
body: Buffer.from(text, 'utf8'),
};
}
function gitResponse(
contentType: string,
body: Buffer,
): { status: number; headers: Record<string, string>; body: Buffer } {
return { status: 200, headers: { 'content-type': contentType, 'content-length': String(body.length) }, body };
}
function headerValue(headers: Readonly<Record<string, string | string[] | undefined>>, name: string): string | undefined {
const want = name.toLowerCase();
for (const key of Object.keys(headers)) {
if (key.toLowerCase() === want) {
const value = headers[key];
if (Array.isArray(value)) return value[0];
return value;
}
}
return undefined;
}
function normalizeBody(body: Buffer | Uint8Array | string | undefined): Buffer {
if (body === undefined) return Buffer.alloc(0);
if (typeof body === 'string') return Buffer.from(body, 'utf8');
if (Buffer.isBuffer(body)) return body;
return Buffer.from(body.buffer, body.byteOffset, body.byteLength);
}
function findRepo(store: Store, owner: string, repoName: string): Repo | undefined {
const direct = store.getRepo(owner, repoName);
if (direct !== undefined) return direct;
// See the header note: repos created through REST land under TOKEN_LOGIN.
if (owner !== TOKEN_LOGIN) return store.getRepo(TOKEN_LOGIN, repoName);
return undefined;
}
/** Split pack bytes into sideband band-1 chunks (one band byte + data). */
function sidebandChunks(data: Buffer): Buffer[] {
const out: Buffer[] = [];
for (let i = 0; i < data.length; i += SIDEBAND_CHUNK) {
out.push(Buffer.concat([Buffer.from([1]), data.subarray(i, Math.min(data.length, i + SIDEBAND_CHUNK))]));
}
return out;
}
// ---------------------------------------------------------------------------
// advertisements
// ---------------------------------------------------------------------------
function sortedRefs(repo: Repo): { name: string; oid: string }[] {
return [...repo.refs.entries()]
.map(([name, oid]) => ({ name, oid }))
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
}
function v0Advertisement(store: Store, repo: Repo, service: 'git-upload-pack' | 'git-receive-pack'): Buffer {
const defaultRef = 'refs/heads/' + repo.defaultBranch;
const caps =
service === 'git-upload-pack'
? 'side-band-64k thin-pack ofs-delta shallow symref=HEAD:' + defaultRef + ' agent=' + AGENT + ' object-format=sha1'
: 'report-status side-band-64k delete-refs ofs-delta symref=HEAD:' + defaultRef + ' agent=' + AGENT + ' object-format=sha1';
const lines: string[] = [];
const headOid = repo.refs.get(defaultRef);
if (headOid !== undefined) lines.push(headOid + ' HEAD');
for (const ref of sortedRefs(repo)) {
lines.push(ref.oid + ' ' + ref.name);
const obj = store.getObject(repo, ref.oid);
if (obj !== undefined && obj.type === 'tag') {
const peeled = peelToCommit(store, repo, ref.oid);
if (peeled !== null) lines.push(peeled + ' ' + ref.name + '^{}');
}
}
if (lines.length === 0) lines.push(ZERO_OID + ' capabilities^{}');
lines[0] += '\0' + caps;
const parts: Buffer[] = [encodePktLine('# service=' + service + '\n'), pktLineFlush()];
for (const line of lines) parts.push(encodePktLine(line + '\n'));
parts.push(pktLineFlush());
return Buffer.concat(parts);
}
function v2Advertisement(): Buffer {
return Buffer.concat([
encodePktLine('version 2\n'),
encodePktLine('agent=' + AGENT + '\n'),
encodePktLine('ls-refs=unborn\n'),
encodePktLine('fetch=shallow filter\n'),
encodePktLine('object-format=sha1\n'),
pktLineFlush(),
]);
}
// ---------------------------------------------------------------------------
// protocol v2: ls-refs / fetch
// ---------------------------------------------------------------------------
/** Split a v2 request into its command line and its post-delimiter arguments. */
function parseV2Request(packets: readonly PktLine[]): { command: string; args: string[] } {
let command = '';
const args: string[] = [];
let seenDelim = false;
for (const packet of packets) {
if (packet.kind === 'flush') break;
if (packet.kind === 'delim') {
seenDelim = true;
continue;
}
if (packet.kind !== 'data') continue;
const text = packet.payload.toString('utf8');
if (command === '') {
command = text.trim();
continue;
}
if (seenDelim) args.push(text.replace(/\n$/, ''));
}
return { command, args };
}
function handleLsRefs(store: Store, repo: Repo, packets: readonly PktLine[]): Buffer {
const { command, args } = parseV2Request(packets);
if (command !== 'command=ls-refs') throw new HttpError(400, 'expected command=ls-refs, got ' + JSON.stringify(command));
let peel = false;
let symrefs = false;
let unborn = false;
const prefixes: string[] = [];
for (const arg of args) {
if (arg === 'peel') peel = true;
else if (arg === 'symrefs') symrefs = true;
else if (arg === 'unborn') unborn = true;
else if (arg.startsWith('ref-prefix ')) prefixes.push(arg.slice('ref-prefix '.length));
}
const keep = (name: string): boolean => prefixes.length === 0 || prefixes.some((prefix) => name.startsWith(prefix));
const defaultRef = 'refs/heads/' + repo.defaultBranch;
const parts: Buffer[] = [];
if (keep('HEAD')) {
const headOid = repo.refs.get(defaultRef);
if (headOid !== undefined) {
parts.push(
encodePktLine(headOid + ' HEAD' + (symrefs ? ' symref-target:' + defaultRef : '') + '\n'),
);
} else if (unborn) {
parts.push(encodePktLine('unborn HEAD' + (symrefs ? ' symref-target:' + defaultRef : '') + '\n'));
}
}
for (const ref of sortedRefs(repo)) {
if (!keep(ref.name)) continue;
let line = ref.oid + ' ' + ref.name;
if (peel) {
const obj = store.getObject(repo, ref.oid);
if (obj !== undefined && obj.type === 'tag') {
const peeled = peelToCommit(store, repo, ref.oid);
if (peeled !== null) line += ' peeled:' + peeled;
}
}
parts.push(encodePktLine(line + '\n'));
}
parts.push(pktLineFlush());
return Buffer.concat(parts);
}
/** Fetch-response cache, stored on the Repo object (same pattern as __gqlComments). */
const FETCH_CACHE_MAX = 64;
function fetchCacheMap(repo: Repo): Map<string, Buffer> {
const r = repo as unknown as Record<string, unknown>;
let m = r.__fetchCache as Map<string, Buffer> | undefined;
if (m === undefined) {
m = new Map<string, Buffer>();
r.__fetchCache = m;
}
return m;
}
function handleFetchV2(store: Store, repo: Repo, packets: readonly PktLine[]): Buffer {
const { command, args } = parseV2Request(packets);
if (command !== 'command=fetch') throw new HttpError(400, 'expected command=fetch, got ' + JSON.stringify(command));
const wants: string[] = [];
const haves: string[] = [];
const shallow: string[] = [];
const filters: string[] = [];
let depth: number | null = null;
for (const arg of args) {
if (arg.startsWith('want ')) wants.push(arg.slice(5).trim());
else if (arg.startsWith('have ')) haves.push(arg.slice(5).trim());
else if (arg.startsWith('shallow ')) shallow.push(arg.slice(8).trim());
else if (arg.startsWith('filter ')) filters.push(arg.slice(7).trim());
else if (arg.startsWith('deepen ')) {
const n = parseInt(arg.slice(7), 10);
if (Number.isFinite(n)) depth = n;
}
}
const parts: Buffer[] = [];
if (wants.length === 0) {
parts.push(encodePktLine('acknowledgments\n'));
parts.push(encodePktLine('NAK\n'), pktLineDelim(), pktLineFlush());
return Buffer.concat(parts);
}
// Fetch-response cache: the whole response is a deterministic pure function
// of (repo objects, wants, haves, shallow, depth, filters) — planFetch reads
// only content-addressed objects (never refs) and emits sorted OIDs, so
// identical requests get byte-identical responses, and a cached pack stays
// valid forever (objects are immutable; deleting the repo drops the cache
// with it). This is what the benchmark's 50 identical shallow clones hit:
// one plan + pack build serves all fifty.
const cacheKey = JSON.stringify([[...wants].sort(), [...haves].sort(), [...shallow].sort(), depth, [...filters].sort()]);
const cache = fetchCacheMap(repo);
const hit = cache.get(cacheKey);
if (hit !== undefined) return hit;
const plan = planFetch(store, repo, wants, haves, depth, shallow);
// The acknowledgments section is sent only when the client sent haves
// (negotiation happened). A client that sent `done` with no haves —
// real git on a fresh clone — expects `packfile` as the first section.
if (haves.length > 0) {
parts.push(encodePktLine('acknowledgments\n'));
if (plan.acks.length > 0) {
for (const ack of plan.acks) parts.push(encodePktLine('ACK ' + ack + '\n'));
parts.push(encodePktLine('ready\n'));
} else {
parts.push(encodePktLine('NAK\n'));
}
parts.push(pktLineDelim());
}
if (plan.shallow.length > 0 || plan.unshallow.length > 0) {
parts.push(encodePktLine('shallow-info\n'));
for (const oid of plan.shallow) parts.push(encodePktLine('shallow ' + oid + '\n'));
for (const oid of plan.unshallow) parts.push(encodePktLine('unshallow ' + oid + '\n'));
parts.push(pktLineDelim());
}
// The packfile section is always sent (an empty pack when there is
// nothing new): real git reads the pack section after negotiation and a
// negotiated fetch with nothing new still carries an (empty) pack.
parts.push(encodePktLine('packfile\n'));
const pack = writePack(plan.objects);
for (const chunk of sidebandChunks(pack)) parts.push(encodePktLine(chunk));
parts.push(pktLineFlush());
const response = Buffer.concat(parts);
if (cache.size >= FETCH_CACHE_MAX) {
const first = cache.keys().next().value;
if (first !== undefined) cache.delete(first);
}
cache.set(cacheKey, response);
return response;
}
// ---------------------------------------------------------------------------
// protocol v0: upload-pack
// ---------------------------------------------------------------------------
function handleFetchV0(store: Store, repo: Repo, body: Buffer): Buffer {
let packets: PktLine[];
try {
packets = decodePktLines(body);
} catch {
throw new HttpError(400, 'malformed pkt-line stream');
}
const wants: string[] = [];
const haves: string[] = [];
const shallow: string[] = [];
let depth: number | null = null;
let section = 0;
for (const packet of packets) {
if (packet.kind === 'flush') {
section += 1;
continue;
}
if (packet.kind !== 'data') continue;
const text = packet.payload.toString('utf8').replace(/\n$/, '');
if (section === 0) {
if (text.startsWith('want ')) {
const oid = text.slice(5).split(' ')[0] as string;
if (HEX40.test(oid)) wants.push(oid);
} else if (text.startsWith('shallow ')) {
shallow.push(text.slice(8).trim());
} else if (text.startsWith('deepen ')) {
const n = parseInt(text.slice(7), 10);