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
|
#define USE_THE_REPOSITORY_VARIABLE
#include "test-tool.h"
#include "git-compat-util.h"
#include "git-zlib.h"
#include "hash.h"
#include "hex.h"
#include "object-file.h"
#include "object.h"
#include "pack.h"
#include "parse-options.h"
#include "parse.h"
#include "repository.h"
#include "setup.h"
#include "strbuf.h"
#include "write-or-die.h"
#define BLOCK_SIZE 0xffff
static const unsigned char zeros[BLOCK_SIZE];
/*
* Write data as an uncompressed zlib stream.
* For data larger than 64KB, writes multiple uncompressed blocks.
* If data is NULL, writes zeros.
* Updates the pack checksum context.
*/
static void write_uncompressed_zlib(FILE *f, struct git_hash_ctx *pack_ctx,
const void *data, size_t len)
{
unsigned char zlib_header[2] = { 0x78, 0x01 }; /* CMF, FLG */
unsigned char block_header[5];
const unsigned char *p = data;
size_t remaining = len;
uint32_t adler = 1L; /* adler32 initial value */
unsigned char adler_buf[4];
/* Write zlib header */
fwrite_or_die(f, zlib_header, sizeof(zlib_header));
git_hash_update(pack_ctx, zlib_header, 2);
/* Write uncompressed blocks (max 64KB each) */
do {
size_t block_len = remaining > BLOCK_SIZE ? BLOCK_SIZE : remaining;
int is_final = (block_len == remaining);
const unsigned char *block_data = data ? p : zeros;
block_header[0] = is_final ? 0x01 : 0x00;
block_header[1] = block_len & 0xff;
block_header[2] = (block_len >> 8) & 0xff;
block_header[3] = block_header[1] ^ 0xff;
block_header[4] = block_header[2] ^ 0xff;
fwrite_or_die(f, block_header, sizeof(block_header));
git_hash_update(pack_ctx, block_header, 5);
if (block_len) {
fwrite_or_die(f, block_data, block_len);
git_hash_update(pack_ctx, block_data, block_len);
adler = adler32(adler, block_data, block_len);
}
if (data)
p += block_len;
remaining -= block_len;
} while (remaining > 0);
/* Write adler32 checksum */
put_be32(adler_buf, adler);
fwrite_or_die(f, adler_buf, sizeof(adler_buf));
git_hash_update(pack_ctx, adler_buf, 4);
}
/*
* Write an uncompressed object to the pack file.
* If `data == NULL`, it is treated like a buffer to NUL bytes.
* Updates the pack checksum context.
*/
static void write_pack_object(FILE *f, struct git_hash_ctx *pack_ctx,
enum object_type type,
const void *data, size_t len,
struct object_id *oid,
const struct git_hash_algo *algo)
{
unsigned char pack_header[MAX_PACK_OBJECT_HEADER];
char object_header[32];
int pack_header_len, object_header_len;
struct git_hash_ctx ctx;
/* Write pack object header */
pack_header_len = encode_in_pack_object_header(pack_header,
sizeof(pack_header),
type, len);
fwrite_or_die(f, pack_header, pack_header_len);
git_hash_update(pack_ctx, pack_header, pack_header_len);
/* Write the data as uncompressed zlib */
write_uncompressed_zlib(f, pack_ctx, data, len);
git_hash_init(&ctx, algo);
object_header_len = format_object_header(object_header,
sizeof(object_header),
type, len);
git_hash_update(&ctx, object_header, object_header_len);
if (data)
git_hash_update(&ctx, data, len);
else {
for (size_t i = len / BLOCK_SIZE; i; i--)
git_hash_update(&ctx, zeros, BLOCK_SIZE);
git_hash_update(&ctx, zeros, len % BLOCK_SIZE);
}
git_hash_final_oid(oid, &ctx);
}
/*
* Fast path: precomputed pack data for a 4 GiB + 1 all-NUL blob.
*
* The generated pack is almost entirely zeros with a small constant
* prefix, periodic deflate block headers, and a constant suffix
* containing the tree, two commits, and the pack checksum. Because
* every byte is deterministic for a given blob size and hash algorithm,
* we can write the pack without computing any hashes at all, reducing
* runtime from minutes of hash computation to seconds of pure I/O.
*
* The blob is stored as an uncompressed deflate stream: a two-byte
* zlib header, then 65538 blocks of up to 0xffff bytes each, followed
* by an adler32 checksum. The pack header and deflate framing are
* shared across hash algorithms; only the suffix (which contains OIDs
* and the pack checksum) differs.
*
* Constants were generated by running the generic path and extracting
* the non-zero bytes from the resulting pack file.
*/
#define FAST_PACK_4G1_BLOB_SIZE ((size_t)4 * 1024 * 1024 * 1024 + 1)
#define FAST_PACK_4G1_N_FULL_BLOCKS 65537
/*
* Per-hash-algorithm constants for the fast path. The prefix and
* deflate block structure are identical across algorithms; only the
* suffix (tree, commits, pack checksum) and the commit OID differ.
*/
struct fast_pack {
uint32_t format_id;
const unsigned char *suffix;
size_t suffix_len;
const char *commit_oid;
};
/* Pack header + pack object header + zlib header + first block header */
static const unsigned char fast_pack_prefix[] = {
/* PACK header: signature, version 2, 5 objects */
0x50, 0x41, 0x43, 0x4b, 0x00, 0x00, 0x00, 0x02,
0x00, 0x00, 0x00, 0x05,
/* pack object header: blob, size = 4294967297 */
0xb1, 0x80, 0x80, 0x80, 0x80, 0x01,
/* zlib header: CMF=0x78, FLG=0x01 */
0x78, 0x01,
/* first non-final block header: BFINAL=0, LEN=0xffff, NLEN=0x0000 */
0x00, 0xff, 0xff, 0x00, 0x00
};
/* Every non-final deflate block header is identical */
static const unsigned char fast_pack_block_header[] = {
0x00, 0xff, 0xff, 0x00, 0x00
};
/* Final block (2 data bytes) + adler32 of 4294967297 NUL bytes */
static const unsigned char fast_pack_final_block[] = {
/* BFINAL=1, LEN=2, NLEN=0xfffd */
0x01, 0x02, 0x00, 0xfd, 0xff,
/* 2 NUL data bytes */
0x00, 0x00,
/* adler32 */
0x00, 0xe2, 0x00, 0x01
};
/*
* SHA-1 suffix: tree, commit, empty tree, final commit, pack checksum.
*/
static const unsigned char fast_pack_sha1_suffix[] = {
0xa0, 0x02, 0x78, 0x01, 0x01, 0x20, 0x00, 0xdf,
0xff, 0x31, 0x30, 0x30, 0x36, 0x34, 0x34, 0x20,
0x66, 0x69, 0x6c, 0x65, 0x00, 0x3e, 0xb7, 0xfe,
0xb1, 0x41, 0x3c, 0x75, 0x7f, 0x0d, 0x81, 0x81,
0xde, 0xb2, 0x8d, 0x1d, 0xab, 0x03, 0xd6, 0x48,
0x46, 0xb4, 0xb4, 0x0c, 0x60, 0x95, 0x0b, 0x78,
0x01, 0x01, 0xb5, 0x00, 0x4a, 0xff, 0x74, 0x72,
0x65, 0x65, 0x20, 0x63, 0x36, 0x38, 0x33, 0x66,
0x63, 0x63, 0x37, 0x64, 0x31, 0x64, 0x38, 0x33,
0x65, 0x66, 0x32, 0x66, 0x65, 0x31, 0x61, 0x66,
0x35, 0x35, 0x32, 0x31, 0x35, 0x64, 0x30, 0x31,
0x36, 0x38, 0x64, 0x62, 0x35, 0x32, 0x61, 0x33,
0x61, 0x33, 0x62, 0x0a, 0x61, 0x75, 0x74, 0x68,
0x6f, 0x72, 0x20, 0x41, 0x20, 0x55, 0x20, 0x54,
0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61, 0x75, 0x74,
0x68, 0x6f, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e,
0x20, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x30, 0x20, 0x2b, 0x30, 0x30, 0x30,
0x30, 0x0a, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
0x74, 0x65, 0x72, 0x20, 0x43, 0x20, 0x4f, 0x20,
0x4d, 0x69, 0x74, 0x74, 0x65, 0x72, 0x20, 0x3c,
0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x74, 0x65,
0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c,
0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0x20, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x30, 0x20, 0x2b, 0x30, 0x30, 0x30, 0x30, 0x0a,
0x0a, 0x4c, 0x61, 0x72, 0x67, 0x65, 0x20, 0x62,
0x6c, 0x6f, 0x62, 0x20, 0x63, 0x6f, 0x6d, 0x6d,
0x69, 0x74, 0x0a, 0xc6, 0x55, 0x37, 0x6b, 0x20,
0x78, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x00,
0x00, 0x00, 0x01, 0x95, 0x0e, 0x78, 0x01, 0x01,
0xe5, 0x00, 0x1a, 0xff, 0x74, 0x72, 0x65, 0x65,
0x20, 0x34, 0x62, 0x38, 0x32, 0x35, 0x64, 0x63,
0x36, 0x34, 0x32, 0x63, 0x62, 0x36, 0x65, 0x62,
0x39, 0x61, 0x30, 0x36, 0x30, 0x65, 0x35, 0x34,
0x62, 0x66, 0x38, 0x64, 0x36, 0x39, 0x32, 0x38,
0x38, 0x66, 0x62, 0x65, 0x65, 0x34, 0x39, 0x30,
0x34, 0x0a, 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74,
0x20, 0x63, 0x35, 0x62, 0x32, 0x31, 0x63, 0x36,
0x31, 0x31, 0x61, 0x61, 0x35, 0x39, 0x34, 0x65,
0x63, 0x39, 0x66, 0x64, 0x37, 0x65, 0x39, 0x32,
0x63, 0x66, 0x39, 0x36, 0x34, 0x38, 0x39, 0x31,
0x34, 0x63, 0x61, 0x34, 0x63, 0x32, 0x34, 0x31,
0x32, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72,
0x20, 0x41, 0x20, 0x55, 0x20, 0x54, 0x68, 0x6f,
0x72, 0x20, 0x3c, 0x61, 0x75, 0x74, 0x68, 0x6f,
0x72, 0x40, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c,
0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e, 0x20, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x30, 0x20, 0x2b, 0x30, 0x30, 0x30, 0x30, 0x0a,
0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x74, 0x65,
0x72, 0x20, 0x43, 0x20, 0x4f, 0x20, 0x4d, 0x69,
0x74, 0x74, 0x65, 0x72, 0x20, 0x3c, 0x63, 0x6f,
0x6d, 0x6d, 0x69, 0x74, 0x74, 0x65, 0x72, 0x40,
0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
0x63, 0x6f, 0x6d, 0x3e, 0x20, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x20,
0x2b, 0x30, 0x30, 0x30, 0x30, 0x0a, 0x0a, 0x45,
0x6d, 0x70, 0x74, 0x79, 0x20, 0x74, 0x72, 0x65,
0x65, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
0x0a, 0xaa, 0xb8, 0x45, 0x01, 0x8e, 0xfc, 0xf0,
0x2f, 0x9c, 0xc5, 0xcc, 0x4f, 0x6a, 0x1a, 0xc9,
0x2b, 0x23, 0xa9, 0xff, 0x91, 0x06, 0xc2, 0x70,
0xe3
};
/*
* SHA-256 suffix: same structure, but with 32-byte OIDs and SHA-256
* pack checksum (609 bytes vs 513 for SHA-1).
*/
static const unsigned char fast_pack_sha256_suffix[] = {
0xac, 0x02, 0x78, 0x01, 0x01, 0x2c, 0x00, 0xd3,
0xff, 0x31, 0x30, 0x30, 0x36, 0x34, 0x34, 0x20,
0x66, 0x69, 0x6c, 0x65, 0x00, 0x42, 0x53, 0xc1,
0x8a, 0x9f, 0x5e, 0xc3, 0xbb, 0x47, 0xb0, 0x83,
0x8a, 0x19, 0xdb, 0x31, 0xbb, 0x7b, 0x0f, 0x3b,
0x80, 0xa4, 0xbc, 0x2f, 0xaf, 0x72, 0x6b, 0xdb,
0x62, 0xaa, 0xba, 0xdd, 0xde, 0x77, 0xc6, 0x13,
0xeb, 0x9d, 0x0c, 0x78, 0x01, 0x01, 0xcd, 0x00,
0x32, 0xff, 0x74, 0x72, 0x65, 0x65, 0x20, 0x62,
0x36, 0x30, 0x39, 0x37, 0x37, 0x64, 0x37, 0x63,
0x34, 0x63, 0x32, 0x64, 0x31, 0x65, 0x63, 0x63,
0x33, 0x66, 0x62, 0x61, 0x31, 0x64, 0x39, 0x38,
0x65, 0x65, 0x31, 0x32, 0x30, 0x61, 0x64, 0x63,
0x32, 0x34, 0x38, 0x33, 0x34, 0x39, 0x35, 0x30,
0x62, 0x65, 0x34, 0x31, 0x32, 0x64, 0x39, 0x34,
0x63, 0x38, 0x30, 0x39, 0x34, 0x38, 0x30, 0x66,
0x35, 0x38, 0x62, 0x61, 0x39, 0x64, 0x61, 0x0a,
0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x20, 0x41,
0x20, 0x55, 0x20, 0x54, 0x68, 0x6f, 0x72, 0x20,
0x3c, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x40,
0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
0x63, 0x6f, 0x6d, 0x3e, 0x20, 0x31, 0x32, 0x33,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x20,
0x2b, 0x30, 0x30, 0x30, 0x30, 0x0a, 0x63, 0x6f,
0x6d, 0x6d, 0x69, 0x74, 0x74, 0x65, 0x72, 0x20,
0x43, 0x20, 0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74,
0x65, 0x72, 0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d,
0x69, 0x74, 0x74, 0x65, 0x72, 0x40, 0x65, 0x78,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f,
0x6d, 0x3e, 0x20, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x30, 0x20, 0x2b, 0x30,
0x30, 0x30, 0x30, 0x0a, 0x0a, 0x4c, 0x61, 0x72,
0x67, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x62, 0x20,
0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x0a, 0xb7,
0x80, 0x3d, 0xd7, 0x20, 0x78, 0x01, 0x01, 0x00,
0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x95,
0x11, 0x78, 0x01, 0x01, 0x15, 0x01, 0xea, 0xfe,
0x74, 0x72, 0x65, 0x65, 0x20, 0x36, 0x65, 0x66,
0x31, 0x39, 0x62, 0x34, 0x31, 0x32, 0x32, 0x35,
0x63, 0x35, 0x33, 0x36, 0x39, 0x66, 0x31, 0x63,
0x31, 0x30, 0x34, 0x64, 0x34, 0x35, 0x64, 0x38,
0x64, 0x38, 0x35, 0x65, 0x66, 0x61, 0x39, 0x62,
0x30, 0x35, 0x37, 0x62, 0x35, 0x33, 0x62, 0x31,
0x34, 0x62, 0x34, 0x62, 0x39, 0x62, 0x39, 0x33,
0x39, 0x64, 0x64, 0x37, 0x34, 0x64, 0x65, 0x63,
0x63, 0x35, 0x33, 0x32, 0x31, 0x0a, 0x70, 0x61,
0x72, 0x65, 0x6e, 0x74, 0x20, 0x37, 0x35, 0x62,
0x66, 0x30, 0x63, 0x34, 0x37, 0x61, 0x65, 0x34,
0x62, 0x62, 0x33, 0x30, 0x38, 0x65, 0x37, 0x63,
0x63, 0x32, 0x34, 0x38, 0x32, 0x65, 0x32, 0x32,
0x65, 0x66, 0x61, 0x65, 0x33, 0x37, 0x38, 0x37,
0x61, 0x39, 0x36, 0x38, 0x34, 0x38, 0x62, 0x64,
0x31, 0x37, 0x34, 0x39, 0x35, 0x36, 0x37, 0x31,
0x34, 0x37, 0x31, 0x35, 0x32, 0x34, 0x36, 0x64,
0x64, 0x62, 0x64, 0x35, 0x34, 0x0a, 0x61, 0x75,
0x74, 0x68, 0x6f, 0x72, 0x20, 0x41, 0x20, 0x55,
0x20, 0x54, 0x68, 0x6f, 0x72, 0x20, 0x3c, 0x61,
0x75, 0x74, 0x68, 0x6f, 0x72, 0x40, 0x65, 0x78,
0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f,
0x6d, 0x3e, 0x20, 0x31, 0x32, 0x33, 0x34, 0x35,
0x36, 0x37, 0x38, 0x39, 0x30, 0x20, 0x2b, 0x30,
0x30, 0x30, 0x30, 0x0a, 0x63, 0x6f, 0x6d, 0x6d,
0x69, 0x74, 0x74, 0x65, 0x72, 0x20, 0x43, 0x20,
0x4f, 0x20, 0x4d, 0x69, 0x74, 0x74, 0x65, 0x72,
0x20, 0x3c, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74,
0x74, 0x65, 0x72, 0x40, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x3e,
0x20, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x30, 0x20, 0x2b, 0x30, 0x30, 0x30,
0x30, 0x0a, 0x0a, 0x45, 0x6d, 0x70, 0x74, 0x79,
0x20, 0x74, 0x72, 0x65, 0x65, 0x20, 0x63, 0x6f,
0x6d, 0x6d, 0x69, 0x74, 0x0a, 0x6d, 0x6d, 0x51,
0x9a, 0xc9, 0x11, 0x76, 0x61, 0xa3, 0x89, 0x49,
0xb7, 0xa1, 0x58, 0xc6, 0x1d, 0x8c, 0x33, 0x75,
0x8d, 0x7e, 0x4d, 0x8e, 0x58, 0x91, 0xf8, 0x5c,
0x57, 0xd9, 0x89, 0x9e, 0xb8, 0xd2, 0x9a, 0xd8,
0xc9
};
static const struct fast_pack fast_packs[] = {
{
.format_id = GIT_SHA1_FORMAT_ID,
.suffix = fast_pack_sha1_suffix,
.suffix_len = sizeof(fast_pack_sha1_suffix),
.commit_oid = "aac43daf40d0377af31aa9c798a4ae8a31b55c1d",
},
{
.format_id = GIT_SHA256_FORMAT_ID,
.suffix = fast_pack_sha256_suffix,
.suffix_len = sizeof(fast_pack_sha256_suffix),
.commit_oid = "63c46ca51267b1d45be69a044bb84b4bf0559f09"
"d727f861d2ae94ddebdddbc9",
},
};
/*
* Try the fast path for known blob sizes. Returns 1 if the pack was
* written from precomputed constants, 0 if the caller should fall
* through to the generic path.
*/
static int generate_fast_pack(const char *path, size_t blob_size,
const struct git_hash_algo *algo)
{
const struct fast_pack *fp = NULL;
FILE *f;
size_t i;
if (blob_size != FAST_PACK_4G1_BLOB_SIZE)
return 0;
for (i = 0; i < ARRAY_SIZE(fast_packs); i++) {
if (fast_packs[i].format_id == algo->format_id) {
fp = &fast_packs[i];
break;
}
}
if (!fp)
return 0;
f = xfopen(path, "wb");
fwrite_or_die(f, fast_pack_prefix, sizeof(fast_pack_prefix));
/* First full block: 0xffff zero bytes (header already in prefix) */
fwrite_or_die(f, zeros, BLOCK_SIZE);
/* Remaining non-final full blocks */
for (i = 1; i < FAST_PACK_4G1_N_FULL_BLOCKS; i++) {
fwrite_or_die(f, fast_pack_block_header,
sizeof(fast_pack_block_header));
fwrite_or_die(f, zeros, BLOCK_SIZE);
}
/* Final block (2 data bytes) + adler32 */
fwrite_or_die(f, fast_pack_final_block,
sizeof(fast_pack_final_block));
/* Tree, commits, and pack checksum */
fwrite_or_die(f, fp->suffix, fp->suffix_len);
if (fclose(f))
die_errno(_("could not close '%s'"), path);
printf("%s\n", fp->commit_oid);
return 1;
}
/*
* Generate a pack file with a single large (>4GB) reachable object.
*
* Creates:
* 1. A large blob (all NUL bytes)
* 2. A tree containing that blob as "file"
* 3. A commit using that tree
* 4. The empty tree
* 5. A child commit using the empty tree
*
* This is useful for testing that Git can handle objects larger than 4GB.
*/
static int generate_pack_with_large_object(const char *path, size_t blob_size,
const struct git_hash_algo *algo)
{
FILE *f;
struct git_hash_ctx pack_ctx;
unsigned char pack_hash[GIT_MAX_RAWSZ];
struct object_id blob_oid, tree_oid, commit_oid, empty_tree_oid, final_commit_oid;
struct strbuf buf = STRBUF_INIT;
const uint32_t object_count = 5;
struct pack_header pack_header = {
.hdr_signature = htonl(PACK_SIGNATURE),
.hdr_version = htonl(PACK_VERSION),
.hdr_entries = htonl(object_count),
};
if (generate_fast_pack(path, blob_size, algo))
return 0;
f = xfopen(path, "wb");
git_hash_init(&pack_ctx, algo);
/* Write pack header */
fwrite_or_die(f, &pack_header, sizeof(pack_header));
git_hash_update(&pack_ctx, &pack_header, sizeof(pack_header));
/* 1. Write the large blob */
write_pack_object(f, &pack_ctx, OBJ_BLOB, NULL, blob_size, &blob_oid, algo);
/* 2. Write tree containing the blob as "file" */
strbuf_addf(&buf, "100644 file%c", '\0');
strbuf_add(&buf, blob_oid.hash, algo->rawsz);
write_pack_object(f, &pack_ctx, OBJ_TREE, buf.buf, buf.len, &tree_oid, algo);
/* 3. Write commit using that tree */
strbuf_reset(&buf);
strbuf_addf(&buf,
"tree %s\n"
"author A U Thor <author@example.com> 1234567890 +0000\n"
"committer C O Mitter <committer@example.com> 1234567890 +0000\n"
"\n"
"Large blob commit\n",
oid_to_hex(&tree_oid));
write_pack_object(f, &pack_ctx, OBJ_COMMIT, buf.buf, buf.len, &commit_oid, algo);
/* 4. Write the empty tree */
write_pack_object(f, &pack_ctx, OBJ_TREE, "", 0, &empty_tree_oid, algo);
/* 5. Write final commit using empty tree, with previous commit as parent */
strbuf_reset(&buf);
strbuf_addf(&buf,
"tree %s\n"
"parent %s\n"
"author A U Thor <author@example.com> 1234567890 +0000\n"
"committer C O Mitter <committer@example.com> 1234567890 +0000\n"
"\n"
"Empty tree commit\n",
oid_to_hex(&empty_tree_oid),
oid_to_hex(&commit_oid));
write_pack_object(f, &pack_ctx, OBJ_COMMIT, buf.buf, buf.len, &final_commit_oid, algo);
/* Write pack trailer (checksum) */
git_hash_final(pack_hash, &pack_ctx);
fwrite_or_die(f, pack_hash, algo->rawsz);
if (fclose(f))
die_errno(_("could not close '%s'"), path);
strbuf_release(&buf);
/* Print the final commit OID so caller can set up refs */
printf("%s\n", oid_to_hex(&final_commit_oid));
return 0;
}
static int cmd__synthesize__pack(int argc, const char **argv,
const char *prefix UNUSED,
struct repository *repo)
{
int non_git;
int reachable_large = 0;
const struct git_hash_algo *algo;
size_t blob_size;
uintmax_t blob_size_u;
const char *path;
const char * const usage[] = {
"test-tool synthesize pack "
"--reachable-large <blob-size> <filename>",
NULL
};
struct option options[] = {
OPT_BOOL(0, "reachable-large", &reachable_large,
N_("write a pack with a single reachable large blob")),
OPT_END()
};
setup_git_directory_gently(the_repository, &non_git);
repo = the_repository;
algo = unsafe_hash_algo(repo->hash_algo);
argc = parse_options(argc, argv, NULL, options, usage,
PARSE_OPT_KEEP_ARGV0);
if (argc != 3 || !reachable_large)
usage_with_options(usage, options);
if (!git_parse_unsigned(argv[1], &blob_size_u,
maximum_unsigned_value_of_type(size_t)))
die(_("'%s' is not a valid blob size"), argv[1]);
blob_size = blob_size_u;
path = argv[2];
return !!generate_pack_with_large_object(path, blob_size, algo);
}
int cmd__synthesize(int argc, const char **argv)
{
const char *prefix = NULL;
char const * const synthesize_usage[] = {
"test-tool synthesize pack <options>",
NULL,
};
parse_opt_subcommand_fn *fn = NULL;
struct option options[] = {
OPT_SUBCOMMAND("pack", &fn, cmd__synthesize__pack),
OPT_END()
};
argc = parse_options(argc, argv, prefix, options, synthesize_usage, 0);
return !!fn(argc, argv, prefix, NULL);
}
|