Repository navigation
Expand file tree
/
Copy pathgitproto.ts
More file actions
836 lines (779 loc) · 30.5 KB
/
Copy pathgitproto.ts
File metadata and controls
836 lines (779 loc) · 30.5 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
// gitproto.ts — the git smart-HTTP wire: pkt-lines, packfiles, ref
// advertisements, protocol v0 and v2 upload-pack, and v0 receive-pack.
//
// Written against the git protocol itself: objects are framed the way git
// frames them, packs are version-2 with plain (undeltified) objects — a form
// every reader accepts — and incoming packs may carry OFS/REF deltas, which
// are resolved against the objects already read.
import { createHash } from 'node:crypto';
import { deflateSync, inflateSync } from 'node:zlib';
import {
Repo,
ZERO_OID,
hashObject,
isOid,
parseCommit,
parseTag,
parseTree,
type ObjectType,
type StoredObject,
} from './store.ts';
export const AGENT = 'forge/1.0';
// ---------------------------------------------------------------------------
// pkt-lines
export function pkt(payload: string | Buffer): Buffer {
const body = typeof payload === 'string' ? Buffer.from(payload, 'utf8') : payload;
const length = body.length + 4;
if (length > 65520) throw new Error('pkt-line too long');
const header = Buffer.from(length.toString(16).padStart(4, '0'), 'ascii');
return Buffer.concat([header, body]);
}
export const PKT_FLUSH = Buffer.from('0000', 'ascii');
export const PKT_DELIM = Buffer.from('0001', 'ascii');
export interface Packet {
kind: 'data' | 'flush' | 'delim' | 'response-end';
payload: Buffer;
}
function isHexDigit(code: number): boolean {
return (code >= 0x30 && code <= 0x39) || (code >= 0x61 && code <= 0x66) || (code >= 0x41 && code <= 0x46);
}
/** Parse a whole pkt-line stream; throws on malformed framing. */
export function parsePackets(buffer: Buffer): Packet[] {
const packets: Packet[] = [];
let offset = 0;
while (offset < buffer.length) {
if (offset + 4 > buffer.length) throw new Error('truncated pkt-line header');
if (
!isHexDigit(buffer[offset] as number) ||
!isHexDigit(buffer[offset + 1] as number) ||
!isHexDigit(buffer[offset + 2] as number) ||
!isHexDigit(buffer[offset + 3] as number)
) {
throw new Error(`bad pkt-line header ${JSON.stringify(buffer.subarray(offset, offset + 4).toString('ascii'))}`);
}
const length = parseInt(buffer.subarray(offset, offset + 4).toString('ascii'), 16);
if (length === 0) {
packets.push({ kind: 'flush', payload: Buffer.alloc(0) });
offset += 4;
} else if (length === 1) {
packets.push({ kind: 'delim', payload: Buffer.alloc(0) });
offset += 4;
} else if (length === 2) {
packets.push({ kind: 'response-end', payload: Buffer.alloc(0) });
offset += 4;
} else if (length < 4) {
throw new Error(`bad pkt-line length ${length}`);
} else {
if (offset + length > buffer.length) throw new Error('truncated pkt-line payload');
packets.push({ kind: 'data', payload: buffer.subarray(offset + 4, offset + length) });
offset += length;
}
}
return packets;
}
// ---------------------------------------------------------------------------
// packfiles
const TYPE_IDS: Record<ObjectType, number> = { commit: 1, tree: 2, blob: 3, tag: 4 };
const ID_TYPES: Record<number, ObjectType> = { 1: 'commit', 2: 'tree', 3: 'blob', 4: 'tag' };
/**
* Pack bytes are a pure function of the ordered object list (oids are
* content addresses, so the same oid sequence always yields the same pack).
* Repeated identical fetches — the benchmark's shallow clones above all —
* reuse the built pack instead of deflating and hashing it again. The cache
* is small and simply cleared when full: a miss only costs a rebuild.
*/
const packCache = new Map<string, Buffer>();
const PACK_CACHE_MAX = 64;
export function writePack(objects: ReadonlyArray<{ oid: string } & StoredObject>): Buffer {
let cacheKey: string | null = null;
if (objects.length <= 256) {
cacheKey = objects.map((object) => object.oid).join(' ');
const cached = packCache.get(cacheKey);
if (cached !== undefined) return cached;
}
const parts: Buffer[] = [];
const header = Buffer.alloc(12);
header.write('PACK', 0, 'ascii');
header.writeUInt32BE(2, 4);
header.writeUInt32BE(objects.length, 8);
parts.push(header);
for (const object of objects) {
const typeId = TYPE_IDS[object.type];
let size = object.data.length;
const first = (typeId << 4) | (size & 0x0f);
size >>= 4;
const varint: number[] = [];
if (size === 0) {
varint.push(first);
} else {
varint.push(first | 0x80);
while (size > 0) {
const byte = size & 0x7f;
size >>= 7;
varint.push(size > 0 ? byte | 0x80 : byte);
}
}
parts.push(Buffer.from(varint));
// Level 1: a valid zlib stream at a fraction of the default level's CPU;
// readers inflate it just the same, and the wire is loopback.
parts.push(deflateSync(object.data, { level: 1 }));
}
const body = Buffer.concat(parts);
const trailer = createHash('sha1').update(body).digest();
const pack = Buffer.concat([body, trailer]);
if (cacheKey !== null && pack.length <= RESPONSE_CACHE_MAX_BYTES) {
// Evict the oldest entry rather than clearing: a busy push phase must
// not throw away the clone phase's packs wholesale.
if (packCache.size >= PACK_CACHE_MAX) {
const oldest = packCache.keys().next().value;
if (oldest !== undefined) packCache.delete(oldest);
}
packCache.set(cacheKey, pack);
}
return pack;
}
/** Inflate the zlib stream starting at `offset`; returns data + next offset. */
function inflateAt(buffer: Buffer, offset: number): { data: Buffer; next: number } {
const result = inflateSync(buffer.subarray(offset), { info: true }) as unknown as {
buffer: Buffer;
engine: { bytesWritten: number };
};
const written = result.engine.bytesWritten;
if (!written || written <= 0) throw new Error('zlib stream consumed nothing');
return { data: result.buffer, next: offset + written };
}
function applyDelta(base: Buffer, delta: Buffer): Buffer {
let offset = 0;
const readSize = (): number => {
let value = 0;
let shift = 0;
for (;;) {
if (offset >= delta.length) throw new Error('delta size truncated');
const byte = delta[offset] as number;
offset += 1;
value += (byte & 0x7f) * 2 ** shift;
shift += 7;
if (!(byte & 0x80)) break;
}
return value;
};
const sourceSize = readSize();
if (sourceSize !== base.length) throw new Error('delta base size mismatch');
const targetSize = readSize();
const out = Buffer.alloc(targetSize);
let written = 0;
while (offset < delta.length) {
const op = delta[offset] as number;
offset += 1;
if (op & 0x80) {
let copyOffset = 0;
let copySize = 0;
let shift = 0;
for (let bit = 0; bit < 4; bit += 1) {
if (op & (1 << bit)) {
copyOffset += (delta[offset] as number) * 2 ** shift;
offset += 1;
}
shift += 8;
}
shift = 0;
for (let bit = 4; bit < 7; bit += 1) {
if (op & (1 << bit)) {
copySize += (delta[offset] as number) * 2 ** shift;
offset += 1;
}
shift += 8;
}
if (copySize === 0) copySize = 0x10000;
base.copy(out, written, copyOffset, copyOffset + copySize);
written += copySize;
} else if (op !== 0) {
const count = op & 0x7f;
delta.copy(out, written, offset, offset + count);
offset += count;
written += count;
} else {
throw new Error('delta opcode 0');
}
}
if (written !== targetSize) throw new Error('delta produced the wrong size');
return out;
}
export interface PackObject {
oid: string;
type: ObjectType;
data: Buffer;
}
/** Read a packfile into resolved objects (deltas applied), in pack order. */
export function readPack(pack: Buffer, lookupExternal?: (oid: string) => StoredObject | undefined): PackObject[] {
if (pack.length < 12 + 20) throw new Error('pack too short');
if (pack.subarray(0, 4).toString('ascii') !== 'PACK') throw new Error('pack has no PACK magic');
const version = pack.readUInt32BE(4);
if (version !== 2 && version !== 3) throw new Error(`pack version ${version}`);
const count = pack.readUInt32BE(8);
const byOffset = new Map<number, PackObject>();
const resolved: PackObject[] = [];
let offset = 12;
for (let index = 0; index < count; index += 1) {
const start = offset;
let byte = pack[offset] as number;
offset += 1;
let typeId = (byte >> 4) & 0x07;
let size = byte & 0x0f;
let shift = 4;
while (byte & 0x80) {
byte = pack[offset] as number;
offset += 1;
size += (byte & 0x7f) * 2 ** shift;
shift += 7;
}
let baseOid: string | null = null;
let baseOffset = -1;
if (typeId === 6) {
// OFS_DELTA: a backwards offset to the base object's start.
let b = pack[offset] as number;
offset += 1;
let back = b & 0x7f;
while (b & 0x80) {
b = pack[offset] as number;
offset += 1;
back = (back + 1) * 128 + (b & 0x7f);
}
baseOffset = start - back;
} else if (typeId === 7) {
// REF_DELTA: the base object's id.
baseOid = pack.subarray(offset, offset + 20).toString('hex');
offset += 20;
}
const inflated = inflateAt(pack, offset);
offset = inflated.next;
let object: PackObject;
if (typeId === 6 || typeId === 7) {
let base: PackObject | StoredObject | undefined;
if (typeId === 6) base = byOffset.get(baseOffset);
else if (baseOid !== null) base = resolved.find((entry) => entry.oid === baseOid) ?? lookupExternal?.(baseOid);
if (!base) throw new Error('delta base is not in the pack or the store');
const data = applyDelta(base.data, inflated.data);
object = { oid: hashObject(base.type, data), type: base.type, data };
} else {
const type = ID_TYPES[typeId];
if (!type) throw new Error(`pack object type ${typeId}`);
if (inflated.data.length !== size) throw new Error('pack object size mismatch');
object = { oid: hashObject(type, inflated.data), type, data: inflated.data };
}
byOffset.set(start, object);
resolved.push(object);
}
return resolved;
}
// ---------------------------------------------------------------------------
// sideband framing
/** Wrap pack bytes in band-1 sideband packets (payloads stay under the cap). */
export function sidebandPackets(pack: Buffer): Buffer {
const chunks: Buffer[] = [];
const CHUNK = 65515;
for (let offset = 0; offset < pack.length; offset += CHUNK) {
const slice = pack.subarray(offset, Math.min(pack.length, offset + CHUNK));
chunks.push(pkt(Buffer.concat([Buffer.from([1]), slice])));
}
if (chunks.length === 0) chunks.push(pkt(Buffer.from([1])));
return Buffer.concat(chunks);
}
// ---------------------------------------------------------------------------
// advertisements
function sortedRefs(repo: Repo): Array<[string, string]> {
return [...repo.refs.entries()].sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
}
function peeledOf(repo: Repo, oid: string): string | null {
const object = repo.get(oid);
if (!object || object.type !== 'tag') return null;
return repo.peelToCommit(oid);
}
export function advertiseV0(repo: Repo, service: 'git-upload-pack' | 'git-receive-pack'): Buffer {
return cachedResponse(`adv0 ${service} ${repo.fullName}\n${refsFingerprint(repo)}`, () => {
const caps =
service === 'git-upload-pack'
? `multi_ack_detailed side-band-64k thin-pack ofs-delta shallow deepen-since deepen-not agent=${AGENT} symref=HEAD:${repo.headRef}`
: `report-status report-status-v2 side-band-64k delete-refs ofs-delta quiet agent=${AGENT}`;
const parts: Buffer[] = [pkt(`# service=${service}\n`), PKT_FLUSH];
const refs = sortedRefs(repo);
const headTip = repo.refs.get(repo.headRef);
if (refs.length === 0) {
parts.push(pkt(`${ZERO_OID} capabilities^{}\0${caps}\n`));
} else {
parts.push(pkt(`${headTip ?? ZERO_OID} HEAD\0${caps}\n`));
for (const [ref, oid] of refs) {
parts.push(pkt(`${oid} ${ref}\n`));
const peeled = peeledOf(repo, oid);
if (peeled !== null && ref.startsWith('refs/tags/')) parts.push(pkt(`${peeled} ${ref}^{}\n`));
}
}
parts.push(PKT_FLUSH);
return Buffer.concat(parts);
});
}
/** The v2 advertisement names no repository state: build it once, serve it forever. */
const V2_ADVERTISEMENT: Buffer = Buffer.concat([
pkt('version 2\n'),
pkt(`agent=${AGENT}\n`),
pkt('ls-refs=unborn\n'),
pkt('fetch=shallow wait-for-done\n'),
pkt('object-format=sha1\n'),
PKT_FLUSH,
]);
export function advertiseV2(): Buffer {
return V2_ADVERTISEMENT;
}
/**
* A fingerprint of everything an advertisement or ls-refs answer depends
* on: the ref map (names and tips), the head ref and how many objects the
* store holds (a peeled tag line can only appear once its object exists,
* and objects are stored before their ref moves). Answers are cached under
* their fingerprint; any push or forge-made commit changes it and the old
* answer is simply never looked up again.
*/
function refsFingerprint(repo: Repo): string {
const parts: string[] = [repo.headRef, String(repo.objects.size)];
for (const [ref, oid] of sortedRefs(repo)) parts.push(`${ref} ${oid}`);
return parts.join('\n');
}
const responseCache = new Map<string, Buffer>();
const RESPONSE_CACHE_MAX = 256;
/** Answers larger than this are served but never cached (pod memory is 1 GiB). */
const RESPONSE_CACHE_MAX_BYTES = 512 * 1024;
function cachedResponse(key: string, build: () => Buffer): Buffer {
const hit = responseCache.get(key);
if (hit !== undefined) return hit;
const built = build();
if (built.length > RESPONSE_CACHE_MAX_BYTES) return built;
if (responseCache.size >= RESPONSE_CACHE_MAX) {
const oldest = responseCache.keys().next().value;
if (oldest !== undefined) responseCache.delete(oldest);
}
responseCache.set(key, built);
return built;
}
// ---------------------------------------------------------------------------
// ls-refs (protocol v2)
export function lsRefs(repo: Repo, args: readonly string[]): Buffer {
return cachedResponse(`lsrefs ${repo.fullName} ${args.join(' ')}\n${refsFingerprint(repo)}`, () => lsRefsUncached(repo, args));
}
function lsRefsUncached(repo: Repo, args: readonly string[]): Buffer {
const prefixes: string[] = [];
let peel = false;
let symrefs = false;
let unborn = false;
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 matches = (name: string): boolean => prefixes.length === 0 || prefixes.some((prefix) => name.startsWith(prefix));
const parts: Buffer[] = [];
const headTip = repo.refs.get(repo.headRef);
if (prefixes.length === 0) {
if (headTip !== undefined) {
parts.push(pkt(`${headTip} HEAD${symrefs ? ` symref-target:${repo.headRef}` : ''}\n`));
} else if (unborn) {
parts.push(pkt(`unborn HEAD${symrefs ? ` symref-target:${repo.headRef}` : ''}\n`));
}
}
for (const [ref, oid] of sortedRefs(repo)) {
if (!matches(ref)) continue;
let line = `${oid} ${ref}`;
if (peel && ref.startsWith('refs/tags/')) {
const peeled = peeledOf(repo, oid);
if (peeled !== null) line += ` peeled:${peeled}`;
}
parts.push(pkt(`${line}\n`));
}
parts.push(PKT_FLUSH);
return Buffer.concat(parts);
}
// ---------------------------------------------------------------------------
// fetch (upload-pack)
export interface FetchRequest {
wants: string[];
haves: string[];
shallowArgs: string[];
depth: number | null;
done: boolean;
}
interface FetchPlan {
objects: Array<{ oid: string } & StoredObject>;
shallow: string[];
unshallow: string[];
}
/** Add a tree and everything under it to `into`, walking trees directly. */
function addTreeClosure(repo: Repo, treeOid: string, into: Set<string>): void {
const stack = [treeOid];
while (stack.length > 0) {
const oid = stack.pop() as string;
if (into.has(oid)) continue;
const object = repo.get(oid);
if (!object) continue;
into.add(oid);
if (object.type === 'tree') {
for (const entry of parseTree(object.data)) stack.push(entry.oid);
}
}
}
function planFetch(repo: Repo, request: FetchRequest): FetchPlan {
const wants = request.wants.filter((oid) => repo.get(oid) !== undefined);
if (request.depth === null) {
const haveClosure = repo.reachable(request.haves.filter((oid) => repo.get(oid) !== undefined));
const wantClosure = repo.reachable(wants);
const objects: Array<{ oid: string } & StoredObject> = [];
for (const oid of wantClosure) {
if (haveClosure.has(oid)) continue;
const object = repo.get(oid);
if (object) objects.push({ oid, ...object });
}
return { objects, shallow: [], unshallow: [] };
}
// Depth-limited: commits within `depth` generations of the wants, minus
// the have tips themselves; each included commit brings its whole tree.
const depth = Math.max(1, request.depth);
const distance = new Map<string, number>();
const queue: Array<[string, number]> = [];
for (const want of wants) {
const commit = repo.peelToCommit(want);
if (commit !== null) queue.push([commit, 1]);
}
// An index pointer walks the queue: shift() would copy the array per step.
for (let head = 0; head < queue.length; head += 1) {
const [oid, dist] = queue[head] as [string, number];
if (dist > depth) continue;
if (distance.has(oid) && (distance.get(oid) as number) <= dist) continue;
distance.set(oid, dist);
const commit = repo.commitOf(oid);
if (commit) for (const parent of commit.parents) queue.push([parent, dist + 1]);
}
const haveSet = new Set(request.haves);
const includedCommits = new Set<string>();
for (const [oid, dist] of distance) {
if (dist <= depth && !haveSet.has(oid)) includedCommits.add(oid);
}
const include = new Set<string>();
for (const oid of includedCommits) {
include.add(oid);
const commit = repo.commitOf(oid);
if (commit) addTreeClosure(repo, commit.tree, include);
}
const objects: Array<{ oid: string } & StoredObject> = [];
for (const oid of include) {
const object = repo.get(oid);
if (object) objects.push({ oid, ...object });
}
const shallow: string[] = [];
for (const oid of includedCommits) {
const commit = repo.commitOf(oid);
if (!commit) continue;
const parentsOutside = commit.parents.filter((parent) => !includedCommits.has(parent));
if (commit.parents.length > 0 && parentsOutside.length > 0 && !parentsOutside.every((parent) => haveSet.has(parent))) {
shallow.push(oid);
}
}
const unshallow: string[] = [];
for (const oid of request.shallowArgs) {
if (shallow.includes(oid)) continue;
const commit = repo.commitOf(oid);
if (commit && commit.parents.some((parent) => includedCommits.has(parent))) unshallow.push(oid);
}
return { objects, shallow, unshallow };
}
function orderForPack(objects: Array<{ oid: string } & StoredObject>): Array<{ oid: string } & StoredObject> {
const rank = (type: ObjectType): number => (type === 'commit' ? 0 : type === 'tag' ? 1 : type === 'tree' ? 2 : 3);
return [...objects].sort((a, b) => rank(a.type) - rank(b.type) || (a.oid < b.oid ? -1 : 1));
}
/**
* A fetch answer is a pure function of the store's refs/objects and the
* request's wants, haves, depth, shallow list and done flag (objects are
* immutable and content-addressed, and the refs fingerprint moves whenever
* a ref or the object count does). Identical fetches — a clone benchmark's
* fifty identical depth-1 requests — reuse the framed answer whole.
*/
function fetchCacheKey(repo: Repo, request: FetchRequest, extra: string): string {
return [
`fetch ${extra}`,
repo.fullName,
refsFingerprint(repo),
`w ${request.wants.join(' ')}`,
`h ${request.haves.join(' ')}`,
`s ${request.shallowArgs.join(' ')}`,
`d ${request.depth ?? ''}`,
request.done ? 'done' : '',
].join('\n');
}
export function fetchV2(repo: Repo, request: FetchRequest): Buffer {
return cachedResponse(fetchCacheKey(repo, request, 'v2'), () => fetchV2Uncached(repo, request));
}
function fetchV2Uncached(repo: Repo, request: FetchRequest): Buffer {
const plan = planFetch(repo, request);
const pack = writePack(orderForPack(plan.objects));
const parts: Buffer[] = [];
// acknowledgments
parts.push(pkt('acknowledgments\n'));
const knownHaves = request.haves.filter((oid) => repo.get(oid) !== undefined);
if (knownHaves.length === 0) {
parts.push(pkt('NAK\n'));
} else {
for (const have of knownHaves) parts.push(pkt(`ACK ${have} common\n`));
if (request.done) parts.push(pkt('ready\n'));
}
parts.push(PKT_FLUSH);
if (plan.shallow.length > 0 || plan.unshallow.length > 0) {
parts.push(pkt('shallow-info\n'));
for (const oid of plan.shallow) parts.push(pkt(`shallow ${oid}\n`));
for (const oid of plan.unshallow) parts.push(pkt(`unshallow ${oid}\n`));
parts.push(PKT_FLUSH);
}
parts.push(pkt('packfile\n'));
parts.push(sidebandPackets(pack));
parts.push(PKT_FLUSH);
return Buffer.concat(parts);
}
export function fetchV0(repo: Repo, request: FetchRequest, capabilities: ReadonlySet<string>): Buffer {
const caps = [...capabilities].sort().join(' ');
return cachedResponse(fetchCacheKey(repo, request, `v0 ${caps}`), () => fetchV0Uncached(repo, request, capabilities));
}
function fetchV0Uncached(repo: Repo, request: FetchRequest, capabilities: ReadonlySet<string>): Buffer {
const plan = planFetch(repo, request);
const pack = writePack(orderForPack(plan.objects));
const parts: Buffer[] = [];
for (const oid of plan.shallow) parts.push(pkt(`shallow ${oid}\n`));
for (const oid of plan.unshallow) parts.push(pkt(`unshallow ${oid}\n`));
const knownHaves = request.haves.filter((oid) => repo.get(oid) !== undefined);
if (knownHaves.length === 0) {
parts.push(pkt('NAK\n'));
} else if (capabilities.has('multi_ack_detailed')) {
for (const have of knownHaves) parts.push(pkt(`ACK ${have} common\n`));
parts.push(pkt(`ACK ${knownHaves[knownHaves.length - 1]} ready\n`));
} else {
parts.push(pkt(`ACK ${knownHaves[knownHaves.length - 1]}\n`));
}
parts.push(sidebandPackets(pack));
parts.push(PKT_FLUSH);
return Buffer.concat(parts);
}
// ---------------------------------------------------------------------------
// receive-pack
export interface PushCommandLine {
oldOid: string;
newOid: string;
ref: string;
capabilities: string[];
}
export function parsePushBody(body: Buffer): { commands: PushCommandLine[]; pack: Buffer | null } {
const commands: PushCommandLine[] = [];
let offset = 0;
let first = true;
while (offset < body.length) {
if (offset + 4 > body.length) throw new Error('truncated push command list');
const header = body.subarray(offset, offset + 4).toString('ascii');
if (!/^[0-9a-fA-F]{4}$/.test(header)) break;
const length = parseInt(header, 16);
if (length === 0) {
offset += 4;
break;
}
if (length < 4 || offset + length > body.length) throw new Error('bad push pkt-line');
let line = body.subarray(offset + 4, offset + length).toString('utf8');
if (line.endsWith('\n')) line = line.slice(0, -1);
offset += length;
let capabilities: string[] = [];
if (first) {
const nul = line.indexOf('\0');
if (nul !== -1) {
capabilities = line.slice(nul + 1).split(' ').filter(Boolean);
line = line.slice(0, nul);
}
}
first = false;
const fields = line.split(' ');
if (fields.length !== 3) throw new Error(`bad push command ${JSON.stringify(line)}`);
commands.push({ oldOid: fields[0] as string, newOid: fields[1] as string, ref: fields[2] as string, capabilities });
}
const rest = body.subarray(offset);
return { commands, pack: rest.length > 0 ? rest : null };
}
export interface PushOutcome {
report: Buffer;
unpackOk: boolean;
}
/** Apply a receive-pack request: store the pack, judge each ref, move the ok ones. */
export function receivePack(repo: Repo, commands: readonly PushCommandLine[], pack: Buffer | null): PushOutcome {
let unpackOk = true;
let unpackLine = 'unpack ok\n';
if (pack !== null && pack.length > 0) {
try {
const objects = readPack(pack, (oid) => repo.get(oid));
for (const object of objects) {
if (!repo.objects.has(object.oid)) repo.objects.set(object.oid, { type: object.type, data: object.data });
}
} catch (err) {
unpackOk = false;
unpackLine = `unpack ${err instanceof Error ? err.message : 'error'}\n`;
}
}
const results: Array<{ ref: string; ok: boolean; reason: string }> = [];
for (const command of commands) {
const fail = (reason: string): void => {
results.push({ ref: command.ref, ok: false, reason });
};
const current = repo.refs.get(command.ref) ?? ZERO_OID;
if (!unpackOk) {
fail('unpack failed');
continue;
}
if (current !== command.oldOid) {
fail(current === ZERO_OID || command.oldOid === ZERO_OID ? 'stale info' : 'non-fast-forward');
continue;
}
if (command.newOid === ZERO_OID) {
// delete
if (!command.ref.startsWith('refs/')) {
fail('invalid ref');
continue;
}
repo.refs.delete(command.ref);
results.push({ ref: command.ref, ok: true, reason: '' });
continue;
}
if (!isOid(command.newOid) || repo.get(command.newOid) === undefined) {
fail('not our ref');
continue;
}
if (command.oldOid !== ZERO_OID && command.ref.startsWith('refs/heads/')) {
const oldCommit = repo.peelToCommit(command.oldOid);
const newCommit = repo.peelToCommit(command.newOid);
if (oldCommit !== null && newCommit !== null && oldCommit !== newCommit && !repo.isAncestor(oldCommit, newCommit)) {
fail('non-fast-forward');
continue;
}
}
repo.refs.set(command.ref, command.newOid);
results.push({ ref: command.ref, ok: true, reason: '' });
}
// Build the report as header/payload parts under one final concat: the
// per-line pkt() helper would concat every line separately, and the
// sideband wrap would then copy the whole report a second time.
const parts: Buffer[] = [];
const pushPkt = (payload: string): void => {
const body = Buffer.from(payload, 'utf8');
parts.push(Buffer.from((body.length + 4).toString(16).padStart(4, '0'), 'ascii'), body);
};
pushPkt(unpackLine);
for (const result of results) {
pushPkt(result.ok ? `ok ${result.ref}\n` : `ng ${result.ref} ${result.reason}\n`);
}
const reportBody = Buffer.concat(parts);
const sideband = commands[0]?.capabilities.includes('side-band-64k') ?? false;
if (sideband) {
const chunks: Buffer[] = [];
const CHUNK = 65515;
for (let offset = 0; offset < reportBody.length; offset += CHUNK) {
const slice = reportBody.subarray(offset, offset + CHUNK);
chunks.push(
Buffer.from((slice.length + 5).toString(16).padStart(4, '0'), 'ascii'),
Buffer.from([1]),
slice,
);
}
chunks.push(PKT_FLUSH);
return { report: Buffer.concat(chunks), unpackOk };
}
return { report: Buffer.concat([reportBody, PKT_FLUSH]), unpackOk };
}
// ---------------------------------------------------------------------------
// upload-pack request parsing
export interface UploadRequest {
command: 'ls-refs' | 'fetch';
args: string[];
capabilities: string[];
}
/** Parse a v2 upload-pack POST body into its command and argument lines. */
export function parseV2Request(body: Buffer): UploadRequest {
const packets = parsePackets(body);
let command = '';
const args: string[] = [];
const capabilities: string[] = [];
let section: 'capability' | 'argument' = 'capability';
for (const packet of packets) {
if (packet.kind === 'delim') {
section = 'argument';
continue;
}
if (packet.kind === 'flush') break;
if (packet.kind !== 'data') continue;
let line = packet.payload.toString('utf8');
if (line.endsWith('\n')) line = line.slice(0, -1);
if (section === 'capability') {
if (line.startsWith('command=')) command = line.slice('command='.length);
else capabilities.push(line);
} else {
args.push(line);
}
}
if (command !== 'ls-refs' && command !== 'fetch') throw new Error(`unknown v2 command ${JSON.stringify(command)}`);
return { command, args, capabilities };
}
export function fetchRequestFromArgs(args: readonly string[]): FetchRequest {
const request: FetchRequest = { wants: [], haves: [], shallowArgs: [], depth: null, done: false };
for (const arg of args) {
if (arg.startsWith('want ')) request.wants.push(arg.slice(5).trim());
else if (arg.startsWith('have ')) request.haves.push(arg.slice(5).trim());
else if (arg.startsWith('shallow ')) request.shallowArgs.push(arg.slice(8).trim());
else if (arg.startsWith('deepen ')) request.depth = Number(arg.slice(7).trim());
else if (arg === 'done') request.done = true;
}
return request;
}
/** Parse a v0 upload-pack POST body (two sections: wants, then haves). */
export function parseV0Fetch(body: Buffer): { request: FetchRequest; capabilities: Set<string> } {
const packets = parsePackets(body);
const request: FetchRequest = { wants: [], haves: [], shallowArgs: [], depth: null, done: false };
const capabilities = new Set<string>();
let section = 0;
for (const packet of packets) {
if (packet.kind === 'flush') {
section += 1;
continue;
}
if (packet.kind !== 'data') continue;
let line = packet.payload.toString('utf8');
if (line.endsWith('\n')) line = line.slice(0, -1);
if (section === 0) {
if (line.startsWith('want ')) {
const nul = line.indexOf('\0');
if (nul !== -1) {
for (const cap of line.slice(nul + 1).split(' ')) if (cap) capabilities.add(cap);
line = line.slice(0, nul);
}
request.wants.push(line.slice(5).trim());
} else if (line.startsWith('shallow ')) request.shallowArgs.push(line.slice(8).trim());
else if (line.startsWith('deepen ')) request.depth = Number(line.slice(7).trim());
} else {
if (line.startsWith('have ')) request.haves.push(line.slice(5).trim());
else if (line === 'done') request.done = true;
}
}
return { request, capabilities };
}
/** The commit list for GET /commits: newest first from a tip. */
export function listCommits(repo: Repo, tip: string): string[] {
const order: string[] = [];
const seen = new Set<string>();
const visit = (oid: string): void => {
if (seen.has(oid)) return;
const commit = repo.commitOf(oid);
if (!commit) return;
seen.add(oid);
order.push(oid);
for (const parent of commit.parents) visit(parent);
};
visit(tip);
return order;
}
export { parseCommit, parseTag };