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_mutex_t type_cas_mutex;
163 #define type_cas_lock() lock_mutex(&type_cas_mutex)
164 #define type_cas_unlock() unlock_mutex(&type_cas_mutex)
166 static pthread_key_t key;
168 static inline void lock_mutex(pthread_mutex_t *mutex)
171 pthread_mutex_lock(mutex);
174 static inline void unlock_mutex(pthread_mutex_t *mutex)
177 pthread_mutex_unlock(mutex);
181 * Mutex and conditional variable can't be statically-initialized on Windows.
183 static void init_thread(void)
186 init_recursive_mutex(&read_mutex);
187 pthread_mutex_init(&counter_mutex, NULL);
188 pthread_mutex_init(&work_mutex, NULL);
189 pthread_mutex_init(&type_cas_mutex, NULL);
191 pthread_mutex_init(&deepest_delta_mutex, NULL);
192 pthread_key_create(&key, NULL);
193 thread_data = xcalloc(nr_threads, sizeof(*thread_data));
194 for (i = 0; i < nr_threads; i++) {
195 thread_data[i].pack_fd = open(curr_pack, O_RDONLY);
196 if (thread_data[i].pack_fd == -1)
197 die_errno(_("unable to open %s"), curr_pack);
203 static void cleanup_thread(void)
209 pthread_mutex_destroy(&read_mutex);
210 pthread_mutex_destroy(&counter_mutex);
211 pthread_mutex_destroy(&work_mutex);
212 pthread_mutex_destroy(&type_cas_mutex);
214 pthread_mutex_destroy(&deepest_delta_mutex);
215 for (i = 0; i < nr_threads; i++)
216 close(thread_data[i].pack_fd);
217 pthread_key_delete(key);
221 static int mark_link(struct object *obj, int type, void *data, struct fsck_options *options)
226 if (type != OBJ_ANY && obj->type != type)
227 die(_("object type mismatch at %s"), oid_to_hex(&obj->oid));
229 obj->flags |= FLAG_LINK;
233 /* The content of each linked object must have been checked
234 or it must be already present in the object database */
235 static unsigned check_object(struct object *obj)
240 if (!(obj->flags & FLAG_LINK))
243 if (!(obj->flags & FLAG_CHECKED)) {
245 int type = oid_object_info(the_repository, &obj->oid, &size);
247 die(_("did not receive expected object %s"),
248 oid_to_hex(&obj->oid));
249 if (type != obj->type)
250 die(_("object %s: expected type %s, found %s"),
251 oid_to_hex(&obj->oid),
252 type_name(obj->type), type_name(type));
253 obj->flags |= FLAG_CHECKED;
260 static unsigned check_objects(void)
262 unsigned i, max, foreign_nr = 0;
264 max = get_max_object_index();
267 progress = start_delayed_progress(_("Checking objects"), max);
269 for (i = 0; i < max; i++) {
270 foreign_nr += check_object(get_indexed_object(i));
271 display_progress(progress, i + 1);
274 stop_progress(&progress);
279 /* Discard current buffer used content. */
280 static void flush(void)
284 write_or_die(output_fd, input_buffer, input_offset);
285 the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
286 memmove(input_buffer, input_buffer + input_offset, input_len);
292 * Make sure at least "min" bytes are available in the buffer, and
293 * return the pointer to the buffer.
295 static void *fill(int min)
297 if (min <= input_len)
298 return input_buffer + input_offset;
299 if (min > sizeof(input_buffer))
300 die(Q_("cannot fill %d byte",
301 "cannot fill %d bytes",
306 ssize_t ret = xread(input_fd, input_buffer + input_len,
307 sizeof(input_buffer) - input_len);
311 die_errno(_("read error on input"));
315 display_throughput(progress, consumed_bytes + input_len);
316 } while (input_len < min);
320 static void use(int bytes)
322 if (bytes > input_len)
323 die(_("used more bytes than were available"));
324 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
326 input_offset += bytes;
328 /* make sure off_t is sufficiently large not to wrap */
329 if (signed_add_overflows(consumed_bytes, bytes))
330 die(_("pack too large for current definition of off_t"));
331 consumed_bytes += bytes;
332 if (max_input_size && consumed_bytes > max_input_size)
333 die(_("pack exceeds maximum allowed size"));
336 static const char *open_pack_file(const char *pack_name)
341 struct strbuf tmp_file = STRBUF_INIT;
342 output_fd = odb_mkstemp(&tmp_file,
343 "pack/tmp_pack_XXXXXX");
344 pack_name = strbuf_detach(&tmp_file, NULL);
346 output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
348 die_errno(_("unable to create '%s'"), pack_name);
350 nothread_data.pack_fd = output_fd;
352 input_fd = open(pack_name, O_RDONLY);
354 die_errno(_("cannot open packfile '%s'"), pack_name);
356 nothread_data.pack_fd = input_fd;
358 the_hash_algo->init_fn(&input_ctx);
362 static void parse_pack_header(void)
364 struct pack_header *hdr = fill(sizeof(struct pack_header));
366 /* Header consistency check */
367 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
368 die(_("pack signature mismatch"));
369 if (!pack_version_ok(hdr->hdr_version))
370 die(_("pack version %"PRIu32" unsupported"),
371 ntohl(hdr->hdr_version));
373 nr_objects = ntohl(hdr->hdr_entries);
374 use(sizeof(struct pack_header));
377 static NORETURN void bad_object(off_t offset, const char *format,
378 ...) __attribute__((format (printf, 2, 3)));
380 static NORETURN void bad_object(off_t offset, const char *format, ...)
385 va_start(params, format);
386 vsnprintf(buf, sizeof(buf), format, params);
388 die(_("pack has bad object at offset %"PRIuMAX": %s"),
389 (uintmax_t)offset, buf);
392 static inline struct thread_local *get_thread_data(void)
396 return pthread_getspecific(key);
397 assert(!threads_active &&
398 "This should only be reached when all threads are gone");
400 return ¬hread_data;
403 static void set_thread_data(struct thread_local *data)
406 pthread_setspecific(key, data);
409 static void free_base_data(struct base_data *c)
412 FREE_AND_NULL(c->data);
413 base_cache_used -= c->size;
417 static void prune_base_data(struct base_data *retain)
419 struct list_head *pos;
421 if (base_cache_used <= base_cache_limit)
424 list_for_each_prev(pos, &done_head) {
425 struct base_data *b = list_entry(pos, struct base_data, list);
426 if (b->retain_data || b == retain)
430 if (base_cache_used <= base_cache_limit)
435 list_for_each_prev(pos, &work_head) {
436 struct base_data *b = list_entry(pos, struct base_data, list);
437 if (b->retain_data || b == retain)
441 if (base_cache_used <= base_cache_limit)
447 static int is_delta_type(enum object_type type)
449 return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
452 static void *unpack_entry_data(off_t offset, unsigned long size,
453 enum object_type type, struct object_id *oid)
455 static char fixed_buf[8192];
463 if (!is_delta_type(type)) {
464 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX,
465 type_name(type),(uintmax_t)size) + 1;
466 the_hash_algo->init_fn(&c);
467 the_hash_algo->update_fn(&c, hdr, hdrlen);
470 if (type == OBJ_BLOB && size > big_file_threshold)
473 buf = xmallocz(size);
475 memset(&stream, 0, sizeof(stream));
476 git_inflate_init(&stream);
477 stream.next_out = buf;
478 stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size;
481 unsigned char *last_out = stream.next_out;
482 stream.next_in = fill(1);
483 stream.avail_in = input_len;
484 status = git_inflate(&stream, 0);
485 use(input_len - stream.avail_in);
487 the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
488 if (buf == fixed_buf) {
489 stream.next_out = buf;
490 stream.avail_out = sizeof(fixed_buf);
492 } while (status == Z_OK);
493 if (stream.total_out != size || status != Z_STREAM_END)
494 bad_object(offset, _("inflate returned %d"), status);
495 git_inflate_end(&stream);
497 the_hash_algo->final_fn(oid->hash, &c);
498 return buf == fixed_buf ? NULL : buf;
501 static void *unpack_raw_entry(struct object_entry *obj,
503 struct object_id *ref_oid,
504 struct object_id *oid)
507 unsigned long size, c;
512 obj->idx.offset = consumed_bytes;
513 input_crc32 = crc32(0, NULL, 0);
518 obj->type = (c >> 4) & 7;
525 size += (c & 0x7f) << shift;
532 hashcpy(ref_oid->hash, fill(the_hash_algo->rawsz));
533 use(the_hash_algo->rawsz);
539 base_offset = c & 127;
542 if (!base_offset || MSB(base_offset, 7))
543 bad_object(obj->idx.offset, _("offset value overflow for delta base object"));
547 base_offset = (base_offset << 7) + (c & 127);
549 *ofs_offset = obj->idx.offset - base_offset;
550 if (*ofs_offset <= 0 || *ofs_offset >= obj->idx.offset)
551 bad_object(obj->idx.offset, _("delta base offset is out of bound"));
559 bad_object(obj->idx.offset, _("unknown object type %d"), obj->type);
561 obj->hdr_size = consumed_bytes - obj->idx.offset;
563 data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, oid);
564 obj->idx.crc32 = input_crc32;
568 static void *unpack_data(struct object_entry *obj,
569 int (*consume)(const unsigned char *, unsigned long, void *),
572 off_t from = obj[0].idx.offset + obj[0].hdr_size;
573 off_t len = obj[1].idx.offset - from;
574 unsigned char *data, *inbuf;
578 data = xmallocz(consume ? 64*1024 : obj->size);
579 inbuf = xmalloc((len < 64*1024) ? (int)len : 64*1024);
581 memset(&stream, 0, sizeof(stream));
582 git_inflate_init(&stream);
583 stream.next_out = data;
584 stream.avail_out = consume ? 64*1024 : obj->size;
587 ssize_t n = (len < 64*1024) ? (ssize_t)len : 64*1024;
588 n = xpread(get_thread_data()->pack_fd, inbuf, n, from);
590 die_errno(_("cannot pread pack file"));
592 die(Q_("premature end of pack file, %"PRIuMAX" byte missing",
593 "premature end of pack file, %"PRIuMAX" bytes missing",
598 stream.next_in = inbuf;
601 status = git_inflate(&stream, 0);
604 status = git_inflate(&stream, 0);
605 if (consume(data, stream.next_out - data, cb_data)) {
610 stream.next_out = data;
611 stream.avail_out = 64*1024;
612 } while (status == Z_OK && stream.avail_in);
614 } while (len && status == Z_OK && !stream.avail_in);
616 /* This has been inflated OK when first encountered, so... */
617 if (status != Z_STREAM_END || stream.total_out != obj->size)
618 die(_("serious inflate inconsistency"));
620 git_inflate_end(&stream);
628 static void *get_data_from_pack(struct object_entry *obj)
630 return unpack_data(obj, NULL, NULL);
633 static int compare_ofs_delta_bases(off_t offset1, off_t offset2,
634 enum object_type type1,
635 enum object_type type2)
637 int cmp = type1 - type2;
640 return offset1 < offset2 ? -1 :
641 offset1 > offset2 ? 1 :
645 static int find_ofs_delta(const off_t offset)
647 int first = 0, last = nr_ofs_deltas;
649 while (first < last) {
650 int next = first + (last - first) / 2;
651 struct ofs_delta_entry *delta = &ofs_deltas[next];
654 cmp = compare_ofs_delta_bases(offset, delta->offset,
656 objects[delta->obj_no].type);
668 static void find_ofs_delta_children(off_t offset,
669 int *first_index, int *last_index)
671 int first = find_ofs_delta(offset);
673 int end = nr_ofs_deltas - 1;
680 while (first > 0 && ofs_deltas[first - 1].offset == offset)
682 while (last < end && ofs_deltas[last + 1].offset == offset)
684 *first_index = first;
688 static int compare_ref_delta_bases(const struct object_id *oid1,
689 const struct object_id *oid2,
690 enum object_type type1,
691 enum object_type type2)
693 int cmp = type1 - type2;
696 return oidcmp(oid1, oid2);
699 static int find_ref_delta(const struct object_id *oid)
701 int first = 0, last = nr_ref_deltas;
703 while (first < last) {
704 int next = first + (last - first) / 2;
705 struct ref_delta_entry *delta = &ref_deltas[next];
708 cmp = compare_ref_delta_bases(oid, &delta->oid,
710 objects[delta->obj_no].type);
722 static void find_ref_delta_children(const struct object_id *oid,
723 int *first_index, int *last_index)
725 int first = find_ref_delta(oid);
727 int end = nr_ref_deltas - 1;
734 while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid))
736 while (last < end && oideq(&ref_deltas[last + 1].oid, oid))
738 *first_index = first;
742 struct compare_data {
743 struct object_entry *entry;
744 struct git_istream *st;
746 unsigned long buf_size;
749 static int compare_objects(const unsigned char *buf, unsigned long size,
752 struct compare_data *data = cb_data;
754 if (data->buf_size < size) {
756 data->buf = xmalloc(size);
757 data->buf_size = size;
761 ssize_t len = read_istream(data->st, data->buf, size);
763 die(_("SHA1 COLLISION FOUND WITH %s !"),
764 oid_to_hex(&data->entry->idx.oid));
766 die(_("unable to read %s"),
767 oid_to_hex(&data->entry->idx.oid));
768 if (memcmp(buf, data->buf, len))
769 die(_("SHA1 COLLISION FOUND WITH %s !"),
770 oid_to_hex(&data->entry->idx.oid));
777 static int check_collison(struct object_entry *entry)
779 struct compare_data data;
780 enum object_type type;
783 if (entry->size <= big_file_threshold || entry->type != OBJ_BLOB)
786 memset(&data, 0, sizeof(data));
788 data.st = open_istream(the_repository, &entry->idx.oid, &type, &size,
792 if (size != entry->size || type != entry->type)
793 die(_("SHA1 COLLISION FOUND WITH %s !"),
794 oid_to_hex(&entry->idx.oid));
795 unpack_data(entry, compare_objects, &data);
796 close_istream(data.st);
801 static void sha1_object(const void *data, struct object_entry *obj_entry,
802 unsigned long size, enum object_type type,
803 const struct object_id *oid)
805 void *new_data = NULL;
806 int collision_test_needed = 0;
808 assert(data || obj_entry);
810 if (startup_info->have_repository) {
812 collision_test_needed =
813 has_object_file_with_flags(oid, OBJECT_INFO_QUICK);
817 if (collision_test_needed && !data) {
819 if (!check_collison(obj_entry))
820 collision_test_needed = 0;
823 if (collision_test_needed) {
825 enum object_type has_type;
826 unsigned long has_size;
828 has_type = oid_object_info(the_repository, oid, &has_size);
830 die(_("cannot read existing object info %s"), oid_to_hex(oid));
831 if (has_type != type || has_size != size)
832 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
833 has_data = read_object_file(oid, &has_type, &has_size);
836 data = new_data = get_data_from_pack(obj_entry);
838 die(_("cannot read existing object %s"), oid_to_hex(oid));
839 if (size != has_size || type != has_type ||
840 memcmp(data, has_data, size) != 0)
841 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
845 if (strict || do_fsck_object) {
847 if (type == OBJ_BLOB) {
848 struct blob *blob = lookup_blob(the_repository, oid);
850 blob->object.flags |= FLAG_CHECKED;
852 die(_("invalid blob object %s"), oid_to_hex(oid));
853 if (do_fsck_object &&
854 fsck_object(&blob->object, (void *)data, size, &fsck_options))
855 die(_("fsck error in packed object"));
859 void *buf = (void *) data;
861 assert(data && "data can only be NULL for large _blobs_");
864 * we do not need to free the memory here, as the
865 * buf is deleted by the caller.
867 obj = parse_object_buffer(the_repository, oid, type,
871 die(_("invalid %s"), type_name(type));
872 if (do_fsck_object &&
873 fsck_object(obj, buf, size, &fsck_options))
874 die(_("fsck error in packed object"));
875 if (strict && fsck_walk(obj, NULL, &fsck_options))
876 die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
878 if (obj->type == OBJ_TREE) {
879 struct tree *item = (struct tree *) obj;
883 if (obj->type == OBJ_COMMIT) {
884 struct commit *commit = (struct commit *) obj;
885 if (detach_commit_buffer(commit, NULL) != data)
886 BUG("parse_object_buffer transmogrified our buffer");
888 obj->flags |= FLAG_CHECKED;
897 * Walk from current node up
898 * to top parent if necessary to deflate the node. In normal
899 * situation, its parent node would be already deflated, so it just
900 * needs to apply delta.
902 * In the worst case scenario, parent node is no longer deflated because
903 * we're running out of delta_base_cache_limit; we need to re-deflate
904 * parents, possibly up to the top base.
906 * All deflated objects here are subject to be freed if we exceed
907 * delta_base_cache_limit, just like in find_unresolved_deltas(), we
908 * just need to make sure the last node is not freed.
910 static void *get_base_data(struct base_data *c)
913 struct object_entry *obj = c->obj;
914 struct base_data **delta = NULL;
915 int delta_nr = 0, delta_alloc = 0;
917 while (is_delta_type(c->obj->type) && !c->data) {
918 ALLOC_GROW(delta, delta_nr + 1, delta_alloc);
919 delta[delta_nr++] = c;
923 c->data = get_data_from_pack(obj);
925 base_cache_used += c->size;
928 for (; delta_nr > 0; delta_nr--) {
930 c = delta[delta_nr - 1];
932 base = get_base_data(c->base);
933 raw = get_data_from_pack(obj);
934 c->data = patch_delta(
940 bad_object(obj->idx.offset, _("failed to apply delta"));
941 base_cache_used += c->size;
949 static struct base_data *make_base(struct object_entry *obj,
950 struct base_data *parent)
952 struct base_data *base = xcalloc(1, sizeof(struct base_data));
955 find_ref_delta_children(&obj->idx.oid,
956 &base->ref_first, &base->ref_last);
957 find_ofs_delta_children(obj->idx.offset,
958 &base->ofs_first, &base->ofs_last);
959 base->children_remaining = base->ref_last - base->ref_first +
960 base->ofs_last - base->ofs_first + 2;
964 static struct base_data *resolve_delta(struct object_entry *delta_obj,
965 struct base_data *base)
967 void *delta_data, *result_data;
968 struct base_data *result;
969 unsigned long result_size;
972 int i = delta_obj - objects;
973 int j = base->obj - objects;
974 obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1;
975 deepest_delta_lock();
976 if (deepest_delta < obj_stat[i].delta_depth)
977 deepest_delta = obj_stat[i].delta_depth;
978 deepest_delta_unlock();
979 obj_stat[i].base_object_no = j;
981 delta_data = get_data_from_pack(delta_obj);
983 result_data = patch_delta(base->data, base->size,
984 delta_data, delta_obj->size, &result_size);
987 bad_object(delta_obj->idx.offset, _("failed to apply delta"));
988 hash_object_file(the_hash_algo, result_data, result_size,
989 type_name(delta_obj->real_type), &delta_obj->idx.oid);
990 sha1_object(result_data, NULL, result_size, delta_obj->real_type,
991 &delta_obj->idx.oid);
993 result = make_base(delta_obj, base);
994 result->data = result_data;
995 result->size = result_size;
998 nr_resolved_deltas++;
1004 static int compare_ofs_delta_entry(const void *a, const void *b)
1006 const struct ofs_delta_entry *delta_a = a;
1007 const struct ofs_delta_entry *delta_b = b;
1009 return delta_a->offset < delta_b->offset ? -1 :
1010 delta_a->offset > delta_b->offset ? 1 :
1014 static int compare_ref_delta_entry(const void *a, const void *b)
1016 const struct ref_delta_entry *delta_a = a;
1017 const struct ref_delta_entry *delta_b = b;
1019 return oidcmp(&delta_a->oid, &delta_b->oid);
1022 static void *threaded_second_pass(void *data)
1025 set_thread_data(data);
1027 struct base_data *parent = NULL;
1028 struct object_entry *child_obj;
1029 struct base_data *child;
1032 if (list_empty(&work_head)) {
1034 * Take an object from the object array.
1036 while (nr_dispatched < nr_objects &&
1037 is_delta_type(objects[nr_dispatched].type))
1039 if (nr_dispatched >= nr_objects) {
1043 child_obj = &objects[nr_dispatched++];
1046 * Peek at the top of the stack, and take a child from
1049 parent = list_first_entry(&work_head, struct base_data,
1052 if (parent->ref_first <= parent->ref_last) {
1053 int offset = ref_deltas[parent->ref_first++].obj_no;
1054 child_obj = objects + offset;
1055 if (child_obj->real_type != OBJ_REF_DELTA)
1056 die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
1057 (uintmax_t) child_obj->idx.offset,
1058 oid_to_hex(&parent->obj->idx.oid));
1059 child_obj->real_type = parent->obj->real_type;
1061 child_obj = objects +
1062 ofs_deltas[parent->ofs_first++].obj_no;
1063 assert(child_obj->real_type == OBJ_OFS_DELTA);
1064 child_obj->real_type = parent->obj->real_type;
1067 if (parent->ref_first > parent->ref_last &&
1068 parent->ofs_first > parent->ofs_last) {
1070 * This parent has run out of children, so move
1073 list_del(&parent->list);
1074 list_add(&parent->list, &done_head);
1078 * Ensure that the parent has data, since we will need
1081 * NEEDSWORK: If parent data needs to be reloaded, this
1082 * prolongs the time that the current thread spends in
1083 * the mutex. A mitigating factor is that parent data
1084 * needs to be reloaded only if the delta base cache
1085 * limit is exceeded, so in the typical case, this does
1088 get_base_data(parent);
1089 parent->retain_data++;
1094 child = resolve_delta(child_obj, parent);
1095 if (!child->children_remaining)
1096 FREE_AND_NULL(child->data);
1098 child = make_base(child_obj, NULL);
1099 if (child->children_remaining) {
1101 * Since this child has its own delta children,
1102 * we will need this data in the future.
1103 * Inflate now so that future iterations will
1104 * have access to this object's data while
1105 * outside the work mutex.
1107 child->data = get_data_from_pack(child_obj);
1108 child->size = child_obj->size;
1114 parent->retain_data--;
1117 * This child has its own children, so add it to
1120 list_add(&child->list, &work_head);
1121 base_cache_used += child->size;
1122 prune_base_data(NULL);
1125 * This child does not have its own children. It may be
1126 * the last descendant of its ancestors; free those
1129 struct base_data *p = parent;
1132 struct base_data *next_p;
1134 p->children_remaining--;
1135 if (p->children_remaining)
1153 * - find locations of all objects;
1154 * - calculate SHA1 of all non-delta objects;
1155 * - remember base (SHA1 or offset) for all deltas.
1157 static void parse_pack_objects(unsigned char *hash)
1159 int i, nr_delays = 0;
1160 struct ofs_delta_entry *ofs_delta = ofs_deltas;
1161 struct object_id ref_delta_oid;
1165 progress = start_progress(
1166 from_stdin ? _("Receiving objects") : _("Indexing objects"),
1168 for (i = 0; i < nr_objects; i++) {
1169 struct object_entry *obj = &objects[i];
1170 void *data = unpack_raw_entry(obj, &ofs_delta->offset,
1173 obj->real_type = obj->type;
1174 if (obj->type == OBJ_OFS_DELTA) {
1176 ofs_delta->obj_no = i;
1178 } else if (obj->type == OBJ_REF_DELTA) {
1179 ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc);
1180 oidcpy(&ref_deltas[nr_ref_deltas].oid, &ref_delta_oid);
1181 ref_deltas[nr_ref_deltas].obj_no = i;
1184 /* large blobs, check later */
1185 obj->real_type = OBJ_BAD;
1188 sha1_object(data, NULL, obj->size, obj->type,
1191 display_progress(progress, i+1);
1193 objects[i].idx.offset = consumed_bytes;
1194 stop_progress(&progress);
1196 /* Check pack integrity */
1198 the_hash_algo->final_fn(hash, &input_ctx);
1199 if (!hasheq(fill(the_hash_algo->rawsz), hash))
1200 die(_("pack is corrupted (SHA1 mismatch)"));
1201 use(the_hash_algo->rawsz);
1203 /* If input_fd is a file, we should have reached its end now. */
1204 if (fstat(input_fd, &st))
1205 die_errno(_("cannot fstat packfile"));
1206 if (S_ISREG(st.st_mode) &&
1207 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
1208 die(_("pack has junk at the end"));
1210 for (i = 0; i < nr_objects; i++) {
1211 struct object_entry *obj = &objects[i];
1212 if (obj->real_type != OBJ_BAD)
1214 obj->real_type = obj->type;
1215 sha1_object(NULL, obj, obj->size, obj->type,
1220 die(_("confusion beyond insanity in parse_pack_objects()"));
1225 * - for all non-delta objects, look if it is used as a base for
1227 * - if used as a base, uncompress the object and apply all deltas,
1228 * recursively checking if the resulting object is used as a base
1229 * for some more deltas.
1231 static void resolve_deltas(void)
1235 if (!nr_ofs_deltas && !nr_ref_deltas)
1238 /* Sort deltas by base SHA1/offset for fast searching */
1239 QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry);
1240 QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry);
1242 if (verbose || show_resolving_progress)
1243 progress = start_progress(_("Resolving deltas"),
1244 nr_ref_deltas + nr_ofs_deltas);
1247 base_cache_limit = delta_base_cache_limit * nr_threads;
1248 if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
1250 for (i = 0; i < nr_threads; i++) {
1251 int ret = pthread_create(&thread_data[i].thread, NULL,
1252 threaded_second_pass, thread_data + i);
1254 die(_("unable to create thread: %s"),
1257 for (i = 0; i < nr_threads; i++)
1258 pthread_join(thread_data[i].thread, NULL);
1262 threaded_second_pass(¬hread_data);
1267 * - append objects to convert thin pack to full pack if required
1268 * - write the final pack hash
1270 static void fix_unresolved_deltas(struct hashfile *f);
1271 static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash)
1273 if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
1274 stop_progress(&progress);
1275 /* Flush remaining pack final hash. */
1280 if (fix_thin_pack) {
1282 unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ];
1283 struct strbuf msg = STRBUF_INIT;
1284 int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
1285 int nr_objects_initial = nr_objects;
1286 if (nr_unresolved <= 0)
1287 die(_("confusion beyond insanity"));
1288 REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
1289 memset(objects + nr_objects + 1, 0,
1290 nr_unresolved * sizeof(*objects));
1291 f = hashfd(output_fd, curr_pack);
1292 fix_unresolved_deltas(f);
1293 strbuf_addf(&msg, Q_("completed with %d local object",
1294 "completed with %d local objects",
1295 nr_objects - nr_objects_initial),
1296 nr_objects - nr_objects_initial);
1297 stop_progress_msg(&progress, msg.buf);
1298 strbuf_release(&msg);
1299 finalize_hashfile(f, tail_hash, 0);
1300 hashcpy(read_hash, pack_hash);
1301 fixup_pack_header_footer(output_fd, pack_hash,
1302 curr_pack, nr_objects,
1303 read_hash, consumed_bytes-the_hash_algo->rawsz);
1304 if (!hasheq(read_hash, tail_hash))
1305 die(_("Unexpected tail checksum for %s "
1306 "(disk corruption?)"), curr_pack);
1308 if (nr_ofs_deltas + nr_ref_deltas != nr_resolved_deltas)
1309 die(Q_("pack has %d unresolved delta",
1310 "pack has %d unresolved deltas",
1311 nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas),
1312 nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
1315 static int write_compressed(struct hashfile *f, void *in, unsigned int size)
1319 unsigned char outbuf[4096];
1321 git_deflate_init(&stream, zlib_compression_level);
1322 stream.next_in = in;
1323 stream.avail_in = size;
1326 stream.next_out = outbuf;
1327 stream.avail_out = sizeof(outbuf);
1328 status = git_deflate(&stream, Z_FINISH);
1329 hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out);
1330 } while (status == Z_OK);
1332 if (status != Z_STREAM_END)
1333 die(_("unable to deflate appended object (%d)"), status);
1334 size = stream.total_out;
1335 git_deflate_end(&stream);
1339 static struct object_entry *append_obj_to_pack(struct hashfile *f,
1340 const unsigned char *sha1, void *buf,
1341 unsigned long size, enum object_type type)
1343 struct object_entry *obj = &objects[nr_objects++];
1344 unsigned char header[10];
1345 unsigned long s = size;
1347 unsigned char c = (type << 4) | (s & 15);
1350 header[n++] = c | 0x80;
1356 hashwrite(f, header, n);
1358 obj[0].hdr_size = n;
1360 obj[0].real_type = type;
1361 obj[1].idx.offset = obj[0].idx.offset + n;
1362 obj[1].idx.offset += write_compressed(f, buf, size);
1363 obj[0].idx.crc32 = crc32_end(f);
1365 hashcpy(obj->idx.oid.hash, sha1);
1369 static int delta_pos_compare(const void *_a, const void *_b)
1371 struct ref_delta_entry *a = *(struct ref_delta_entry **)_a;
1372 struct ref_delta_entry *b = *(struct ref_delta_entry **)_b;
1373 return a->obj_no - b->obj_no;
1376 static void fix_unresolved_deltas(struct hashfile *f)
1378 struct ref_delta_entry **sorted_by_pos;
1382 * Since many unresolved deltas may well be themselves base objects
1383 * for more unresolved deltas, we really want to include the
1384 * smallest number of base objects that would cover as much delta
1385 * as possible by picking the
1386 * trunc deltas first, allowing for other deltas to resolve without
1387 * additional base objects. Since most base objects are to be found
1388 * before deltas depending on them, a good heuristic is to start
1389 * resolving deltas in the same order as their position in the pack.
1391 ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
1392 for (i = 0; i < nr_ref_deltas; i++)
1393 sorted_by_pos[i] = &ref_deltas[i];
1394 QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare);
1396 if (has_promisor_remote()) {
1398 * Prefetch the delta bases.
1400 struct oid_array to_fetch = OID_ARRAY_INIT;
1401 for (i = 0; i < nr_ref_deltas; i++) {
1402 struct ref_delta_entry *d = sorted_by_pos[i];
1403 if (!oid_object_info_extended(the_repository, &d->oid,
1405 OBJECT_INFO_FOR_PREFETCH))
1407 oid_array_append(&to_fetch, &d->oid);
1409 promisor_remote_get_direct(the_repository,
1410 to_fetch.oid, to_fetch.nr);
1411 oid_array_clear(&to_fetch);
1414 for (i = 0; i < nr_ref_deltas; i++) {
1415 struct ref_delta_entry *d = sorted_by_pos[i];
1416 enum object_type type;
1420 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
1422 data = read_object_file(&d->oid, &type, &size);
1426 if (check_object_signature(the_repository, &d->oid,
1429 die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
1432 * Add this as an object to the objects array and call
1433 * threaded_second_pass() (which will pick up the added
1436 append_obj_to_pack(f, d->oid.hash, data, size, type);
1437 threaded_second_pass(NULL);
1439 display_progress(progress, nr_resolved_deltas);
1441 free(sorted_by_pos);
1444 static const char *derive_filename(const char *pack_name, const char *suffix,
1448 if (!strip_suffix(pack_name, ".pack", &len))
1449 die(_("packfile name '%s' does not end with '.pack'"),
1451 strbuf_add(buf, pack_name, len);
1452 strbuf_addch(buf, '.');
1453 strbuf_addstr(buf, suffix);
1457 static void write_special_file(const char *suffix, const char *msg,
1458 const char *pack_name, const unsigned char *hash,
1459 const char **report)
1461 struct strbuf name_buf = STRBUF_INIT;
1462 const char *filename;
1464 int msg_len = strlen(msg);
1467 filename = derive_filename(pack_name, suffix, &name_buf);
1469 filename = odb_pack_name(&name_buf, hash, suffix);
1471 fd = odb_pack_keep(filename);
1473 if (errno != EEXIST)
1474 die_errno(_("cannot write %s file '%s'"),
1478 write_or_die(fd, msg, msg_len);
1479 write_or_die(fd, "\n", 1);
1482 die_errno(_("cannot close written %s file '%s'"),
1487 strbuf_release(&name_buf);
1490 static void final(const char *final_pack_name, const char *curr_pack_name,
1491 const char *final_index_name, const char *curr_index_name,
1492 const char *keep_msg, const char *promisor_msg,
1493 unsigned char *hash)
1495 const char *report = "pack";
1496 struct strbuf pack_name = STRBUF_INIT;
1497 struct strbuf index_name = STRBUF_INIT;
1503 fsync_or_die(output_fd, curr_pack_name);
1504 err = close(output_fd);
1506 die_errno(_("error while closing pack file"));
1510 write_special_file("keep", keep_msg, final_pack_name, hash,
1513 write_special_file("promisor", promisor_msg, final_pack_name,
1516 if (final_pack_name != curr_pack_name) {
1517 if (!final_pack_name)
1518 final_pack_name = odb_pack_name(&pack_name, hash, "pack");
1519 if (finalize_object_file(curr_pack_name, final_pack_name))
1520 die(_("cannot store pack file"));
1521 } else if (from_stdin)
1522 chmod(final_pack_name, 0444);
1524 if (final_index_name != curr_index_name) {
1525 if (!final_index_name)
1526 final_index_name = odb_pack_name(&index_name, hash, "idx");
1527 if (finalize_object_file(curr_index_name, final_index_name))
1528 die(_("cannot store index file"));
1530 chmod(final_index_name, 0444);
1532 if (do_fsck_object) {
1533 struct packed_git *p;
1534 p = add_packed_git(final_index_name, strlen(final_index_name), 0);
1536 install_packed_git(the_repository, p);
1540 printf("%s\n", hash_to_hex(hash));
1542 struct strbuf buf = STRBUF_INIT;
1544 strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash));
1545 write_or_die(1, buf.buf, buf.len);
1546 strbuf_release(&buf);
1549 * Let's just mimic git-unpack-objects here and write
1550 * the last part of the input buffer to stdout.
1553 err = xwrite(1, input_buffer + input_offset, input_len);
1557 input_offset += err;
1561 strbuf_release(&index_name);
1562 strbuf_release(&pack_name);
1565 static int git_index_pack_config(const char *k, const char *v, void *cb)
1567 struct pack_idx_option *opts = cb;
1569 if (!strcmp(k, "pack.indexversion")) {
1570 opts->version = git_config_int(k, v);
1571 if (opts->version > 2)
1572 die(_("bad pack.indexversion=%"PRIu32), opts->version);
1575 if (!strcmp(k, "pack.threads")) {
1576 nr_threads = git_config_int(k, v);
1578 die(_("invalid number of threads specified (%d)"),
1580 if (!HAVE_THREADS && nr_threads != 1) {
1581 warning(_("no threads support, ignoring %s"), k);
1586 return git_default_config(k, v, cb);
1589 static int cmp_uint32(const void *a_, const void *b_)
1591 uint32_t a = *((uint32_t *)a_);
1592 uint32_t b = *((uint32_t *)b_);
1594 return (a < b) ? -1 : (a != b);
1597 static void read_v2_anomalous_offsets(struct packed_git *p,
1598 struct pack_idx_option *opts)
1600 const uint32_t *idx1, *idx2;
1603 /* The address of the 4-byte offset table */
1604 idx1 = (((const uint32_t *)((const uint8_t *)p->index_data + p->crc_offset))
1605 + p->num_objects /* CRC32 table */
1608 /* The address of the 8-byte offset table */
1609 idx2 = idx1 + p->num_objects;
1611 for (i = 0; i < p->num_objects; i++) {
1612 uint32_t off = ntohl(idx1[i]);
1613 if (!(off & 0x80000000))
1615 off = off & 0x7fffffff;
1616 check_pack_index_ptr(p, &idx2[off * 2]);
1620 * The real offset is ntohl(idx2[off * 2]) in high 4
1621 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1622 * octets. But idx2[off * 2] is Zero!!!
1624 ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
1625 opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
1628 QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32);
1631 static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
1633 struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
1636 die(_("Cannot open existing pack file '%s'"), pack_name);
1637 if (open_pack_index(p))
1638 die(_("Cannot open existing pack idx file for '%s'"), pack_name);
1640 /* Read the attributes from the existing idx file */
1641 opts->version = p->index_version;
1643 if (opts->version == 2)
1644 read_v2_anomalous_offsets(p, opts);
1647 * Get rid of the idx file as we do not need it anymore.
1648 * NEEDSWORK: extract this bit from free_pack_by_name() in
1649 * sha1-file.c, perhaps? It shouldn't matter very much as we
1650 * know we haven't installed this pack (hence we never have
1651 * read anything from it).
1653 close_pack_index(p);
1657 static void show_pack_info(int stat_only)
1659 int i, baseobjects = nr_objects - nr_ref_deltas - nr_ofs_deltas;
1660 unsigned long *chain_histogram = NULL;
1663 chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long));
1665 for (i = 0; i < nr_objects; i++) {
1666 struct object_entry *obj = &objects[i];
1668 if (is_delta_type(obj->type))
1669 chain_histogram[obj_stat[i].delta_depth - 1]++;
1672 printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
1673 oid_to_hex(&obj->idx.oid),
1674 type_name(obj->real_type), (uintmax_t)obj->size,
1675 (uintmax_t)(obj[1].idx.offset - obj->idx.offset),
1676 (uintmax_t)obj->idx.offset);
1677 if (is_delta_type(obj->type)) {
1678 struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
1679 printf(" %u %s", obj_stat[i].delta_depth,
1680 oid_to_hex(&bobj->idx.oid));
1686 printf_ln(Q_("non delta: %d object",
1687 "non delta: %d objects",
1690 for (i = 0; i < deepest_delta; i++) {
1691 if (!chain_histogram[i])
1693 printf_ln(Q_("chain length = %d: %lu object",
1694 "chain length = %d: %lu objects",
1695 chain_histogram[i]),
1697 chain_histogram[i]);
1701 int cmd_index_pack(int argc, const char **argv, const char *prefix)
1703 int i, fix_thin_pack = 0, verify = 0, stat_only = 0;
1704 const char *curr_index;
1705 const char *index_name = NULL, *pack_name = NULL;
1706 const char *keep_msg = NULL;
1707 const char *promisor_msg = NULL;
1708 struct strbuf index_name_buf = STRBUF_INIT;
1709 struct pack_idx_entry **idx_objects;
1710 struct pack_idx_option opts;
1711 unsigned char pack_hash[GIT_MAX_RAWSZ];
1712 unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */
1713 int report_end_of_input = 0;
1717 * index-pack never needs to fetch missing objects except when
1718 * REF_DELTA bases are missing (which are explicitly handled). It only
1719 * accesses the repo to do hash collision checks and to check which
1720 * REF_DELTA bases need to be fetched.
1722 fetch_if_missing = 0;
1724 if (argc == 2 && !strcmp(argv[1], "-h"))
1725 usage(index_pack_usage);
1727 read_replace_refs = 0;
1728 fsck_options.walk = mark_link;
1730 reset_pack_idx_option(&opts);
1731 git_config(git_index_pack_config, &opts);
1732 if (prefix && chdir(prefix))
1733 die(_("Cannot come back to cwd"));
1735 for (i = 1; i < argc; i++) {
1736 const char *arg = argv[i];
1739 if (!strcmp(arg, "--stdin")) {
1741 } else if (!strcmp(arg, "--fix-thin")) {
1743 } else if (skip_to_optional_arg(arg, "--strict", &arg)) {
1746 fsck_set_msg_types(&fsck_options, arg);
1747 } else if (!strcmp(arg, "--check-self-contained-and-connected")) {
1749 check_self_contained_and_connected = 1;
1750 } else if (!strcmp(arg, "--fsck-objects")) {
1752 } else if (!strcmp(arg, "--verify")) {
1754 } else if (!strcmp(arg, "--verify-stat")) {
1757 } else if (!strcmp(arg, "--verify-stat-only")) {
1761 } else if (skip_to_optional_arg(arg, "--keep", &keep_msg)) {
1762 ; /* nothing to do */
1763 } else if (skip_to_optional_arg(arg, "--promisor", &promisor_msg)) {
1764 ; /* already parsed */
1765 } else if (starts_with(arg, "--threads=")) {
1767 nr_threads = strtoul(arg+10, &end, 0);
1768 if (!arg[10] || *end || nr_threads < 0)
1769 usage(index_pack_usage);
1770 if (!HAVE_THREADS && nr_threads != 1) {
1771 warning(_("no threads support, ignoring %s"), arg);
1774 } else if (starts_with(arg, "--pack_header=")) {
1775 struct pack_header *hdr;
1778 hdr = (struct pack_header *)input_buffer;
1779 hdr->hdr_signature = htonl(PACK_SIGNATURE);
1780 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
1782 die(_("bad %s"), arg);
1783 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
1785 die(_("bad %s"), arg);
1786 input_len = sizeof(*hdr);
1787 } else if (!strcmp(arg, "-v")) {
1789 } else if (!strcmp(arg, "--show-resolving-progress")) {
1790 show_resolving_progress = 1;
1791 } else if (!strcmp(arg, "--report-end-of-input")) {
1792 report_end_of_input = 1;
1793 } else if (!strcmp(arg, "-o")) {
1794 if (index_name || (i+1) >= argc)
1795 usage(index_pack_usage);
1796 index_name = argv[++i];
1797 } else if (starts_with(arg, "--index-version=")) {
1799 opts.version = strtoul(arg + 16, &c, 10);
1800 if (opts.version > 2)
1801 die(_("bad %s"), arg);
1803 opts.off32_limit = strtoul(c+1, &c, 0);
1804 if (*c || opts.off32_limit & 0x80000000)
1805 die(_("bad %s"), arg);
1806 } else if (skip_prefix(arg, "--max-input-size=", &arg)) {
1807 max_input_size = strtoumax(arg, NULL, 10);
1808 } else if (skip_prefix(arg, "--object-format=", &arg)) {
1809 hash_algo = hash_algo_by_name(arg);
1810 if (hash_algo == GIT_HASH_UNKNOWN)
1811 die(_("unknown hash algorithm '%s'"), arg);
1812 repo_set_hash_algo(the_repository, hash_algo);
1814 usage(index_pack_usage);
1819 usage(index_pack_usage);
1823 if (!pack_name && !from_stdin)
1824 usage(index_pack_usage);
1825 if (fix_thin_pack && !from_stdin)
1826 die(_("--fix-thin cannot be used without --stdin"));
1827 if (from_stdin && !startup_info->have_repository)
1828 die(_("--stdin requires a git repository"));
1829 if (from_stdin && hash_algo)
1830 die(_("--object-format cannot be used with --stdin"));
1831 if (!index_name && pack_name)
1832 index_name = derive_filename(pack_name, "idx", &index_name_buf);
1836 die(_("--verify with no packfile name given"));
1837 read_idx_option(&opts, index_name);
1838 opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT;
1841 opts.flags |= WRITE_IDX_STRICT;
1843 if (HAVE_THREADS && !nr_threads) {
1844 nr_threads = online_cpus();
1846 * Experiments show that going above 20 threads doesn't help,
1847 * no matter how many cores you have. Below that, we tend to
1848 * max at half the number of online_cpus(), presumably because
1849 * half of those are hyperthreads rather than full cores. We'll
1850 * never reduce the level below "3", though, to match a
1851 * historical value that nobody complained about.
1854 ; /* too few cores to consider capping */
1855 else if (nr_threads < 6)
1856 nr_threads = 3; /* historic cap */
1857 else if (nr_threads < 40)
1860 nr_threads = 20; /* hard cap */
1863 curr_pack = open_pack_file(pack_name);
1864 parse_pack_header();
1865 objects = xcalloc(st_add(nr_objects, 1), sizeof(struct object_entry));
1867 obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat));
1868 ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry));
1869 parse_pack_objects(pack_hash);
1870 if (report_end_of_input)
1871 write_in_full(2, "\0", 1);
1873 conclude_pack(fix_thin_pack, curr_pack, pack_hash);
1877 foreign_nr = check_objects();
1880 show_pack_info(stat_only);
1882 ALLOC_ARRAY(idx_objects, nr_objects);
1883 for (i = 0; i < nr_objects; i++)
1884 idx_objects[i] = &objects[i].idx;
1885 curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
1889 final(pack_name, curr_pack,
1890 index_name, curr_index,
1891 keep_msg, promisor_msg,
1896 if (do_fsck_object && fsck_finish(&fsck_options))
1897 die(_("fsck error in pack objects"));
1900 strbuf_release(&index_name_buf);
1901 if (pack_name == NULL)
1902 free((void *) curr_pack);
1903 if (index_name == NULL)
1904 free((void *) curr_index);
1907 * Let the caller know this pack is not self contained
1909 if (check_self_contained_and_connected && foreign_nr)