4
#include "environment.h"
16
#include "thread-utils.h"
18
#include "pack-revindex.h"
19
#include "object-file.h"
20
#include "object-store-ll.h"
22
#include "replace-object.h"
23
#include "promisor-remote.h"
26
static const char index_pack_usage[] =
27
"git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--[no-]rev-index] [--verify] [--strict[=<msg-id>=<severity>...]] [--fsck-objects[=<msg-id>=<severity>...]] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
30
struct pack_idx_entry idx;
32
unsigned char hdr_size;
34
signed char real_type;
44
struct base_data *base;
45
struct object_entry *obj;
46
int ref_first, ref_last;
47
int ofs_first, ofs_last;
60
int children_remaining;
63
struct list_head list;
75
static LIST_HEAD(work_head);
85
static LIST_HEAD(done_head);
93
static size_t base_cache_used;
94
static size_t base_cache_limit;
102
#define FLAG_LINK (1u<<20)
103
#define FLAG_CHECKED (1u<<21)
105
struct ofs_delta_entry {
110
struct ref_delta_entry {
111
struct object_id oid;
115
static struct object_entry *objects;
116
static struct object_stat *obj_stat;
117
static struct ofs_delta_entry *ofs_deltas;
118
static struct ref_delta_entry *ref_deltas;
119
static struct thread_local nothread_data;
120
static int nr_objects;
121
static int nr_ofs_deltas;
122
static int nr_ref_deltas;
123
static int ref_deltas_alloc;
124
static int nr_resolved_deltas;
125
static int nr_threads;
127
static int from_stdin;
129
static int do_fsck_object;
130
static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES;
132
static const char *progress_title;
133
static int show_resolving_progress;
135
static int check_self_contained_and_connected;
137
static struct progress *progress;
140
static unsigned char input_buffer[4096];
141
static unsigned int input_offset, input_len;
142
static off_t consumed_bytes;
143
static off_t max_input_size;
144
static unsigned deepest_delta;
145
static git_hash_ctx input_ctx;
146
static uint32_t input_crc32;
147
static int input_fd, output_fd;
148
static const char *curr_pack;
150
static struct thread_local *thread_data;
151
static int nr_dispatched;
152
static int threads_active;
154
static pthread_mutex_t read_mutex;
155
#define read_lock() lock_mutex(&read_mutex)
156
#define read_unlock() unlock_mutex(&read_mutex)
158
static pthread_mutex_t counter_mutex;
159
#define counter_lock() lock_mutex(&counter_mutex)
160
#define counter_unlock() unlock_mutex(&counter_mutex)
162
static pthread_mutex_t work_mutex;
163
#define work_lock() lock_mutex(&work_mutex)
164
#define work_unlock() unlock_mutex(&work_mutex)
166
static pthread_mutex_t deepest_delta_mutex;
167
#define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
168
#define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
170
static pthread_key_t key;
172
static inline void lock_mutex(pthread_mutex_t *mutex)
175
pthread_mutex_lock(mutex);
178
static inline void unlock_mutex(pthread_mutex_t *mutex)
181
pthread_mutex_unlock(mutex);
187
static void init_thread(void)
190
init_recursive_mutex(&read_mutex);
191
pthread_mutex_init(&counter_mutex, NULL);
192
pthread_mutex_init(&work_mutex, NULL);
194
pthread_mutex_init(&deepest_delta_mutex, NULL);
195
pthread_key_create(&key, NULL);
196
CALLOC_ARRAY(thread_data, nr_threads);
197
for (i = 0; i < nr_threads; i++) {
198
thread_data[i].pack_fd = xopen(curr_pack, O_RDONLY);
204
static void cleanup_thread(void)
210
pthread_mutex_destroy(&read_mutex);
211
pthread_mutex_destroy(&counter_mutex);
212
pthread_mutex_destroy(&work_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, enum object_type type,
223
struct fsck_options *options UNUSED)
228
if (type != OBJ_ANY && obj->type != type)
229
die(_("object type mismatch at %s"), oid_to_hex(&obj->oid));
231
obj->flags |= FLAG_LINK;
237
static unsigned check_object(struct object *obj)
242
if (!(obj->flags & FLAG_LINK))
245
if (!(obj->flags & FLAG_CHECKED)) {
247
int type = oid_object_info(the_repository, &obj->oid, &size);
249
die(_("did not receive expected object %s"),
250
oid_to_hex(&obj->oid));
251
if (type != obj->type)
252
die(_("object %s: expected type %s, found %s"),
253
oid_to_hex(&obj->oid),
254
type_name(obj->type), type_name(type));
255
obj->flags |= FLAG_CHECKED;
262
static unsigned check_objects(void)
264
unsigned i, max, foreign_nr = 0;
266
max = get_max_object_index();
269
progress = start_delayed_progress(_("Checking objects"), max);
271
for (i = 0; i < max; i++) {
272
foreign_nr += check_object(get_indexed_object(i));
273
display_progress(progress, i + 1);
276
stop_progress(&progress);
282
static void flush(void)
286
write_or_die(output_fd, input_buffer, input_offset);
287
the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
288
memmove(input_buffer, input_buffer + input_offset, input_len);
297
static void *fill(int min)
299
if (min <= input_len)
300
return input_buffer + input_offset;
301
if (min > sizeof(input_buffer))
302
die(Q_("cannot fill %d byte",
303
"cannot fill %d bytes",
308
ssize_t ret = xread(input_fd, input_buffer + input_len,
309
sizeof(input_buffer) - input_len);
313
die_errno(_("read error on input"));
317
display_throughput(progress, consumed_bytes + input_len);
318
} while (input_len < min);
322
static void use(int bytes)
324
if (bytes > input_len)
325
die(_("used more bytes than were available"));
326
input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
328
input_offset += bytes;
331
if (signed_add_overflows(consumed_bytes, bytes))
332
die(_("pack too large for current definition of off_t"));
333
consumed_bytes += bytes;
334
if (max_input_size && consumed_bytes > max_input_size) {
335
struct strbuf size_limit = STRBUF_INIT;
336
strbuf_humanise_bytes(&size_limit, max_input_size);
337
die(_("pack exceeds maximum allowed size (%s)"),
342
static const char *open_pack_file(const char *pack_name)
347
struct strbuf tmp_file = STRBUF_INIT;
348
output_fd = odb_mkstemp(&tmp_file,
349
"pack/tmp_pack_XXXXXX");
350
pack_name = strbuf_detach(&tmp_file, NULL);
352
output_fd = xopen(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
354
nothread_data.pack_fd = output_fd;
356
input_fd = xopen(pack_name, O_RDONLY);
358
nothread_data.pack_fd = input_fd;
360
the_hash_algo->init_fn(&input_ctx);
364
static void parse_pack_header(void)
366
struct pack_header *hdr = fill(sizeof(struct pack_header));
369
if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
370
die(_("pack signature mismatch"));
371
if (!pack_version_ok(hdr->hdr_version))
372
die(_("pack version %"PRIu32" unsupported"),
373
ntohl(hdr->hdr_version));
375
nr_objects = ntohl(hdr->hdr_entries);
376
use(sizeof(struct pack_header));
379
__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 = format_object_header(hdr, sizeof(hdr), type, size);
465
the_hash_algo->init_fn(&c);
466
the_hash_algo->update_fn(&c, hdr, hdrlen);
469
if (type == OBJ_BLOB && size > big_file_threshold)
472
buf = xmallocz(size);
474
memset(&stream, 0, sizeof(stream));
475
git_inflate_init(&stream);
476
stream.next_out = buf;
477
stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size;
480
unsigned char *last_out = stream.next_out;
481
stream.next_in = fill(1);
482
stream.avail_in = input_len;
483
status = git_inflate(&stream, 0);
484
use(input_len - stream.avail_in);
486
the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
487
if (buf == fixed_buf) {
488
stream.next_out = buf;
489
stream.avail_out = sizeof(fixed_buf);
491
} while (status == Z_OK);
492
if (stream.total_out != size || status != Z_STREAM_END)
493
bad_object(offset, _("inflate returned %d"), status);
494
git_inflate_end(&stream);
496
the_hash_algo->final_oid_fn(oid, &c);
497
return buf == fixed_buf ? NULL : buf;
500
static void *unpack_raw_entry(struct object_entry *obj,
502
struct object_id *ref_oid,
503
struct object_id *oid)
506
unsigned long size, c;
511
obj->idx.offset = consumed_bytes;
512
input_crc32 = crc32(0, NULL, 0);
517
obj->type = (c >> 4) & 7;
524
size += (c & 0x7f) << shift;
531
oidread(ref_oid, fill(the_hash_algo->rawsz),
532
the_repository->hash_algo);
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);
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;
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
repo_has_object_file_with_flags(the_repository, oid,
818
if (collision_test_needed && !data) {
820
if (!check_collison(obj_entry))
821
collision_test_needed = 0;
824
if (collision_test_needed) {
826
enum object_type has_type;
827
unsigned long has_size;
829
has_type = oid_object_info(the_repository, oid, &has_size);
831
die(_("cannot read existing object info %s"), oid_to_hex(oid));
832
if (has_type != type || has_size != size)
833
die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
834
has_data = repo_read_object_file(the_repository, oid,
835
&has_type, &has_size);
838
data = new_data = get_data_from_pack(obj_entry);
840
die(_("cannot read existing object %s"), oid_to_hex(oid));
841
if (size != has_size || type != has_type ||
842
memcmp(data, has_data, size) != 0)
843
die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid));
847
if (strict || do_fsck_object) {
849
if (type == OBJ_BLOB) {
850
struct blob *blob = lookup_blob(the_repository, oid);
852
blob->object.flags |= FLAG_CHECKED;
854
die(_("invalid blob object %s"), oid_to_hex(oid));
855
if (do_fsck_object &&
856
fsck_object(&blob->object, (void *)data, size, &fsck_options))
857
die(_("fsck error in packed object"));
861
void *buf = (void *) data;
863
assert(data && "data can only be NULL for large _blobs_");
869
obj = parse_object_buffer(the_repository, oid, type,
873
die(_("invalid %s"), type_name(type));
874
if (do_fsck_object &&
875
fsck_object(obj, buf, size, &fsck_options))
876
die(_("fsck error in packed object"));
877
if (strict && fsck_walk(obj, NULL, &fsck_options))
878
die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
880
if (obj->type == OBJ_TREE) {
881
struct tree *item = (struct tree *) obj;
885
if (obj->type == OBJ_COMMIT) {
886
struct commit *commit = (struct commit *) obj;
887
if (detach_commit_buffer(commit, NULL) != data)
888
BUG("parse_object_buffer transmogrified our buffer");
890
obj->flags |= FLAG_CHECKED;
909
static void *get_base_data(struct base_data *c)
912
struct object_entry *obj = c->obj;
913
struct base_data **delta = NULL;
914
int delta_nr = 0, delta_alloc = 0;
916
while (is_delta_type(c->obj->type) && !c->data) {
917
ALLOC_GROW(delta, delta_nr + 1, delta_alloc);
918
delta[delta_nr++] = c;
922
c->data = get_data_from_pack(obj);
924
base_cache_used += c->size;
927
for (; delta_nr > 0; delta_nr--) {
929
c = delta[delta_nr - 1];
931
base = get_base_data(c->base);
932
raw = get_data_from_pack(obj);
933
c->data = patch_delta(
939
bad_object(obj->idx.offset, _("failed to apply delta"));
940
base_cache_used += c->size;
948
static struct base_data *make_base(struct object_entry *obj,
949
struct base_data *parent)
951
struct base_data *base = xcalloc(1, sizeof(struct base_data));
954
find_ref_delta_children(&obj->idx.oid,
955
&base->ref_first, &base->ref_last);
956
find_ofs_delta_children(obj->idx.offset,
957
&base->ofs_first, &base->ofs_last);
958
base->children_remaining = base->ref_last - base->ref_first +
959
base->ofs_last - base->ofs_first + 2;
963
static struct base_data *resolve_delta(struct object_entry *delta_obj,
964
struct base_data *base)
966
void *delta_data, *result_data;
967
struct base_data *result;
968
unsigned long result_size;
971
int i = delta_obj - objects;
972
int j = base->obj - objects;
973
obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1;
974
deepest_delta_lock();
975
if (deepest_delta < obj_stat[i].delta_depth)
976
deepest_delta = obj_stat[i].delta_depth;
977
deepest_delta_unlock();
978
obj_stat[i].base_object_no = j;
980
delta_data = get_data_from_pack(delta_obj);
982
result_data = patch_delta(base->data, base->size,
983
delta_data, delta_obj->size, &result_size);
986
bad_object(delta_obj->idx.offset, _("failed to apply delta"));
987
hash_object_file(the_hash_algo, result_data, result_size,
988
delta_obj->real_type, &delta_obj->idx.oid);
989
sha1_object(result_data, NULL, result_size, delta_obj->real_type,
990
&delta_obj->idx.oid);
992
result = make_base(delta_obj, base);
993
result->data = result_data;
994
result->size = result_size;
997
nr_resolved_deltas++;
1003
static int compare_ofs_delta_entry(const void *a, const void *b)
1005
const struct ofs_delta_entry *delta_a = a;
1006
const struct ofs_delta_entry *delta_b = b;
1008
return delta_a->offset < delta_b->offset ? -1 :
1009
delta_a->offset > delta_b->offset ? 1 :
1013
static int compare_ref_delta_entry(const void *a, const void *b)
1015
const struct ref_delta_entry *delta_a = a;
1016
const struct ref_delta_entry *delta_b = b;
1018
return oidcmp(&delta_a->oid, &delta_b->oid);
1021
static void *threaded_second_pass(void *data)
1024
set_thread_data(data);
1026
struct base_data *parent = NULL;
1027
struct object_entry *child_obj;
1028
struct base_data *child;
1031
display_progress(progress, nr_resolved_deltas);
1035
if (list_empty(&work_head)) {
1039
while (nr_dispatched < nr_objects &&
1040
is_delta_type(objects[nr_dispatched].type))
1042
if (nr_dispatched >= nr_objects) {
1046
child_obj = &objects[nr_dispatched++];
1052
parent = list_first_entry(&work_head, struct base_data,
1055
if (parent->ref_first <= parent->ref_last) {
1056
int offset = ref_deltas[parent->ref_first++].obj_no;
1057
child_obj = objects + offset;
1058
if (child_obj->real_type != OBJ_REF_DELTA)
1059
die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
1060
(uintmax_t) child_obj->idx.offset,
1061
oid_to_hex(&parent->obj->idx.oid));
1062
child_obj->real_type = parent->obj->real_type;
1064
child_obj = objects +
1065
ofs_deltas[parent->ofs_first++].obj_no;
1066
assert(child_obj->real_type == OBJ_OFS_DELTA);
1067
child_obj->real_type = parent->obj->real_type;
1070
if (parent->ref_first > parent->ref_last &&
1071
parent->ofs_first > parent->ofs_last) {
1076
list_del(&parent->list);
1077
list_add(&parent->list, &done_head);
1091
get_base_data(parent);
1092
parent->retain_data++;
1097
child = resolve_delta(child_obj, parent);
1098
if (!child->children_remaining)
1099
FREE_AND_NULL(child->data);
1101
child = make_base(child_obj, NULL);
1102
if (child->children_remaining) {
1110
child->data = get_data_from_pack(child_obj);
1111
child->size = child_obj->size;
1117
parent->retain_data--;
1123
list_add(&child->list, &work_head);
1124
base_cache_used += child->size;
1125
prune_base_data(NULL);
1126
free_base_data(child);
1133
struct base_data *p = parent;
1136
struct base_data *next_p;
1138
p->children_remaining--;
1139
if (p->children_remaining)
1149
FREE_AND_NULL(child);
1162
static void parse_pack_objects(unsigned char *hash)
1164
int i, nr_delays = 0;
1165
struct ofs_delta_entry *ofs_delta = ofs_deltas;
1166
struct object_id ref_delta_oid;
1168
git_hash_ctx tmp_ctx;
1171
progress = start_progress(
1172
progress_title ? progress_title :
1173
from_stdin ? _("Receiving objects") : _("Indexing objects"),
1175
for (i = 0; i < nr_objects; i++) {
1176
struct object_entry *obj = &objects[i];
1177
void *data = unpack_raw_entry(obj, &ofs_delta->offset,
1180
obj->real_type = obj->type;
1181
if (obj->type == OBJ_OFS_DELTA) {
1183
ofs_delta->obj_no = i;
1185
} else if (obj->type == OBJ_REF_DELTA) {
1186
ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc);
1187
oidcpy(&ref_deltas[nr_ref_deltas].oid, &ref_delta_oid);
1188
ref_deltas[nr_ref_deltas].obj_no = i;
1192
obj->real_type = OBJ_BAD;
1195
sha1_object(data, NULL, obj->size, obj->type,
1198
display_progress(progress, i+1);
1200
objects[i].idx.offset = consumed_bytes;
1201
stop_progress(&progress);
1205
the_hash_algo->init_fn(&tmp_ctx);
1206
the_hash_algo->clone_fn(&tmp_ctx, &input_ctx);
1207
the_hash_algo->final_fn(hash, &tmp_ctx);
1208
if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo))
1209
die(_("pack is corrupted (SHA1 mismatch)"));
1210
use(the_hash_algo->rawsz);
1213
if (fstat(input_fd, &st))
1214
die_errno(_("cannot fstat packfile"));
1215
if (S_ISREG(st.st_mode) &&
1216
lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
1217
die(_("pack has junk at the end"));
1219
for (i = 0; i < nr_objects; i++) {
1220
struct object_entry *obj = &objects[i];
1221
if (obj->real_type != OBJ_BAD)
1223
obj->real_type = obj->type;
1224
sha1_object(NULL, obj, obj->size, obj->type,
1229
die(_("confusion beyond insanity in parse_pack_objects()"));
1240
static void resolve_deltas(void)
1244
if (!nr_ofs_deltas && !nr_ref_deltas)
1248
QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry);
1249
QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry);
1251
if (verbose || show_resolving_progress)
1252
progress = start_progress(_("Resolving deltas"),
1253
nr_ref_deltas + nr_ofs_deltas);
1256
base_cache_limit = delta_base_cache_limit * nr_threads;
1257
if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) {
1260
for (i = 0; i < nr_threads; i++) {
1261
int ret = pthread_create(&thread_data[i].thread, NULL,
1262
threaded_second_pass, thread_data + i);
1264
die(_("unable to create thread: %s"),
1268
for (i = 0; i < nr_threads; i++)
1269
pthread_join(thread_data[i].thread, NULL);
1273
threaded_second_pass(¬hread_data);
1281
static void fix_unresolved_deltas(struct hashfile *f);
1282
static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash)
1284
if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) {
1285
stop_progress(&progress);
1291
if (fix_thin_pack) {
1293
unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ];
1294
struct strbuf msg = STRBUF_INIT;
1295
int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas;
1296
int nr_objects_initial = nr_objects;
1297
if (nr_unresolved <= 0)
1298
die(_("confusion beyond insanity"));
1299
REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1);
1300
memset(objects + nr_objects + 1, 0,
1301
nr_unresolved * sizeof(*objects));
1302
f = hashfd(output_fd, curr_pack);
1303
fix_unresolved_deltas(f);
1304
strbuf_addf(&msg, Q_("completed with %d local object",
1305
"completed with %d local objects",
1306
nr_objects - nr_objects_initial),
1307
nr_objects - nr_objects_initial);
1308
stop_progress_msg(&progress, msg.buf);
1309
strbuf_release(&msg);
1310
finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0);
1311
hashcpy(read_hash, pack_hash, the_repository->hash_algo);
1312
fixup_pack_header_footer(output_fd, pack_hash,
1313
curr_pack, nr_objects,
1314
read_hash, consumed_bytes-the_hash_algo->rawsz);
1315
if (!hasheq(read_hash, tail_hash, the_repository->hash_algo))
1316
die(_("Unexpected tail checksum for %s "
1317
"(disk corruption?)"), curr_pack);
1319
if (nr_ofs_deltas + nr_ref_deltas != nr_resolved_deltas)
1320
die(Q_("pack has %d unresolved delta",
1321
"pack has %d unresolved deltas",
1322
nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas),
1323
nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
1326
static int write_compressed(struct hashfile *f, void *in, unsigned int size)
1330
unsigned char outbuf[4096];
1332
git_deflate_init(&stream, zlib_compression_level);
1333
stream.next_in = in;
1334
stream.avail_in = size;
1337
stream.next_out = outbuf;
1338
stream.avail_out = sizeof(outbuf);
1339
status = git_deflate(&stream, Z_FINISH);
1340
hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out);
1341
} while (status == Z_OK);
1343
if (status != Z_STREAM_END)
1344
die(_("unable to deflate appended object (%d)"), status);
1345
size = stream.total_out;
1346
git_deflate_end(&stream);
1350
static struct object_entry *append_obj_to_pack(struct hashfile *f,
1351
const unsigned char *sha1, void *buf,
1352
unsigned long size, enum object_type type)
1354
struct object_entry *obj = &objects[nr_objects++];
1355
unsigned char header[10];
1356
unsigned long s = size;
1358
unsigned char c = (type << 4) | (s & 15);
1361
header[n++] = c | 0x80;
1367
hashwrite(f, header, n);
1369
obj[0].hdr_size = n;
1371
obj[0].real_type = type;
1372
obj[1].idx.offset = obj[0].idx.offset + n;
1373
obj[1].idx.offset += write_compressed(f, buf, size);
1374
obj[0].idx.crc32 = crc32_end(f);
1376
oidread(&obj->idx.oid, sha1, the_repository->hash_algo);
1380
static int delta_pos_compare(const void *_a, const void *_b)
1382
struct ref_delta_entry *a = *(struct ref_delta_entry **)_a;
1383
struct ref_delta_entry *b = *(struct ref_delta_entry **)_b;
1384
return a->obj_no - b->obj_no;
1387
static void fix_unresolved_deltas(struct hashfile *f)
1389
struct ref_delta_entry **sorted_by_pos;
1402
ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
1403
for (i = 0; i < nr_ref_deltas; i++)
1404
sorted_by_pos[i] = &ref_deltas[i];
1405
QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare);
1407
if (repo_has_promisor_remote(the_repository)) {
1411
struct oid_array to_fetch = OID_ARRAY_INIT;
1412
for (i = 0; i < nr_ref_deltas; i++) {
1413
struct ref_delta_entry *d = sorted_by_pos[i];
1414
if (!oid_object_info_extended(the_repository, &d->oid,
1416
OBJECT_INFO_FOR_PREFETCH))
1418
oid_array_append(&to_fetch, &d->oid);
1420
promisor_remote_get_direct(the_repository,
1421
to_fetch.oid, to_fetch.nr);
1422
oid_array_clear(&to_fetch);
1425
for (i = 0; i < nr_ref_deltas; i++) {
1426
struct ref_delta_entry *d = sorted_by_pos[i];
1427
enum object_type type;
1431
if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
1433
data = repo_read_object_file(the_repository, &d->oid, &type,
1438
if (check_object_signature(the_repository, &d->oid, data, size,
1440
die(_("local object %s is corrupt"), oid_to_hex(&d->oid));
1447
append_obj_to_pack(f, d->oid.hash, data, size, type);
1449
threaded_second_pass(NULL);
1451
display_progress(progress, nr_resolved_deltas);
1453
free(sorted_by_pos);
1456
static const char *derive_filename(const char *pack_name, const char *strip,
1457
const char *suffix, struct strbuf *buf)
1460
if (!strip_suffix(pack_name, strip, &len) || !len ||
1461
pack_name[len - 1] != '.')
1462
die(_("packfile name '%s' does not end with '.%s'"),
1464
strbuf_add(buf, pack_name, len);
1465
strbuf_addstr(buf, suffix);
1469
static void write_special_file(const char *suffix, const char *msg,
1470
const char *pack_name, const unsigned char *hash,
1471
const char **report)
1473
struct strbuf name_buf = STRBUF_INIT;
1474
const char *filename;
1476
int msg_len = strlen(msg);
1479
filename = derive_filename(pack_name, "pack", suffix, &name_buf);
1481
filename = odb_pack_name(&name_buf, hash, suffix);
1483
fd = odb_pack_keep(filename);
1485
if (errno != EEXIST)
1486
die_errno(_("cannot write %s file '%s'"),
1490
write_or_die(fd, msg, msg_len);
1491
write_or_die(fd, "\n", 1);
1494
die_errno(_("cannot close written %s file '%s'"),
1499
strbuf_release(&name_buf);
1502
static void rename_tmp_packfile(const char **final_name,
1503
const char *curr_name,
1504
struct strbuf *name, unsigned char *hash,
1505
const char *ext, int make_read_only_if_same)
1507
if (*final_name != curr_name) {
1509
*final_name = odb_pack_name(name, hash, ext);
1510
if (finalize_object_file(curr_name, *final_name))
1511
die(_("unable to rename temporary '*.%s' file to '%s'"),
1513
} else if (make_read_only_if_same) {
1514
chmod(*final_name, 0444);
1518
static void final(const char *final_pack_name, const char *curr_pack_name,
1519
const char *final_index_name, const char *curr_index_name,
1520
const char *final_rev_index_name, const char *curr_rev_index_name,
1521
const char *keep_msg, const char *promisor_msg,
1522
unsigned char *hash)
1524
const char *report = "pack";
1525
struct strbuf pack_name = STRBUF_INIT;
1526
struct strbuf index_name = STRBUF_INIT;
1527
struct strbuf rev_index_name = STRBUF_INIT;
1532
fsync_component_or_die(FSYNC_COMPONENT_PACK, output_fd, curr_pack_name);
1533
if (close(output_fd))
1534
die_errno(_("error while closing pack file"));
1538
write_special_file("keep", keep_msg, final_pack_name, hash,
1541
write_special_file("promisor", promisor_msg, final_pack_name,
1544
rename_tmp_packfile(&final_pack_name, curr_pack_name, &pack_name,
1545
hash, "pack", from_stdin);
1546
if (curr_rev_index_name)
1547
rename_tmp_packfile(&final_rev_index_name, curr_rev_index_name,
1548
&rev_index_name, hash, "rev", 1);
1549
rename_tmp_packfile(&final_index_name, curr_index_name, &index_name,
1552
if (do_fsck_object) {
1553
struct packed_git *p;
1554
p = add_packed_git(final_index_name, strlen(final_index_name), 0);
1556
install_packed_git(the_repository, p);
1560
printf("%s\n", hash_to_hex(hash));
1562
struct strbuf buf = STRBUF_INIT;
1564
strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash));
1565
write_or_die(1, buf.buf, buf.len);
1566
strbuf_release(&buf);
1569
write_in_full(1, input_buffer + input_offset, input_len);
1572
strbuf_release(&rev_index_name);
1573
strbuf_release(&index_name);
1574
strbuf_release(&pack_name);
1577
static int git_index_pack_config(const char *k, const char *v,
1578
const struct config_context *ctx, void *cb)
1580
struct pack_idx_option *opts = cb;
1582
if (!strcmp(k, "pack.indexversion")) {
1583
opts->version = git_config_int(k, v, ctx->kvi);
1584
if (opts->version > 2)
1585
die(_("bad pack.indexVersion=%"PRIu32), opts->version);
1588
if (!strcmp(k, "pack.threads")) {
1589
nr_threads = git_config_int(k, v, ctx->kvi);
1591
die(_("invalid number of threads specified (%d)"),
1593
if (!HAVE_THREADS && nr_threads != 1) {
1594
warning(_("no threads support, ignoring %s"), k);
1599
if (!strcmp(k, "pack.writereverseindex")) {
1600
if (git_config_bool(k, v))
1601
opts->flags |= WRITE_REV;
1603
opts->flags &= ~WRITE_REV;
1605
return git_default_config(k, v, ctx, cb);
1608
static int cmp_uint32(const void *a_, const void *b_)
1610
uint32_t a = *((uint32_t *)a_);
1611
uint32_t b = *((uint32_t *)b_);
1613
return (a < b) ? -1 : (a != b);
1616
static void read_v2_anomalous_offsets(struct packed_git *p,
1617
struct pack_idx_option *opts)
1619
const uint32_t *idx1, *idx2;
1623
idx1 = (((const uint32_t *)((const uint8_t *)p->index_data + p->crc_offset))
1624
+ (size_t)p->num_objects
1628
idx2 = idx1 + p->num_objects;
1630
for (i = 0; i < p->num_objects; i++) {
1631
uint32_t off = ntohl(idx1[i]);
1632
if (!(off & 0x80000000))
1634
off = off & 0x7fffffff;
1635
check_pack_index_ptr(p, &idx2[off * 2]);
1643
ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
1644
opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
1647
QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32);
1650
static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
1652
struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
1655
die(_("Cannot open existing pack file '%s'"), pack_name);
1656
if (open_pack_index(p))
1657
die(_("Cannot open existing pack idx file for '%s'"), pack_name);
1660
opts->version = p->index_version;
1662
if (opts->version == 2)
1663
read_v2_anomalous_offsets(p, opts);
1672
close_pack_index(p);
1676
static void show_pack_info(int stat_only)
1678
int i, baseobjects = nr_objects - nr_ref_deltas - nr_ofs_deltas;
1679
unsigned long *chain_histogram = NULL;
1682
CALLOC_ARRAY(chain_histogram, deepest_delta);
1684
for (i = 0; i < nr_objects; i++) {
1685
struct object_entry *obj = &objects[i];
1687
if (is_delta_type(obj->type))
1688
chain_histogram[obj_stat[i].delta_depth - 1]++;
1691
printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
1692
oid_to_hex(&obj->idx.oid),
1693
type_name(obj->real_type), (uintmax_t)obj->size,
1694
(uintmax_t)(obj[1].idx.offset - obj->idx.offset),
1695
(uintmax_t)obj->idx.offset);
1696
if (is_delta_type(obj->type)) {
1697
struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
1698
printf(" %u %s", obj_stat[i].delta_depth,
1699
oid_to_hex(&bobj->idx.oid));
1705
printf_ln(Q_("non delta: %d object",
1706
"non delta: %d objects",
1709
for (i = 0; i < deepest_delta; i++) {
1710
if (!chain_histogram[i])
1712
printf_ln(Q_("chain length = %d: %lu object",
1713
"chain length = %d: %lu objects",
1714
chain_histogram[i]),
1716
chain_histogram[i]);
1718
free(chain_histogram);
1721
int cmd_index_pack(int argc, const char **argv, const char *prefix)
1723
int i, fix_thin_pack = 0, verify = 0, stat_only = 0, rev_index;
1724
const char *curr_index;
1725
const char *curr_rev_index = NULL;
1726
const char *index_name = NULL, *pack_name = NULL, *rev_index_name = NULL;
1727
const char *keep_msg = NULL;
1728
const char *promisor_msg = NULL;
1729
struct strbuf index_name_buf = STRBUF_INIT;
1730
struct strbuf rev_index_name_buf = STRBUF_INIT;
1731
struct pack_idx_entry **idx_objects;
1732
struct pack_idx_option opts;
1733
unsigned char pack_hash[GIT_MAX_RAWSZ];
1734
unsigned foreign_nr = 1;
1735
int report_end_of_input = 0;
1744
fetch_if_missing = 0;
1746
if (argc == 2 && !strcmp(argv[1], "-h"))
1747
usage(index_pack_usage);
1749
disable_replace_refs();
1750
fsck_options.walk = mark_link;
1752
reset_pack_idx_option(&opts);
1753
opts.flags |= WRITE_REV;
1754
git_config(git_index_pack_config, &opts);
1755
if (prefix && chdir(prefix))
1756
die(_("Cannot come back to cwd"));
1758
if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
1761
rev_index = !!(opts.flags & (WRITE_REV_VERIFY | WRITE_REV));
1763
for (i = 1; i < argc; i++) {
1764
const char *arg = argv[i];
1767
if (!strcmp(arg, "--stdin")) {
1769
} else if (!strcmp(arg, "--fix-thin")) {
1771
} else if (skip_to_optional_arg(arg, "--strict", &arg)) {
1774
fsck_set_msg_types(&fsck_options, arg);
1775
} else if (!strcmp(arg, "--check-self-contained-and-connected")) {
1777
check_self_contained_and_connected = 1;
1778
} else if (skip_to_optional_arg(arg, "--fsck-objects", &arg)) {
1780
fsck_set_msg_types(&fsck_options, arg);
1781
} else if (!strcmp(arg, "--verify")) {
1783
} else if (!strcmp(arg, "--verify-stat")) {
1786
} else if (!strcmp(arg, "--verify-stat-only")) {
1790
} else if (skip_to_optional_arg(arg, "--keep", &keep_msg)) {
1792
} else if (skip_to_optional_arg(arg, "--promisor", &promisor_msg)) {
1794
} else if (starts_with(arg, "--threads=")) {
1796
nr_threads = strtoul(arg+10, &end, 0);
1797
if (!arg[10] || *end || nr_threads < 0)
1798
usage(index_pack_usage);
1799
if (!HAVE_THREADS && nr_threads != 1) {
1800
warning(_("no threads support, ignoring %s"), arg);
1803
} else if (starts_with(arg, "--pack_header=")) {
1804
struct pack_header *hdr;
1807
hdr = (struct pack_header *)input_buffer;
1808
hdr->hdr_signature = htonl(PACK_SIGNATURE);
1809
hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
1811
die(_("bad %s"), arg);
1812
hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
1814
die(_("bad %s"), arg);
1815
input_len = sizeof(*hdr);
1816
} else if (!strcmp(arg, "-v")) {
1818
} else if (!strcmp(arg, "--progress-title")) {
1819
if (progress_title || (i+1) >= argc)
1820
usage(index_pack_usage);
1821
progress_title = argv[++i];
1822
} else if (!strcmp(arg, "--show-resolving-progress")) {
1823
show_resolving_progress = 1;
1824
} else if (!strcmp(arg, "--report-end-of-input")) {
1825
report_end_of_input = 1;
1826
} else if (!strcmp(arg, "-o")) {
1827
if (index_name || (i+1) >= argc)
1828
usage(index_pack_usage);
1829
index_name = argv[++i];
1830
} else if (starts_with(arg, "--index-version=")) {
1832
opts.version = strtoul(arg + 16, &c, 10);
1833
if (opts.version > 2)
1834
die(_("bad %s"), arg);
1836
opts.off32_limit = strtoul(c+1, &c, 0);
1837
if (*c || opts.off32_limit & 0x80000000)
1838
die(_("bad %s"), arg);
1839
} else if (skip_prefix(arg, "--max-input-size=", &arg)) {
1840
max_input_size = strtoumax(arg, NULL, 10);
1841
} else if (skip_prefix(arg, "--object-format=", &arg)) {
1842
hash_algo = hash_algo_by_name(arg);
1843
if (hash_algo == GIT_HASH_UNKNOWN)
1844
die(_("unknown hash algorithm '%s'"), arg);
1845
repo_set_hash_algo(the_repository, hash_algo);
1846
} else if (!strcmp(arg, "--rev-index")) {
1848
} else if (!strcmp(arg, "--no-rev-index")) {
1851
usage(index_pack_usage);
1856
usage(index_pack_usage);
1860
if (!pack_name && !from_stdin)
1861
usage(index_pack_usage);
1862
if (fix_thin_pack && !from_stdin)
1863
die(_("the option '%s' requires '%s'"), "--fix-thin", "--stdin");
1864
if (from_stdin && !startup_info->have_repository)
1865
die(_("--stdin requires a git repository"));
1866
if (from_stdin && hash_algo)
1867
die(_("options '%s' and '%s' cannot be used together"), "--object-format", "--stdin");
1868
if (!index_name && pack_name)
1869
index_name = derive_filename(pack_name, "pack", "idx", &index_name_buf);
1871
opts.flags &= ~(WRITE_REV | WRITE_REV_VERIFY);
1873
opts.flags |= verify ? WRITE_REV_VERIFY : WRITE_REV;
1875
rev_index_name = derive_filename(index_name,
1877
&rev_index_name_buf);
1882
die(_("--verify with no packfile name given"));
1883
read_idx_option(&opts, index_name);
1884
opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT;
1887
opts.flags |= WRITE_IDX_STRICT;
1889
if (HAVE_THREADS && !nr_threads) {
1890
nr_threads = online_cpus();
1901
else if (nr_threads < 6)
1903
else if (nr_threads < 40)
1909
curr_pack = open_pack_file(pack_name);
1910
parse_pack_header();
1911
CALLOC_ARRAY(objects, st_add(nr_objects, 1));
1913
CALLOC_ARRAY(obj_stat, st_add(nr_objects, 1));
1914
CALLOC_ARRAY(ofs_deltas, nr_objects);
1915
parse_pack_objects(pack_hash);
1916
if (report_end_of_input)
1917
write_in_full(2, "\0", 1);
1919
conclude_pack(fix_thin_pack, curr_pack, pack_hash);
1923
foreign_nr = check_objects();
1926
show_pack_info(stat_only);
1928
ALLOC_ARRAY(idx_objects, nr_objects);
1929
for (i = 0; i < nr_objects; i++)
1930
idx_objects[i] = &objects[i].idx;
1931
curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash);
1933
curr_rev_index = write_rev_file(rev_index_name, idx_objects,
1934
nr_objects, pack_hash,
1939
final(pack_name, curr_pack,
1940
index_name, curr_index,
1941
rev_index_name, curr_rev_index,
1942
keep_msg, promisor_msg,
1947
if (do_fsck_object && fsck_finish(&fsck_options))
1948
die(_("fsck error in pack objects"));
1952
strbuf_release(&index_name_buf);
1953
strbuf_release(&rev_index_name_buf);
1955
free((void *) curr_pack);
1957
free((void *) curr_index);
1958
if (!rev_index_name)
1959
free((void *) curr_rev_index);
1964
if (check_self_contained_and_connected && foreign_nr)