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