]> git.ipfire.org Git - thirdparty/git.git/blame - index-pack.c
improve index-pack tests
[thirdparty/git.git] / index-pack.c
CommitLineData
9cf6d335
SV
1#include "cache.h"
2#include "delta.h"
3#include "pack.h"
4#include "csum-file.h"
8e440259
PE
5#include "blob.h"
6#include "commit.h"
7#include "tag.h"
8#include "tree.h"
96a02f8f 9#include "progress.h"
0153be05 10#include "fsck.h"
9cf6d335
SV
11
12static const char index_pack_usage[] =
1b1dd23f 13"git index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] [--strict] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
9cf6d335
SV
14
15struct object_entry
16{
aa7e44bf 17 struct pack_idx_entry idx;
2d477051
NP
18 unsigned long size;
19 unsigned int hdr_size;
9cf6d335
SV
20 enum object_type type;
21 enum object_type real_type;
9cf6d335
SV
22};
23
53dda6ff
NP
24union delta_base {
25 unsigned char sha1[20];
d7dd0223 26 off_t offset;
53dda6ff
NP
27};
28
f41aebd4 29struct base_data {
4a438cab
SP
30 struct base_data *base;
31 struct base_data *child;
03993e13 32 struct object_entry *obj;
f41aebd4
SP
33 void *data;
34 unsigned long size;
35};
36
3c552873
NP
37/*
38 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
39 * to memcmp() only the first 20 bytes.
40 */
41#define UNION_BASE_SZ 20
42
0153be05
MK
43#define FLAG_LINK (1u<<20)
44#define FLAG_CHECKED (1u<<21)
45
9cf6d335
SV
46struct delta_entry
47{
53dda6ff 48 union delta_base base;
636171cb 49 int obj_no;
9cf6d335
SV
50};
51
9cf6d335
SV
52static struct object_entry *objects;
53static struct delta_entry *deltas;
4a438cab 54static struct base_data *base_cache;
92392b4a 55static size_t base_cache_used;
9cf6d335
SV
56static int nr_objects;
57static int nr_deltas;
636171cb 58static int nr_resolved_deltas;
9cf6d335 59
e42797f5 60static int from_stdin;
0153be05 61static int strict;
3c9af366
NP
62static int verbose;
63
dc6a0757 64static struct progress *progress;
e42797f5 65
2d477051
NP
66/* We always read in 4kB chunks. */
67static unsigned char input_buffer[4096];
d7dd0223
NP
68static unsigned int input_offset, input_len;
69static off_t consumed_bytes;
9126f009 70static git_SHA_CTX input_ctx;
ee5743ce 71static uint32_t input_crc32;
6d2fa7f1 72static int input_fd, output_fd, pack_fd;
2d477051 73
0153be05
MK
74static int mark_link(struct object *obj, int type, void *data)
75{
76 if (!obj)
77 return -1;
78
79 if (type != OBJ_ANY && obj->type != type)
80 die("object type mismatch at %s", sha1_to_hex(obj->sha1));
81
82 obj->flags |= FLAG_LINK;
83 return 0;
84}
85
86/* The content of each linked object must have been checked
87 or it must be already present in the object database */
88static void check_object(struct object *obj)
89{
90 if (!obj)
91 return;
92
93 if (!(obj->flags & FLAG_LINK))
94 return;
95
96 if (!(obj->flags & FLAG_CHECKED)) {
97 unsigned long size;
98 int type = sha1_object_info(obj->sha1, &size);
99 if (type != obj->type || type <= 0)
100 die("object of unexpected type");
101 obj->flags |= FLAG_CHECKED;
102 return;
103 }
104}
105
106static void check_objects(void)
107{
108 unsigned i, max;
109
110 max = get_max_object_index();
111 for (i = 0; i < max; i++)
112 check_object(get_indexed_object(i));
113}
114
115
636171cb 116/* Discard current buffer used content. */
a6e8a767 117static void flush(void)
636171cb
NP
118{
119 if (input_offset) {
120 if (output_fd >= 0)
121 write_or_die(output_fd, input_buffer, input_offset);
9126f009 122 git_SHA1_Update(&input_ctx, input_buffer, input_offset);
554a2636 123 memmove(input_buffer, input_buffer + input_offset, input_len);
636171cb
NP
124 input_offset = 0;
125 }
126}
127
2d477051
NP
128/*
129 * Make sure at least "min" bytes are available in the buffer, and
130 * return the pointer to the buffer.
131 */
b89c4e93 132static void *fill(int min)
9cf6d335 133{
2d477051
NP
134 if (min <= input_len)
135 return input_buffer + input_offset;
136 if (min > sizeof(input_buffer))
137 die("cannot fill %d bytes", min);
636171cb 138 flush();
2d477051 139 do {
8a912bcb 140 ssize_t ret = xread(input_fd, input_buffer + input_len,
2d477051
NP
141 sizeof(input_buffer) - input_len);
142 if (ret <= 0) {
143 if (!ret)
144 die("early EOF");
145 die("read error on input: %s", strerror(errno));
146 }
147 input_len += ret;
218558af
NP
148 if (from_stdin)
149 display_throughput(progress, consumed_bytes + input_len);
2d477051
NP
150 } while (input_len < min);
151 return input_buffer;
152}
153
154static void use(int bytes)
155{
156 if (bytes > input_len)
157 die("used more bytes than were available");
ee5743ce 158 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
2d477051
NP
159 input_len -= bytes;
160 input_offset += bytes;
d7dd0223
NP
161
162 /* make sure off_t is sufficiently large not to wrap */
163 if (consumed_bytes > consumed_bytes + bytes)
164 die("pack too large for current definition of off_t");
2d477051
NP
165 consumed_bytes += bytes;
166}
9cf6d335 167
4049b9cf 168static char *open_pack_file(char *pack_name)
2d477051 169{
e42797f5
NP
170 if (from_stdin) {
171 input_fd = 0;
172 if (!pack_name) {
173 static char tmpfile[PATH_MAX];
174 snprintf(tmpfile, sizeof(tmpfile),
8b4eb6b6 175 "%s/pack/tmp_pack_XXXXXX", get_object_directory());
7647b17f 176 output_fd = xmkstemp(tmpfile);
e42797f5
NP
177 pack_name = xstrdup(tmpfile);
178 } else
179 output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
180 if (output_fd < 0)
181 die("unable to create %s: %s\n", pack_name, strerror(errno));
6d2fa7f1 182 pack_fd = output_fd;
e42797f5
NP
183 } else {
184 input_fd = open(pack_name, O_RDONLY);
185 if (input_fd < 0)
186 die("cannot open packfile '%s': %s",
187 pack_name, strerror(errno));
188 output_fd = -1;
6d2fa7f1 189 pack_fd = input_fd;
e42797f5 190 }
9126f009 191 git_SHA1_Init(&input_ctx);
e42797f5 192 return pack_name;
9cf6d335
SV
193}
194
195static void parse_pack_header(void)
196{
2d477051 197 struct pack_header *hdr = fill(sizeof(struct pack_header));
9cf6d335
SV
198
199 /* Header consistency check */
9cf6d335 200 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
e42797f5 201 die("pack signature mismatch");
d60fc1c8 202 if (!pack_version_ok(hdr->hdr_version))
6e1c2344
RJ
203 die("pack version %"PRIu32" unsupported",
204 ntohl(hdr->hdr_version));
9cf6d335
SV
205
206 nr_objects = ntohl(hdr->hdr_entries);
2d477051 207 use(sizeof(struct pack_header));
9cf6d335
SV
208}
209
210static void bad_object(unsigned long offset, const char *format,
211 ...) NORETURN __attribute__((format (printf, 2, 3)));
212
213static void bad_object(unsigned long offset, const char *format, ...)
214{
215 va_list params;
216 char buf[1024];
217
218 va_start(params, format);
219 vsnprintf(buf, sizeof(buf), format, params);
220 va_end(params);
e42797f5 221 die("pack has bad object at offset %lu: %s", offset, buf);
9cf6d335
SV
222}
223
6a87ed97
NP
224static void free_base_data(struct base_data *c)
225{
226 if (c->data) {
227 free(c->data);
228 c->data = NULL;
229 base_cache_used -= c->size;
230 }
231}
232
92392b4a
SP
233static void prune_base_data(struct base_data *retain)
234{
235 struct base_data *b = base_cache;
236 for (b = base_cache;
237 base_cache_used > delta_base_cache_limit && b;
238 b = b->child) {
6a87ed97
NP
239 if (b->data && b != retain)
240 free_base_data(b);
92392b4a
SP
241 }
242}
243
4a438cab
SP
244static void link_base_data(struct base_data *base, struct base_data *c)
245{
246 if (base)
247 base->child = c;
248 else
249 base_cache = c;
250
251 c->base = base;
252 c->child = NULL;
9441b61d
NP
253 if (c->data)
254 base_cache_used += c->size;
92392b4a 255 prune_base_data(c);
4a438cab
SP
256}
257
258static void unlink_base_data(struct base_data *c)
259{
260 struct base_data *base = c->base;
261 if (base)
262 base->child = NULL;
263 else
264 base_cache = NULL;
6a87ed97 265 free_base_data(c);
4a438cab
SP
266}
267
2d477051 268static void *unpack_entry_data(unsigned long offset, unsigned long size)
9cf6d335 269{
9cf6d335
SV
270 z_stream stream;
271 void *buf = xmalloc(size);
272
273 memset(&stream, 0, sizeof(stream));
274 stream.next_out = buf;
275 stream.avail_out = size;
2d477051
NP
276 stream.next_in = fill(1);
277 stream.avail_in = input_len;
9cf6d335
SV
278 inflateInit(&stream);
279
280 for (;;) {
281 int ret = inflate(&stream, 0);
2d477051
NP
282 use(input_len - stream.avail_in);
283 if (stream.total_out == size && ret == Z_STREAM_END)
9cf6d335
SV
284 break;
285 if (ret != Z_OK)
286 bad_object(offset, "inflate returned %d", ret);
2d477051
NP
287 stream.next_in = fill(1);
288 stream.avail_in = input_len;
9cf6d335
SV
289 }
290 inflateEnd(&stream);
9cf6d335
SV
291 return buf;
292}
293
2d477051 294static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
9cf6d335 295{
2d477051 296 unsigned char *p, c;
d7dd0223
NP
297 unsigned long size;
298 off_t base_offset;
9cf6d335 299 unsigned shift;
ee5743ce 300 void *data;
9cf6d335 301
aa7e44bf 302 obj->idx.offset = consumed_bytes;
ee5743ce 303 input_crc32 = crc32(0, Z_NULL, 0);
2d477051
NP
304
305 p = fill(1);
306 c = *p;
307 use(1);
308 obj->type = (c >> 4) & 7;
9cf6d335
SV
309 size = (c & 15);
310 shift = 4;
311 while (c & 0x80) {
2d477051
NP
312 p = fill(1);
313 c = *p;
314 use(1);
9cf6d335
SV
315 size += (c & 0x7fUL) << shift;
316 shift += 7;
317 }
2d477051 318 obj->size = size;
9cf6d335 319
2d477051 320 switch (obj->type) {
eb32d236 321 case OBJ_REF_DELTA:
2d477051
NP
322 hashcpy(delta_base->sha1, fill(20));
323 use(20);
53dda6ff
NP
324 break;
325 case OBJ_OFS_DELTA:
326 memset(delta_base, 0, sizeof(*delta_base));
2d477051
NP
327 p = fill(1);
328 c = *p;
329 use(1);
53dda6ff
NP
330 base_offset = c & 127;
331 while (c & 128) {
332 base_offset += 1;
8723f216 333 if (!base_offset || MSB(base_offset, 7))
aa7e44bf 334 bad_object(obj->idx.offset, "offset value overflow for delta base object");
2d477051
NP
335 p = fill(1);
336 c = *p;
337 use(1);
53dda6ff
NP
338 base_offset = (base_offset << 7) + (c & 127);
339 }
aa7e44bf
GB
340 delta_base->offset = obj->idx.offset - base_offset;
341 if (delta_base->offset >= obj->idx.offset)
342 bad_object(obj->idx.offset, "delta base offset is out of bound");
53dda6ff 343 break;
9cf6d335
SV
344 case OBJ_COMMIT:
345 case OBJ_TREE:
346 case OBJ_BLOB:
347 case OBJ_TAG:
9cf6d335
SV
348 break;
349 default:
aa7e44bf 350 bad_object(obj->idx.offset, "unknown object type %d", obj->type);
9cf6d335 351 }
aa7e44bf 352 obj->hdr_size = consumed_bytes - obj->idx.offset;
2d477051 353
aa7e44bf
GB
354 data = unpack_entry_data(obj->idx.offset, obj->size);
355 obj->idx.crc32 = input_crc32;
ee5743ce 356 return data;
2d477051
NP
357}
358
b89c4e93 359static void *get_data_from_pack(struct object_entry *obj)
2d477051 360{
a91ef6e7 361 off_t from = obj[0].idx.offset + obj[0].hdr_size;
aa7e44bf 362 unsigned long len = obj[1].idx.offset - from;
a91d49cd 363 unsigned long rdy = 0;
6d2fa7f1 364 unsigned char *src, *data;
2d477051
NP
365 z_stream stream;
366 int st;
9cf6d335 367
6d2fa7f1 368 src = xmalloc(len);
a91d49cd
SP
369 data = src;
370 do {
371 ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
fb742436 372 if (n < 0)
a91d49cd 373 die("cannot pread pack file: %s", strerror(errno));
fb742436
ST
374 if (!n)
375 die("premature end of pack file, %lu bytes missing",
376 len - rdy);
a91d49cd
SP
377 rdy += n;
378 } while (rdy < len);
2d477051
NP
379 data = xmalloc(obj->size);
380 memset(&stream, 0, sizeof(stream));
381 stream.next_out = data;
382 stream.avail_out = obj->size;
6d2fa7f1 383 stream.next_in = src;
2d477051
NP
384 stream.avail_in = len;
385 inflateInit(&stream);
386 while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
387 inflateEnd(&stream);
388 if (st != Z_STREAM_END || stream.total_out != obj->size)
389 die("serious inflate inconsistency");
6d2fa7f1 390 free(src);
9cf6d335
SV
391 return data;
392}
393
53dda6ff 394static int find_delta(const union delta_base *base)
9cf6d335
SV
395{
396 int first = 0, last = nr_deltas;
397
398 while (first < last) {
399 int next = (first + last) / 2;
400 struct delta_entry *delta = &deltas[next];
401 int cmp;
402
3c552873 403 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
9cf6d335
SV
404 if (!cmp)
405 return next;
406 if (cmp < 0) {
407 last = next;
408 continue;
409 }
410 first = next+1;
411 }
412 return -first-1;
413}
414
6a87ed97
NP
415static void find_delta_children(const union delta_base *base,
416 int *first_index, int *last_index)
9cf6d335 417{
53dda6ff 418 int first = find_delta(base);
9cf6d335
SV
419 int last = first;
420 int end = nr_deltas - 1;
421
6a87ed97
NP
422 if (first < 0) {
423 *first_index = 0;
424 *last_index = -1;
425 return;
426 }
3c552873 427 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
9cf6d335 428 --first;
3c552873 429 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
9cf6d335
SV
430 ++last;
431 *first_index = first;
432 *last_index = last;
9cf6d335
SV
433}
434
435static void sha1_object(const void *data, unsigned long size,
9096c660 436 enum object_type type, unsigned char *sha1)
9cf6d335 437{
ce9fbf16 438 hash_sha1_file(data, size, typename(type), sha1);
9096c660 439 if (has_sha1_file(sha1)) {
8685da42
NP
440 void *has_data;
441 enum object_type has_type;
442 unsigned long has_size;
443 has_data = read_sha1_file(sha1, &has_type, &has_size);
444 if (!has_data)
445 die("cannot read existing object %s", sha1_to_hex(sha1));
446 if (size != has_size || type != has_type ||
447 memcmp(data, has_data, size) != 0)
448 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
bbf4b41b 449 free(has_data);
8685da42 450 }
0153be05
MK
451 if (strict) {
452 if (type == OBJ_BLOB) {
453 struct blob *blob = lookup_blob(sha1);
454 if (blob)
455 blob->object.flags |= FLAG_CHECKED;
456 else
457 die("invalid blob object %s", sha1_to_hex(sha1));
458 } else {
459 struct object *obj;
460 int eaten;
461 void *buf = (void *) data;
462
463 /*
464 * we do not need to free the memory here, as the
465 * buf is deleted by the caller.
466 */
467 obj = parse_object_buffer(sha1, type, size, buf, &eaten);
468 if (!obj)
469 die("invalid %s", typename(type));
470 if (fsck_object(obj, 1, fsck_error_function))
471 die("Error in object");
472 if (fsck_walk(obj, mark_link, 0))
473 die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1));
474
475 if (obj->type == OBJ_TREE) {
476 struct tree *item = (struct tree *) obj;
477 item->buffer = NULL;
478 }
479 if (obj->type == OBJ_COMMIT) {
480 struct commit *commit = (struct commit *) obj;
481 commit->buffer = NULL;
482 }
483 obj->flags |= FLAG_CHECKED;
484 }
485 }
9cf6d335
SV
486}
487
92392b4a
SP
488static void *get_base_data(struct base_data *c)
489{
490 if (!c->data) {
491 struct object_entry *obj = c->obj;
492
493 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
494 void *base = get_base_data(c->base);
495 void *raw = get_data_from_pack(obj);
496 c->data = patch_delta(
497 base, c->base->size,
498 raw, obj->size,
499 &c->size);
500 free(raw);
501 if (!c->data)
502 bad_object(obj->idx.offset, "failed to apply delta");
9441b61d 503 } else {
92392b4a 504 c->data = get_data_from_pack(obj);
9441b61d
NP
505 c->size = obj->size;
506 }
92392b4a
SP
507
508 base_cache_used += c->size;
509 prune_base_data(c);
510 }
511 return c->data;
512}
513
f41aebd4 514static void resolve_delta(struct object_entry *delta_obj,
9441b61d 515 struct base_data *base, struct base_data *result)
9cf6d335 516{
ce3f6dc6 517 void *base_data, *delta_data;
9cf6d335 518
ce3f6dc6 519 delta_obj->real_type = base->obj->real_type;
636171cb 520 delta_data = get_data_from_pack(delta_obj);
ce3f6dc6 521 base_data = get_base_data(base);
9441b61d 522 result->obj = delta_obj;
ce3f6dc6
NP
523 result->data = patch_delta(base_data, base->size,
524 delta_data, delta_obj->size, &result->size);
9cf6d335 525 free(delta_data);
9441b61d 526 if (!result->data)
aa7e44bf 527 bad_object(delta_obj->idx.offset, "failed to apply delta");
9441b61d
NP
528 sha1_object(result->data, result->size, delta_obj->real_type,
529 delta_obj->idx.sha1);
636171cb 530 nr_resolved_deltas++;
9441b61d
NP
531}
532
533static void find_unresolved_deltas(struct base_data *base,
534 struct base_data *prev_base)
535{
6a87ed97 536 int i, ref_first, ref_last, ofs_first, ofs_last;
9441b61d
NP
537
538 /*
539 * This is a recursive function. Those brackets should help reducing
540 * stack usage by limiting the scope of the delta_base union.
541 */
542 {
543 union delta_base base_spec;
544
545 hashcpy(base_spec.sha1, base->obj->idx.sha1);
6a87ed97 546 find_delta_children(&base_spec, &ref_first, &ref_last);
9441b61d
NP
547
548 memset(&base_spec, 0, sizeof(base_spec));
549 base_spec.offset = base->obj->idx.offset;
6a87ed97 550 find_delta_children(&base_spec, &ofs_first, &ofs_last);
9441b61d
NP
551 }
552
6a87ed97 553 if (ref_last == -1 && ofs_last == -1)
9441b61d 554 return;
53dda6ff 555
9441b61d 556 link_base_data(prev_base, base);
4a438cab 557
6a87ed97
NP
558 for (i = ref_first; i <= ref_last; i++) {
559 struct object_entry *child = objects + deltas[i].obj_no;
560 if (child->real_type == OBJ_REF_DELTA) {
561 struct base_data result;
562 resolve_delta(child, base, &result);
563 if (i == ref_last && ofs_last == -1)
564 free_base_data(base);
565 find_unresolved_deltas(&result, base);
636171cb 566 }
53dda6ff
NP
567 }
568
6a87ed97
NP
569 for (i = ofs_first; i <= ofs_last; i++) {
570 struct object_entry *child = objects + deltas[i].obj_no;
571 if (child->real_type == OBJ_OFS_DELTA) {
572 struct base_data result;
573 resolve_delta(child, base, &result);
574 if (i == ofs_last)
575 free_base_data(base);
576 find_unresolved_deltas(&result, base);
636171cb 577 }
9cf6d335 578 }
53dda6ff 579
9441b61d 580 unlink_base_data(base);
9cf6d335
SV
581}
582
583static int compare_delta_entry(const void *a, const void *b)
584{
585 const struct delta_entry *delta_a = a;
586 const struct delta_entry *delta_b = b;
3c552873 587 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
9cf6d335
SV
588}
589
2d477051
NP
590/* Parse all objects and return the pack content SHA1 hash */
591static void parse_pack_objects(unsigned char *sha1)
9cf6d335 592{
96a02f8f 593 int i;
53dda6ff 594 struct delta_entry *delta = deltas;
2d477051 595 struct stat st;
9cf6d335
SV
596
597 /*
598 * First pass:
599 * - find locations of all objects;
600 * - calculate SHA1 of all non-delta objects;
b89c4e93 601 * - remember base (SHA1 or offset) for all deltas.
9cf6d335 602 */
13aaf148 603 if (verbose)
29e63ed3
NP
604 progress = start_progress(
605 from_stdin ? "Receiving objects" : "Indexing objects",
606 nr_objects);
9cf6d335
SV
607 for (i = 0; i < nr_objects; i++) {
608 struct object_entry *obj = &objects[i];
f41aebd4 609 void *data = unpack_raw_entry(obj, &delta->base);
9cf6d335 610 obj->real_type = obj->type;
53dda6ff
NP
611 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
612 nr_deltas++;
636171cb 613 delta->obj_no = i;
53dda6ff 614 delta++;
9cf6d335 615 } else
aa7e44bf 616 sha1_object(data, obj->size, obj->type, obj->idx.sha1);
9cf6d335 617 free(data);
4d4fcc54 618 display_progress(progress, i+1);
9cf6d335 619 }
aa7e44bf 620 objects[i].idx.offset = consumed_bytes;
4d4fcc54 621 stop_progress(&progress);
2d477051
NP
622
623 /* Check pack integrity */
636171cb 624 flush();
9126f009 625 git_SHA1_Final(sha1, &input_ctx);
2d477051 626 if (hashcmp(fill(20), sha1))
e42797f5 627 die("pack is corrupted (SHA1 mismatch)");
9bee2478 628 use(20);
2d477051
NP
629
630 /* If input_fd is a file, we should have reached its end now. */
631 if (fstat(input_fd, &st))
e42797f5 632 die("cannot fstat packfile: %s", strerror(errno));
fa257b05
JS
633 if (S_ISREG(st.st_mode) &&
634 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
e42797f5 635 die("pack has junk at the end");
9cf6d335 636
3c9af366
NP
637 if (!nr_deltas)
638 return;
639
53dda6ff 640 /* Sort deltas by base SHA1/offset for fast searching */
9cf6d335
SV
641 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
642 compare_delta_entry);
643
644 /*
645 * Second pass:
646 * - for all non-delta objects, look if it is used as a base for
647 * deltas;
648 * - if used as a base, uncompress the object and apply all deltas,
649 * recursively checking if the resulting object is used as a base
650 * for some more deltas.
651 */
13aaf148 652 if (verbose)
dc6a0757 653 progress = start_progress("Resolving deltas", nr_deltas);
9cf6d335
SV
654 for (i = 0; i < nr_objects; i++) {
655 struct object_entry *obj = &objects[i];
f41aebd4 656 struct base_data base_obj;
9cf6d335 657
53dda6ff 658 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
9cf6d335 659 continue;
03993e13 660 base_obj.obj = obj;
9441b61d
NP
661 base_obj.data = NULL;
662 find_unresolved_deltas(&base_obj, NULL);
4d4fcc54 663 display_progress(progress, nr_resolved_deltas);
9cf6d335 664 }
636171cb
NP
665}
666
8522148f 667static int write_compressed(struct sha1file *f, void *in, unsigned int size)
636171cb
NP
668{
669 z_stream stream;
670 unsigned long maxsize;
671 void *out;
672
673 memset(&stream, 0, sizeof(stream));
674 deflateInit(&stream, zlib_compression_level);
675 maxsize = deflateBound(&stream, size);
676 out = xmalloc(maxsize);
677
678 /* Compress it */
679 stream.next_in = in;
680 stream.avail_in = size;
681 stream.next_out = out;
682 stream.avail_out = maxsize;
683 while (deflate(&stream, Z_FINISH) == Z_OK);
684 deflateEnd(&stream);
685
686 size = stream.total_out;
8522148f 687 sha1write(f, out, size);
636171cb
NP
688 free(out);
689 return size;
690}
691
8522148f 692static struct object_entry *append_obj_to_pack(struct sha1file *f,
03993e13 693 const unsigned char *sha1, void *buf,
636171cb
NP
694 unsigned long size, enum object_type type)
695{
696 struct object_entry *obj = &objects[nr_objects++];
697 unsigned char header[10];
698 unsigned long s = size;
699 int n = 0;
700 unsigned char c = (type << 4) | (s & 15);
701 s >>= 4;
702 while (s) {
703 header[n++] = c | 0x80;
704 c = s & 0x7f;
705 s >>= 7;
706 }
707 header[n++] = c;
8522148f
NP
708 crc32_begin(f);
709 sha1write(f, header, n);
72de2883
BS
710 obj[0].size = size;
711 obj[0].hdr_size = n;
712 obj[0].type = type;
713 obj[0].real_type = type;
aa7e44bf 714 obj[1].idx.offset = obj[0].idx.offset + n;
8522148f
NP
715 obj[1].idx.offset += write_compressed(f, buf, size);
716 obj[0].idx.crc32 = crc32_end(f);
838cd346 717 sha1flush(f);
aa7e44bf 718 hashcpy(obj->idx.sha1, sha1);
03993e13 719 return obj;
636171cb
NP
720}
721
722static int delta_pos_compare(const void *_a, const void *_b)
723{
724 struct delta_entry *a = *(struct delta_entry **)_a;
725 struct delta_entry *b = *(struct delta_entry **)_b;
726 return a->obj_no - b->obj_no;
727}
9cf6d335 728
8522148f 729static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
636171cb
NP
730{
731 struct delta_entry **sorted_by_pos;
96a02f8f 732 int i, n = 0;
636171cb
NP
733
734 /*
735 * Since many unresolved deltas may well be themselves base objects
736 * for more unresolved deltas, we really want to include the
737 * smallest number of base objects that would cover as much delta
738 * as possible by picking the
739 * trunc deltas first, allowing for other deltas to resolve without
740 * additional base objects. Since most base objects are to be found
741 * before deltas depending on them, a good heuristic is to start
742 * resolving deltas in the same order as their position in the pack.
743 */
744 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
9cf6d335 745 for (i = 0; i < nr_deltas; i++) {
636171cb
NP
746 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
747 continue;
748 sorted_by_pos[n++] = &deltas[i];
9cf6d335 749 }
636171cb
NP
750 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
751
752 for (i = 0; i < n; i++) {
753 struct delta_entry *d = sorted_by_pos[i];
21666f1a 754 enum object_type type;
f41aebd4 755 struct base_data base_obj;
636171cb
NP
756
757 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
758 continue;
f41aebd4
SP
759 base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size);
760 if (!base_obj.data)
636171cb 761 continue;
636171cb 762
03993e13
SP
763 if (check_sha1_signature(d->base.sha1, base_obj.data,
764 base_obj.size, typename(type)))
765 die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
8522148f
NP
766 base_obj.obj = append_obj_to_pack(f, d->base.sha1,
767 base_obj.data, base_obj.size, type);
9441b61d 768 find_unresolved_deltas(&base_obj, NULL);
4d4fcc54 769 display_progress(progress, nr_resolved_deltas);
636171cb
NP
770 }
771 free(sorted_by_pos);
772}
773
e42797f5
NP
774static void final(const char *final_pack_name, const char *curr_pack_name,
775 const char *final_index_name, const char *curr_index_name,
b8077709 776 const char *keep_name, const char *keep_msg,
e42797f5
NP
777 unsigned char *sha1)
778{
3a55602e 779 const char *report = "pack";
e42797f5
NP
780 char name[PATH_MAX];
781 int err;
782
783 if (!from_stdin) {
784 close(input_fd);
785 } else {
4c81b03e 786 fsync_or_die(output_fd, curr_pack_name);
e42797f5
NP
787 err = close(output_fd);
788 if (err)
789 die("error while closing pack file: %s", strerror(errno));
790 chmod(curr_pack_name, 0444);
791 }
792
b8077709
SP
793 if (keep_msg) {
794 int keep_fd, keep_msg_len = strlen(keep_msg);
795 if (!keep_name) {
796 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
797 get_object_directory(), sha1_to_hex(sha1));
798 keep_name = name;
799 }
9ca4a201
NP
800 keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
801 if (keep_fd < 0) {
802 if (errno != EEXIST)
803 die("cannot write keep file");
804 } else {
805 if (keep_msg_len > 0) {
806 write_or_die(keep_fd, keep_msg, keep_msg_len);
807 write_or_die(keep_fd, "\n", 1);
808 }
91c8d590
JM
809 if (close(keep_fd) != 0)
810 die("cannot write keep file");
576162a4 811 report = "keep";
b8077709 812 }
b8077709
SP
813 }
814
e42797f5
NP
815 if (final_pack_name != curr_pack_name) {
816 if (!final_pack_name) {
817 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
818 get_object_directory(), sha1_to_hex(sha1));
819 final_pack_name = name;
820 }
821 if (move_temp_to_file(curr_pack_name, final_pack_name))
822 die("cannot store pack file");
823 }
824
825 chmod(curr_index_name, 0444);
826 if (final_index_name != curr_index_name) {
827 if (!final_index_name) {
828 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
829 get_object_directory(), sha1_to_hex(sha1));
830 final_index_name = name;
831 }
832 if (move_temp_to_file(curr_index_name, final_index_name))
833 die("cannot store index file");
834 }
576162a4
NP
835
836 if (!from_stdin) {
837 printf("%s\n", sha1_to_hex(sha1));
838 } else {
839 char buf[48];
840 int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
841 report, sha1_to_hex(sha1));
d1b2ddc8 842 write_or_die(1, buf, len);
576162a4
NP
843
844 /*
845 * Let's just mimic git-unpack-objects here and write
846 * the last part of the input buffer to stdout.
847 */
848 while (input_len) {
849 err = xwrite(1, input_buffer + input_offset, input_len);
850 if (err <= 0)
851 break;
852 input_len -= err;
853 input_offset += err;
854 }
855 }
9cf6d335
SV
856}
857
ef90d6d4 858static int git_index_pack_config(const char *k, const char *v, void *cb)
4d00bda2
NP
859{
860 if (!strcmp(k, "pack.indexversion")) {
861 pack_idx_default_version = git_config_int(k, v);
862 if (pack_idx_default_version > 2)
6e1c2344
RJ
863 die("bad pack.indexversion=%"PRIu32,
864 pack_idx_default_version);
4d00bda2
NP
865 return 0;
866 }
ef90d6d4 867 return git_default_config(k, v, cb);
4d00bda2
NP
868}
869
9cf6d335
SV
870int main(int argc, char **argv)
871{
636171cb 872 int i, fix_thin_pack = 0;
4049b9cf
NP
873 char *curr_pack, *pack_name = NULL;
874 char *curr_index, *index_name = NULL;
b8077709
SP
875 const char *keep_name = NULL, *keep_msg = NULL;
876 char *index_name_buf = NULL, *keep_name_buf = NULL;
aa7e44bf 877 struct pack_idx_entry **idx_objects;
8522148f 878 unsigned char pack_sha1[20];
d0b92a3f 879 int nongit = 0;
9cf6d335 880
d0b92a3f 881 setup_git_directory_gently(&nongit);
ef90d6d4 882 git_config(git_index_pack_config, NULL);
4d00bda2 883
9cf6d335 884 for (i = 1; i < argc; i++) {
4049b9cf 885 char *arg = argv[i];
9cf6d335
SV
886
887 if (*arg == '-') {
e42797f5
NP
888 if (!strcmp(arg, "--stdin")) {
889 from_stdin = 1;
636171cb
NP
890 } else if (!strcmp(arg, "--fix-thin")) {
891 fix_thin_pack = 1;
0153be05
MK
892 } else if (!strcmp(arg, "--strict")) {
893 strict = 1;
b8077709
SP
894 } else if (!strcmp(arg, "--keep")) {
895 keep_msg = "";
cc44c765 896 } else if (!prefixcmp(arg, "--keep=")) {
b8077709 897 keep_msg = arg + 7;
cc44c765 898 } else if (!prefixcmp(arg, "--pack_header=")) {
bed006fb
NP
899 struct pack_header *hdr;
900 char *c;
901
902 hdr = (struct pack_header *)input_buffer;
903 hdr->hdr_signature = htonl(PACK_SIGNATURE);
904 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
905 if (*c != ',')
906 die("bad %s", arg);
907 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
908 if (*c)
909 die("bad %s", arg);
910 input_len = sizeof(*hdr);
3c9af366
NP
911 } else if (!strcmp(arg, "-v")) {
912 verbose = 1;
e42797f5 913 } else if (!strcmp(arg, "-o")) {
9cf6d335
SV
914 if (index_name || (i+1) >= argc)
915 usage(index_pack_usage);
916 index_name = argv[++i];
4ba7d711
NP
917 } else if (!prefixcmp(arg, "--index-version=")) {
918 char *c;
aa7e44bf
GB
919 pack_idx_default_version = strtoul(arg + 16, &c, 10);
920 if (pack_idx_default_version > 2)
4ba7d711
NP
921 die("bad %s", arg);
922 if (*c == ',')
aa7e44bf
GB
923 pack_idx_off32_limit = strtoul(c+1, &c, 0);
924 if (*c || pack_idx_off32_limit & 0x80000000)
4ba7d711 925 die("bad %s", arg);
9cf6d335
SV
926 } else
927 usage(index_pack_usage);
928 continue;
929 }
930
931 if (pack_name)
932 usage(index_pack_usage);
933 pack_name = arg;
934 }
935
e42797f5 936 if (!pack_name && !from_stdin)
9cf6d335 937 usage(index_pack_usage);
636171cb
NP
938 if (fix_thin_pack && !from_stdin)
939 die("--fix-thin cannot be used without --stdin");
e42797f5 940 if (!index_name && pack_name) {
9cf6d335 941 int len = strlen(pack_name);
5bb1cda5 942 if (!has_extension(pack_name, ".pack"))
9cf6d335
SV
943 die("packfile name '%s' does not end with '.pack'",
944 pack_name);
6689f087 945 index_name_buf = xmalloc(len);
9cf6d335
SV
946 memcpy(index_name_buf, pack_name, len - 5);
947 strcpy(index_name_buf + len - 5, ".idx");
948 index_name = index_name_buf;
949 }
b8077709
SP
950 if (keep_msg && !keep_name && pack_name) {
951 int len = strlen(pack_name);
952 if (!has_extension(pack_name, ".pack"))
953 die("packfile name '%s' does not end with '.pack'",
954 pack_name);
955 keep_name_buf = xmalloc(len);
956 memcpy(keep_name_buf, pack_name, len - 5);
957 strcpy(keep_name_buf + len - 5, ".keep");
958 keep_name = keep_name_buf;
959 }
9cf6d335 960
e42797f5 961 curr_pack = open_pack_file(pack_name);
9cf6d335 962 parse_pack_header();
636171cb
NP
963 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
964 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
8522148f 965 parse_pack_objects(pack_sha1);
96a02f8f 966 if (nr_deltas == nr_resolved_deltas) {
4d4fcc54 967 stop_progress(&progress);
96a02f8f
NP
968 /* Flush remaining pack final 20-byte SHA1. */
969 flush();
970 } else {
636171cb 971 if (fix_thin_pack) {
8522148f
NP
972 struct sha1file *f;
973 unsigned char read_sha1[20], tail_sha1[20];
a984a06a 974 char msg[48];
636171cb 975 int nr_unresolved = nr_deltas - nr_resolved_deltas;
3c9af366 976 int nr_objects_initial = nr_objects;
636171cb
NP
977 if (nr_unresolved <= 0)
978 die("confusion beyond insanity");
979 objects = xrealloc(objects,
980 (nr_objects + nr_unresolved + 1)
981 * sizeof(*objects));
8522148f
NP
982 f = sha1fd(output_fd, curr_pack);
983 fix_unresolved_deltas(f, nr_unresolved);
a984a06a
NP
984 sprintf(msg, "completed with %d local objects",
985 nr_objects - nr_objects_initial);
986 stop_progress_msg(&progress, msg);
8522148f
NP
987 sha1close(f, tail_sha1, 0);
988 hashcpy(read_sha1, pack_sha1);
989 fixup_pack_header_footer(output_fd, pack_sha1,
abeb40e5 990 curr_pack, nr_objects,
8522148f
NP
991 read_sha1, consumed_bytes-20);
992 if (hashcmp(read_sha1, tail_sha1) != 0)
993 die("Unexpected tail checksum for %s "
994 "(disk corruption?)", curr_pack);
636171cb
NP
995 }
996 if (nr_deltas != nr_resolved_deltas)
997 die("pack has %d unresolved deltas",
998 nr_deltas - nr_resolved_deltas);
636171cb 999 }
9cf6d335 1000 free(deltas);
0153be05
MK
1001 if (strict)
1002 check_objects();
aa7e44bf
GB
1003
1004 idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1005 for (i = 0; i < nr_objects; i++)
1006 idx_objects[i] = &objects[i].idx;
8522148f 1007 curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1);
aa7e44bf
GB
1008 free(idx_objects);
1009
b8077709
SP
1010 final(pack_name, curr_pack,
1011 index_name, curr_index,
1012 keep_name, keep_msg,
8522148f 1013 pack_sha1);
9cf6d335
SV
1014 free(objects);
1015 free(index_name_buf);
b8077709 1016 free(keep_name_buf);
c85228ed
NP
1017 if (pack_name == NULL)
1018 free(curr_pack);
1019 if (index_name == NULL)
1020 free(curr_index);
9cf6d335
SV
1021
1022 return 0;
1023}