13 #include "streaming.h"
14 #include "thread-utils.h"
16 #include "object-store.h"
17 #include "promisor-remote.h"
19 static const char index_pack_usage[] =
20 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
23 struct pack_idx_entry idx;
25 unsigned char hdr_size;
27 signed char real_type;
36 /* Initialized by make_base(). */
37 struct base_data *base;
38 struct object_entry *obj;
39 int ref_first, ref_last;
40 int ofs_first, ofs_last;
42 * Threads should increment retain_data if they are about to call
43 * patch_delta() using this struct's data as a base, and decrement this
44 * when they are done. While retain_data is nonzero, this struct's data
45 * will not be freed even if the delta base cache limit is exceeded.
49 * The number of direct children that have not been fully processed
50 * (entered work_head, entered done_head, left done_head). When this
51 * number reaches zero, this struct base_data can be freed.
53 int children_remaining;
55 /* Not initialized by make_base(). */
56 struct list_head list;
62 * Stack of struct base_data that have unprocessed children.
63 * threaded_second_pass() uses this as a source of work (the other being the
66 * Guarded by work_mutex.
68 static LIST_HEAD(work_head);
71 * Stack of struct base_data that have children, all of whom have been
72 * processed or are being processed, and at least one child is being processed.
73 * These struct base_data must be kept around until the last child is
76 * Guarded by work_mutex.
78 static LIST_HEAD(done_head);
81 * All threads share one delta base cache.
83 * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only
86 static size_t base_cache_used;
87 static size_t base_cache_limit;
94 /* Remember to update object flag allocation in object.h */
95 #define FLAG_LINK (1u<<20)
96 #define FLAG_CHECKED (1u<<21)
98 struct ofs_delta_entry {
103 struct ref_delta_entry {
104 struct object_id oid;
108 static struct object_entry *objects;
109 static struct object_stat *obj_stat;
110 static struct ofs_delta_entry *ofs_deltas;
111 static struct ref_delta_entry *ref_deltas;
112 static struct thread_local nothread_data;
113 static int nr_objects;
114 static int nr_ofs_deltas;
115 static int nr_ref_deltas;
116 static int ref_deltas_alloc;
117 static int nr_resolved_deltas;
118 static int nr_threads;
120 static int from_stdin;
122 static int do_fsck_object;
123 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
125 static int show_resolving_progress;
126 static int show_stat;
127 static int check_self_contained_and_connected;
129 static struct progress *progress;
131 /* We always read in 4kB chunks. */
132 static unsigned char input_buffer[4096];
133 static unsigned int input_offset, input_len;
134 static off_t consumed_bytes;
135 static off_t max_input_size;
136 static unsigned deepest_delta;
137 static git_hash_ctx input_ctx;
138 static uint32_t input_crc32;
139 static int input_fd, output_fd;
140 static const char *curr_pack;
142 static struct thread_local *thread_data;
143 static int nr_dispatched;
144 static int threads_active;
146 static pthread_mutex_t read_mutex;
147 #define read_lock() lock_mutex(&read_mutex)
148 #define read_unlock() unlock_mutex(&read_mutex)
150 static pthread_mutex_t counter_mutex;
151 #define counter_lock() lock_mutex(&counter_mutex)
152 #define counter_unlock() unlock_mutex(&counter_mutex)
154 static pthread_mutex_t work_mutex;
155 #define work_lock() lock_mutex(&work_mutex)
156 #define work_unlock() unlock_mutex(&work_mutex)
158 static pthread_mutex_t deepest_delta_mutex;
159 #define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
160 #define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
162 static pthread_key_t key;
164 static inline void lock_mutex(pthread_mutex_t *mutex)
167 pthread_mutex_lock(mutex);
170 static inline void unlock_mutex(pthread_mutex_t *mutex)
173 pthread_mutex_unlock(mutex);
177 * Mutex and conditional variable can't be statically-initialized on Windows.
179 static void init_thread(void)
182 init_recursive_mutex(&read_mutex);
183 pthread_mutex_init(&counter_mutex, NULL);
184 pthread_mutex_init(&work_mutex, NULL);
186 pthread_mutex_init(&deepest_delta_mutex, NULL);
187 pthread_key_create(&key, NULL);
188 thread_data = xcalloc(nr_threads, sizeof(*thread_data));
189 for (i = 0; i < nr_threads; i++) {
190 thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
191 if (thread_data[i].pack_fd == -1)
192 die_errno(_("unable to open %s"), curr_pack);
198 static void cleanup_thread(void)
204 pthread_mutex_destroy(&read_mutex);
205 pthread_mutex_destroy(&counter_mutex);
206 pthread_mutex_destroy(&work_mutex);
208 pthread_mutex_destroy(&deepest_delta_mutex);
209 for (i = 0; i < nr_threads; i++)
210 close(thread_data[i].pack_fd);
211 pthread_key_delete(key);
215 static int mark_link(struct object *obj, int type, void *data, struct fsck_options *options)
220 if (type != OBJ_ANY && obj->type != type)
221 die(_("object type mismatch at %s"), oid_to_hex(&obj->oid));
223 obj->flags |= FLAG_LINK;
227 /* The content of each linked object must have been checked
228 or it must be already present in the object database */
229 static unsigned check_object(struct object *obj)
234 if (!(obj->flags & FLAG_LINK))
237 if (!(obj->flags & FLAG_CHECKED)) {
239 int type = oid_object_info(the_repository, &obj->oid, &size);
241 die(_("did not receive expected object %s"),
242 oid_to_hex(&obj->oid));
243 if (type != obj->type)
244 die(_("object %s: expected type %s, found %s"),
245 oid_to_hex(&obj->oid),
246 type_name(obj->type), type_name(type));
247 obj->flags |= FLAG_CHECKED;
254 static unsigned check_objects(void)
256 unsigned i, max, foreign_nr = 0;
258 max = get_max_object_index();
261 progress = start_delayed_progress(_("Checking objects"), max);
263 for (i = 0; i < max; i++) {
264 foreign_nr += check_object(get_indexed_object(i));
265 display_progress(progress, i + 1);
268 stop_progress(&progress);
273 /* Discard current buffer used content. */
274 static void flush(void)
278 write_or_die(output_fd, input_buffer, input_offset);
279 the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
280 memmove(input_buffer, input_buffer + input_offset, input_len);
286 * Make sure at least "min" bytes are available in the buffer, and
287 * return the pointer to the buffer.
289 static void *fill(int min)
291 if (min <= input_len)
292 return input_buffer + input_offset;
293 if (min > sizeof(input_buffer))
294 die(Q_("cannot fill %d byte",
295 "cannot fill %d bytes",
300 ssize_t ret = xread(input_fd, input_buffer + input_len,
301 sizeof(input_buffer) - input_len);
305 die_errno(_("read error on input"));
309 display_throughput(progress, consumed_bytes + input_len);
310 } while (input_len < min);
314 static void use(int bytes)
316 if (bytes > input_len)
317 die(_("used more bytes than were available"));
318 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
320 input_offset += bytes;
322 /* make sure off_t is sufficiently large not to wrap */
323 if (signed_add_overflows(consumed_bytes, bytes))
324 die(_("pack too large for current definition of off_t"));
325 consumed_bytes += bytes;
326 if (max_input_size && consumed_bytes > max_input_size)
327 die(_("pack exceeds maximum allowed size"));
330 static const char *open_pack_file(const char *pack_name)
335 struct strbuf tmp_file = STRBUF_INIT;
336 output_fd = odb_mkstemp(&tmp_file,
337 "pack/tmp_pack_XXXXXX");
338 pack_name = strbuf_detach(&tmp_file, NULL);
340 output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
342 die_errno(_("unable to create '%s'"), pack_name);
344 nothread_data.pack_fd = output_fd;
346 input_fd = open(pack_name, O_RDONLY);
348 die_errno(_("cannot open packfile '%s'"), pack_name);
350 nothread_data.pack_fd = input_fd;
352 the_hash_algo->init_fn(&input_ctx);
356 static void parse_pack_header(void)
358 struct pack_header *hdr = fill(sizeof(struct pack_header));
360 /* Header consistency check */
361 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
362 die(_("pack signature mismatch"));
363 if (!pack_version_ok(hdr->hdr_version))
364 die(_("pack version %"PRIu32" unsupported"),
365 ntohl(hdr->hdr_version));
367 nr_objects = ntohl(hdr->hdr_entries);
368 use(sizeof(struct pack_header));
371 static NORETURN void bad_object(off_t offset, const char *format,
372 ...) __attribute__((format (printf, 2, 3)));
374 static NORETURN void bad_object(off_t offset, const char *format, ...)
379 va_start(params, format);
380 vsnprintf(buf, sizeof(buf), format, params);
382 die(_("pack has bad object at offset %"PRIuMAX": %s"),
383 (uintmax_t)offset, buf);
386 static inline struct thread_local *get_thread_data(void)
390 return pthread_getspecific(key);
391 assert(!threads_active &&
392 "This should only be reached when all threads are gone");
394 return ¬hread_data;
397 static void set_thread_data(struct thread_local *data)
400 pthread_setspecific(key, data);
403 static void free_base_data(struct base_data *c)
406 FREE_AND_NULL(c->data);
407 base_cache_used -= c->size;
411 static void prune_base_data(struct base_data *retain)
413 struct list_head *pos;
415 if (base_cache_used <= base_cache_limit)
418 list_for_each_prev(pos, &done_head) {
419 struct base_data *b = list_entry(pos, struct base_data, list);
420 if (b->retain_data || b == retain)
424 if (base_cache_used <= base_cache_limit)
429 list_for_each_prev(pos, &work_head) {
430 struct base_data *b = list_entry(pos, struct base_data, list);
431 if (b->retain_data || b == retain)
435 if (base_cache_used <= base_cache_limit)
441 static int is_delta_type(enum object_type type)
443 return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
446 static void *unpack_entry_data(off_t offset, unsigned long size,
447 enum object_type type, struct object_id *oid)
449 static char fixed_buf[8192];
457 if (!is_delta_type(type)) {
458 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX,
459 type_name(type),(uintmax_t)size) + 1;
460 the_hash_algo->init_fn(&c);
461 the_hash_algo->update_fn(&c, hdr, hdrlen);
464 if (type == OBJ_BLOB && size > big_file_threshold)
467 buf = xmallocz(size);
469 memset(&stream, 0, sizeof(stream));
470 git_inflate_init(&stream);
471 stream.next_out = buf;
472 stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size;
475 unsigned char *last_out = stream.next_out;
476 stream.next_in = fill(1);
477 stream.avail_in = input_len;
478 status = git_inflate(&stream, 0);
479 use(input_len - stream.avail_in);
481 the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
482 if (buf == fixed_buf) {
483 stream.next_out = buf;
484 stream.avail_out = sizeof(fixed_buf);
486 } while (status == Z_OK);
487 if (stream.total_out != size || status != Z_STREAM_END)
488 bad_object(offset, _("inflate returned %d"), status);
489 git_inflate_end(&stream);
491 the_hash_algo->final_fn(oid->hash, &c);
492 return buf == fixed_buf ? NULL : buf;
495 static void *unpack_raw_entry(struct object_entry *obj,
497 struct object_id *ref_oid,
498 struct object_id *oid)
501 unsigned long size, c;
506 obj->idx.offset = consumed_bytes;
507 input_crc32 = crc32(0, NULL, 0);
512 obj->type = (c >> 4) & 7;
519 size += (c & 0x7f) << shift;
526 hashcpy(ref_oid->hash, fill(the_hash_algo->rawsz));
527 use(the_hash_algo->rawsz);
533 base_offset = c & 127;
536 if (!base_offset || MSB(base_offset, 7))
537 bad_object(obj->idx.offset, _("offset value overflow for delta base object"));
541 base_offset = (base_offset << 7) + (c & 127);
543 *ofs_offset = obj->idx.offset - base_offset;
544 if (*ofs_offset <= 0 || *ofs_offset >= obj->idx.offset)
545 bad_object(obj->idx.offset, _("delta base offset is out of bound"));
553 bad_object(obj->idx.offset, _("unknown object type %d"), obj->type);
555 obj->hdr_size = consumed_bytes - obj->idx.offset;
557 data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, oid);
558 obj->idx.crc32 = input_crc32;
562 static void *unpack_data(struct object_entry *obj,
563 int (*consume)(const unsigned char *, unsigned long, void *),
566 off_t from = obj[0].idx.offset + obj[0].hdr_size;
567 off_t len = obj[1].idx.offset - from;
568 unsigned char *data, *inbuf;
572 data = xmallocz(consume ? 64*1024 : obj->size);
573 inbuf = xmalloc((len < 64*1024) ? (int)len : 64*1024);
575 memset(&stream, 0, sizeof(stream));
576 git_inflate_init(&stream);
577 stream.next_out = data;
578 stream.avail_out = consume ? 64*1024 : obj->size;
581 ssize_t n = (len < 64*1024) ? (ssize_t)len : 64*1024;
582 n = xpread(get_thread_data()->pack_fd, inbuf, n, from);
584 die_errno(_("cannot pread pack file"));
586 die(Q_("premature end of pack file, %"PRIuMAX" byte missing",
587 "premature end of pack file, %"PRIuMAX" bytes missing",
592 stream.next_in = inbuf;
595 status = git_inflate(&stream, 0);
598 status = git_inflate(&stream, 0);
599 if (consume(data, stream.next_out - data, cb_data)) {
604 stream.next_out = data;
605 stream.avail_out = 64*1024;
606 } while (status == Z_OK && stream.avail_in);
608 } while (len && status == Z_OK && !stream.avail_in);
610 /* This has been inflated OK when first encountered, so... */
611 if (status != Z_STREAM_END || stream.total_out != obj->size)
612 die(_("serious inflate inconsistency"));
614 git_inflate_end(&stream);
622 static void *get_data_from_pack(struct object_entry *obj)
624 return unpack_data(obj, NULL, NULL);
627 static int compare_ofs_delta_bases(off_t offset1, off_t offset2,
628 enum object_type type1,
629 enum object_type type2)
631 int cmp = type1 - type2;
634 return offset1 < offset2 ? -1 :
635 offset1 > offset2 ? 1 :
639 static int find_ofs_delta(const off_t offset)
641 int first = 0, last = nr_ofs_deltas;
643 while (first < last) {
644 int next = first + (last - first) / 2;
645 struct ofs_delta_entry *delta = &ofs_deltas[next];
648 cmp = compare_ofs_delta_bases(offset, delta->offset,
650 objects[delta->obj_no].type);
662 static void find_ofs_delta_children(off_t offset,
663 int *first_index, int *last_index)
665 int first = find_ofs_delta(offset);
667 int end = nr_ofs_deltas - 1;
674 while (first > 0 && ofs_deltas[first - 1].offset == offset)
676 while (last < end && ofs_deltas[last + 1].offset == offset)
678 *first_index = first;
682 static int compare_ref_delta_bases(const struct object_id *oid1,
683 const struct object_id *oid2,
684 enum object_type type1,
685 enum object_type type2)
687 int cmp = type1 - type2;
690 return oidcmp(oid1, oid2);
693 static int find_ref_delta(const struct object_id *oid)
695 int first = 0, last = nr_ref_deltas;
697 while (first < last) {
698 int next = first + (last - first) / 2;
699 struct ref_delta_entry *delta = &ref_deltas[next];
702 cmp = compare_ref_delta_bases(oid, &delta->oid,
704 objects[delta->obj_no].type);
716 static void find_ref_delta_children(const struct object_id *oid,
717 int *first_index, int *last_index)
719 int first = find_ref_delta(oid);
721 int end = nr_ref_deltas - 1;
728 while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid))
730 while (last < end && oideq(&ref_deltas[last + 1].oid, oid))
732 *first_index = first;
736 struct compare_data {
737 struct object_entry *entry;
738 struct git_istream *st;
740 unsigned long buf_size;
743 static int compare_objects(const unsigned char *buf, unsigned long size,
746 struct compare_data *data = cb_data;
748 if (data->buf_size < size) {
750 data->buf = xmalloc(size);
751 data->buf_size = size;
755 ssize_t len = read_istream(data->st, data->buf, size);
757 die(_("SHA1 COLLISION FOUND WITH %s !"),
758 oid_to_hex(&data->entry->idx.oid));
760 die(_("unable to read %s"),
761 oid_to_hex(&data->entry->idx.oid));
762 if (memcmp(buf, data->buf, len))
763 die(_("SHA1 COLLISION FOUND WITH %s !"),
764 oid_to_hex(&data->entry->idx.oid));
771 static int check_collison(struct object_entry *entry)
773 struct compare_data data;
774 enum object_type type;
777 if (entry->size <= big_file_threshold || entry->type != OBJ_BLOB)
780 memset(&data, 0, sizeof(data));
782 data.st = open_istream(the_repository, &entry->idx.oid, &type, &size,
786 if (size != entry->size || type != entry->type)
787 die(_("SHA1 COLLISION FOUND WITH %s !"),
788 oid_to_hex(&entry->idx.oid));
789 unpack_data(entry, compare_objects, &data);
790 close_istream(data.st);
795 static void sha1_object(const void *data, struct object_entry *obj_entry,
796 unsigned long size, enum object_type type,
797 const struct object_id *oid)
799 void *new_data = NULL;
800 int collision_test_needed = 0;
802 assert(data || obj_entry);
804 if (startup_info->have_repository) {
806 collision_test_needed =
807 has_object_file_with_flags(oid, OBJECT_INFO_QUICK);
811 if (collision_test_needed && !data) {
813 if (!check_collison(obj_entry))
814 collision_test_needed = 0;
817 if (collision_test_needed) {
819 enum object_type has_type;
820 unsigned long has_size;
822 has_type = oid_object_info(the_repository, oid, &has_size);
824 die(_("cannot read existing object info %s"), oid_to_hex(oid));
825 if (has_type != type || has_size != size)
826 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
827 has_data = read_object_file(oid, &has_type, &has_size);
830 data = new_data = get_data_from_pack(obj_entry);
832 die(_("cannot read existing object %s"), oid_to_hex(oid));
833 if (size != has_size || type != has_type ||
834 memcmp(data, has_data, size) != 0)
835 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
839 if (strict || do_fsck_object) {
841 if (type == OBJ_BLOB) {
842 struct blob *blob = lookup_blob(the_repository, oid);
844 blob->object.flags |= FLAG_CHECKED;
846 die(_("invalid blob object %s"), oid_to_hex(oid));
847 if (do_fsck_object &&
848 fsck_object(&blob->object, (void *)data, size, &fsck_options))
849 die(_("fsck error in packed object"));
853 void *buf = (void *) data;
855 assert(data && "data can only be NULL for large _blobs_");
858 * we do not need to free the memory here, as the
859 * buf is deleted by the caller.
861 obj = parse_object_buffer(the_repository, oid, type,
865 die(_("invalid %s"), type_name(type));
866 if (do_fsck_object &&
867 fsck_object(obj, buf, size, &fsck_options))
868 die(_("fsck error in packed object"));
869 if (strict && fsck_walk(obj, NULL, &fsck_options))
870 die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
872 if (obj->type == OBJ_TREE) {
873 struct tree *item = (struct tree *) obj;
877 if (obj->type == OBJ_COMMIT) {
878 struct commit *commit = (struct commit *) obj;
879 if (detach_commit_buffer(commit, NULL) != data)
880 BUG("parse_object_buffer transmogrified our buffer");
882 obj->flags |= FLAG_CHECKED;
891 * Walk from current node up
892 * to top parent if necessary to deflate the node. In normal
893 * situation, its parent node would be already deflated, so it just
894 * needs to apply delta.
896 * In the worst case scenario, parent node is no longer deflated because
897 * we're running out of delta_base_cache_limit; we need to re-deflate
898 * parents, possibly up to the top base.
900 * All deflated objects here are subject to be freed if we exceed
901 * delta_base_cache_limit, just like in find_unresolved_deltas(), we
902 * just need to make sure the last node is not freed.
904 static void *get_base_data(struct base_data *c)
907 struct object_entry *obj = c->obj;
908 struct base_data **delta = NULL;
909 int delta_nr = 0, delta_alloc = 0;
911 while (is_delta_type(c->obj->type) && !c->data) {
912 ALLOC_GROW(delta, delta_nr + 1, delta_alloc);
913 delta[delta_nr++] = c;
917 c->data = get_data_from_pack(obj);
919 base_cache_used += c->size;
922 for (; delta_nr > 0; delta_nr--) {
924 c = delta[delta_nr - 1];
926 base = get_base_data(c->base);
927 raw = get_data_from_pack(obj);
928 c->data = patch_delta(
934 bad_object(obj->idx.offset, _("failed to apply delta"));
935 base_cache_used += c->size;
943 static struct base_data *make_base(struct object_entry *obj,
944 struct base_data *parent)
946 struct base_data *base = xcalloc(1, sizeof(struct base_data));
949 find_ref_delta_children(&obj->idx.oid,
950 &base->ref_first, &base->ref_last);
951 find_ofs_delta_children(obj->idx.offset,
952 &base->ofs_first, &base->ofs_last);
953 base->children_remaining = base->ref_last - base->ref_first +
954 base->ofs_last - base->ofs_first + 2;
958 static struct base_data *resolve_delta(struct object_entry *delta_obj,
959 struct base_data *base)
961 void *delta_data, *result_data;
962 struct base_data *result;
963 unsigned long result_size;
966 int i = delta_obj - objects;
967 int j = base->obj - objects;
968 obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1;
969 deepest_delta_lock();
970 if (deepest_delta < obj_stat[i].delta_depth)
971 deepest_delta = obj_stat[i].delta_depth;
972 deepest_delta_unlock();
973 obj_stat[i].base_object_no = j;
975 delta_data = get_data_from_pack(delta_obj);
977 result_data = patch_delta(base->data, base->size,
978 delta_data, delta_obj->size, &result_size);
981 bad_object(delta_obj->idx.offset, _("failed to apply delta"));
982 hash_object_file(the_hash_algo, result_data, result_size,
983 type_name(delta_obj->real_type), &delta_obj->idx.oid);
984 sha1_object(result_data, NULL, result_size, delta_obj->real_type,
985 &delta_obj->idx.oid);
987 result = make_base(delta_obj, base);
988 result->data = result_data;
989 result->size = result_size;
992 nr_resolved_deltas++;
998 static int compare_ofs_delta_entry(const void *a, const void *b)
1000 const struct ofs_delta_entry *delta_a = a;
1001 const struct ofs_delta_entry *delta_b = b;
1003 return delta_a->offset < delta_b->offset ? -1 :
1004 delta_a->offset > delta_b->offset ? 1 :
1008 static int compare_ref_delta_entry(const void *a, const void *b)
1010 const struct ref_delta_entry *delta_a = a;
1011 const struct ref_delta_entry *delta_b = b;
1013 return oidcmp(&delta_a->oid, &delta_b->oid);
1016 static void *threaded_second_pass(void *data)
1019 set_thread_data(data);
1021 struct base_data *parent = NULL;
1022 struct object_entry *child_obj;
1023 struct base_data *child;
1026 display_progress(progress, nr_resolved_deltas);
1030 if (list_empty(&work_head)) {
1032 * Take an object from the object array.
1034 while (nr_dispatched < nr_objects &&
1035 is_delta_type(objects[nr_dispatched].type))
1037 if (nr_dispatched >= nr_objects) {
1041 child_obj = &objects[nr_dispatched++];
1044 * Peek at the top of the stack, and take a child from
1047 parent = list_first_entry(&work_head, struct base_data,
1050 if (parent->ref_first <= parent->ref_last) {
1051 int offset = ref_deltas[parent->ref_first++].obj_no;
1052 child_obj = objects + offset;
1053 if (child_obj->real_type != OBJ_REF_DELTA)
1054 die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
1055 (uintmax_t) child_obj->idx.offset,
1056 oid_to_hex(&parent->obj->idx.oid));
1057 child_obj->real_type = parent->obj->real_type;
1059 child_obj = objects +
1060 ofs_deltas[parent->ofs_first++].obj_no;
1061 assert(child_obj->real_type == OBJ_OFS_DELTA);
1062 child_obj->real_type = parent->obj->real_type;
1065 if (parent->ref_first > parent->ref_last &&
1066 parent->ofs_first > parent->ofs_last) {
1068 * This parent has run out of children, so move
1071 list_del(&parent->list);
1072 list_add(&parent->list, &done_head);
1076 * Ensure that the parent has data, since we will need
1079 * NEEDSWORK: If parent data needs to be reloaded, this
1080 * prolongs the time that the current thread spends in
1081 * the mutex. A mitigating factor is that parent data
1082 * needs to be reloaded only if the delta base cache
1083 * limit is exceeded, so in the typical case, this does
1086 get_base_data(parent);
1087 parent->retain_data++;
1092 child = resolve_delta(child_obj, parent);
1093 if (!child->children_remaining)
1094 FREE_AND_NULL(child->data);
1096 child = make_base(child_obj, NULL);
1097 if (child->children_remaining) {
1099 * Since this child has its own delta children,
1100 * we will need this data in the future.
1101 * Inflate now so that future iterations will
1102 * have access to this object's data while
1103 * outside the work mutex.
1105 child->data = get_data_from_pack(child_obj);
1106 child->size = child_obj->size;
1112 parent->retain_data--;
1115 * This child has its own children, so add it to
1118 list_add(&child->list, &work_head);
1119 base_cache_used += child->size;
1120 prune_base_data(NULL);
1123 * This child does not have its own children. It may be
1124 * the last descendant of its ancestors; free those
1127 struct base_data *p = parent;
1130 struct base_data *next_p;
1132 p->children_remaining--;
1133 if (p->children_remaining)
1151 * - find locations of all objects;
1152 * - calculate SHA1 of all non-delta objects;
1153 * - remember base (SHA1 or offset) for all deltas.
1155 static void parse_pack_objects(unsigned char *hash)
1157 int i, nr_delays = 0;
1158 struct ofs_delta_entry *ofs_delta = ofs_deltas;
1159 struct object_id ref_delta_oid;
1163 progress = start_progress(
1164 from_stdin ? _("Receiving objects") : _("Indexing objects"),
1166 for (i = 0; i < nr_objects; i++) {
1167 struct object_entry *obj = &objects[i];
1168 void *data = unpack_raw_entry(obj, &ofs_delta->offset,
1171 obj->real_type = obj->type;
1172 if (obj->type == OBJ_OFS_DELTA) {
1174 ofs_delta->obj_no = i;
1176 } else if (obj->type == OBJ_REF_DELTA) {
1177 ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc);
1178 oidcpy(&ref_deltas[nr_ref_deltas].oid, &ref_delta_oid);
1179 ref_deltas[nr_ref_deltas].obj_no = i;
1182 /* large blobs, check later */
1183 obj->real_type = OBJ_BAD;
1186 sha1_object(data, NULL, obj->size, obj->type,
1189 display_progress(progress, i+1);
1191 objects[i].idx.offset = consumed_bytes;
1192 stop_progress(&progress);
1194 /* Check pack integrity */
1196 the_hash_algo->final_fn(hash, &input_ctx);
1197 if (!hasheq(fill(the_hash_algo->rawsz), hash))
1198 die(_("pack is corrupted (SHA1 mismatch)"));
1199 use(the_hash_algo->rawsz);
1201 /* If input_fd is a file, we should have reached its end now. */
1202 if (fstat(input_fd, &st))
1203 die_errno(_("cannot fstat packfile"));
1204 if (S_ISREG(st.st_mode) &&
1205 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
1206 die(_("pack has junk at the end"));
1208 for (i = 0; i < nr_objects; i++) {
1209 struct object_entry *obj = &objects[i];
1210 if (obj->real_type != OBJ_BAD)
1212 obj->real_type = obj->type;
1213 sha1_object(NULL, obj, obj->size, obj->type,
1218 die(_("confusion beyond insanity in parse_pack_objects()"));
1223 * - for all non-delta objects, look if it is used as a base for
1225 * - if used as a base, uncompress the object and apply all deltas,
1226 * recursively checking if the resulting object is used as a base
1227 * for some more deltas.
1229 static void resolve_deltas(void)
1233 if (!nr_ofs_deltas && !nr_ref_deltas)
1236 /* Sort deltas by base SHA1/offset for fast searching */
1237 QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry);
1238 QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry);
1240 if (verbose || show_resolving_progress)
1241 progress = start_progress(_("Resolving deltas"),
1242 nr_ref_deltas + nr_ofs_deltas);
1245 base_cache_limit = delta_base_cache_limit * nr_threads;
1246 if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
1248 for (i = 0; i < nr_threads; i++) {
1249 int ret = pthread_create(&thread_data[i].thread, NULL,
1250 threaded_second_pass, thread_data + i);
1252 die(_("unable to create thread: %s"),
1255 for (i = 0; i < nr_threads; i++)
1256 pthread_join(thread_data[i].thread, NULL);
1260 threaded_second_pass(¬hread_data);
1265 * - append objects to convert thin pack to full pack if required
1266 * - write the final pack hash
1268 static void fix_unresolved_deltas(struct hashfile *f);
1269 static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash)
1271 if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
1272 stop_progress(&progress);
1273 /* Flush remaining pack final hash. */
1278 if (fix_thin_pack) {
1280 unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ];
1281 struct strbuf msg = STRBUF_INIT;
1282 int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
1283 int nr_objects_initial = nr_objects;
1284 if (nr_unresolved <= 0)
1285 die(_("confusion beyond insanity"));
1286 REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
1287 memset(objects + nr_objects + 1, 0,
1288 nr_unresolved * sizeof(*objects));
1289 f = hashfd(output_fd, curr_pack);
1290 fix_unresolved_deltas(f);
1291 strbuf_addf(&msg, Q_("completed with %d local object",
1292 "completed with %d local objects",
1293 nr_objects - nr_objects_initial),
1294 nr_objects - nr_objects_initial);
1295 stop_progress_msg(&progress, msg.buf);
1296 strbuf_release(&msg);
1297 finalize_hashfile(f, tail_hash, 0);
1298 hashcpy(read_hash, pack_hash);
1299 fixup_pack_header_footer(output_fd, pack_hash,
1300 curr_pack, nr_objects,
1301 read_hash, consumed_bytes-the_hash_algo->rawsz);
1302 if (!hasheq(read_hash, tail_hash))
1303 die(_("Unexpected tail checksum for %s "
1304 "(disk corruption?)"), curr_pack);
1306 if (nr_ofs_deltas + nr_ref_deltas != nr_resolved_deltas)
1307 die(Q_("pack has %d unresolved delta",
1308 "pack has %d unresolved deltas",
1309 nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas),
1310 nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
1313 static int write_compressed(struct hashfile *f, void *in, unsigned int size)
1317 unsigned char outbuf[4096];
1319 git_deflate_init(&stream, zlib_compression_level);
1320 stream.next_in = in;
1321 stream.avail_in = size;
1324 stream.next_out = outbuf;
1325 stream.avail_out = sizeof(outbuf);
1326 status = git_deflate(&stream, Z_FINISH);
1327 hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out);
1328 } while (status == Z_OK);
1330 if (status != Z_STREAM_END)
1331 die(_("unable to deflate appended object (%d)"), status);
1332 size = stream.total_out;
1333 git_deflate_end(&stream);
1337 static struct object_entry *append_obj_to_pack(struct hashfile *f,
1338 const unsigned char *sha1, void *buf,
1339 unsigned long size, enum object_type type)
1341 struct object_entry *obj = &objects[nr_objects++];
1342 unsigned char header[10];
1343 unsigned long s = size;
1345 unsigned char c = (type << 4) | (s & 15);
1348 header[n++] = c | 0x80;
1354 hashwrite(f, header, n);
1356 obj[0].hdr_size = n;
1358 obj[0].real_type = type;
1359 obj[1].idx.offset = obj[0].idx.offset + n;
1360 obj[1].idx.offset += write_compressed(f, buf, size);
1361 obj[0].idx.crc32 = crc32_end(f);
1363 hashcpy(obj->idx.oid.hash, sha1);
1367 static int delta_pos_compare(const void *_a, const void *_b)
1369 struct ref_delta_entry *a = *(struct ref_delta_entry **)_a;
1370 struct ref_delta_entry *b = *(struct ref_delta_entry **)_b;
1371 return a->obj_no - b->obj_no;
1374 static void fix_unresolved_deltas(struct hashfile *f)
1376 struct ref_delta_entry **sorted_by_pos;
1380 * Since many unresolved deltas may well be themselves base objects
1381 * for more unresolved deltas, we really want to include the
1382 * smallest number of base objects that would cover as much delta
1383 * as possible by picking the
1384 * trunc deltas first, allowing for other deltas to resolve without
1385 * additional base objects. Since most base objects are to be found
1386 * before deltas depending on them, a good heuristic is to start
1387 * resolving deltas in the same order as their position in the pack.
1389 ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
1390 for (i = 0; i < nr_ref_deltas; i++)
1391 sorted_by_pos[i] = &ref_deltas[i];
1392 QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare);
1394 if (has_promisor_remote()) {
1396 * Prefetch the delta bases.
1398 struct oid_array to_fetch = OID_ARRAY_INIT;
1399 for (i = 0; i < nr_ref_deltas; i++) {
1400 struct ref_delta_entry *d = sorted_by_pos[i];
1401 if (!oid_object_info_extended(the_repository, &d->oid,
1403 OBJECT_INFO_FOR_PREFETCH))
1405 oid_array_append(&to_fetch, &d->oid);
1407 promisor_remote_get_direct(the_repository,
1408 to_fetch.oid, to_fetch.nr);
1409 oid_array_clear(&to_fetch);
1412 for (i = 0; i < nr_ref_deltas; i++) {
1413 struct ref_delta_entry *d = sorted_by_pos[i];
1414 enum object_type type;
1418 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
1420 data = read_object_file(&d->oid, &type, &size);
1424 if (check_object_signature(the_repository, &d->oid,
1427 die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
1430 * Add this as an object to the objects array and call
1431 * threaded_second_pass() (which will pick up the added
1434 append_obj_to_pack(f, d->oid.hash, data, size, type);
1435 threaded_second_pass(NULL);
1437 display_progress(progress, nr_resolved_deltas);
1439 free(sorted_by_pos);
1442 static const char *derive_filename(const char *pack_name, const char *suffix,
1446 if (!strip_suffix(pack_name, ".pack", &len))
1447 die(_("packfile name '%s' does not end with '.pack'"),
1449 strbuf_add(buf, pack_name, len);
1450 strbuf_addch(buf, '.');
1451 strbuf_addstr(buf, suffix);
1455 static void write_special_file(const char *suffix, const char *msg,
1456 const char *pack_name, const unsigned char *hash,
1457 const char **report)
1459 struct strbuf name_buf = STRBUF_INIT;
1460 const char *filename;
1462 int msg_len = strlen(msg);
1465 filename = derive_filename(pack_name, suffix, &name_buf);
1467 filename = odb_pack_name(&name_buf, hash, suffix);
1469 fd = odb_pack_keep(filename);
1471 if (errno != EEXIST)
1472 die_errno(_("cannot write %s file '%s'"),
1476 write_or_die(fd, msg, msg_len);
1477 write_or_die(fd, "\n", 1);
1480 die_errno(_("cannot close written %s file '%s'"),
1485 strbuf_release(&name_buf);
1488 static void final(const char *final_pack_name, const char *curr_pack_name,
1489 const char *final_index_name, const char *curr_index_name,
1490 const char *keep_msg, const char *promisor_msg,
1491 unsigned char *hash)
1493 const char *report = "pack";
1494 struct strbuf pack_name = STRBUF_INIT;
1495 struct strbuf index_name = STRBUF_INIT;
1501 fsync_or_die(output_fd, curr_pack_name);
1502 err = close(output_fd);
1504 die_errno(_("error while closing pack file"));
1508 write_special_file("keep", keep_msg, final_pack_name, hash,
1511 write_special_file("promisor", promisor_msg, final_pack_name,
1514 if (final_pack_name != curr_pack_name) {
1515 if (!final_pack_name)
1516 final_pack_name = odb_pack_name(&pack_name, hash, "pack");
1517 if (finalize_object_file(curr_pack_name, final_pack_name))
1518 die(_("cannot store pack file"));
1519 } else if (from_stdin)
1520 chmod(final_pack_name, 0444);
1522 if (final_index_name != curr_index_name) {
1523 if (!final_index_name)
1524 final_index_name = odb_pack_name(&index_name, hash, "idx");
1525 if (finalize_object_file(curr_index_name, final_index_name))
1526 die(_("cannot store index file"));
1528 chmod(final_index_name, 0444);
1530 if (do_fsck_object) {
1531 struct packed_git *p;
1532 p = add_packed_git(final_index_name, strlen(final_index_name), 0);
1534 install_packed_git(the_repository, p);
1538 printf("%s\n", hash_to_hex(hash));
1540 struct strbuf buf = STRBUF_INIT;
1542 strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash));
1543 write_or_die(1, buf.buf, buf.len);
1544 strbuf_release(&buf);
1547 * Let's just mimic git-unpack-objects here and write
1548 * the last part of the input buffer to stdout.
1551 err = xwrite(1, input_buffer + input_offset, input_len);
1555 input_offset += err;
1559 strbuf_release(&index_name);
1560 strbuf_release(&pack_name);
1563 static int git_index_pack_config(const char *k, const char *v, void *cb)
1565 struct pack_idx_option *opts = cb;
1567 if (!strcmp(k, "pack.indexversion")) {
1568 opts->version = git_config_int(k, v);
1569 if (opts->version > 2)
1570 die(_("bad pack.indexversion=%"PRIu32), opts->version);
1573 if (!strcmp(k, "pack.threads")) {
1574 nr_threads = git_config_int(k, v);
1576 die(_("invalid number of threads specified (%d)"),
1578 if (!HAVE_THREADS && nr_threads != 1) {
1579 warning(_("no threads support, ignoring %s"), k);
1584 return git_default_config(k, v, cb);
1587 static int cmp_uint32(const void *a_, const void *b_)
1589 uint32_t a = *((uint32_t *)a_);
1590 uint32_t b = *((uint32_t *)b_);
1592 return (a < b) ? -1 : (a != b);
1595 static void read_v2_anomalous_offsets(struct packed_git *p,
1596 struct pack_idx_option *opts)
1598 const uint32_t *idx1, *idx2;
1601 /* The address of the 4-byte offset table */
1602 idx1 = (((const uint32_t *)((const uint8_t *)p->index_data + p->crc_offset))
1603 + p->num_objects /* CRC32 table */
1606 /* The address of the 8-byte offset table */
1607 idx2 = idx1 + p->num_objects;
1609 for (i = 0; i < p->num_objects; i++) {
1610 uint32_t off = ntohl(idx1[i]);
1611 if (!(off & 0x80000000))
1613 off = off & 0x7fffffff;
1614 check_pack_index_ptr(p, &idx2[off * 2]);
1618 * The real offset is ntohl(idx2[off * 2]) in high 4
1619 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1620 * octets. But idx2[off * 2] is Zero!!!
1622 ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
1623 opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
1626 QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32);
1629 static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
1631 struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
1634 die(_("Cannot open existing pack file '%s'"), pack_name);
1635 if (open_pack_index(p))
1636 die(_("Cannot open existing pack idx file for '%s'"), pack_name);
1638 /* Read the attributes from the existing idx file */
1639 opts->version = p->index_version;
1641 if (opts->version == 2)
1642 read_v2_anomalous_offsets(p, opts);
1645 * Get rid of the idx file as we do not need it anymore.
1646 * NEEDSWORK: extract this bit from free_pack_by_name() in
1647 * sha1-file.c, perhaps? It shouldn't matter very much as we
1648 * know we haven't installed this pack (hence we never have
1649 * read anything from it).
1651 close_pack_index(p);
1655 static void show_pack_info(int stat_only)
1657 int i, baseobjects = nr_objects - nr_ref_deltas - nr_ofs_deltas;
1658 unsigned long *chain_histogram = NULL;
1661 chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long));
1663 for (i = 0; i < nr_objects; i++) {
1664 struct object_entry *obj = &objects[i];
1666 if (is_delta_type(obj->type))
1667 chain_histogram[obj_stat[i].delta_depth - 1]++;
1670 printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
1671 oid_to_hex(&obj->idx.oid),
1672 type_name(obj->real_type), (uintmax_t)obj->size,
1673 (uintmax_t)(obj[1].idx.offset - obj->idx.offset),
1674 (uintmax_t)obj->idx.offset);
1675 if (is_delta_type(obj->type)) {
1676 struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
1677 printf(" %u %s", obj_stat[i].delta_depth,
1678 oid_to_hex(&bobj->idx.oid));
1684 printf_ln(Q_("non delta: %d object",
1685 "non delta: %d objects",
1688 for (i = 0; i < deepest_delta; i++) {
1689 if (!chain_histogram[i])
1691 printf_ln(Q_("chain length = %d: %lu object",
1692 "chain length = %d: %lu objects",
1693 chain_histogram[i]),
1695 chain_histogram[i]);
1699 int cmd_index_pack(int argc, const char **argv, const char *prefix)
1701 int i, fix_thin_pack = 0, verify = 0, stat_only = 0;
1702 const char *curr_index;
1703 const char *index_name = NULL, *pack_name = NULL;
1704 const char *keep_msg = NULL;
1705 const char *promisor_msg = NULL;
1706 struct strbuf index_name_buf = STRBUF_INIT;
1707 struct pack_idx_entry **idx_objects;
1708 struct pack_idx_option opts;
1709 unsigned char pack_hash[GIT_MAX_RAWSZ];
1710 unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
1711 int report_end_of_input = 0;
1715 * index-pack never needs to fetch missing objects except when
1716 * REF_DELTA bases are missing (which are explicitly handled). It only
1717 * accesses the repo to do hash collision checks and to check which
1718 * REF_DELTA bases need to be fetched.
1720 fetch_if_missing = 0;
1722 if (argc == 2 && !strcmp(argv[1], "-h"))
1723 usage(index_pack_usage);
1725 read_replace_refs = 0;
1726 fsck_options.walk = mark_link;
1728 reset_pack_idx_option(&opts);
1729 git_config(git_index_pack_config, &opts);
1730 if (prefix && chdir(prefix))
1731 die(_("Cannot come back to cwd"));
1733 for (i = 1; i < argc; i++) {
1734 const char *arg = argv[i];
1737 if (!strcmp(arg, "--stdin")) {
1739 } else if (!strcmp(arg, "--fix-thin")) {
1741 } else if (skip_to_optional_arg(arg, "--strict", &arg)) {
1744 fsck_set_msg_types(&fsck_options, arg);
1745 } else if (!strcmp(arg, "--check-self-contained-and-connected")) {
1747 check_self_contained_and_connected = 1;
1748 } else if (!strcmp(arg, "--fsck-objects")) {
1750 } else if (!strcmp(arg, "--verify")) {
1752 } else if (!strcmp(arg, "--verify-stat")) {
1755 } else if (!strcmp(arg, "--verify-stat-only")) {
1759 } else if (skip_to_optional_arg(arg, "--keep", &keep_msg)) {
1760 ; /* nothing to do */
1761 } else if (skip_to_optional_arg(arg, "--promisor", &promisor_msg)) {
1762 ; /* already parsed */
1763 } else if (starts_with(arg, "--threads=")) {
1765 nr_threads = strtoul(arg+10, &end, 0);
1766 if (!arg[10] || *end || nr_threads < 0)
1767 usage(index_pack_usage);
1768 if (!HAVE_THREADS && nr_threads != 1) {
1769 warning(_("no threads support, ignoring %s"), arg);
1772 } else if (starts_with(arg, "--pack_header=")) {
1773 struct pack_header *hdr;
1776 hdr = (struct pack_header *)input_buffer;
1777 hdr->hdr_signature = htonl(PACK_SIGNATURE);
1778 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
1780 die(_("bad %s"), arg);
1781 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
1783 die(_("bad %s"), arg);
1784 input_len = sizeof(*hdr);
1785 } else if (!strcmp(arg, "-v")) {
1787 } else if (!strcmp(arg, "--show-resolving-progress")) {
1788 show_resolving_progress = 1;
1789 } else if (!strcmp(arg, "--report-end-of-input")) {
1790 report_end_of_input = 1;
1791 } else if (!strcmp(arg, "-o")) {
1792 if (index_name || (i+1) >= argc)
1793 usage(index_pack_usage);
1794 index_name = argv[++i];
1795 } else if (starts_with(arg, "--index-version=")) {
1797 opts.version = strtoul(arg + 16, &c, 10);
1798 if (opts.version > 2)
1799 die(_("bad %s"), arg);
1801 opts.off32_limit = strtoul(c+1, &c, 0);
1802 if (*c || opts.off32_limit & 0x80000000)
1803 die(_("bad %s"), arg);
1804 } else if (skip_prefix(arg, "--max-input-size=", &arg)) {
1805 max_input_size = strtoumax(arg, NULL, 10);
1806 } else if (skip_prefix(arg, "--object-format=", &arg)) {
1807 hash_algo = hash_algo_by_name(arg);
1808 if (hash_algo == GIT_HASH_UNKNOWN)
1809 die(_("unknown hash algorithm '%s'"), arg);
1810 repo_set_hash_algo(the_repository, hash_algo);
1812 usage(index_pack_usage);
1817 usage(index_pack_usage);
1821 if (!pack_name && !from_stdin)
1822 usage(index_pack_usage);
1823 if (fix_thin_pack && !from_stdin)
1824 die(_("--fix-thin cannot be used without --stdin"));
1825 if (from_stdin && !startup_info->have_repository)
1826 die(_("--stdin requires a git repository"));
1827 if (from_stdin && hash_algo)
1828 die(_("--object-format cannot be used with --stdin"));
1829 if (!index_name && pack_name)
1830 index_name = derive_filename(pack_name, "idx", &index_name_buf);
1834 die(_("--verify with no packfile name given"));
1835 read_idx_option(&opts, index_name);
1836 opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT;
1839 opts.flags |= WRITE_IDX_STRICT;
1841 if (HAVE_THREADS && !nr_threads) {
1842 nr_threads = online_cpus();
1843 /* An experiment showed that more threads does not mean faster */
1848 curr_pack = open_pack_file(pack_name);
1849 parse_pack_header();
1850 objects = xcalloc(st_add(nr_objects, 1), sizeof(struct object_entry));
1852 obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
1853 ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
1854 parse_pack_objects(pack_hash);
1855 if (report_end_of_input)
1856 write_in_full(2, "\0", 1);
1858 conclude_pack(fix_thin_pack, curr_pack, pack_hash);
1862 foreign_nr = check_objects();
1865 show_pack_info(stat_only);
1867 ALLOC_ARRAY(idx_objects, nr_objects);
1868 for (i = 0; i < nr_objects; i++)
1869 idx_objects[i] = &objects[i].idx;
1870 curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
1874 final(pack_name, curr_pack,
1875 index_name, curr_index,
1876 keep_msg, promisor_msg,
1881 if (do_fsck_object && fsck_finish(&fsck_options))
1882 die(_("fsck error in pack objects"));
1885 strbuf_release(&index_name_buf);
1886 if (pack_name == NULL)
1887 free((void *) curr_pack);
1888 if (index_name == NULL)
1889 free((void *) curr_index);
1892 * Let the caller know this pack is not self contained
1894 if (check_self_contained_and_connected && foreign_nr)