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