show_object(): push path_name() call further down
[git/dkf.git] / index-pack.c
blobc99a1a152c7ddbb63073461b86179f2be5bf9640
1 #include "cache.h"
2 #include "delta.h"
3 #include "pack.h"
4 #include "csum-file.h"
5 #include "blob.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "tree.h"
9 #include "progress.h"
10 #include "fsck.h"
12 static const char index_pack_usage[] =
13 "git index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] [--strict] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
15 struct object_entry
17 struct pack_idx_entry idx;
18 unsigned long size;
19 unsigned int hdr_size;
20 enum object_type type;
21 enum object_type real_type;
24 union delta_base {
25 unsigned char sha1[20];
26 off_t offset;
29 struct base_data {
30 struct base_data *base;
31 struct base_data *child;
32 struct object_entry *obj;
33 void *data;
34 unsigned long size;
38 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
39 * to memcmp() only the first 20 bytes.
41 #define UNION_BASE_SZ 20
43 #define FLAG_LINK (1u<<20)
44 #define FLAG_CHECKED (1u<<21)
46 struct delta_entry
48 union delta_base base;
49 int obj_no;
52 static struct object_entry *objects;
53 static struct delta_entry *deltas;
54 static struct base_data *base_cache;
55 static size_t base_cache_used;
56 static int nr_objects;
57 static int nr_deltas;
58 static int nr_resolved_deltas;
60 static int from_stdin;
61 static int strict;
62 static int verbose;
64 static struct progress *progress;
66 /* We always read in 4kB chunks. */
67 static unsigned char input_buffer[4096];
68 static unsigned int input_offset, input_len;
69 static off_t consumed_bytes;
70 static SHA_CTX input_ctx;
71 static uint32_t input_crc32;
72 static int input_fd, output_fd, pack_fd;
74 static int mark_link(struct object *obj, int type, void *data)
76 if (!obj)
77 return -1;
79 if (type != OBJ_ANY && obj->type != type)
80 die("object type mismatch at %s", sha1_to_hex(obj->sha1));
82 obj->flags |= FLAG_LINK;
83 return 0;
86 /* The content of each linked object must have been checked
87 or it must be already present in the object database */
88 static void check_object(struct object *obj)
90 if (!obj)
91 return;
93 if (!(obj->flags & FLAG_LINK))
94 return;
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;
106 static void check_objects(void)
108 unsigned i, max;
110 max = get_max_object_index();
111 for (i = 0; i < max; i++)
112 check_object(get_indexed_object(i));
116 /* Discard current buffer used content. */
117 static void flush(void)
119 if (input_offset) {
120 if (output_fd >= 0)
121 write_or_die(output_fd, input_buffer, input_offset);
122 SHA1_Update(&input_ctx, input_buffer, input_offset);
123 memmove(input_buffer, input_buffer + input_offset, input_len);
124 input_offset = 0;
129 * Make sure at least "min" bytes are available in the buffer, and
130 * return the pointer to the buffer.
132 static void *fill(int min)
134 if (min <= input_len)
135 return input_buffer + input_offset;
136 if (min > sizeof(input_buffer))
137 die("cannot fill %d bytes", min);
138 flush();
139 do {
140 ssize_t ret = xread(input_fd, input_buffer + input_len,
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));
147 input_len += ret;
148 if (from_stdin)
149 display_throughput(progress, consumed_bytes + input_len);
150 } while (input_len < min);
151 return input_buffer;
154 static void use(int bytes)
156 if (bytes > input_len)
157 die("used more bytes than were available");
158 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
159 input_len -= bytes;
160 input_offset += bytes;
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");
165 consumed_bytes += bytes;
168 static char *open_pack_file(char *pack_name)
170 if (from_stdin) {
171 input_fd = 0;
172 if (!pack_name) {
173 static char tmpfile[PATH_MAX];
174 snprintf(tmpfile, sizeof(tmpfile),
175 "%s/pack/tmp_pack_XXXXXX", get_object_directory());
176 output_fd = xmkstemp(tmpfile);
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));
182 pack_fd = output_fd;
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;
189 pack_fd = input_fd;
191 SHA1_Init(&input_ctx);
192 return pack_name;
195 static void parse_pack_header(void)
197 struct pack_header *hdr = fill(sizeof(struct pack_header));
199 /* Header consistency check */
200 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
201 die("pack signature mismatch");
202 if (!pack_version_ok(hdr->hdr_version))
203 die("pack version %"PRIu32" unsupported",
204 ntohl(hdr->hdr_version));
206 nr_objects = ntohl(hdr->hdr_entries);
207 use(sizeof(struct pack_header));
210 static void bad_object(unsigned long offset, const char *format,
211 ...) NORETURN __attribute__((format (printf, 2, 3)));
213 static void bad_object(unsigned long offset, const char *format, ...)
215 va_list params;
216 char buf[1024];
218 va_start(params, format);
219 vsnprintf(buf, sizeof(buf), format, params);
220 va_end(params);
221 die("pack has bad object at offset %lu: %s", offset, buf);
224 static void prune_base_data(struct base_data *retain)
226 struct base_data *b = base_cache;
227 for (b = base_cache;
228 base_cache_used > delta_base_cache_limit && b;
229 b = b->child) {
230 if (b->data && b != retain) {
231 free(b->data);
232 b->data = NULL;
233 base_cache_used -= b->size;
238 static void link_base_data(struct base_data *base, struct base_data *c)
240 if (base)
241 base->child = c;
242 else
243 base_cache = c;
245 c->base = base;
246 c->child = NULL;
247 base_cache_used += c->size;
248 prune_base_data(c);
251 static void unlink_base_data(struct base_data *c)
253 struct base_data *base = c->base;
254 if (base)
255 base->child = NULL;
256 else
257 base_cache = NULL;
258 if (c->data) {
259 free(c->data);
260 base_cache_used -= c->size;
264 static void *unpack_entry_data(unsigned long offset, unsigned long size)
266 z_stream stream;
267 void *buf = xmalloc(size);
269 memset(&stream, 0, sizeof(stream));
270 stream.next_out = buf;
271 stream.avail_out = size;
272 stream.next_in = fill(1);
273 stream.avail_in = input_len;
274 inflateInit(&stream);
276 for (;;) {
277 int ret = inflate(&stream, 0);
278 use(input_len - stream.avail_in);
279 if (stream.total_out == size && ret == Z_STREAM_END)
280 break;
281 if (ret != Z_OK)
282 bad_object(offset, "inflate returned %d", ret);
283 stream.next_in = fill(1);
284 stream.avail_in = input_len;
286 inflateEnd(&stream);
287 return buf;
290 static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
292 unsigned char *p, c;
293 unsigned long size;
294 off_t base_offset;
295 unsigned shift;
296 void *data;
298 obj->idx.offset = consumed_bytes;
299 input_crc32 = crc32(0, Z_NULL, 0);
301 p = fill(1);
302 c = *p;
303 use(1);
304 obj->type = (c >> 4) & 7;
305 size = (c & 15);
306 shift = 4;
307 while (c & 0x80) {
308 p = fill(1);
309 c = *p;
310 use(1);
311 size += (c & 0x7fUL) << shift;
312 shift += 7;
314 obj->size = size;
316 switch (obj->type) {
317 case OBJ_REF_DELTA:
318 hashcpy(delta_base->sha1, fill(20));
319 use(20);
320 break;
321 case OBJ_OFS_DELTA:
322 memset(delta_base, 0, sizeof(*delta_base));
323 p = fill(1);
324 c = *p;
325 use(1);
326 base_offset = c & 127;
327 while (c & 128) {
328 base_offset += 1;
329 if (!base_offset || MSB(base_offset, 7))
330 bad_object(obj->idx.offset, "offset value overflow for delta base object");
331 p = fill(1);
332 c = *p;
333 use(1);
334 base_offset = (base_offset << 7) + (c & 127);
336 delta_base->offset = obj->idx.offset - base_offset;
337 if (delta_base->offset >= obj->idx.offset)
338 bad_object(obj->idx.offset, "delta base offset is out of bound");
339 break;
340 case OBJ_COMMIT:
341 case OBJ_TREE:
342 case OBJ_BLOB:
343 case OBJ_TAG:
344 break;
345 default:
346 bad_object(obj->idx.offset, "unknown object type %d", obj->type);
348 obj->hdr_size = consumed_bytes - obj->idx.offset;
350 data = unpack_entry_data(obj->idx.offset, obj->size);
351 obj->idx.crc32 = input_crc32;
352 return data;
355 static void *get_data_from_pack(struct object_entry *obj)
357 off_t from = obj[0].idx.offset + obj[0].hdr_size;
358 unsigned long len = obj[1].idx.offset - from;
359 unsigned long rdy = 0;
360 unsigned char *src, *data;
361 z_stream stream;
362 int st;
364 src = xmalloc(len);
365 data = src;
366 do {
367 ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
368 if (n < 0)
369 die("cannot pread pack file: %s", strerror(errno));
370 if (!n)
371 die("premature end of pack file, %lu bytes missing",
372 len - rdy);
373 rdy += n;
374 } while (rdy < len);
375 data = xmalloc(obj->size);
376 memset(&stream, 0, sizeof(stream));
377 stream.next_out = data;
378 stream.avail_out = obj->size;
379 stream.next_in = src;
380 stream.avail_in = len;
381 inflateInit(&stream);
382 while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
383 inflateEnd(&stream);
384 if (st != Z_STREAM_END || stream.total_out != obj->size)
385 die("serious inflate inconsistency");
386 free(src);
387 return data;
390 static int find_delta(const union delta_base *base)
392 int first = 0, last = nr_deltas;
394 while (first < last) {
395 int next = (first + last) / 2;
396 struct delta_entry *delta = &deltas[next];
397 int cmp;
399 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
400 if (!cmp)
401 return next;
402 if (cmp < 0) {
403 last = next;
404 continue;
406 first = next+1;
408 return -first-1;
411 static int find_delta_children(const union delta_base *base,
412 int *first_index, int *last_index)
414 int first = find_delta(base);
415 int last = first;
416 int end = nr_deltas - 1;
418 if (first < 0)
419 return -1;
420 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
421 --first;
422 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
423 ++last;
424 *first_index = first;
425 *last_index = last;
426 return 0;
429 static void sha1_object(const void *data, unsigned long size,
430 enum object_type type, unsigned char *sha1)
432 hash_sha1_file(data, size, typename(type), sha1);
433 if (has_sha1_file(sha1)) {
434 void *has_data;
435 enum object_type has_type;
436 unsigned long has_size;
437 has_data = read_sha1_file(sha1, &has_type, &has_size);
438 if (!has_data)
439 die("cannot read existing object %s", sha1_to_hex(sha1));
440 if (size != has_size || type != has_type ||
441 memcmp(data, has_data, size) != 0)
442 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
443 free(has_data);
445 if (strict) {
446 if (type == OBJ_BLOB) {
447 struct blob *blob = lookup_blob(sha1);
448 if (blob)
449 blob->object.flags |= FLAG_CHECKED;
450 else
451 die("invalid blob object %s", sha1_to_hex(sha1));
452 } else {
453 struct object *obj;
454 int eaten;
455 void *buf = (void *) data;
458 * we do not need to free the memory here, as the
459 * buf is deleted by the caller.
461 obj = parse_object_buffer(sha1, type, size, buf, &eaten);
462 if (!obj)
463 die("invalid %s", typename(type));
464 if (fsck_object(obj, 1, fsck_error_function))
465 die("Error in object");
466 if (fsck_walk(obj, mark_link, 0))
467 die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1));
469 if (obj->type == OBJ_TREE) {
470 struct tree *item = (struct tree *) obj;
471 item->buffer = NULL;
473 if (obj->type == OBJ_COMMIT) {
474 struct commit *commit = (struct commit *) obj;
475 commit->buffer = NULL;
477 obj->flags |= FLAG_CHECKED;
482 static void *get_base_data(struct base_data *c)
484 if (!c->data) {
485 struct object_entry *obj = c->obj;
487 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
488 void *base = get_base_data(c->base);
489 void *raw = get_data_from_pack(obj);
490 c->data = patch_delta(
491 base, c->base->size,
492 raw, obj->size,
493 &c->size);
494 free(raw);
495 if (!c->data)
496 bad_object(obj->idx.offset, "failed to apply delta");
497 } else
498 c->data = get_data_from_pack(obj);
500 base_cache_used += c->size;
501 prune_base_data(c);
503 return c->data;
506 static void resolve_delta(struct object_entry *delta_obj,
507 struct base_data *base_obj, enum object_type type)
509 void *delta_data;
510 unsigned long delta_size;
511 union delta_base delta_base;
512 int j, first, last;
513 struct base_data result;
515 delta_obj->real_type = type;
516 delta_data = get_data_from_pack(delta_obj);
517 delta_size = delta_obj->size;
518 result.data = patch_delta(get_base_data(base_obj), base_obj->size,
519 delta_data, delta_size,
520 &result.size);
521 free(delta_data);
522 if (!result.data)
523 bad_object(delta_obj->idx.offset, "failed to apply delta");
524 sha1_object(result.data, result.size, type, delta_obj->idx.sha1);
525 nr_resolved_deltas++;
527 result.obj = delta_obj;
528 link_base_data(base_obj, &result);
530 hashcpy(delta_base.sha1, delta_obj->idx.sha1);
531 if (!find_delta_children(&delta_base, &first, &last)) {
532 for (j = first; j <= last; j++) {
533 struct object_entry *child = objects + deltas[j].obj_no;
534 if (child->real_type == OBJ_REF_DELTA)
535 resolve_delta(child, &result, type);
539 memset(&delta_base, 0, sizeof(delta_base));
540 delta_base.offset = delta_obj->idx.offset;
541 if (!find_delta_children(&delta_base, &first, &last)) {
542 for (j = first; j <= last; j++) {
543 struct object_entry *child = objects + deltas[j].obj_no;
544 if (child->real_type == OBJ_OFS_DELTA)
545 resolve_delta(child, &result, type);
549 unlink_base_data(&result);
552 static int compare_delta_entry(const void *a, const void *b)
554 const struct delta_entry *delta_a = a;
555 const struct delta_entry *delta_b = b;
556 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
559 /* Parse all objects and return the pack content SHA1 hash */
560 static void parse_pack_objects(unsigned char *sha1)
562 int i;
563 struct delta_entry *delta = deltas;
564 struct stat st;
567 * First pass:
568 * - find locations of all objects;
569 * - calculate SHA1 of all non-delta objects;
570 * - remember base (SHA1 or offset) for all deltas.
572 if (verbose)
573 progress = start_progress(
574 from_stdin ? "Receiving objects" : "Indexing objects",
575 nr_objects);
576 for (i = 0; i < nr_objects; i++) {
577 struct object_entry *obj = &objects[i];
578 void *data = unpack_raw_entry(obj, &delta->base);
579 obj->real_type = obj->type;
580 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
581 nr_deltas++;
582 delta->obj_no = i;
583 delta++;
584 } else
585 sha1_object(data, obj->size, obj->type, obj->idx.sha1);
586 free(data);
587 display_progress(progress, i+1);
589 objects[i].idx.offset = consumed_bytes;
590 stop_progress(&progress);
592 /* Check pack integrity */
593 flush();
594 SHA1_Final(sha1, &input_ctx);
595 if (hashcmp(fill(20), sha1))
596 die("pack is corrupted (SHA1 mismatch)");
597 use(20);
599 /* If input_fd is a file, we should have reached its end now. */
600 if (fstat(input_fd, &st))
601 die("cannot fstat packfile: %s", strerror(errno));
602 if (S_ISREG(st.st_mode) &&
603 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
604 die("pack has junk at the end");
606 if (!nr_deltas)
607 return;
609 /* Sort deltas by base SHA1/offset for fast searching */
610 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
611 compare_delta_entry);
614 * Second pass:
615 * - for all non-delta objects, look if it is used as a base for
616 * deltas;
617 * - if used as a base, uncompress the object and apply all deltas,
618 * recursively checking if the resulting object is used as a base
619 * for some more deltas.
621 if (verbose)
622 progress = start_progress("Resolving deltas", nr_deltas);
623 for (i = 0; i < nr_objects; i++) {
624 struct object_entry *obj = &objects[i];
625 union delta_base base;
626 int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
627 struct base_data base_obj;
629 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
630 continue;
631 hashcpy(base.sha1, obj->idx.sha1);
632 ref = !find_delta_children(&base, &ref_first, &ref_last);
633 memset(&base, 0, sizeof(base));
634 base.offset = obj->idx.offset;
635 ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
636 if (!ref && !ofs)
637 continue;
638 base_obj.data = get_data_from_pack(obj);
639 base_obj.size = obj->size;
640 base_obj.obj = obj;
641 link_base_data(NULL, &base_obj);
643 if (ref)
644 for (j = ref_first; j <= ref_last; j++) {
645 struct object_entry *child = objects + deltas[j].obj_no;
646 if (child->real_type == OBJ_REF_DELTA)
647 resolve_delta(child, &base_obj, obj->type);
649 if (ofs)
650 for (j = ofs_first; j <= ofs_last; j++) {
651 struct object_entry *child = objects + deltas[j].obj_no;
652 if (child->real_type == OBJ_OFS_DELTA)
653 resolve_delta(child, &base_obj, obj->type);
655 unlink_base_data(&base_obj);
656 display_progress(progress, nr_resolved_deltas);
660 static int write_compressed(struct sha1file *f, void *in, unsigned int size)
662 z_stream stream;
663 unsigned long maxsize;
664 void *out;
666 memset(&stream, 0, sizeof(stream));
667 deflateInit(&stream, zlib_compression_level);
668 maxsize = deflateBound(&stream, size);
669 out = xmalloc(maxsize);
671 /* Compress it */
672 stream.next_in = in;
673 stream.avail_in = size;
674 stream.next_out = out;
675 stream.avail_out = maxsize;
676 while (deflate(&stream, Z_FINISH) == Z_OK);
677 deflateEnd(&stream);
679 size = stream.total_out;
680 sha1write(f, out, size);
681 free(out);
682 return size;
685 static struct object_entry *append_obj_to_pack(struct sha1file *f,
686 const unsigned char *sha1, void *buf,
687 unsigned long size, enum object_type type)
689 struct object_entry *obj = &objects[nr_objects++];
690 unsigned char header[10];
691 unsigned long s = size;
692 int n = 0;
693 unsigned char c = (type << 4) | (s & 15);
694 s >>= 4;
695 while (s) {
696 header[n++] = c | 0x80;
697 c = s & 0x7f;
698 s >>= 7;
700 header[n++] = c;
701 crc32_begin(f);
702 sha1write(f, header, n);
703 obj[0].size = size;
704 obj[0].hdr_size = n;
705 obj[0].type = type;
706 obj[0].real_type = type;
707 obj[1].idx.offset = obj[0].idx.offset + n;
708 obj[1].idx.offset += write_compressed(f, buf, size);
709 obj[0].idx.crc32 = crc32_end(f);
710 sha1flush(f);
711 hashcpy(obj->idx.sha1, sha1);
712 return obj;
715 static int delta_pos_compare(const void *_a, const void *_b)
717 struct delta_entry *a = *(struct delta_entry **)_a;
718 struct delta_entry *b = *(struct delta_entry **)_b;
719 return a->obj_no - b->obj_no;
722 static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
724 struct delta_entry **sorted_by_pos;
725 int i, n = 0;
728 * Since many unresolved deltas may well be themselves base objects
729 * for more unresolved deltas, we really want to include the
730 * smallest number of base objects that would cover as much delta
731 * as possible by picking the
732 * trunc deltas first, allowing for other deltas to resolve without
733 * additional base objects. Since most base objects are to be found
734 * before deltas depending on them, a good heuristic is to start
735 * resolving deltas in the same order as their position in the pack.
737 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
738 for (i = 0; i < nr_deltas; i++) {
739 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
740 continue;
741 sorted_by_pos[n++] = &deltas[i];
743 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
745 for (i = 0; i < n; i++) {
746 struct delta_entry *d = sorted_by_pos[i];
747 enum object_type type;
748 int j, first, last;
749 struct base_data base_obj;
751 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
752 continue;
753 base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size);
754 if (!base_obj.data)
755 continue;
757 if (check_sha1_signature(d->base.sha1, base_obj.data,
758 base_obj.size, typename(type)))
759 die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
760 base_obj.obj = append_obj_to_pack(f, d->base.sha1,
761 base_obj.data, base_obj.size, type);
762 link_base_data(NULL, &base_obj);
764 find_delta_children(&d->base, &first, &last);
765 for (j = first; j <= last; j++) {
766 struct object_entry *child = objects + deltas[j].obj_no;
767 if (child->real_type == OBJ_REF_DELTA)
768 resolve_delta(child, &base_obj, type);
771 unlink_base_data(&base_obj);
772 display_progress(progress, nr_resolved_deltas);
774 free(sorted_by_pos);
777 static void final(const char *final_pack_name, const char *curr_pack_name,
778 const char *final_index_name, const char *curr_index_name,
779 const char *keep_name, const char *keep_msg,
780 unsigned char *sha1)
782 const char *report = "pack";
783 char name[PATH_MAX];
784 int err;
786 if (!from_stdin) {
787 close(input_fd);
788 } else {
789 fsync_or_die(output_fd, curr_pack_name);
790 err = close(output_fd);
791 if (err)
792 die("error while closing pack file: %s", strerror(errno));
793 chmod(curr_pack_name, 0444);
796 if (keep_msg) {
797 int keep_fd, keep_msg_len = strlen(keep_msg);
798 if (!keep_name) {
799 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
800 get_object_directory(), sha1_to_hex(sha1));
801 keep_name = name;
803 keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
804 if (keep_fd < 0) {
805 if (errno != EEXIST)
806 die("cannot write keep file");
807 } else {
808 if (keep_msg_len > 0) {
809 write_or_die(keep_fd, keep_msg, keep_msg_len);
810 write_or_die(keep_fd, "\n", 1);
812 if (close(keep_fd) != 0)
813 die("cannot write keep file");
814 report = "keep";
818 if (final_pack_name != curr_pack_name) {
819 if (!final_pack_name) {
820 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
821 get_object_directory(), sha1_to_hex(sha1));
822 final_pack_name = name;
824 if (move_temp_to_file(curr_pack_name, final_pack_name))
825 die("cannot store pack file");
828 chmod(curr_index_name, 0444);
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;
835 if (move_temp_to_file(curr_index_name, final_index_name))
836 die("cannot store index file");
839 if (!from_stdin) {
840 printf("%s\n", sha1_to_hex(sha1));
841 } else {
842 char buf[48];
843 int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
844 report, sha1_to_hex(sha1));
845 write_or_die(1, buf, len);
848 * Let's just mimic git-unpack-objects here and write
849 * the last part of the input buffer to stdout.
851 while (input_len) {
852 err = xwrite(1, input_buffer + input_offset, input_len);
853 if (err <= 0)
854 break;
855 input_len -= err;
856 input_offset += err;
861 static int git_index_pack_config(const char *k, const char *v, void *cb)
863 if (!strcmp(k, "pack.indexversion")) {
864 pack_idx_default_version = git_config_int(k, v);
865 if (pack_idx_default_version > 2)
866 die("bad pack.indexversion=%"PRIu32,
867 pack_idx_default_version);
868 return 0;
870 return git_default_config(k, v, cb);
873 int main(int argc, char **argv)
875 int i, fix_thin_pack = 0;
876 char *curr_pack, *pack_name = NULL;
877 char *curr_index, *index_name = NULL;
878 const char *keep_name = NULL, *keep_msg = NULL;
879 char *index_name_buf = NULL, *keep_name_buf = NULL;
880 struct pack_idx_entry **idx_objects;
881 unsigned char pack_sha1[20];
884 * We wish to read the repository's config file if any, and
885 * for that it is necessary to call setup_git_directory_gently().
886 * However if the cwd was inside .git/objects/pack/ then we need
887 * to go back there or all the pack name arguments will be wrong.
888 * And in that case we cannot rely on any prefix returned by
889 * setup_git_directory_gently() either.
892 char cwd[PATH_MAX+1];
893 int nongit;
895 if (!getcwd(cwd, sizeof(cwd)-1))
896 die("Unable to get current working directory");
897 setup_git_directory_gently(&nongit);
898 git_config(git_index_pack_config, NULL);
899 if (chdir(cwd))
900 die("Cannot come back to cwd");
903 for (i = 1; i < argc; i++) {
904 char *arg = argv[i];
906 if (*arg == '-') {
907 if (!strcmp(arg, "--stdin")) {
908 from_stdin = 1;
909 } else if (!strcmp(arg, "--fix-thin")) {
910 fix_thin_pack = 1;
911 } else if (!strcmp(arg, "--strict")) {
912 strict = 1;
913 } else if (!strcmp(arg, "--keep")) {
914 keep_msg = "";
915 } else if (!prefixcmp(arg, "--keep=")) {
916 keep_msg = arg + 7;
917 } else if (!prefixcmp(arg, "--pack_header=")) {
918 struct pack_header *hdr;
919 char *c;
921 hdr = (struct pack_header *)input_buffer;
922 hdr->hdr_signature = htonl(PACK_SIGNATURE);
923 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
924 if (*c != ',')
925 die("bad %s", arg);
926 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
927 if (*c)
928 die("bad %s", arg);
929 input_len = sizeof(*hdr);
930 } else if (!strcmp(arg, "-v")) {
931 verbose = 1;
932 } else if (!strcmp(arg, "-o")) {
933 if (index_name || (i+1) >= argc)
934 usage(index_pack_usage);
935 index_name = argv[++i];
936 } else if (!prefixcmp(arg, "--index-version=")) {
937 char *c;
938 pack_idx_default_version = strtoul(arg + 16, &c, 10);
939 if (pack_idx_default_version > 2)
940 die("bad %s", arg);
941 if (*c == ',')
942 pack_idx_off32_limit = strtoul(c+1, &c, 0);
943 if (*c || pack_idx_off32_limit & 0x80000000)
944 die("bad %s", arg);
945 } else
946 usage(index_pack_usage);
947 continue;
950 if (pack_name)
951 usage(index_pack_usage);
952 pack_name = arg;
955 if (!pack_name && !from_stdin)
956 usage(index_pack_usage);
957 if (fix_thin_pack && !from_stdin)
958 die("--fix-thin cannot be used without --stdin");
959 if (!index_name && pack_name) {
960 int len = strlen(pack_name);
961 if (!has_extension(pack_name, ".pack"))
962 die("packfile name '%s' does not end with '.pack'",
963 pack_name);
964 index_name_buf = xmalloc(len);
965 memcpy(index_name_buf, pack_name, len - 5);
966 strcpy(index_name_buf + len - 5, ".idx");
967 index_name = index_name_buf;
969 if (keep_msg && !keep_name && pack_name) {
970 int len = strlen(pack_name);
971 if (!has_extension(pack_name, ".pack"))
972 die("packfile name '%s' does not end with '.pack'",
973 pack_name);
974 keep_name_buf = xmalloc(len);
975 memcpy(keep_name_buf, pack_name, len - 5);
976 strcpy(keep_name_buf + len - 5, ".keep");
977 keep_name = keep_name_buf;
980 curr_pack = open_pack_file(pack_name);
981 parse_pack_header();
982 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
983 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
984 parse_pack_objects(pack_sha1);
985 if (nr_deltas == nr_resolved_deltas) {
986 stop_progress(&progress);
987 /* Flush remaining pack final 20-byte SHA1. */
988 flush();
989 } else {
990 if (fix_thin_pack) {
991 struct sha1file *f;
992 unsigned char read_sha1[20], tail_sha1[20];
993 char msg[48];
994 int nr_unresolved = nr_deltas - nr_resolved_deltas;
995 int nr_objects_initial = nr_objects;
996 if (nr_unresolved <= 0)
997 die("confusion beyond insanity");
998 objects = xrealloc(objects,
999 (nr_objects + nr_unresolved + 1)
1000 * sizeof(*objects));
1001 f = sha1fd(output_fd, curr_pack);
1002 fix_unresolved_deltas(f, nr_unresolved);
1003 sprintf(msg, "completed with %d local objects",
1004 nr_objects - nr_objects_initial);
1005 stop_progress_msg(&progress, msg);
1006 sha1close(f, tail_sha1, 0);
1007 hashcpy(read_sha1, pack_sha1);
1008 fixup_pack_header_footer(output_fd, pack_sha1,
1009 curr_pack, nr_objects,
1010 read_sha1, consumed_bytes-20);
1011 if (hashcmp(read_sha1, tail_sha1) != 0)
1012 die("Unexpected tail checksum for %s "
1013 "(disk corruption?)", curr_pack);
1015 if (nr_deltas != nr_resolved_deltas)
1016 die("pack has %d unresolved deltas",
1017 nr_deltas - nr_resolved_deltas);
1019 free(deltas);
1020 if (strict)
1021 check_objects();
1023 idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1024 for (i = 0; i < nr_objects; i++)
1025 idx_objects[i] = &objects[i].idx;
1026 curr_index = write_idx_file(index_name, idx_objects, nr_objects, pack_sha1);
1027 free(idx_objects);
1029 final(pack_name, curr_pack,
1030 index_name, curr_index,
1031 keep_name, keep_msg,
1032 pack_sha1);
1033 free(objects);
1034 free(index_name_buf);
1035 free(keep_name_buf);
1036 if (pack_name == NULL)
1037 free(curr_pack);
1038 if (index_name == NULL)
1039 free(curr_index);
1041 return 0;