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