]>
Commit | Line | Data |
---|---|---|
03eae9af | 1 | #define USE_THE_REPOSITORY_VARIABLE |
41f43b82 PS |
2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
3 | ||
2ad9d4cb | 4 | #include "builtin.h" |
b2141fc1 | 5 | #include "config.h" |
9cf6d335 | 6 | #include "delta.h" |
32a8f510 | 7 | #include "environment.h" |
f394e093 | 8 | #include "gettext.h" |
41771fa4 | 9 | #include "hex.h" |
9cf6d335 SV |
10 | #include "pack.h" |
11 | #include "csum-file.h" | |
8e440259 PE |
12 | #include "blob.h" |
13 | #include "commit.h" | |
c08589ef | 14 | #include "tag.h" |
8e440259 | 15 | #include "tree.h" |
96a02f8f | 16 | #include "progress.h" |
0153be05 | 17 | #include "fsck.h" |
a034e910 | 18 | #include "strbuf.h" |
4614043c | 19 | #include "streaming.h" |
b8a2486f | 20 | #include "thread-utils.h" |
4f39cd82 | 21 | #include "packfile.h" |
75f273d9 | 22 | #include "pack-revindex.h" |
87bed179 | 23 | #include "object-file.h" |
68cd492a | 24 | #include "object-store.h" |
6f2d7430 | 25 | #include "oid-array.h" |
c08589ef JT |
26 | #include "oidset.h" |
27 | #include "path.h" | |
cbeab747 | 28 | #include "replace-object.h" |
c08589ef | 29 | #include "tree-walk.h" |
b14ed5ad | 30 | #include "promisor-remote.h" |
c08589ef | 31 | #include "run-command.h" |
e38da487 | 32 | #include "setup.h" |
c08589ef | 33 | #include "strvec.h" |
9cf6d335 SV |
34 | |
35 | static const char index_pack_usage[] = | |
0f8edf73 | 36 | "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>])"; |
9cf6d335 | 37 | |
9cba13ca | 38 | struct object_entry { |
aa7e44bf | 39 | struct pack_idx_entry idx; |
2d477051 | 40 | unsigned long size; |
41730576 NTND |
41 | unsigned char hdr_size; |
42 | signed char type; | |
43 | signed char real_type; | |
9cf6d335 SV |
44 | }; |
45 | ||
41730576 | 46 | struct object_stat { |
38a45561 JH |
47 | unsigned delta_depth; |
48 | int base_object_no; | |
53dda6ff NP |
49 | }; |
50 | ||
f41aebd4 | 51 | struct base_data { |
b4718cae | 52 | /* Initialized by make_base(). */ |
4a438cab | 53 | struct base_data *base; |
03993e13 | 54 | struct object_entry *obj; |
2baad220 NTND |
55 | int ref_first, ref_last; |
56 | int ofs_first, ofs_last; | |
f08cbf60 JT |
57 | /* |
58 | * Threads should increment retain_data if they are about to call | |
59 | * patch_delta() using this struct's data as a base, and decrement this | |
60 | * when they are done. While retain_data is nonzero, this struct's data | |
61 | * will not be freed even if the delta base cache limit is exceeded. | |
62 | */ | |
63 | int retain_data; | |
64 | /* | |
65 | * The number of direct children that have not been fully processed | |
66 | * (entered work_head, entered done_head, left done_head). When this | |
67 | * number reaches zero, this struct base_data can be freed. | |
68 | */ | |
69 | int children_remaining; | |
b4718cae JT |
70 | |
71 | /* Not initialized by make_base(). */ | |
f08cbf60 | 72 | struct list_head list; |
b4718cae JT |
73 | void *data; |
74 | unsigned long size; | |
f41aebd4 SP |
75 | }; |
76 | ||
f08cbf60 JT |
77 | /* |
78 | * Stack of struct base_data that have unprocessed children. | |
79 | * threaded_second_pass() uses this as a source of work (the other being the | |
80 | * objects array). | |
81 | * | |
82 | * Guarded by work_mutex. | |
83 | */ | |
84 | static LIST_HEAD(work_head); | |
85 | ||
86 | /* | |
87 | * Stack of struct base_data that have children, all of whom have been | |
88 | * processed or are being processed, and at least one child is being processed. | |
89 | * These struct base_data must be kept around until the last child is | |
90 | * processed. | |
91 | * | |
92 | * Guarded by work_mutex. | |
93 | */ | |
94 | static LIST_HEAD(done_head); | |
95 | ||
96 | /* | |
97 | * All threads share one delta base cache. | |
98 | * | |
99 | * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only | |
100 | * in a thread. | |
101 | */ | |
102 | static size_t base_cache_used; | |
103 | static size_t base_cache_limit; | |
104 | ||
e8b3bcf4 | 105 | struct thread_local_data { |
b8a2486f | 106 | pthread_t thread; |
39539495 | 107 | int pack_fd; |
b8a2486f NTND |
108 | }; |
109 | ||
95308d64 | 110 | /* Remember to update object flag allocation in object.h */ |
0153be05 MK |
111 | #define FLAG_LINK (1u<<20) |
112 | #define FLAG_CHECKED (1u<<21) | |
113 | ||
c6458e60 NTND |
114 | struct ofs_delta_entry { |
115 | off_t offset; | |
116 | int obj_no; | |
117 | }; | |
118 | ||
119 | struct ref_delta_entry { | |
af8caf33 | 120 | struct object_id oid; |
636171cb | 121 | int obj_no; |
9cf6d335 SV |
122 | }; |
123 | ||
9cf6d335 | 124 | static struct object_entry *objects; |
41730576 | 125 | static struct object_stat *obj_stat; |
c6458e60 NTND |
126 | static struct ofs_delta_entry *ofs_deltas; |
127 | static struct ref_delta_entry *ref_deltas; | |
e8b3bcf4 | 128 | static struct thread_local_data nothread_data; |
9cf6d335 | 129 | static int nr_objects; |
c6458e60 NTND |
130 | static int nr_ofs_deltas; |
131 | static int nr_ref_deltas; | |
132 | static int ref_deltas_alloc; | |
636171cb | 133 | static int nr_resolved_deltas; |
b8a2486f | 134 | static int nr_threads; |
9cf6d335 | 135 | |
e42797f5 | 136 | static int from_stdin; |
0153be05 | 137 | static int strict; |
c6807a40 | 138 | static int do_fsck_object; |
3745e269 | 139 | static struct fsck_options fsck_options = FSCK_OPTIONS_MISSING_GITMODULES; |
3c9af366 | 140 | static int verbose; |
f46c46e4 | 141 | static const char *progress_title; |
e376f17f | 142 | static int show_resolving_progress; |
3aba2fdd | 143 | static int show_stat; |
c6807a40 | 144 | static int check_self_contained_and_connected; |
3c9af366 | 145 | |
dc6a0757 | 146 | static struct progress *progress; |
e42797f5 | 147 | |
2d477051 NP |
148 | /* We always read in 4kB chunks. */ |
149 | static unsigned char input_buffer[4096]; | |
d7dd0223 NP |
150 | static unsigned int input_offset, input_len; |
151 | static off_t consumed_bytes; | |
411481be | 152 | static off_t max_input_size; |
d1a0ed18 | 153 | static unsigned deepest_delta; |
7346e340 | 154 | static struct git_hash_ctx input_ctx; |
ee5743ce | 155 | static uint32_t input_crc32; |
39539495 NTND |
156 | static int input_fd, output_fd; |
157 | static const char *curr_pack; | |
2d477051 | 158 | |
c08589ef | 159 | /* |
911d1420 JT |
160 | * outgoing_links is guarded by read_mutex, and record_outgoing_links is |
161 | * read-only in a thread. | |
c08589ef | 162 | */ |
911d1420 JT |
163 | static struct oidset outgoing_links = OIDSET_INIT; |
164 | static int record_outgoing_links; | |
c08589ef | 165 | |
e8b3bcf4 | 166 | static struct thread_local_data *thread_data; |
b8a2486f NTND |
167 | static int nr_dispatched; |
168 | static int threads_active; | |
169 | ||
170 | static pthread_mutex_t read_mutex; | |
171 | #define read_lock() lock_mutex(&read_mutex) | |
172 | #define read_unlock() unlock_mutex(&read_mutex) | |
173 | ||
174 | static pthread_mutex_t counter_mutex; | |
175 | #define counter_lock() lock_mutex(&counter_mutex) | |
176 | #define counter_unlock() unlock_mutex(&counter_mutex) | |
177 | ||
178 | static pthread_mutex_t work_mutex; | |
179 | #define work_lock() lock_mutex(&work_mutex) | |
180 | #define work_unlock() unlock_mutex(&work_mutex) | |
181 | ||
3aba2fdd NTND |
182 | static pthread_mutex_t deepest_delta_mutex; |
183 | #define deepest_delta_lock() lock_mutex(&deepest_delta_mutex) | |
184 | #define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex) | |
185 | ||
b8a2486f NTND |
186 | static pthread_key_t key; |
187 | ||
188 | static inline void lock_mutex(pthread_mutex_t *mutex) | |
189 | { | |
190 | if (threads_active) | |
191 | pthread_mutex_lock(mutex); | |
192 | } | |
193 | ||
194 | static inline void unlock_mutex(pthread_mutex_t *mutex) | |
195 | { | |
196 | if (threads_active) | |
197 | pthread_mutex_unlock(mutex); | |
198 | } | |
199 | ||
200 | /* | |
201 | * Mutex and conditional variable can't be statically-initialized on Windows. | |
202 | */ | |
203 | static void init_thread(void) | |
204 | { | |
39539495 | 205 | int i; |
b8a2486f NTND |
206 | init_recursive_mutex(&read_mutex); |
207 | pthread_mutex_init(&counter_mutex, NULL); | |
208 | pthread_mutex_init(&work_mutex, NULL); | |
3aba2fdd NTND |
209 | if (show_stat) |
210 | pthread_mutex_init(&deepest_delta_mutex, NULL); | |
b8a2486f | 211 | pthread_key_create(&key, NULL); |
ca56dadb | 212 | CALLOC_ARRAY(thread_data, nr_threads); |
39539495 | 213 | for (i = 0; i < nr_threads; i++) { |
6346f704 | 214 | thread_data[i].pack_fd = xopen(curr_pack, O_RDONLY); |
39539495 NTND |
215 | } |
216 | ||
b8a2486f NTND |
217 | threads_active = 1; |
218 | } | |
219 | ||
220 | static void cleanup_thread(void) | |
221 | { | |
39539495 | 222 | int i; |
b8a2486f NTND |
223 | if (!threads_active) |
224 | return; | |
225 | threads_active = 0; | |
226 | pthread_mutex_destroy(&read_mutex); | |
227 | pthread_mutex_destroy(&counter_mutex); | |
228 | pthread_mutex_destroy(&work_mutex); | |
3aba2fdd NTND |
229 | if (show_stat) |
230 | pthread_mutex_destroy(&deepest_delta_mutex); | |
39539495 NTND |
231 | for (i = 0; i < nr_threads; i++) |
232 | close(thread_data[i].pack_fd); | |
b8a2486f NTND |
233 | pthread_key_delete(key); |
234 | free(thread_data); | |
235 | } | |
236 | ||
a1aad716 | 237 | static int mark_link(struct object *obj, enum object_type type, |
0b4e9013 JK |
238 | void *data UNUSED, |
239 | struct fsck_options *options UNUSED) | |
0153be05 MK |
240 | { |
241 | if (!obj) | |
242 | return -1; | |
243 | ||
244 | if (type != OBJ_ANY && obj->type != type) | |
f2fd0760 | 245 | die(_("object type mismatch at %s"), oid_to_hex(&obj->oid)); |
0153be05 MK |
246 | |
247 | obj->flags |= FLAG_LINK; | |
248 | return 0; | |
249 | } | |
250 | ||
251 | /* The content of each linked object must have been checked | |
252 | or it must be already present in the object database */ | |
c6807a40 | 253 | static unsigned check_object(struct object *obj) |
0153be05 MK |
254 | { |
255 | if (!obj) | |
c6807a40 | 256 | return 0; |
0153be05 MK |
257 | |
258 | if (!(obj->flags & FLAG_LINK)) | |
c6807a40 | 259 | return 0; |
0153be05 MK |
260 | |
261 | if (!(obj->flags & FLAG_CHECKED)) { | |
262 | unsigned long size; | |
0df8e965 | 263 | int type = oid_object_info(the_repository, &obj->oid, &size); |
77583e77 JK |
264 | if (type <= 0) |
265 | die(_("did not receive expected object %s"), | |
f2fd0760 | 266 | oid_to_hex(&obj->oid)); |
77583e77 JK |
267 | if (type != obj->type) |
268 | die(_("object %s: expected type %s, found %s"), | |
f2fd0760 | 269 | oid_to_hex(&obj->oid), |
debca9d2 | 270 | type_name(obj->type), type_name(type)); |
0153be05 | 271 | obj->flags |= FLAG_CHECKED; |
c6807a40 | 272 | return 1; |
0153be05 | 273 | } |
c6807a40 NTND |
274 | |
275 | return 0; | |
0153be05 MK |
276 | } |
277 | ||
c6807a40 | 278 | static unsigned check_objects(void) |
0153be05 | 279 | { |
c6807a40 | 280 | unsigned i, max, foreign_nr = 0; |
0153be05 | 281 | |
74d414c9 | 282 | max = get_max_object_index(the_repository); |
79e3aa66 SG |
283 | |
284 | if (verbose) | |
1f7e6478 PS |
285 | progress = start_delayed_progress(the_repository, |
286 | _("Checking objects"), max); | |
79e3aa66 SG |
287 | |
288 | for (i = 0; i < max; i++) { | |
74d414c9 | 289 | foreign_nr += check_object(get_indexed_object(the_repository, i)); |
79e3aa66 SG |
290 | display_progress(progress, i + 1); |
291 | } | |
292 | ||
293 | stop_progress(&progress); | |
c6807a40 | 294 | return foreign_nr; |
0153be05 MK |
295 | } |
296 | ||
297 | ||
636171cb | 298 | /* Discard current buffer used content. */ |
a6e8a767 | 299 | static void flush(void) |
636171cb NP |
300 | { |
301 | if (input_offset) { | |
302 | if (output_fd >= 0) | |
303 | write_or_die(output_fd, input_buffer, input_offset); | |
0578f1e6 | 304 | git_hash_update(&input_ctx, input_buffer, input_offset); |
554a2636 | 305 | memmove(input_buffer, input_buffer + input_offset, input_len); |
636171cb NP |
306 | input_offset = 0; |
307 | } | |
308 | } | |
309 | ||
2d477051 NP |
310 | /* |
311 | * Make sure at least "min" bytes are available in the buffer, and | |
312 | * return the pointer to the buffer. | |
313 | */ | |
b89c4e93 | 314 | static void *fill(int min) |
9cf6d335 | 315 | { |
2d477051 NP |
316 | if (min <= input_len) |
317 | return input_buffer + input_offset; | |
318 | if (min > sizeof(input_buffer)) | |
c2b97ecf NTND |
319 | die(Q_("cannot fill %d byte", |
320 | "cannot fill %d bytes", | |
321 | min), | |
322 | min); | |
636171cb | 323 | flush(); |
2d477051 | 324 | do { |
8a912bcb | 325 | ssize_t ret = xread(input_fd, input_buffer + input_len, |
2d477051 NP |
326 | sizeof(input_buffer) - input_len); |
327 | if (ret <= 0) { | |
328 | if (!ret) | |
c2b97ecf NTND |
329 | die(_("early EOF")); |
330 | die_errno(_("read error on input")); | |
2d477051 NP |
331 | } |
332 | input_len += ret; | |
218558af NP |
333 | if (from_stdin) |
334 | display_throughput(progress, consumed_bytes + input_len); | |
2d477051 NP |
335 | } while (input_len < min); |
336 | return input_buffer; | |
337 | } | |
338 | ||
339 | static void use(int bytes) | |
340 | { | |
341 | if (bytes > input_len) | |
c2b97ecf | 342 | die(_("used more bytes than were available")); |
ee5743ce | 343 | input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes); |
2d477051 NP |
344 | input_len -= bytes; |
345 | input_offset += bytes; | |
d7dd0223 NP |
346 | |
347 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 348 | if (signed_add_overflows(consumed_bytes, bytes)) |
c2b97ecf | 349 | die(_("pack too large for current definition of off_t")); |
2d477051 | 350 | consumed_bytes += bytes; |
0cf5fbc2 MC |
351 | if (max_input_size && consumed_bytes > max_input_size) { |
352 | struct strbuf size_limit = STRBUF_INIT; | |
353 | strbuf_humanise_bytes(&size_limit, max_input_size); | |
354 | die(_("pack exceeds maximum allowed size (%s)"), | |
355 | size_limit.buf); | |
356 | } | |
2d477051 | 357 | } |
9cf6d335 | 358 | |
3bb72562 | 359 | static const char *open_pack_file(const char *pack_name) |
2d477051 | 360 | { |
e42797f5 NP |
361 | if (from_stdin) { |
362 | input_fd = 0; | |
363 | if (!pack_name) { | |
594fa999 JK |
364 | struct strbuf tmp_file = STRBUF_INIT; |
365 | output_fd = odb_mkstemp(&tmp_file, | |
6e180cdc | 366 | "pack/tmp_pack_XXXXXX"); |
594fa999 | 367 | pack_name = strbuf_detach(&tmp_file, NULL); |
892e723a | 368 | } else { |
66e905b7 | 369 | output_fd = xopen(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); |
892e723a | 370 | } |
39539495 | 371 | nothread_data.pack_fd = output_fd; |
e42797f5 | 372 | } else { |
66e905b7 | 373 | input_fd = xopen(pack_name, O_RDONLY); |
e42797f5 | 374 | output_fd = -1; |
39539495 | 375 | nothread_data.pack_fd = input_fd; |
e42797f5 | 376 | } |
454253f0 | 377 | the_hash_algo->init_fn(&input_ctx); |
e42797f5 | 378 | return pack_name; |
9cf6d335 SV |
379 | } |
380 | ||
381 | static void parse_pack_header(void) | |
382 | { | |
f1299bff | 383 | unsigned char *hdr = fill(sizeof(struct pack_header)); |
9cf6d335 SV |
384 | |
385 | /* Header consistency check */ | |
f1299bff | 386 | if (get_be32(hdr) != PACK_SIGNATURE) |
c2b97ecf | 387 | die(_("pack signature mismatch")); |
f1299bff JK |
388 | hdr += 4; |
389 | if (!pack_version_ok_native(get_be32(hdr))) | |
f350df42 | 390 | die(_("pack version %"PRIu32" unsupported"), |
f1299bff JK |
391 | get_be32(hdr)); |
392 | hdr += 4; | |
9cf6d335 | 393 | |
f1299bff | 394 | nr_objects = get_be32(hdr); |
2d477051 | 395 | use(sizeof(struct pack_header)); |
9cf6d335 SV |
396 | } |
397 | ||
103e02c7 | 398 | __attribute__((format (printf, 2, 3))) |
fd3e6747 | 399 | static NORETURN void bad_object(off_t offset, const char *format, ...) |
9cf6d335 SV |
400 | { |
401 | va_list params; | |
402 | char buf[1024]; | |
403 | ||
404 | va_start(params, format); | |
405 | vsnprintf(buf, sizeof(buf), format, params); | |
406 | va_end(params); | |
fd3e6747 NTND |
407 | die(_("pack has bad object at offset %"PRIuMAX": %s"), |
408 | (uintmax_t)offset, buf); | |
9cf6d335 SV |
409 | } |
410 | ||
e8b3bcf4 | 411 | static inline struct thread_local_data *get_thread_data(void) |
b8a2486f | 412 | { |
2094c5e5 NTND |
413 | if (HAVE_THREADS) { |
414 | if (threads_active) | |
415 | return pthread_getspecific(key); | |
416 | assert(!threads_active && | |
417 | "This should only be reached when all threads are gone"); | |
418 | } | |
b8a2486f NTND |
419 | return ¬hread_data; |
420 | } | |
421 | ||
e8b3bcf4 | 422 | static void set_thread_data(struct thread_local_data *data) |
b8a2486f NTND |
423 | { |
424 | if (threads_active) | |
425 | pthread_setspecific(key, data); | |
426 | } | |
b8a2486f | 427 | |
6a87ed97 NP |
428 | static void free_base_data(struct base_data *c) |
429 | { | |
430 | if (c->data) { | |
6a83d902 | 431 | FREE_AND_NULL(c->data); |
f08cbf60 | 432 | base_cache_used -= c->size; |
6a87ed97 NP |
433 | } |
434 | } | |
435 | ||
92392b4a SP |
436 | static void prune_base_data(struct base_data *retain) |
437 | { | |
f08cbf60 | 438 | struct list_head *pos; |
92392b4a | 439 | |
f08cbf60 | 440 | if (base_cache_used <= base_cache_limit) |
a7f7e84a | 441 | return; |
4a438cab | 442 | |
f08cbf60 JT |
443 | list_for_each_prev(pos, &done_head) { |
444 | struct base_data *b = list_entry(pos, struct base_data, list); | |
445 | if (b->retain_data || b == retain) | |
446 | continue; | |
447 | if (b->data) { | |
448 | free_base_data(b); | |
449 | if (base_cache_used <= base_cache_limit) | |
450 | return; | |
451 | } | |
92392b4a | 452 | } |
4a438cab | 453 | |
f08cbf60 JT |
454 | list_for_each_prev(pos, &work_head) { |
455 | struct base_data *b = list_entry(pos, struct base_data, list); | |
456 | if (b->retain_data || b == retain) | |
457 | continue; | |
458 | if (b->data) { | |
459 | free_base_data(b); | |
460 | if (base_cache_used <= base_cache_limit) | |
461 | return; | |
462 | } | |
a7f7e84a | 463 | } |
4a438cab SP |
464 | } |
465 | ||
681b07de NTND |
466 | static int is_delta_type(enum object_type type) |
467 | { | |
468 | return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA); | |
469 | } | |
470 | ||
da49a7da | 471 | static void *unpack_entry_data(off_t offset, unsigned long size, |
454253f0 | 472 | enum object_type type, struct object_id *oid) |
9cf6d335 | 473 | { |
9ec2dde9 | 474 | static char fixed_buf[8192]; |
7ce4721a | 475 | int status; |
ef49a7a0 | 476 | git_zstream stream; |
9ec2dde9 | 477 | void *buf; |
7346e340 | 478 | struct git_hash_ctx c; |
681b07de NTND |
479 | char hdr[32]; |
480 | int hdrlen; | |
481 | ||
482 | if (!is_delta_type(type)) { | |
b04cdea4 | 483 | hdrlen = format_object_header(hdr, sizeof(hdr), type, size); |
454253f0 | 484 | the_hash_algo->init_fn(&c); |
0578f1e6 | 485 | git_hash_update(&c, hdr, hdrlen); |
681b07de | 486 | } else |
454253f0 | 487 | oid = NULL; |
7835ee75 PS |
488 | if (type == OBJ_BLOB && |
489 | size > repo_settings_get_big_file_threshold(the_repository)) | |
9ec2dde9 NTND |
490 | buf = fixed_buf; |
491 | else | |
a1e920a0 | 492 | buf = xmallocz(size); |
9cf6d335 SV |
493 | |
494 | memset(&stream, 0, sizeof(stream)); | |
7ce4721a | 495 | git_inflate_init(&stream); |
9cf6d335 | 496 | stream.next_out = buf; |
9ec2dde9 | 497 | stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size; |
9cf6d335 | 498 | |
7ce4721a | 499 | do { |
681b07de | 500 | unsigned char *last_out = stream.next_out; |
2d477051 NP |
501 | stream.next_in = fill(1); |
502 | stream.avail_in = input_len; | |
7ce4721a NP |
503 | status = git_inflate(&stream, 0); |
504 | use(input_len - stream.avail_in); | |
454253f0 | 505 | if (oid) |
0578f1e6 | 506 | git_hash_update(&c, last_out, stream.next_out - last_out); |
9ec2dde9 NTND |
507 | if (buf == fixed_buf) { |
508 | stream.next_out = buf; | |
509 | stream.avail_out = sizeof(fixed_buf); | |
510 | } | |
7ce4721a NP |
511 | } while (status == Z_OK); |
512 | if (stream.total_out != size || status != Z_STREAM_END) | |
c2b97ecf | 513 | bad_object(offset, _("inflate returned %d"), status); |
39c68542 | 514 | git_inflate_end(&stream); |
454253f0 | 515 | if (oid) |
0578f1e6 | 516 | git_hash_final_oid(oid, &c); |
9ec2dde9 | 517 | return buf == fixed_buf ? NULL : buf; |
9cf6d335 SV |
518 | } |
519 | ||
681b07de | 520 | static void *unpack_raw_entry(struct object_entry *obj, |
c6458e60 | 521 | off_t *ofs_offset, |
454253f0 | 522 | struct object_id *ref_oid, |
523 | struct object_id *oid) | |
9cf6d335 | 524 | { |
48fb7deb LT |
525 | unsigned char *p; |
526 | unsigned long size, c; | |
d7dd0223 | 527 | off_t base_offset; |
9cf6d335 | 528 | unsigned shift; |
ee5743ce | 529 | void *data; |
9cf6d335 | 530 | |
aa7e44bf | 531 | obj->idx.offset = consumed_bytes; |
1e4cd68c | 532 | input_crc32 = crc32(0, NULL, 0); |
2d477051 NP |
533 | |
534 | p = fill(1); | |
535 | c = *p; | |
536 | use(1); | |
537 | obj->type = (c >> 4) & 7; | |
9cf6d335 SV |
538 | size = (c & 15); |
539 | shift = 4; | |
540 | while (c & 0x80) { | |
2d477051 NP |
541 | p = fill(1); |
542 | c = *p; | |
543 | use(1); | |
48fb7deb | 544 | size += (c & 0x7f) << shift; |
9cf6d335 SV |
545 | shift += 7; |
546 | } | |
2d477051 | 547 | obj->size = size; |
9cf6d335 | 548 | |
2d477051 | 549 | switch (obj->type) { |
eb32d236 | 550 | case OBJ_REF_DELTA: |
9da95bda PS |
551 | oidread(ref_oid, fill(the_hash_algo->rawsz), |
552 | the_repository->hash_algo); | |
454253f0 | 553 | use(the_hash_algo->rawsz); |
53dda6ff NP |
554 | break; |
555 | case OBJ_OFS_DELTA: | |
2d477051 NP |
556 | p = fill(1); |
557 | c = *p; | |
558 | use(1); | |
53dda6ff NP |
559 | base_offset = c & 127; |
560 | while (c & 128) { | |
561 | base_offset += 1; | |
8723f216 | 562 | if (!base_offset || MSB(base_offset, 7)) |
c2b97ecf | 563 | bad_object(obj->idx.offset, _("offset value overflow for delta base object")); |
2d477051 NP |
564 | p = fill(1); |
565 | c = *p; | |
566 | use(1); | |
53dda6ff NP |
567 | base_offset = (base_offset << 7) + (c & 127); |
568 | } | |
c6458e60 NTND |
569 | *ofs_offset = obj->idx.offset - base_offset; |
570 | if (*ofs_offset <= 0 || *ofs_offset >= obj->idx.offset) | |
c2b97ecf | 571 | bad_object(obj->idx.offset, _("delta base offset is out of bound")); |
53dda6ff | 572 | break; |
9cf6d335 SV |
573 | case OBJ_COMMIT: |
574 | case OBJ_TREE: | |
575 | case OBJ_BLOB: | |
576 | case OBJ_TAG: | |
9cf6d335 SV |
577 | break; |
578 | default: | |
c2b97ecf | 579 | bad_object(obj->idx.offset, _("unknown object type %d"), obj->type); |
9cf6d335 | 580 | } |
aa7e44bf | 581 | obj->hdr_size = consumed_bytes - obj->idx.offset; |
2d477051 | 582 | |
454253f0 | 583 | data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, oid); |
aa7e44bf | 584 | obj->idx.crc32 = input_crc32; |
ee5743ce | 585 | return data; |
2d477051 NP |
586 | } |
587 | ||
8a2e163c NTND |
588 | static void *unpack_data(struct object_entry *obj, |
589 | int (*consume)(const unsigned char *, unsigned long, void *), | |
590 | void *cb_data) | |
2d477051 | 591 | { |
a91ef6e7 | 592 | off_t from = obj[0].idx.offset + obj[0].hdr_size; |
7171a0b0 | 593 | off_t len = obj[1].idx.offset - from; |
776ea370 | 594 | unsigned char *data, *inbuf; |
ef49a7a0 | 595 | git_zstream stream; |
776ea370 | 596 | int status; |
9cf6d335 | 597 | |
a1e920a0 | 598 | data = xmallocz(consume ? 64*1024 : obj->size); |
7171a0b0 | 599 | inbuf = xmalloc((len < 64*1024) ? (int)len : 64*1024); |
776ea370 | 600 | |
2d477051 | 601 | memset(&stream, 0, sizeof(stream)); |
776ea370 | 602 | git_inflate_init(&stream); |
2d477051 | 603 | stream.next_out = data; |
8a2e163c | 604 | stream.avail_out = consume ? 64*1024 : obj->size; |
776ea370 NP |
605 | |
606 | do { | |
7171a0b0 | 607 | ssize_t n = (len < 64*1024) ? (ssize_t)len : 64*1024; |
53f52cd9 | 608 | n = xpread(get_thread_data()->pack_fd, inbuf, n, from); |
776ea370 | 609 | if (n < 0) |
c2b97ecf | 610 | die_errno(_("cannot pread pack file")); |
776ea370 | 611 | if (!n) |
7171a0b0 NTND |
612 | die(Q_("premature end of pack file, %"PRIuMAX" byte missing", |
613 | "premature end of pack file, %"PRIuMAX" bytes missing", | |
6f693252 | 614 | len), |
7171a0b0 | 615 | (uintmax_t)len); |
776ea370 NP |
616 | from += n; |
617 | len -= n; | |
618 | stream.next_in = inbuf; | |
619 | stream.avail_in = n; | |
f8b09038 JK |
620 | if (!consume) |
621 | status = git_inflate(&stream, 0); | |
622 | else { | |
623 | do { | |
624 | status = git_inflate(&stream, 0); | |
625 | if (consume(data, stream.next_out - data, cb_data)) { | |
626 | free(inbuf); | |
627 | free(data); | |
628 | return NULL; | |
629 | } | |
630 | stream.next_out = data; | |
631 | stream.avail_out = 64*1024; | |
632 | } while (status == Z_OK && stream.avail_in); | |
8a2e163c | 633 | } |
776ea370 NP |
634 | } while (len && status == Z_OK && !stream.avail_in); |
635 | ||
636 | /* This has been inflated OK when first encountered, so... */ | |
637 | if (status != Z_STREAM_END || stream.total_out != obj->size) | |
c2b97ecf | 638 | die(_("serious inflate inconsistency")); |
776ea370 NP |
639 | |
640 | git_inflate_end(&stream); | |
641 | free(inbuf); | |
8a2e163c | 642 | if (consume) { |
6a83d902 | 643 | FREE_AND_NULL(data); |
8a2e163c | 644 | } |
9cf6d335 SV |
645 | return data; |
646 | } | |
647 | ||
8a2e163c NTND |
648 | static void *get_data_from_pack(struct object_entry *obj) |
649 | { | |
650 | return unpack_data(obj, NULL, NULL); | |
651 | } | |
652 | ||
c6458e60 NTND |
653 | static int compare_ofs_delta_bases(off_t offset1, off_t offset2, |
654 | enum object_type type1, | |
655 | enum object_type type2) | |
7218a215 JH |
656 | { |
657 | int cmp = type1 - type2; | |
658 | if (cmp) | |
659 | return cmp; | |
f0e7f11d JK |
660 | return offset1 < offset2 ? -1 : |
661 | offset1 > offset2 ? 1 : | |
662 | 0; | |
7218a215 JH |
663 | } |
664 | ||
fc968e26 | 665 | static int find_ofs_delta(const off_t offset) |
9cf6d335 | 666 | { |
c6458e60 NTND |
667 | int first = 0, last = nr_ofs_deltas; |
668 | ||
669 | while (first < last) { | |
19716b21 | 670 | int next = first + (last - first) / 2; |
c6458e60 NTND |
671 | struct ofs_delta_entry *delta = &ofs_deltas[next]; |
672 | int cmp; | |
673 | ||
674 | cmp = compare_ofs_delta_bases(offset, delta->offset, | |
fc968e26 JT |
675 | OBJ_OFS_DELTA, |
676 | objects[delta->obj_no].type); | |
c6458e60 NTND |
677 | if (!cmp) |
678 | return next; | |
679 | if (cmp < 0) { | |
680 | last = next; | |
681 | continue; | |
682 | } | |
683 | first = next+1; | |
684 | } | |
685 | return -first-1; | |
9cf6d335 SV |
686 | } |
687 | ||
c6458e60 | 688 | static void find_ofs_delta_children(off_t offset, |
fc968e26 | 689 | int *first_index, int *last_index) |
9cf6d335 | 690 | { |
fc968e26 | 691 | int first = find_ofs_delta(offset); |
9cf6d335 | 692 | int last = first; |
c6458e60 | 693 | int end = nr_ofs_deltas - 1; |
9cf6d335 | 694 | |
6a87ed97 NP |
695 | if (first < 0) { |
696 | *first_index = 0; | |
697 | *last_index = -1; | |
698 | return; | |
699 | } | |
c6458e60 | 700 | while (first > 0 && ofs_deltas[first - 1].offset == offset) |
9cf6d335 | 701 | --first; |
c6458e60 NTND |
702 | while (last < end && ofs_deltas[last + 1].offset == offset) |
703 | ++last; | |
704 | *first_index = first; | |
705 | *last_index = last; | |
706 | } | |
707 | ||
af8caf33 | 708 | static int compare_ref_delta_bases(const struct object_id *oid1, |
709 | const struct object_id *oid2, | |
c6458e60 NTND |
710 | enum object_type type1, |
711 | enum object_type type2) | |
712 | { | |
713 | int cmp = type1 - type2; | |
714 | if (cmp) | |
715 | return cmp; | |
af8caf33 | 716 | return oidcmp(oid1, oid2); |
c6458e60 NTND |
717 | } |
718 | ||
fc968e26 | 719 | static int find_ref_delta(const struct object_id *oid) |
c6458e60 NTND |
720 | { |
721 | int first = 0, last = nr_ref_deltas; | |
722 | ||
723 | while (first < last) { | |
19716b21 | 724 | int next = first + (last - first) / 2; |
c6458e60 NTND |
725 | struct ref_delta_entry *delta = &ref_deltas[next]; |
726 | int cmp; | |
727 | ||
af8caf33 | 728 | cmp = compare_ref_delta_bases(oid, &delta->oid, |
fc968e26 JT |
729 | OBJ_REF_DELTA, |
730 | objects[delta->obj_no].type); | |
c6458e60 NTND |
731 | if (!cmp) |
732 | return next; | |
733 | if (cmp < 0) { | |
734 | last = next; | |
735 | continue; | |
736 | } | |
737 | first = next+1; | |
738 | } | |
739 | return -first-1; | |
740 | } | |
741 | ||
af8caf33 | 742 | static void find_ref_delta_children(const struct object_id *oid, |
fc968e26 | 743 | int *first_index, int *last_index) |
c6458e60 | 744 | { |
fc968e26 | 745 | int first = find_ref_delta(oid); |
c6458e60 NTND |
746 | int last = first; |
747 | int end = nr_ref_deltas - 1; | |
748 | ||
749 | if (first < 0) { | |
750 | *first_index = 0; | |
751 | *last_index = -1; | |
752 | return; | |
753 | } | |
4a7e27e9 | 754 | while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid)) |
c6458e60 | 755 | --first; |
4a7e27e9 | 756 | while (last < end && oideq(&ref_deltas[last + 1].oid, oid)) |
9cf6d335 SV |
757 | ++last; |
758 | *first_index = first; | |
759 | *last_index = last; | |
9cf6d335 SV |
760 | } |
761 | ||
4614043c NTND |
762 | struct compare_data { |
763 | struct object_entry *entry; | |
764 | struct git_istream *st; | |
765 | unsigned char *buf; | |
766 | unsigned long buf_size; | |
767 | }; | |
768 | ||
769 | static int compare_objects(const unsigned char *buf, unsigned long size, | |
770 | void *cb_data) | |
771 | { | |
772 | struct compare_data *data = cb_data; | |
773 | ||
774 | if (data->buf_size < size) { | |
775 | free(data->buf); | |
776 | data->buf = xmalloc(size); | |
777 | data->buf_size = size; | |
778 | } | |
779 | ||
780 | while (size) { | |
781 | ssize_t len = read_istream(data->st, data->buf, size); | |
782 | if (len == 0) | |
783 | die(_("SHA1 COLLISION FOUND WITH %s !"), | |
e6a492b7 | 784 | oid_to_hex(&data->entry->idx.oid)); |
4614043c NTND |
785 | if (len < 0) |
786 | die(_("unable to read %s"), | |
e6a492b7 | 787 | oid_to_hex(&data->entry->idx.oid)); |
4614043c NTND |
788 | if (memcmp(buf, data->buf, len)) |
789 | die(_("SHA1 COLLISION FOUND WITH %s !"), | |
e6a492b7 | 790 | oid_to_hex(&data->entry->idx.oid)); |
4614043c NTND |
791 | size -= len; |
792 | buf += len; | |
793 | } | |
794 | return 0; | |
795 | } | |
796 | ||
797 | static int check_collison(struct object_entry *entry) | |
798 | { | |
799 | struct compare_data data; | |
800 | enum object_type type; | |
801 | unsigned long size; | |
802 | ||
7835ee75 PS |
803 | if (entry->size <= repo_settings_get_big_file_threshold(the_repository) || |
804 | entry->type != OBJ_BLOB) | |
4614043c NTND |
805 | return -1; |
806 | ||
807 | memset(&data, 0, sizeof(data)); | |
808 | data.entry = entry; | |
c8123e72 MT |
809 | data.st = open_istream(the_repository, &entry->idx.oid, &type, &size, |
810 | NULL); | |
4614043c NTND |
811 | if (!data.st) |
812 | return -1; | |
813 | if (size != entry->size || type != entry->type) | |
814 | die(_("SHA1 COLLISION FOUND WITH %s !"), | |
e6a492b7 | 815 | oid_to_hex(&entry->idx.oid)); |
4614043c NTND |
816 | unpack_data(entry, compare_objects, &data); |
817 | close_istream(data.st); | |
818 | free(data.buf); | |
819 | return 0; | |
820 | } | |
821 | ||
911d1420 | 822 | static void record_outgoing_link(const struct object_id *oid) |
c08589ef | 823 | { |
911d1420 | 824 | oidset_insert(&outgoing_links, oid); |
c08589ef JT |
825 | } |
826 | ||
36198026 JT |
827 | static void maybe_record_name_entry(const struct name_entry *entry) |
828 | { | |
829 | /* | |
830 | * Checking only trees here results in a significantly faster packfile | |
831 | * indexing, but the drawback is that if the packfile to be indexed | |
832 | * references a local blob only directly (that is, never through a | |
833 | * local tree), that local blob is in danger of being garbage | |
834 | * collected. Such a situation may arise if we push local commits, | |
835 | * including one with a change to a blob in the root tree, and then the | |
836 | * server incorporates them into its main branch through a "rebase" or | |
837 | * "squash" merge strategy, and then we fetch the new main branch from | |
838 | * the server. | |
839 | * | |
840 | * This situation has not been observed yet - we have only noticed | |
841 | * missing commits, not missing trees or blobs. (In fact, if it were | |
842 | * believed that only missing commits are problematic, one could argue | |
843 | * that we should also exclude trees during the outgoing link check; | |
844 | * but it is safer to include them.) | |
845 | * | |
846 | * Due to the rarity of the situation (it has not been observed to | |
847 | * happen in real life), and because the "penalty" in such a situation | |
848 | * is merely to refetch the missing blob when it's needed (and this | |
849 | * happens only once - when refetched, the blob goes into a promisor | |
850 | * pack, so it won't be GC-ed, the tradeoff seems worth it. | |
851 | */ | |
852 | if (S_ISDIR(entry->mode)) | |
853 | record_outgoing_link(&entry->oid); | |
854 | } | |
855 | ||
911d1420 | 856 | static void do_record_outgoing_links(struct object *obj) |
c08589ef JT |
857 | { |
858 | if (obj->type == OBJ_TREE) { | |
859 | struct tree *tree = (struct tree *)obj; | |
860 | struct tree_desc desc; | |
861 | struct name_entry entry; | |
862 | if (init_tree_desc_gently(&desc, &tree->object.oid, | |
863 | tree->buffer, tree->size, 0)) | |
864 | /* | |
865 | * Error messages are given when packs are | |
866 | * verified, so do not print any here. | |
867 | */ | |
868 | return; | |
869 | while (tree_entry_gently(&desc, &entry)) | |
36198026 | 870 | maybe_record_name_entry(&entry); |
c08589ef JT |
871 | } else if (obj->type == OBJ_COMMIT) { |
872 | struct commit *commit = (struct commit *) obj; | |
873 | struct commit_list *parents = commit->parents; | |
874 | ||
1a14c857 | 875 | record_outgoing_link(get_commit_tree_oid(commit)); |
c08589ef | 876 | for (; parents; parents = parents->next) |
911d1420 | 877 | record_outgoing_link(&parents->item->object.oid); |
c08589ef JT |
878 | } else if (obj->type == OBJ_TAG) { |
879 | struct tag *tag = (struct tag *) obj; | |
911d1420 | 880 | record_outgoing_link(get_tagged_oid(tag)); |
c08589ef JT |
881 | } |
882 | } | |
883 | ||
9ec2dde9 NTND |
884 | static void sha1_object(const void *data, struct object_entry *obj_entry, |
885 | unsigned long size, enum object_type type, | |
3e930981 | 886 | const struct object_id *oid) |
9cf6d335 | 887 | { |
9ec2dde9 | 888 | void *new_data = NULL; |
29401e15 | 889 | int collision_test_needed = 0; |
9ec2dde9 NTND |
890 | |
891 | assert(data || obj_entry); | |
892 | ||
29401e15 JK |
893 | if (startup_info->have_repository) { |
894 | read_lock(); | |
062b914c PS |
895 | collision_test_needed = has_object(the_repository, oid, |
896 | HAS_OBJECT_FETCH_PROMISOR); | |
29401e15 JK |
897 | read_unlock(); |
898 | } | |
4614043c NTND |
899 | |
900 | if (collision_test_needed && !data) { | |
901 | read_lock(); | |
902 | if (!check_collison(obj_entry)) | |
903 | collision_test_needed = 0; | |
904 | read_unlock(); | |
905 | } | |
906 | if (collision_test_needed) { | |
8685da42 NP |
907 | void *has_data; |
908 | enum object_type has_type; | |
909 | unsigned long has_size; | |
4614043c | 910 | read_lock(); |
0df8e965 | 911 | has_type = oid_object_info(the_repository, oid, &has_size); |
51054177 | 912 | if (has_type < 0) |
3e930981 | 913 | die(_("cannot read existing object info %s"), oid_to_hex(oid)); |
4614043c | 914 | if (has_type != type || has_size != size) |
3e930981 | 915 | die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid)); |
bc726bd0 ÆAB |
916 | has_data = repo_read_object_file(the_repository, oid, |
917 | &has_type, &has_size); | |
b8a2486f | 918 | read_unlock(); |
4614043c NTND |
919 | if (!data) |
920 | data = new_data = get_data_from_pack(obj_entry); | |
8685da42 | 921 | if (!has_data) |
3e930981 | 922 | die(_("cannot read existing object %s"), oid_to_hex(oid)); |
8685da42 NP |
923 | if (size != has_size || type != has_type || |
924 | memcmp(data, has_data, size) != 0) | |
3e930981 | 925 | die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid)); |
bbf4b41b | 926 | free(has_data); |
4614043c | 927 | } |
b8a2486f | 928 | |
911d1420 | 929 | if (strict || do_fsck_object || record_outgoing_links) { |
b8a2486f | 930 | read_lock(); |
0153be05 | 931 | if (type == OBJ_BLOB) { |
da14a7ff | 932 | struct blob *blob = lookup_blob(the_repository, oid); |
0153be05 MK |
933 | if (blob) |
934 | blob->object.flags |= FLAG_CHECKED; | |
935 | else | |
3e930981 | 936 | die(_("invalid blob object %s"), oid_to_hex(oid)); |
73c3f0f7 JK |
937 | if (do_fsck_object && |
938 | fsck_object(&blob->object, (void *)data, size, &fsck_options)) | |
939 | die(_("fsck error in packed object")); | |
0153be05 MK |
940 | } else { |
941 | struct object *obj; | |
942 | int eaten; | |
943 | void *buf = (void *) data; | |
944 | ||
920734b0 | 945 | assert(data && "data can only be NULL for large _blobs_"); |
9ec2dde9 | 946 | |
0153be05 MK |
947 | /* |
948 | * we do not need to free the memory here, as the | |
949 | * buf is deleted by the caller. | |
950 | */ | |
1ec5bfd2 SB |
951 | obj = parse_object_buffer(the_repository, oid, type, |
952 | size, buf, | |
c251c83d | 953 | &eaten); |
0153be05 | 954 | if (!obj) |
debca9d2 | 955 | die(_("invalid %s"), type_name(type)); |
c6807a40 | 956 | if (do_fsck_object && |
22410549 | 957 | fsck_object(obj, buf, size, &fsck_options)) |
db5a58c1 | 958 | die(_("fsck error in packed object")); |
ffb2c0fe | 959 | if (strict && fsck_walk(obj, NULL, &fsck_options)) |
f2fd0760 | 960 | die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid)); |
911d1420 JT |
961 | if (record_outgoing_links) |
962 | do_record_outgoing_links(obj); | |
0153be05 MK |
963 | |
964 | if (obj->type == OBJ_TREE) { | |
965 | struct tree *item = (struct tree *) obj; | |
966 | item->buffer = NULL; | |
6e454b9a | 967 | obj->parsed = 0; |
0153be05 MK |
968 | } |
969 | if (obj->type == OBJ_COMMIT) { | |
970 | struct commit *commit = (struct commit *) obj; | |
8597ea3a | 971 | if (detach_commit_buffer(commit, NULL) != data) |
033abf97 | 972 | BUG("parse_object_buffer transmogrified our buffer"); |
0153be05 MK |
973 | } |
974 | obj->flags |= FLAG_CHECKED; | |
975 | } | |
b8a2486f | 976 | read_unlock(); |
0153be05 | 977 | } |
9ec2dde9 NTND |
978 | |
979 | free(new_data); | |
9cf6d335 SV |
980 | } |
981 | ||
20e95d0a | 982 | /* |
ec6a8f97 | 983 | * Ensure that this node has been reconstructed and return its contents. |
20e95d0a | 984 | * |
ec6a8f97 JT |
985 | * In the typical and best case, this node would already be reconstructed |
986 | * (through the invocation to resolve_delta() in threaded_second_pass()) and it | |
987 | * would not be pruned. However, if pruning of this node was necessary due to | |
988 | * reaching delta_base_cache_limit, this function will find the closest | |
989 | * ancestor with reconstructed data that has not been pruned (or if there is | |
990 | * none, the ultimate base object), and reconstruct each node in the delta | |
991 | * chain in order to generate the reconstructed data for this node. | |
20e95d0a | 992 | */ |
92392b4a SP |
993 | static void *get_base_data(struct base_data *c) |
994 | { | |
995 | if (!c->data) { | |
996 | struct object_entry *obj = c->obj; | |
20e95d0a NTND |
997 | struct base_data **delta = NULL; |
998 | int delta_nr = 0, delta_alloc = 0; | |
92392b4a | 999 | |
20e95d0a NTND |
1000 | while (is_delta_type(c->obj->type) && !c->data) { |
1001 | ALLOC_GROW(delta, delta_nr + 1, delta_alloc); | |
1002 | delta[delta_nr++] = c; | |
1003 | c = c->base; | |
1004 | } | |
1005 | if (!delta_nr) { | |
1006 | c->data = get_data_from_pack(obj); | |
1007 | c->size = obj->size; | |
f08cbf60 | 1008 | base_cache_used += c->size; |
20e95d0a NTND |
1009 | prune_base_data(c); |
1010 | } | |
1011 | for (; delta_nr > 0; delta_nr--) { | |
1012 | void *base, *raw; | |
1013 | c = delta[delta_nr - 1]; | |
1014 | obj = c->obj; | |
1015 | base = get_base_data(c->base); | |
1016 | raw = get_data_from_pack(obj); | |
92392b4a SP |
1017 | c->data = patch_delta( |
1018 | base, c->base->size, | |
1019 | raw, obj->size, | |
1020 | &c->size); | |
1021 | free(raw); | |
1022 | if (!c->data) | |
c2b97ecf | 1023 | bad_object(obj->idx.offset, _("failed to apply delta")); |
f08cbf60 | 1024 | base_cache_used += c->size; |
20e95d0a | 1025 | prune_base_data(c); |
9441b61d | 1026 | } |
20e95d0a | 1027 | free(delta); |
92392b4a SP |
1028 | } |
1029 | return c->data; | |
1030 | } | |
1031 | ||
b4718cae JT |
1032 | static struct base_data *make_base(struct object_entry *obj, |
1033 | struct base_data *parent) | |
9cf6d335 | 1034 | { |
b4718cae JT |
1035 | struct base_data *base = xcalloc(1, sizeof(struct base_data)); |
1036 | base->base = parent; | |
1037 | base->obj = obj; | |
1038 | find_ref_delta_children(&obj->idx.oid, | |
1039 | &base->ref_first, &base->ref_last); | |
1040 | find_ofs_delta_children(obj->idx.offset, | |
1041 | &base->ofs_first, &base->ofs_last); | |
f08cbf60 JT |
1042 | base->children_remaining = base->ref_last - base->ref_first + |
1043 | base->ofs_last - base->ofs_first + 2; | |
b4718cae JT |
1044 | return base; |
1045 | } | |
1046 | ||
1047 | static struct base_data *resolve_delta(struct object_entry *delta_obj, | |
1048 | struct base_data *base) | |
1049 | { | |
ee6f0583 | 1050 | void *delta_data, *result_data; |
b4718cae JT |
1051 | struct base_data *result; |
1052 | unsigned long result_size; | |
9cf6d335 | 1053 | |
3aba2fdd | 1054 | if (show_stat) { |
41730576 NTND |
1055 | int i = delta_obj - objects; |
1056 | int j = base->obj - objects; | |
1057 | obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1; | |
3aba2fdd | 1058 | deepest_delta_lock(); |
41730576 NTND |
1059 | if (deepest_delta < obj_stat[i].delta_depth) |
1060 | deepest_delta = obj_stat[i].delta_depth; | |
3aba2fdd | 1061 | deepest_delta_unlock(); |
41730576 | 1062 | obj_stat[i].base_object_no = j; |
3aba2fdd | 1063 | } |
636171cb | 1064 | delta_data = get_data_from_pack(delta_obj); |
ee6f0583 JT |
1065 | assert(base->data); |
1066 | result_data = patch_delta(base->data, base->size, | |
b4718cae | 1067 | delta_data, delta_obj->size, &result_size); |
9cf6d335 | 1068 | free(delta_data); |
b4718cae | 1069 | if (!result_data) |
c2b97ecf | 1070 | bad_object(delta_obj->idx.offset, _("failed to apply delta")); |
b4718cae | 1071 | hash_object_file(the_hash_algo, result_data, result_size, |
44439c1c | 1072 | delta_obj->real_type, &delta_obj->idx.oid); |
b4718cae | 1073 | sha1_object(result_data, NULL, result_size, delta_obj->real_type, |
3e930981 | 1074 | &delta_obj->idx.oid); |
b4718cae JT |
1075 | |
1076 | result = make_base(delta_obj, base); | |
f08cbf60 JT |
1077 | result->data = result_data; |
1078 | result->size = result_size; | |
b4718cae | 1079 | |
b8a2486f | 1080 | counter_lock(); |
636171cb | 1081 | nr_resolved_deltas++; |
b8a2486f | 1082 | counter_unlock(); |
ab791dd1 | 1083 | |
b4718cae | 1084 | return result; |
9cf6d335 SV |
1085 | } |
1086 | ||
c6458e60 | 1087 | static int compare_ofs_delta_entry(const void *a, const void *b) |
9cf6d335 | 1088 | { |
c6458e60 NTND |
1089 | const struct ofs_delta_entry *delta_a = a; |
1090 | const struct ofs_delta_entry *delta_b = b; | |
7218a215 | 1091 | |
f0e7f11d JK |
1092 | return delta_a->offset < delta_b->offset ? -1 : |
1093 | delta_a->offset > delta_b->offset ? 1 : | |
1094 | 0; | |
c6458e60 NTND |
1095 | } |
1096 | ||
1097 | static int compare_ref_delta_entry(const void *a, const void *b) | |
9cf6d335 | 1098 | { |
c6458e60 NTND |
1099 | const struct ref_delta_entry *delta_a = a; |
1100 | const struct ref_delta_entry *delta_b = b; | |
7218a215 | 1101 | |
af8caf33 | 1102 | return oidcmp(&delta_a->oid, &delta_b->oid); |
9cf6d335 SV |
1103 | } |
1104 | ||
b8a2486f NTND |
1105 | static void *threaded_second_pass(void *data) |
1106 | { | |
f08cbf60 JT |
1107 | if (data) |
1108 | set_thread_data(data); | |
b8a2486f | 1109 | for (;;) { |
f08cbf60 | 1110 | struct base_data *parent = NULL; |
98f8854c DS |
1111 | struct object_entry *child_obj = NULL; |
1112 | struct base_data *child = NULL; | |
f08cbf60 | 1113 | |
cea69151 JK |
1114 | counter_lock(); |
1115 | display_progress(progress, nr_resolved_deltas); | |
1116 | counter_unlock(); | |
1117 | ||
8f82aad4 | 1118 | work_lock(); |
f08cbf60 JT |
1119 | if (list_empty(&work_head)) { |
1120 | /* | |
1121 | * Take an object from the object array. | |
1122 | */ | |
1123 | while (nr_dispatched < nr_objects && | |
1124 | is_delta_type(objects[nr_dispatched].type)) | |
1125 | nr_dispatched++; | |
1126 | if (nr_dispatched >= nr_objects) { | |
1127 | work_unlock(); | |
1128 | break; | |
1129 | } | |
1130 | child_obj = &objects[nr_dispatched++]; | |
1131 | } else { | |
1132 | /* | |
1133 | * Peek at the top of the stack, and take a child from | |
1134 | * it. | |
1135 | */ | |
1136 | parent = list_first_entry(&work_head, struct base_data, | |
1137 | list); | |
1138 | ||
98f8854c | 1139 | while (parent->ref_first <= parent->ref_last) { |
f08cbf60 JT |
1140 | int offset = ref_deltas[parent->ref_first++].obj_no; |
1141 | child_obj = objects + offset; | |
98f8854c DS |
1142 | if (child_obj->real_type != OBJ_REF_DELTA) { |
1143 | child_obj = NULL; | |
1144 | continue; | |
1145 | } | |
f08cbf60 | 1146 | child_obj->real_type = parent->obj->real_type; |
98f8854c DS |
1147 | break; |
1148 | } | |
1149 | ||
1150 | if (!child_obj && parent->ofs_first <= parent->ofs_last) { | |
f08cbf60 JT |
1151 | child_obj = objects + |
1152 | ofs_deltas[parent->ofs_first++].obj_no; | |
1153 | assert(child_obj->real_type == OBJ_OFS_DELTA); | |
1154 | child_obj->real_type = parent->obj->real_type; | |
1155 | } | |
1156 | ||
1157 | if (parent->ref_first > parent->ref_last && | |
1158 | parent->ofs_first > parent->ofs_last) { | |
1159 | /* | |
1160 | * This parent has run out of children, so move | |
1161 | * it to done_head. | |
1162 | */ | |
1163 | list_del(&parent->list); | |
1164 | list_add(&parent->list, &done_head); | |
1165 | } | |
1166 | ||
1167 | /* | |
1168 | * Ensure that the parent has data, since we will need | |
1169 | * it later. | |
1170 | * | |
1171 | * NEEDSWORK: If parent data needs to be reloaded, this | |
1172 | * prolongs the time that the current thread spends in | |
1173 | * the mutex. A mitigating factor is that parent data | |
1174 | * needs to be reloaded only if the delta base cache | |
1175 | * limit is exceeded, so in the typical case, this does | |
1176 | * not happen. | |
1177 | */ | |
1178 | get_base_data(parent); | |
1179 | parent->retain_data++; | |
b8a2486f | 1180 | } |
b8a2486f NTND |
1181 | work_unlock(); |
1182 | ||
98f8854c DS |
1183 | if (child_obj) { |
1184 | if (parent) { | |
1185 | child = resolve_delta(child_obj, parent); | |
1186 | if (!child->children_remaining) | |
1187 | FREE_AND_NULL(child->data); | |
1188 | } else{ | |
1189 | child = make_base(child_obj, NULL); | |
1190 | if (child->children_remaining) { | |
1191 | /* | |
1192 | * Since this child has its own delta children, | |
1193 | * we will need this data in the future. | |
1194 | * Inflate now so that future iterations will | |
1195 | * have access to this object's data while | |
1196 | * outside the work mutex. | |
1197 | */ | |
1198 | child->data = get_data_from_pack(child_obj); | |
1199 | child->size = child_obj->size; | |
1200 | } | |
f08cbf60 JT |
1201 | } |
1202 | } | |
1203 | ||
1204 | work_lock(); | |
1205 | if (parent) | |
1206 | parent->retain_data--; | |
98f8854c DS |
1207 | |
1208 | if (child && child->data) { | |
f08cbf60 JT |
1209 | /* |
1210 | * This child has its own children, so add it to | |
1211 | * work_head. | |
1212 | */ | |
1213 | list_add(&child->list, &work_head); | |
1214 | base_cache_used += child->size; | |
1215 | prune_base_data(NULL); | |
f2bcc69e | 1216 | free_base_data(child); |
98f8854c | 1217 | } else if (child) { |
f08cbf60 JT |
1218 | /* |
1219 | * This child does not have its own children. It may be | |
1220 | * the last descendant of its ancestors; free those | |
1221 | * that we can. | |
1222 | */ | |
1223 | struct base_data *p = parent; | |
1224 | ||
1225 | while (p) { | |
1226 | struct base_data *next_p; | |
1227 | ||
1228 | p->children_remaining--; | |
1229 | if (p->children_remaining) | |
1230 | break; | |
1231 | ||
1232 | next_p = p->base; | |
1233 | free_base_data(p); | |
1234 | list_del(&p->list); | |
1235 | free(p); | |
1236 | ||
1237 | p = next_p; | |
1238 | } | |
f2bcc69e | 1239 | FREE_AND_NULL(child); |
f08cbf60 JT |
1240 | } |
1241 | work_unlock(); | |
b8a2486f NTND |
1242 | } |
1243 | return NULL; | |
1244 | } | |
b8a2486f | 1245 | |
5272f755 NTND |
1246 | /* |
1247 | * First pass: | |
1248 | * - find locations of all objects; | |
1249 | * - calculate SHA1 of all non-delta objects; | |
1250 | * - remember base (SHA1 or offset) for all deltas. | |
1251 | */ | |
454253f0 | 1252 | static void parse_pack_objects(unsigned char *hash) |
9cf6d335 | 1253 | { |
9ec2dde9 | 1254 | int i, nr_delays = 0; |
c6458e60 | 1255 | struct ofs_delta_entry *ofs_delta = ofs_deltas; |
454253f0 | 1256 | struct object_id ref_delta_oid; |
2d477051 | 1257 | struct stat st; |
7346e340 | 1258 | struct git_hash_ctx tmp_ctx; |
9cf6d335 | 1259 | |
13aaf148 | 1260 | if (verbose) |
29e63ed3 | 1261 | progress = start_progress( |
1f7e6478 | 1262 | the_repository, |
f46c46e4 | 1263 | progress_title ? progress_title : |
c2b97ecf | 1264 | from_stdin ? _("Receiving objects") : _("Indexing objects"), |
29e63ed3 | 1265 | nr_objects); |
9cf6d335 SV |
1266 | for (i = 0; i < nr_objects; i++) { |
1267 | struct object_entry *obj = &objects[i]; | |
c6458e60 | 1268 | void *data = unpack_raw_entry(obj, &ofs_delta->offset, |
454253f0 | 1269 | &ref_delta_oid, |
1270 | &obj->idx.oid); | |
9cf6d335 | 1271 | obj->real_type = obj->type; |
c6458e60 NTND |
1272 | if (obj->type == OBJ_OFS_DELTA) { |
1273 | nr_ofs_deltas++; | |
1274 | ofs_delta->obj_no = i; | |
1275 | ofs_delta++; | |
1276 | } else if (obj->type == OBJ_REF_DELTA) { | |
1277 | ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc); | |
af8caf33 | 1278 | oidcpy(&ref_deltas[nr_ref_deltas].oid, &ref_delta_oid); |
c6458e60 NTND |
1279 | ref_deltas[nr_ref_deltas].obj_no = i; |
1280 | nr_ref_deltas++; | |
9ec2dde9 NTND |
1281 | } else if (!data) { |
1282 | /* large blobs, check later */ | |
1283 | obj->real_type = OBJ_BAD; | |
1284 | nr_delays++; | |
9cf6d335 | 1285 | } else |
e6a492b7 | 1286 | sha1_object(data, NULL, obj->size, obj->type, |
3e930981 | 1287 | &obj->idx.oid); |
9cf6d335 | 1288 | free(data); |
4d4fcc54 | 1289 | display_progress(progress, i+1); |
9cf6d335 | 1290 | } |
aa7e44bf | 1291 | objects[i].idx.offset = consumed_bytes; |
4d4fcc54 | 1292 | stop_progress(&progress); |
2d477051 NP |
1293 | |
1294 | /* Check pack integrity */ | |
636171cb | 1295 | flush(); |
d39f04b6 | 1296 | the_hash_algo->init_fn(&tmp_ctx); |
0578f1e6 PS |
1297 | git_hash_clone(&tmp_ctx, &input_ctx); |
1298 | git_hash_final(hash, &tmp_ctx); | |
f4836570 | 1299 | if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo)) |
c2b97ecf | 1300 | die(_("pack is corrupted (SHA1 mismatch)")); |
454253f0 | 1301 | use(the_hash_algo->rawsz); |
2d477051 NP |
1302 | |
1303 | /* If input_fd is a file, we should have reached its end now. */ | |
1304 | if (fstat(input_fd, &st)) | |
c2b97ecf | 1305 | die_errno(_("cannot fstat packfile")); |
fa257b05 JS |
1306 | if (S_ISREG(st.st_mode) && |
1307 | lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size) | |
c2b97ecf | 1308 | die(_("pack has junk at the end")); |
9ec2dde9 NTND |
1309 | |
1310 | for (i = 0; i < nr_objects; i++) { | |
1311 | struct object_entry *obj = &objects[i]; | |
1312 | if (obj->real_type != OBJ_BAD) | |
1313 | continue; | |
1314 | obj->real_type = obj->type; | |
e6a492b7 | 1315 | sha1_object(NULL, obj, obj->size, obj->type, |
3e930981 | 1316 | &obj->idx.oid); |
9ec2dde9 NTND |
1317 | nr_delays--; |
1318 | } | |
1319 | if (nr_delays) | |
1320 | die(_("confusion beyond insanity in parse_pack_objects()")); | |
5272f755 NTND |
1321 | } |
1322 | ||
1323 | /* | |
1324 | * Second pass: | |
1325 | * - for all non-delta objects, look if it is used as a base for | |
1326 | * deltas; | |
1327 | * - if used as a base, uncompress the object and apply all deltas, | |
1328 | * recursively checking if the resulting object is used as a base | |
1329 | * for some more deltas. | |
1330 | */ | |
d6b2d21f | 1331 | static void resolve_deltas(struct pack_idx_option *opts) |
5272f755 NTND |
1332 | { |
1333 | int i; | |
9cf6d335 | 1334 | |
c6458e60 | 1335 | if (!nr_ofs_deltas && !nr_ref_deltas) |
3c9af366 NP |
1336 | return; |
1337 | ||
53dda6ff | 1338 | /* Sort deltas by base SHA1/offset for fast searching */ |
9ed0d8d6 RS |
1339 | QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry); |
1340 | QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry); | |
9cf6d335 | 1341 | |
e376f17f | 1342 | if (verbose || show_resolving_progress) |
1f7e6478 PS |
1343 | progress = start_progress(the_repository, |
1344 | _("Resolving deltas"), | |
c6458e60 | 1345 | nr_ref_deltas + nr_ofs_deltas); |
b8a2486f | 1346 | |
b8a2486f | 1347 | nr_dispatched = 0; |
d6b2d21f | 1348 | base_cache_limit = opts->delta_base_cache_limit * nr_threads; |
b8a2486f NTND |
1349 | if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) { |
1350 | init_thread(); | |
1351 | for (i = 0; i < nr_threads; i++) { | |
1352 | int ret = pthread_create(&thread_data[i].thread, NULL, | |
1353 | threaded_second_pass, thread_data + i); | |
1354 | if (ret) | |
f350df42 NTND |
1355 | die(_("unable to create thread: %s"), |
1356 | strerror(ret)); | |
b8a2486f NTND |
1357 | } |
1358 | for (i = 0; i < nr_threads; i++) | |
1359 | pthread_join(thread_data[i].thread, NULL); | |
1360 | cleanup_thread(); | |
1361 | return; | |
1362 | } | |
46e6fb1e | 1363 | threaded_second_pass(¬hread_data); |
636171cb NP |
1364 | } |
1365 | ||
5272f755 NTND |
1366 | /* |
1367 | * Third pass: | |
1368 | * - append objects to convert thin pack to full pack if required | |
454253f0 | 1369 | * - write the final pack hash |
5272f755 | 1370 | */ |
98a3beab | 1371 | static void fix_unresolved_deltas(struct hashfile *f); |
454253f0 | 1372 | static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash) |
5272f755 | 1373 | { |
c6458e60 | 1374 | if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) { |
5272f755 | 1375 | stop_progress(&progress); |
454253f0 | 1376 | /* Flush remaining pack final hash. */ |
5272f755 NTND |
1377 | flush(); |
1378 | return; | |
1379 | } | |
1380 | ||
1381 | if (fix_thin_pack) { | |
98a3beab | 1382 | struct hashfile *f; |
454253f0 | 1383 | unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ]; |
5c3459fc | 1384 | struct strbuf msg = STRBUF_INIT; |
c6458e60 | 1385 | int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas; |
5272f755 NTND |
1386 | int nr_objects_initial = nr_objects; |
1387 | if (nr_unresolved <= 0) | |
cc13431a | 1388 | die(_("confusion beyond insanity")); |
2756ca43 | 1389 | REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1); |
57165db0 JK |
1390 | memset(objects + nr_objects + 1, 0, |
1391 | nr_unresolved * sizeof(*objects)); | |
228457c9 | 1392 | f = hashfd(the_repository->hash_algo, output_fd, curr_pack); |
781d9306 | 1393 | fix_unresolved_deltas(f); |
71d99b81 VA |
1394 | strbuf_addf(&msg, Q_("completed with %d local object", |
1395 | "completed with %d local objects", | |
1396 | nr_objects - nr_objects_initial), | |
5c3459fc NTND |
1397 | nr_objects - nr_objects_initial); |
1398 | stop_progress_msg(&progress, msg.buf); | |
1399 | strbuf_release(&msg); | |
020406ea | 1400 | finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0); |
f4836570 | 1401 | hashcpy(read_hash, pack_hash, the_repository->hash_algo); |
8244d01d | 1402 | fixup_pack_header_footer(the_hash_algo, output_fd, pack_hash, |
5272f755 | 1403 | curr_pack, nr_objects, |
454253f0 | 1404 | read_hash, consumed_bytes-the_hash_algo->rawsz); |
f4836570 | 1405 | if (!hasheq(read_hash, tail_hash, the_repository->hash_algo)) |
f350df42 NTND |
1406 | die(_("Unexpected tail checksum for %s " |
1407 | "(disk corruption?)"), curr_pack); | |
5272f755 | 1408 | } |
c6458e60 | 1409 | if (nr_ofs_deltas + nr_ref_deltas != nr_resolved_deltas) |
cc13431a JH |
1410 | die(Q_("pack has %d unresolved delta", |
1411 | "pack has %d unresolved deltas", | |
c6458e60 NTND |
1412 | nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas), |
1413 | nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas); | |
5272f755 NTND |
1414 | } |
1415 | ||
98a3beab | 1416 | static int write_compressed(struct hashfile *f, void *in, unsigned int size) |
636171cb | 1417 | { |
ef49a7a0 | 1418 | git_zstream stream; |
7734d7f2 NP |
1419 | int status; |
1420 | unsigned char outbuf[4096]; | |
636171cb | 1421 | |
55bb5c91 | 1422 | git_deflate_init(&stream, zlib_compression_level); |
636171cb NP |
1423 | stream.next_in = in; |
1424 | stream.avail_in = size; | |
636171cb | 1425 | |
7734d7f2 NP |
1426 | do { |
1427 | stream.next_out = outbuf; | |
1428 | stream.avail_out = sizeof(outbuf); | |
55bb5c91 | 1429 | status = git_deflate(&stream, Z_FINISH); |
98a3beab | 1430 | hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out); |
7734d7f2 NP |
1431 | } while (status == Z_OK); |
1432 | ||
1433 | if (status != Z_STREAM_END) | |
c2b97ecf | 1434 | die(_("unable to deflate appended object (%d)"), status); |
636171cb | 1435 | size = stream.total_out; |
55bb5c91 | 1436 | git_deflate_end(&stream); |
636171cb NP |
1437 | return size; |
1438 | } | |
1439 | ||
98a3beab | 1440 | static struct object_entry *append_obj_to_pack(struct hashfile *f, |
03993e13 | 1441 | const unsigned char *sha1, void *buf, |
636171cb NP |
1442 | unsigned long size, enum object_type type) |
1443 | { | |
1444 | struct object_entry *obj = &objects[nr_objects++]; | |
1445 | unsigned char header[10]; | |
1446 | unsigned long s = size; | |
1447 | int n = 0; | |
1448 | unsigned char c = (type << 4) | (s & 15); | |
1449 | s >>= 4; | |
1450 | while (s) { | |
1451 | header[n++] = c | 0x80; | |
1452 | c = s & 0x7f; | |
1453 | s >>= 7; | |
1454 | } | |
1455 | header[n++] = c; | |
8522148f | 1456 | crc32_begin(f); |
98a3beab | 1457 | hashwrite(f, header, n); |
72de2883 BS |
1458 | obj[0].size = size; |
1459 | obj[0].hdr_size = n; | |
1460 | obj[0].type = type; | |
1461 | obj[0].real_type = type; | |
aa7e44bf | 1462 | obj[1].idx.offset = obj[0].idx.offset + n; |
8522148f NP |
1463 | obj[1].idx.offset += write_compressed(f, buf, size); |
1464 | obj[0].idx.crc32 = crc32_end(f); | |
98a3beab | 1465 | hashflush(f); |
9da95bda | 1466 | oidread(&obj->idx.oid, sha1, the_repository->hash_algo); |
03993e13 | 1467 | return obj; |
636171cb NP |
1468 | } |
1469 | ||
1470 | static int delta_pos_compare(const void *_a, const void *_b) | |
1471 | { | |
c6458e60 NTND |
1472 | struct ref_delta_entry *a = *(struct ref_delta_entry **)_a; |
1473 | struct ref_delta_entry *b = *(struct ref_delta_entry **)_b; | |
636171cb NP |
1474 | return a->obj_no - b->obj_no; |
1475 | } | |
9cf6d335 | 1476 | |
98a3beab | 1477 | static void fix_unresolved_deltas(struct hashfile *f) |
636171cb | 1478 | { |
c6458e60 | 1479 | struct ref_delta_entry **sorted_by_pos; |
781d9306 | 1480 | int i; |
636171cb NP |
1481 | |
1482 | /* | |
1483 | * Since many unresolved deltas may well be themselves base objects | |
1484 | * for more unresolved deltas, we really want to include the | |
1485 | * smallest number of base objects that would cover as much delta | |
1486 | * as possible by picking the | |
1487 | * trunc deltas first, allowing for other deltas to resolve without | |
1488 | * additional base objects. Since most base objects are to be found | |
1489 | * before deltas depending on them, a good heuristic is to start | |
1490 | * resolving deltas in the same order as their position in the pack. | |
1491 | */ | |
b32fa95f | 1492 | ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas); |
c6458e60 | 1493 | for (i = 0; i < nr_ref_deltas; i++) |
781d9306 | 1494 | sorted_by_pos[i] = &ref_deltas[i]; |
9ed0d8d6 | 1495 | QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare); |
636171cb | 1496 | |
a5183d76 | 1497 | if (repo_has_promisor_remote(the_repository)) { |
8a30a1ef JT |
1498 | /* |
1499 | * Prefetch the delta bases. | |
1500 | */ | |
1501 | struct oid_array to_fetch = OID_ARRAY_INIT; | |
1502 | for (i = 0; i < nr_ref_deltas; i++) { | |
1503 | struct ref_delta_entry *d = sorted_by_pos[i]; | |
1504 | if (!oid_object_info_extended(the_repository, &d->oid, | |
1505 | NULL, | |
1506 | OBJECT_INFO_FOR_PREFETCH)) | |
1507 | continue; | |
1508 | oid_array_append(&to_fetch, &d->oid); | |
1509 | } | |
db7ed741 JT |
1510 | promisor_remote_get_direct(the_repository, |
1511 | to_fetch.oid, to_fetch.nr); | |
8a30a1ef JT |
1512 | oid_array_clear(&to_fetch); |
1513 | } | |
1514 | ||
781d9306 | 1515 | for (i = 0; i < nr_ref_deltas; i++) { |
c6458e60 | 1516 | struct ref_delta_entry *d = sorted_by_pos[i]; |
21666f1a | 1517 | enum object_type type; |
b4718cae JT |
1518 | void *data; |
1519 | unsigned long size; | |
636171cb NP |
1520 | |
1521 | if (objects[d->obj_no].real_type != OBJ_REF_DELTA) | |
1522 | continue; | |
bc726bd0 ÆAB |
1523 | data = repo_read_object_file(the_repository, &d->oid, &type, |
1524 | &size); | |
b4718cae | 1525 | if (!data) |
636171cb | 1526 | continue; |
636171cb | 1527 | |
ee213de2 | 1528 | if (check_object_signature(the_repository, &d->oid, data, size, |
44439c1c | 1529 | type) < 0) |
af8caf33 | 1530 | die(_("local object %s is corrupt"), oid_to_hex(&d->oid)); |
f08cbf60 JT |
1531 | |
1532 | /* | |
1533 | * Add this as an object to the objects array and call | |
1534 | * threaded_second_pass() (which will pick up the added | |
1535 | * object). | |
1536 | */ | |
1537 | append_obj_to_pack(f, d->oid.hash, data, size, type); | |
f2bcc69e | 1538 | free(data); |
f08cbf60 JT |
1539 | threaded_second_pass(NULL); |
1540 | ||
4d4fcc54 | 1541 | display_progress(progress, nr_resolved_deltas); |
636171cb NP |
1542 | } |
1543 | free(sorted_by_pos); | |
1544 | } | |
1545 | ||
84d54494 TB |
1546 | static const char *derive_filename(const char *pack_name, const char *strip, |
1547 | const char *suffix, struct strbuf *buf) | |
8e29c7c3 JT |
1548 | { |
1549 | size_t len; | |
84d54494 TB |
1550 | if (!strip_suffix(pack_name, strip, &len) || !len || |
1551 | pack_name[len - 1] != '.') | |
1552 | die(_("packfile name '%s' does not end with '.%s'"), | |
1553 | pack_name, strip); | |
8e29c7c3 | 1554 | strbuf_add(buf, pack_name, len); |
8e29c7c3 JT |
1555 | strbuf_addstr(buf, suffix); |
1556 | return buf->buf; | |
1557 | } | |
1558 | ||
1559 | static void write_special_file(const char *suffix, const char *msg, | |
0fd90dab | 1560 | const char *pack_name, const unsigned char *hash, |
8e29c7c3 JT |
1561 | const char **report) |
1562 | { | |
1563 | struct strbuf name_buf = STRBUF_INIT; | |
1564 | const char *filename; | |
1565 | int fd; | |
1566 | int msg_len = strlen(msg); | |
1567 | ||
1568 | if (pack_name) | |
84d54494 | 1569 | filename = derive_filename(pack_name, "pack", suffix, &name_buf); |
8e29c7c3 | 1570 | else |
873b0059 | 1571 | filename = odb_pack_name(the_repository, &name_buf, hash, suffix); |
8e29c7c3 | 1572 | |
0b8ed25b | 1573 | fd = safe_create_file_with_leading_directories(the_repository, filename); |
8e29c7c3 JT |
1574 | if (fd < 0) { |
1575 | if (errno != EEXIST) | |
1576 | die_errno(_("cannot write %s file '%s'"), | |
1577 | suffix, filename); | |
1578 | } else { | |
1579 | if (msg_len > 0) { | |
1580 | write_or_die(fd, msg, msg_len); | |
1581 | write_or_die(fd, "\n", 1); | |
1582 | } | |
1583 | if (close(fd) != 0) | |
1584 | die_errno(_("cannot close written %s file '%s'"), | |
1585 | suffix, filename); | |
88e2f9ed JT |
1586 | if (report) |
1587 | *report = suffix; | |
8e29c7c3 JT |
1588 | } |
1589 | strbuf_release(&name_buf); | |
1590 | } | |
1591 | ||
8737dab3 ÆAB |
1592 | static void rename_tmp_packfile(const char **final_name, |
1593 | const char *curr_name, | |
1594 | struct strbuf *name, unsigned char *hash, | |
1595 | const char *ext, int make_read_only_if_same) | |
1596 | { | |
2f0ee051 | 1597 | if (!*final_name || strcmp(*final_name, curr_name)) { |
8737dab3 | 1598 | if (!*final_name) |
873b0059 | 1599 | *final_name = odb_pack_name(the_repository, name, hash, ext); |
8737dab3 | 1600 | if (finalize_object_file(curr_name, *final_name)) |
f7337193 | 1601 | die(_("unable to rename temporary '*.%s' file to '%s'"), |
8737dab3 ÆAB |
1602 | ext, *final_name); |
1603 | } else if (make_read_only_if_same) { | |
1604 | chmod(*final_name, 0444); | |
1605 | } | |
1606 | } | |
1607 | ||
e42797f5 NP |
1608 | static void final(const char *final_pack_name, const char *curr_pack_name, |
1609 | const char *final_index_name, const char *curr_index_name, | |
e37d0b87 | 1610 | const char *final_rev_index_name, const char *curr_rev_index_name, |
88e2f9ed | 1611 | const char *keep_msg, const char *promisor_msg, |
454253f0 | 1612 | unsigned char *hash) |
e42797f5 | 1613 | { |
3a55602e | 1614 | const char *report = "pack"; |
f2075480 JK |
1615 | struct strbuf pack_name = STRBUF_INIT; |
1616 | struct strbuf index_name = STRBUF_INIT; | |
e37d0b87 | 1617 | struct strbuf rev_index_name = STRBUF_INIT; |
e42797f5 NP |
1618 | |
1619 | if (!from_stdin) { | |
1620 | close(input_fd); | |
1621 | } else { | |
020406ea | 1622 | fsync_component_or_die(FSYNC_COMPONENT_PACK, output_fd, curr_pack_name); |
fa6c3833 | 1623 | if (close(output_fd)) |
c2b97ecf | 1624 | die_errno(_("error while closing pack file")); |
e42797f5 NP |
1625 | } |
1626 | ||
8e29c7c3 | 1627 | if (keep_msg) |
0fd90dab | 1628 | write_special_file("keep", keep_msg, final_pack_name, hash, |
8e29c7c3 | 1629 | &report); |
88e2f9ed JT |
1630 | if (promisor_msg) |
1631 | write_special_file("promisor", promisor_msg, final_pack_name, | |
0fd90dab | 1632 | hash, NULL); |
b8077709 | 1633 | |
8737dab3 ÆAB |
1634 | rename_tmp_packfile(&final_pack_name, curr_pack_name, &pack_name, |
1635 | hash, "pack", from_stdin); | |
8737dab3 ÆAB |
1636 | if (curr_rev_index_name) |
1637 | rename_tmp_packfile(&final_rev_index_name, curr_rev_index_name, | |
1638 | &rev_index_name, hash, "rev", 1); | |
522a5c2c TB |
1639 | rename_tmp_packfile(&final_index_name, curr_index_name, &index_name, |
1640 | hash, "idx", 1); | |
e37d0b87 | 1641 | |
368b4e59 JK |
1642 | if (do_fsck_object) { |
1643 | struct packed_git *p; | |
2cf3fe63 KN |
1644 | p = add_packed_git(the_repository, final_index_name, |
1645 | strlen(final_index_name), 0); | |
368b4e59 | 1646 | if (p) |
549ca8aa | 1647 | install_packed_git(the_repository, p); |
368b4e59 | 1648 | } |
73c3f0f7 | 1649 | |
576162a4 | 1650 | if (!from_stdin) { |
69fa3370 | 1651 | printf("%s\n", hash_to_hex(hash)); |
576162a4 | 1652 | } else { |
5b1ef2ce JK |
1653 | struct strbuf buf = STRBUF_INIT; |
1654 | ||
69fa3370 | 1655 | strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash)); |
5b1ef2ce JK |
1656 | write_or_die(1, buf.buf, buf.len); |
1657 | strbuf_release(&buf); | |
576162a4 | 1658 | |
fa6c3833 JH |
1659 | /* Write the last part of the buffer to stdout */ |
1660 | write_in_full(1, input_buffer + input_offset, input_len); | |
576162a4 | 1661 | } |
ba47a308 | 1662 | |
e37d0b87 | 1663 | strbuf_release(&rev_index_name); |
f2075480 JK |
1664 | strbuf_release(&index_name); |
1665 | strbuf_release(&pack_name); | |
9cf6d335 SV |
1666 | } |
1667 | ||
a4e7e317 GC |
1668 | static int git_index_pack_config(const char *k, const char *v, |
1669 | const struct config_context *ctx, void *cb) | |
4d00bda2 | 1670 | { |
ebcfb379 JH |
1671 | struct pack_idx_option *opts = cb; |
1672 | ||
4d00bda2 | 1673 | if (!strcmp(k, "pack.indexversion")) { |
8868b1eb | 1674 | opts->version = git_config_int(k, v, ctx->kvi); |
ebcfb379 | 1675 | if (opts->version > 2) |
b4eda05d | 1676 | die(_("bad pack.indexVersion=%"PRIu32), opts->version); |
4d00bda2 NP |
1677 | return 0; |
1678 | } | |
b8a2486f | 1679 | if (!strcmp(k, "pack.threads")) { |
8868b1eb | 1680 | nr_threads = git_config_int(k, v, ctx->kvi); |
b8a2486f | 1681 | if (nr_threads < 0) |
f350df42 | 1682 | die(_("invalid number of threads specified (%d)"), |
b8a2486f | 1683 | nr_threads); |
2094c5e5 | 1684 | if (!HAVE_THREADS && nr_threads != 1) { |
f350df42 | 1685 | warning(_("no threads support, ignoring %s"), k); |
2094c5e5 NTND |
1686 | nr_threads = 1; |
1687 | } | |
b8a2486f NTND |
1688 | return 0; |
1689 | } | |
e37d0b87 TB |
1690 | if (!strcmp(k, "pack.writereverseindex")) { |
1691 | if (git_config_bool(k, v)) | |
1692 | opts->flags |= WRITE_REV; | |
1693 | else | |
1694 | opts->flags &= ~WRITE_REV; | |
1695 | } | |
d6b2d21f KN |
1696 | if (!strcmp(k, "core.deltabasecachelimit")) { |
1697 | opts->delta_base_cache_limit = git_config_ulong(k, v, ctx->kvi); | |
1698 | return 0; | |
1699 | } | |
a4e7e317 | 1700 | return git_default_config(k, v, ctx, cb); |
4d00bda2 NP |
1701 | } |
1702 | ||
3c9fc074 JH |
1703 | static int cmp_uint32(const void *a_, const void *b_) |
1704 | { | |
1705 | uint32_t a = *((uint32_t *)a_); | |
1706 | uint32_t b = *((uint32_t *)b_); | |
1707 | ||
1708 | return (a < b) ? -1 : (a != b); | |
1709 | } | |
1710 | ||
1711 | static void read_v2_anomalous_offsets(struct packed_git *p, | |
1712 | struct pack_idx_option *opts) | |
1713 | { | |
1714 | const uint32_t *idx1, *idx2; | |
1715 | uint32_t i; | |
1716 | ||
1717 | /* The address of the 4-byte offset table */ | |
629dffc4 | 1718 | idx1 = (((const uint32_t *)((const uint8_t *)p->index_data + p->crc_offset)) |
f86f7695 | 1719 | + (size_t)p->num_objects /* CRC32 table */ |
3c9fc074 JH |
1720 | ); |
1721 | ||
1722 | /* The address of the 8-byte offset table */ | |
1723 | idx2 = idx1 + p->num_objects; | |
1724 | ||
1725 | for (i = 0; i < p->num_objects; i++) { | |
1726 | uint32_t off = ntohl(idx1[i]); | |
1727 | if (!(off & 0x80000000)) | |
1728 | continue; | |
1729 | off = off & 0x7fffffff; | |
47fe3f6e | 1730 | check_pack_index_ptr(p, &idx2[off * 2]); |
3c9fc074 JH |
1731 | if (idx2[off * 2]) |
1732 | continue; | |
1733 | /* | |
1734 | * The real offset is ntohl(idx2[off * 2]) in high 4 | |
1735 | * octets, and ntohl(idx2[off * 2 + 1]) in low 4 | |
1736 | * octets. But idx2[off * 2] is Zero!!! | |
1737 | */ | |
1738 | ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc); | |
1739 | opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]); | |
1740 | } | |
1741 | ||
1b5294de | 1742 | QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32); |
3c9fc074 JH |
1743 | } |
1744 | ||
e337a04d JH |
1745 | static void read_idx_option(struct pack_idx_option *opts, const char *pack_name) |
1746 | { | |
2cf3fe63 KN |
1747 | struct packed_git *p = add_packed_git(the_repository, pack_name, |
1748 | strlen(pack_name), 1); | |
e337a04d JH |
1749 | |
1750 | if (!p) | |
c2b97ecf | 1751 | die(_("Cannot open existing pack file '%s'"), pack_name); |
e337a04d | 1752 | if (open_pack_index(p)) |
c2b97ecf | 1753 | die(_("Cannot open existing pack idx file for '%s'"), pack_name); |
e337a04d JH |
1754 | |
1755 | /* Read the attributes from the existing idx file */ | |
1756 | opts->version = p->index_version; | |
1757 | ||
3c9fc074 JH |
1758 | if (opts->version == 2) |
1759 | read_v2_anomalous_offsets(p, opts); | |
1760 | ||
e337a04d JH |
1761 | /* |
1762 | * Get rid of the idx file as we do not need it anymore. | |
1763 | * NEEDSWORK: extract this bit from free_pack_by_name() in | |
e5afd444 | 1764 | * object-file.c, perhaps? It shouldn't matter very much as we |
e337a04d JH |
1765 | * know we haven't installed this pack (hence we never have |
1766 | * read anything from it). | |
1767 | */ | |
1768 | close_pack_index(p); | |
1769 | free(p); | |
1770 | } | |
1771 | ||
38a45561 JH |
1772 | static void show_pack_info(int stat_only) |
1773 | { | |
c6458e60 | 1774 | int i, baseobjects = nr_objects - nr_ref_deltas - nr_ofs_deltas; |
d1a0ed18 JH |
1775 | unsigned long *chain_histogram = NULL; |
1776 | ||
1777 | if (deepest_delta) | |
ca56dadb | 1778 | CALLOC_ARRAY(chain_histogram, deepest_delta); |
d1a0ed18 | 1779 | |
38a45561 JH |
1780 | for (i = 0; i < nr_objects; i++) { |
1781 | struct object_entry *obj = &objects[i]; | |
1782 | ||
d1a0ed18 | 1783 | if (is_delta_type(obj->type)) |
41730576 | 1784 | chain_histogram[obj_stat[i].delta_depth - 1]++; |
38a45561 JH |
1785 | if (stat_only) |
1786 | continue; | |
ca473cef | 1787 | printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX, |
e6a492b7 | 1788 | oid_to_hex(&obj->idx.oid), |
ca473cef TB |
1789 | type_name(obj->real_type), (uintmax_t)obj->size, |
1790 | (uintmax_t)(obj[1].idx.offset - obj->idx.offset), | |
38a45561 JH |
1791 | (uintmax_t)obj->idx.offset); |
1792 | if (is_delta_type(obj->type)) { | |
41730576 | 1793 | struct object_entry *bobj = &objects[obj_stat[i].base_object_no]; |
e6a492b7 | 1794 | printf(" %u %s", obj_stat[i].delta_depth, |
1795 | oid_to_hex(&bobj->idx.oid)); | |
38a45561 JH |
1796 | } |
1797 | putchar('\n'); | |
1798 | } | |
d1a0ed18 JH |
1799 | |
1800 | if (baseobjects) | |
c2b97ecf NTND |
1801 | printf_ln(Q_("non delta: %d object", |
1802 | "non delta: %d objects", | |
1803 | baseobjects), | |
1804 | baseobjects); | |
d1a0ed18 JH |
1805 | for (i = 0; i < deepest_delta; i++) { |
1806 | if (!chain_histogram[i]) | |
1807 | continue; | |
c2b97ecf NTND |
1808 | printf_ln(Q_("chain length = %d: %lu object", |
1809 | "chain length = %d: %lu objects", | |
1810 | chain_histogram[i]), | |
1811 | i + 1, | |
1812 | chain_histogram[i]); | |
d1a0ed18 | 1813 | } |
f2bcc69e | 1814 | free(chain_histogram); |
38a45561 JH |
1815 | } |
1816 | ||
c08589ef JT |
1817 | static void repack_local_links(void) |
1818 | { | |
1819 | struct child_process cmd = CHILD_PROCESS_INIT; | |
1820 | FILE *out; | |
1821 | struct strbuf line = STRBUF_INIT; | |
1822 | struct oidset_iter iter; | |
1823 | struct object_id *oid; | |
911d1420 | 1824 | char *base_name = NULL; |
c08589ef | 1825 | |
911d1420 | 1826 | if (!oidset_size(&outgoing_links)) |
c08589ef JT |
1827 | return; |
1828 | ||
911d1420 JT |
1829 | oidset_iter_init(&outgoing_links, &iter); |
1830 | while ((oid = oidset_iter_next(&iter))) { | |
1831 | struct object_info info = OBJECT_INFO_INIT; | |
1832 | if (oid_object_info_extended(the_repository, oid, &info, 0)) | |
1833 | /* Missing; assume it is a promisor object */ | |
1834 | continue; | |
1835 | if (info.whence == OI_PACKED && info.u.packed.pack->pack_promisor) | |
1836 | continue; | |
c08589ef | 1837 | |
911d1420 JT |
1838 | if (!cmd.args.nr) { |
1839 | base_name = mkpathdup( | |
1840 | "%s/pack/pack", | |
1841 | repo_get_object_directory(the_repository)); | |
1842 | strvec_push(&cmd.args, "pack-objects"); | |
1843 | strvec_push(&cmd.args, | |
1844 | "--exclude-promisor-objects-best-effort"); | |
1845 | strvec_push(&cmd.args, base_name); | |
1846 | cmd.git_cmd = 1; | |
1847 | cmd.in = -1; | |
1848 | cmd.out = -1; | |
1849 | if (start_command(&cmd)) | |
1850 | die(_("could not start pack-objects to repack local links")); | |
1851 | } | |
c08589ef | 1852 | |
c08589ef JT |
1853 | if (write_in_full(cmd.in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 || |
1854 | write_in_full(cmd.in, "\n", 1) < 0) | |
1855 | die(_("failed to feed local object to pack-objects")); | |
1856 | } | |
911d1420 JT |
1857 | |
1858 | if (!cmd.args.nr) | |
1859 | return; | |
1860 | ||
c08589ef JT |
1861 | close(cmd.in); |
1862 | ||
1863 | out = xfdopen(cmd.out, "r"); | |
1864 | while (strbuf_getline_lf(&line, out) != EOF) { | |
1865 | unsigned char binary[GIT_MAX_RAWSZ]; | |
1866 | if (line.len != the_hash_algo->hexsz || | |
1867 | !hex_to_bytes(binary, line.buf, line.len)) | |
1868 | die(_("index-pack: Expecting full hex object ID lines only from pack-objects.")); | |
1869 | ||
1870 | /* | |
1871 | * pack-objects creates the .pack and .idx files, but not the | |
1872 | * .promisor file. Create the .promisor file, which is empty. | |
1873 | */ | |
1874 | write_special_file("promisor", "", NULL, binary, NULL); | |
1875 | } | |
1876 | ||
1877 | fclose(out); | |
1878 | if (finish_command(&cmd)) | |
1879 | die(_("could not finish pack-objects to repack local links")); | |
1880 | strbuf_release(&line); | |
1881 | free(base_name); | |
1882 | } | |
1883 | ||
9b1cb507 JC |
1884 | int cmd_index_pack(int argc, |
1885 | const char **argv, | |
1886 | const char *prefix, | |
1887 | struct repository *repo UNUSED) | |
9cf6d335 | 1888 | { |
e37d0b87 | 1889 | int i, fix_thin_pack = 0, verify = 0, stat_only = 0, rev_index; |
39539495 | 1890 | const char *curr_index; |
2f0ee051 | 1891 | char *curr_rev_index = NULL; |
e37d0b87 | 1892 | const char *index_name = NULL, *pack_name = NULL, *rev_index_name = NULL; |
8e29c7c3 | 1893 | const char *keep_msg = NULL; |
88e2f9ed | 1894 | const char *promisor_msg = NULL; |
8e29c7c3 | 1895 | struct strbuf index_name_buf = STRBUF_INIT; |
e37d0b87 | 1896 | struct strbuf rev_index_name_buf = STRBUF_INIT; |
aa7e44bf | 1897 | struct pack_idx_entry **idx_objects; |
ebcfb379 | 1898 | struct pack_idx_option opts; |
454253f0 | 1899 | unsigned char pack_hash[GIT_MAX_RAWSZ]; |
c6807a40 | 1900 | unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */ |
83558686 | 1901 | int report_end_of_input = 0; |
586740aa | 1902 | int hash_algo = 0; |
9cf6d335 | 1903 | |
8b4c0103 | 1904 | /* |
8a30a1ef JT |
1905 | * index-pack never needs to fetch missing objects except when |
1906 | * REF_DELTA bases are missing (which are explicitly handled). It only | |
1907 | * accesses the repo to do hash collision checks and to check which | |
1908 | * REF_DELTA bases need to be fetched. | |
8b4c0103 JT |
1909 | */ |
1910 | fetch_if_missing = 0; | |
1911 | ||
f66d1423 | 1912 | show_usage_if_asked(argc, argv, index_pack_usage); |
99caeed0 | 1913 | |
d24eda4e | 1914 | disable_replace_refs(); |
22410549 | 1915 | fsck_options.walk = mark_link; |
6e2a09d2 | 1916 | |
ebcfb379 | 1917 | reset_pack_idx_option(&opts); |
a8dd7e05 | 1918 | opts.flags |= WRITE_REV; |
ebcfb379 | 1919 | git_config(git_index_pack_config, &opts); |
3f8099fc | 1920 | if (prefix && chdir(prefix)) |
c2b97ecf | 1921 | die(_("Cannot come back to cwd")); |
4d00bda2 | 1922 | |
9f7f10a2 TB |
1923 | if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0)) |
1924 | rev_index = 0; | |
e8c58f89 TB |
1925 | else |
1926 | rev_index = !!(opts.flags & (WRITE_REV_VERIFY | WRITE_REV)); | |
e37d0b87 | 1927 | |
9cf6d335 | 1928 | for (i = 1; i < argc; i++) { |
3bb72562 | 1929 | const char *arg = argv[i]; |
9cf6d335 SV |
1930 | |
1931 | if (*arg == '-') { | |
e42797f5 NP |
1932 | if (!strcmp(arg, "--stdin")) { |
1933 | from_stdin = 1; | |
636171cb NP |
1934 | } else if (!strcmp(arg, "--fix-thin")) { |
1935 | fix_thin_pack = 1; | |
72885a6d | 1936 | } else if (skip_to_optional_arg(arg, "--strict", &arg)) { |
5d477a33 JS |
1937 | strict = 1; |
1938 | do_fsck_object = 1; | |
1939 | fsck_set_msg_types(&fsck_options, arg); | |
c6807a40 NTND |
1940 | } else if (!strcmp(arg, "--check-self-contained-and-connected")) { |
1941 | strict = 1; | |
1942 | check_self_contained_and_connected = 1; | |
0f8edf73 | 1943 | } else if (skip_to_optional_arg(arg, "--fsck-objects", &arg)) { |
ffb2c0fe | 1944 | do_fsck_object = 1; |
0f8edf73 | 1945 | fsck_set_msg_types(&fsck_options, arg); |
e337a04d JH |
1946 | } else if (!strcmp(arg, "--verify")) { |
1947 | verify = 1; | |
38a45561 JH |
1948 | } else if (!strcmp(arg, "--verify-stat")) { |
1949 | verify = 1; | |
3aba2fdd | 1950 | show_stat = 1; |
38a45561 JH |
1951 | } else if (!strcmp(arg, "--verify-stat-only")) { |
1952 | verify = 1; | |
3aba2fdd | 1953 | show_stat = 1; |
38a45561 | 1954 | stat_only = 1; |
72885a6d CC |
1955 | } else if (skip_to_optional_arg(arg, "--keep", &keep_msg)) { |
1956 | ; /* nothing to do */ | |
f3d618d2 | 1957 | } else if (skip_to_optional_arg(arg, "--promisor", &promisor_msg)) { |
911d1420 | 1958 | record_outgoing_links = 1; |
59556548 | 1959 | } else if (starts_with(arg, "--threads=")) { |
b8a2486f NTND |
1960 | char *end; |
1961 | nr_threads = strtoul(arg+10, &end, 0); | |
1962 | if (!arg[10] || *end || nr_threads < 0) | |
1963 | usage(index_pack_usage); | |
2094c5e5 NTND |
1964 | if (!HAVE_THREADS && nr_threads != 1) { |
1965 | warning(_("no threads support, ignoring %s"), arg); | |
1966 | nr_threads = 1; | |
1967 | } | |
98046591 JK |
1968 | } else if (skip_prefix(arg, "--pack_header=", &arg)) { |
1969 | if (parse_pack_header_option(arg, | |
798e0f45 JK |
1970 | input_buffer, |
1971 | &input_len) < 0) | |
98046591 | 1972 | die(_("bad --pack_header: %s"), arg); |
3c9af366 NP |
1973 | } else if (!strcmp(arg, "-v")) { |
1974 | verbose = 1; | |
f46c46e4 ÆAB |
1975 | } else if (!strcmp(arg, "--progress-title")) { |
1976 | if (progress_title || (i+1) >= argc) | |
1977 | usage(index_pack_usage); | |
1978 | progress_title = argv[++i]; | |
e376f17f JK |
1979 | } else if (!strcmp(arg, "--show-resolving-progress")) { |
1980 | show_resolving_progress = 1; | |
83558686 JK |
1981 | } else if (!strcmp(arg, "--report-end-of-input")) { |
1982 | report_end_of_input = 1; | |
e42797f5 | 1983 | } else if (!strcmp(arg, "-o")) { |
9cf6d335 SV |
1984 | if (index_name || (i+1) >= argc) |
1985 | usage(index_pack_usage); | |
1986 | index_name = argv[++i]; | |
59556548 | 1987 | } else if (starts_with(arg, "--index-version=")) { |
4ba7d711 | 1988 | char *c; |
ebcfb379 JH |
1989 | opts.version = strtoul(arg + 16, &c, 10); |
1990 | if (opts.version > 2) | |
c2b97ecf | 1991 | die(_("bad %s"), arg); |
4ba7d711 | 1992 | if (*c == ',') |
ebcfb379 JH |
1993 | opts.off32_limit = strtoul(c+1, &c, 0); |
1994 | if (*c || opts.off32_limit & 0x80000000) | |
c2b97ecf | 1995 | die(_("bad %s"), arg); |
411481be JK |
1996 | } else if (skip_prefix(arg, "--max-input-size=", &arg)) { |
1997 | max_input_size = strtoumax(arg, NULL, 10); | |
586740aa | 1998 | } else if (skip_prefix(arg, "--object-format=", &arg)) { |
1999 | hash_algo = hash_algo_by_name(arg); | |
2000 | if (hash_algo == GIT_HASH_UNKNOWN) | |
2001 | die(_("unknown hash algorithm '%s'"), arg); | |
2002 | repo_set_hash_algo(the_repository, hash_algo); | |
e37d0b87 TB |
2003 | } else if (!strcmp(arg, "--rev-index")) { |
2004 | rev_index = 1; | |
2005 | } else if (!strcmp(arg, "--no-rev-index")) { | |
2006 | rev_index = 0; | |
9cf6d335 SV |
2007 | } else |
2008 | usage(index_pack_usage); | |
2009 | continue; | |
2010 | } | |
2011 | ||
2012 | if (pack_name) | |
2013 | usage(index_pack_usage); | |
2014 | pack_name = arg; | |
2015 | } | |
2016 | ||
e42797f5 | 2017 | if (!pack_name && !from_stdin) |
9cf6d335 | 2018 | usage(index_pack_usage); |
636171cb | 2019 | if (fix_thin_pack && !from_stdin) |
6fa00ee8 | 2020 | die(_("the option '%s' requires '%s'"), "--fix-thin", "--stdin"); |
1f2be8be JT |
2021 | if (promisor_msg && pack_name) |
2022 | die(_("--promisor cannot be used with a pack name")); | |
7176a314 JK |
2023 | if (from_stdin && !startup_info->have_repository) |
2024 | die(_("--stdin requires a git repository")); | |
586740aa | 2025 | if (from_stdin && hash_algo) |
12909b6b | 2026 | die(_("options '%s' and '%s' cannot be used together"), "--object-format", "--stdin"); |
bfee614a | 2027 | if (!index_name && pack_name) |
84d54494 | 2028 | index_name = derive_filename(pack_name, "pack", "idx", &index_name_buf); |
bfee614a | 2029 | |
b2dbf97f PS |
2030 | /* |
2031 | * Packfiles and indices do not carry enough information to be able to | |
2032 | * identify their object hash. So when we are neither in a repository | |
2033 | * nor has the user told us which object hash to use we have no other | |
2034 | * choice but to guess the object hash. | |
2035 | */ | |
2036 | if (!the_repository->hash_algo) | |
2037 | repo_set_hash_algo(the_repository, GIT_HASH_SHA1); | |
2038 | ||
e37d0b87 TB |
2039 | opts.flags &= ~(WRITE_REV | WRITE_REV_VERIFY); |
2040 | if (rev_index) { | |
2041 | opts.flags |= verify ? WRITE_REV_VERIFY : WRITE_REV; | |
2042 | if (index_name) | |
2043 | rev_index_name = derive_filename(index_name, | |
2044 | "idx", "rev", | |
2045 | &rev_index_name_buf); | |
2046 | } | |
2047 | ||
e337a04d JH |
2048 | if (verify) { |
2049 | if (!index_name) | |
c2b97ecf | 2050 | die(_("--verify with no packfile name given")); |
e337a04d | 2051 | read_idx_option(&opts, index_name); |
68be2fea | 2052 | opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT; |
e337a04d | 2053 | } |
68be2fea JH |
2054 | if (strict) |
2055 | opts.flags |= WRITE_IDX_STRICT; | |
9cf6d335 | 2056 | |
2094c5e5 | 2057 | if (HAVE_THREADS && !nr_threads) { |
b8a2486f | 2058 | nr_threads = online_cpus(); |
fbff95b6 JK |
2059 | /* |
2060 | * Experiments show that going above 20 threads doesn't help, | |
2061 | * no matter how many cores you have. Below that, we tend to | |
2062 | * max at half the number of online_cpus(), presumably because | |
2063 | * half of those are hyperthreads rather than full cores. We'll | |
2064 | * never reduce the level below "3", though, to match a | |
2065 | * historical value that nobody complained about. | |
2066 | */ | |
2067 | if (nr_threads < 4) | |
2068 | ; /* too few cores to consider capping */ | |
2069 | else if (nr_threads < 6) | |
2070 | nr_threads = 3; /* historic cap */ | |
2071 | else if (nr_threads < 40) | |
2072 | nr_threads /= 2; | |
2073 | else | |
2074 | nr_threads = 20; /* hard cap */ | |
b8a2486f | 2075 | } |
b8a2486f | 2076 | |
e42797f5 | 2077 | curr_pack = open_pack_file(pack_name); |
9cf6d335 | 2078 | parse_pack_header(); |
ca56dadb | 2079 | CALLOC_ARRAY(objects, st_add(nr_objects, 1)); |
41730576 | 2080 | if (show_stat) |
ca56dadb RS |
2081 | CALLOC_ARRAY(obj_stat, st_add(nr_objects, 1)); |
2082 | CALLOC_ARRAY(ofs_deltas, nr_objects); | |
454253f0 | 2083 | parse_pack_objects(pack_hash); |
83558686 JK |
2084 | if (report_end_of_input) |
2085 | write_in_full(2, "\0", 1); | |
d6b2d21f | 2086 | resolve_deltas(&opts); |
454253f0 | 2087 | conclude_pack(fix_thin_pack, curr_pack, pack_hash); |
c6458e60 NTND |
2088 | free(ofs_deltas); |
2089 | free(ref_deltas); | |
0153be05 | 2090 | if (strict) |
c6807a40 | 2091 | foreign_nr = check_objects(); |
aa7e44bf | 2092 | |
3aba2fdd | 2093 | if (show_stat) |
38a45561 JH |
2094 | show_pack_info(stat_only); |
2095 | ||
b32fa95f | 2096 | ALLOC_ARRAY(idx_objects, nr_objects); |
aa7e44bf GB |
2097 | for (i = 0; i < nr_objects; i++) |
2098 | idx_objects[i] = &objects[i].idx; | |
2582846f | 2099 | curr_index = write_idx_file(the_repository, index_name, idx_objects, |
7653e9af | 2100 | nr_objects, &opts, pack_hash); |
e37d0b87 | 2101 | if (rev_index) |
2582846f | 2102 | curr_rev_index = write_rev_file(the_repository, rev_index_name, |
6b2aa7fd KN |
2103 | idx_objects, nr_objects, |
2104 | pack_hash, opts.flags); | |
aa7e44bf GB |
2105 | free(idx_objects); |
2106 | ||
e337a04d JH |
2107 | if (!verify) |
2108 | final(pack_name, curr_pack, | |
2109 | index_name, curr_index, | |
e37d0b87 | 2110 | rev_index_name, curr_rev_index, |
88e2f9ed | 2111 | keep_msg, promisor_msg, |
454253f0 | 2112 | pack_hash); |
e337a04d JH |
2113 | else |
2114 | close(input_fd); | |
73c3f0f7 | 2115 | |
462f5cae ÆAB |
2116 | if (do_fsck_object && fsck_finish(&fsck_options)) |
2117 | die(_("fsck error in pack objects")); | |
73c3f0f7 | 2118 | |
f2bcc69e | 2119 | free(opts.anomaly); |
9cf6d335 | 2120 | free(objects); |
592ce208 | 2121 | strbuf_release(&index_name_buf); |
e37d0b87 | 2122 | strbuf_release(&rev_index_name_buf); |
afe8a907 | 2123 | if (!pack_name) |
3bb72562 | 2124 | free((void *) curr_pack); |
afe8a907 | 2125 | if (!index_name) |
3bb72562 | 2126 | free((void *) curr_index); |
2f0ee051 | 2127 | free(curr_rev_index); |
9cf6d335 | 2128 | |
c08589ef JT |
2129 | repack_local_links(); |
2130 | ||
c6807a40 NTND |
2131 | /* |
2132 | * Let the caller know this pack is not self contained | |
2133 | */ | |
2134 | if (check_self_contained_and_connected && foreign_nr) | |
2135 | return 1; | |
2136 | ||
9cf6d335 SV |
2137 | return 0; |
2138 | } |