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