Raw
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "packfile.h"
7 #include "object-file.h"
8 #include "hash-lookup.h"
9 #include "midx.h"
10 #include "progress.h"
11 #include "trace2.h"
12 #include "run-command.h"
13 #include "chunk-format.h"
14 #include "pack-bitmap.h"
15 #include "refs.h"
16 #include "revision.h"
17 #include "list-objects.h"
18 #include "path.h"
19 #include "pack-revindex.h"
20
21 #define PACK_EXPIRED UINT_MAX
22 #define BITMAP_POS_UNKNOWN (~((uint32_t)0))
23 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
24 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
25 #define NO_PREFERRED_PACK (~((uint32_t)0))
26
27 extern int midx_checksum_valid(struct multi_pack_index *m);
28 extern void clear_midx_files_ext(struct odb_source *source, const char *ext,
29 const char *keep_hash);
30 extern void clear_incremental_midx_files_ext(struct odb_source *source,
31 const char *ext,
32 const struct strvec *keep_hashes);
33 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
34 const char *idx_name);
35
36 static size_t write_midx_header(const struct git_hash_algo *hash_algo,
37 struct hashfile *f, unsigned char num_chunks,
38 uint32_t num_packs, int version)
39 {
40 if (version != MIDX_VERSION_V1 && version != MIDX_VERSION_V2)
41 BUG("unexpected MIDX version: %d", version);
42
43 hashwrite_be32(f, MIDX_SIGNATURE);
44 hashwrite_u8(f, version);
45 hashwrite_u8(f, oid_version(hash_algo));
46 hashwrite_u8(f, num_chunks);
47 hashwrite_u8(f, 0); /* unused */
48 hashwrite_be32(f, num_packs);
49
50 return MIDX_HEADER_SIZE;
51 }
52
53 struct pack_info {
54 uint32_t orig_pack_int_id;
55 char *pack_name;
56 struct packed_git *p;
57
58 uint32_t bitmap_pos;
59 uint32_t bitmap_nr;
60
61 unsigned expired : 1;
62 };
63
64 static void fill_pack_info(struct pack_info *info,
65 struct packed_git *p, const char *pack_name,
66 uint32_t orig_pack_int_id)
67 {
68 memset(info, 0, sizeof(struct pack_info));
69
70 info->orig_pack_int_id = orig_pack_int_id;
71 info->pack_name = xstrdup(pack_name);
72 info->p = p;
73 info->bitmap_pos = BITMAP_POS_UNKNOWN;
74 }
75
76 static int pack_info_compare(const void *_a, const void *_b)
77 {
78 struct pack_info *a = (struct pack_info *)_a;
79 struct pack_info *b = (struct pack_info *)_b;
80 return strcmp(a->pack_name, b->pack_name);
81 }
82
83 static int idx_or_pack_name_cmp(const void *_va, const void *_vb)
84 {
85 const char *pack_name = _va;
86 const struct pack_info *compar = _vb;
87
88 return cmp_idx_or_pack_name(pack_name, compar->pack_name);
89 }
90
91 struct write_midx_context {
92 struct pack_info *info;
93 size_t nr;
94 size_t alloc;
95 struct multi_pack_index *m;
96 struct multi_pack_index *base_midx;
97 struct progress *progress;
98 unsigned pack_paths_checked;
99
100 struct pack_midx_entry *entries;
101 size_t entries_nr;
102
103 uint32_t *pack_perm;
104 uint32_t *pack_order;
105 unsigned large_offsets_needed:1;
106 uint32_t num_large_offsets;
107
108 uint32_t preferred_pack_idx;
109
110 int version; /* must be MIDX_VERSION_V1 or _V2 */
111
112 int incremental;
113 uint32_t num_multi_pack_indexes_before;
114
115 struct multi_pack_index *compact_from;
116 struct multi_pack_index *compact_to;
117 int compact;
118
119 struct string_list *to_include;
120
121 struct repository *repo;
122 struct odb_source *source;
123 };
124
125 static uint32_t midx_pack_perm(struct write_midx_context *ctx,
126 uint32_t orig_pack_int_id)
127 {
128 if (ctx->compact)
129 orig_pack_int_id -= ctx->compact_from->num_packs_in_base;
130 return ctx->pack_perm[orig_pack_int_id];
131 }
132
133 static int should_include_pack(const struct write_midx_context *ctx,
134 const char *file_name)
135 {
136 /*
137 * Note that at most one of ctx->m and ctx->to_include are set,
138 * so we are testing midx_contains_pack() and
139 * string_list_has_string() independently (guarded by the
140 * appropriate NULL checks).
141 *
142 * We could support passing to_include while reusing an existing
143 * MIDX, but don't currently since the reuse process drags
144 * forward all packs from an existing MIDX (without checking
145 * whether or not they appear in the to_include list).
146 *
147 * If we added support for that, these next two conditional
148 * should be performed independently (likely checking
149 * to_include before the existing MIDX).
150 */
151 if (ctx->m && midx_contains_pack(ctx->m, file_name))
152 return 0;
153 else if (ctx->base_midx && midx_contains_pack(ctx->base_midx,
154 file_name))
155 return 0;
156 else if (ctx->to_include &&
157 !string_list_has_string(ctx->to_include, file_name))
158 return 0;
159 return 1;
160 }
161
162 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
163 const char *file_name, void *data)
164 {
165 struct write_midx_context *ctx = data;
166 struct packed_git *p;
167
168 if (ends_with(file_name, ".idx")) {
169 display_progress(ctx->progress, ++ctx->pack_paths_checked);
170
171 if (!should_include_pack(ctx, file_name))
172 return;
173
174 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
175 p = add_packed_git(ctx->repo, full_path, full_path_len, 0);
176 if (!p) {
177 warning(_("failed to add packfile '%s'"),
178 full_path);
179 return;
180 }
181
182 if (open_pack_index(p)) {
183 warning(_("failed to open pack-index '%s'"),
184 full_path);
185 close_pack(p);
186 free(p);
187 return;
188 }
189
190 fill_pack_info(&ctx->info[ctx->nr], p, file_name, ctx->nr);
191 ctx->nr++;
192 }
193 }
194
195 struct pack_midx_entry {
196 struct object_id oid;
197 uint32_t pack_int_id;
198 time_t pack_mtime;
199 uint64_t offset;
200 unsigned preferred : 1;
201 };
202
203 static int midx_oid_compare(const void *_a, const void *_b)
204 {
205 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
206 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
207 int cmp = oidcmp(&a->oid, &b->oid);
208
209 if (cmp)
210 return cmp;
211
212 /* Sort objects in a preferred pack first when multiple copies exist. */
213 if (a->preferred > b->preferred)
214 return -1;
215 if (a->preferred < b->preferred)
216 return 1;
217
218 if (a->pack_mtime > b->pack_mtime)
219 return -1;
220 else if (a->pack_mtime < b->pack_mtime)
221 return 1;
222
223 return a->pack_int_id - b->pack_int_id;
224 }
225
226 static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
227 struct pack_midx_entry *e,
228 uint32_t pos)
229 {
230 if (pos >= m->num_objects + m->num_objects_in_base)
231 return 1;
232
233 nth_midxed_object_oid(&e->oid, m, pos);
234 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
235 e->offset = nth_midxed_offset(m, pos);
236
237 /* consider objects in midx to be from "old" packs */
238 e->pack_mtime = 0;
239 return 0;
240 }
241
242 static void fill_pack_entry(uint32_t pack_int_id,
243 struct packed_git *p,
244 uint32_t cur_object,
245 struct pack_midx_entry *entry,
246 int preferred)
247 {
248 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
249 die(_("failed to locate object %d in packfile"), cur_object);
250
251 entry->pack_int_id = pack_int_id;
252 entry->pack_mtime = p->mtime;
253
254 entry->offset = nth_packed_object_offset(p, cur_object);
255 entry->preferred = !!preferred;
256 }
257
258 struct midx_fanout {
259 struct pack_midx_entry *entries;
260 size_t nr, alloc;
261 };
262
263 static void midx_fanout_grow(struct midx_fanout *fanout, size_t nr)
264 {
265 if (nr < fanout->nr)
266 BUG("negative growth in midx_fanout_grow() (%"PRIuMAX" < %"PRIuMAX")",
267 (uintmax_t)nr, (uintmax_t)fanout->nr);
268 ALLOC_GROW(fanout->entries, nr, fanout->alloc);
269 }
270
271 static void midx_fanout_sort(struct midx_fanout *fanout)
272 {
273 QSORT(fanout->entries, fanout->nr, midx_oid_compare);
274 }
275
276 static void midx_fanout_add_midx_fanout_1(struct midx_fanout *fanout,
277 struct multi_pack_index *m,
278 uint32_t cur_fanout,
279 uint32_t preferred_pack)
280 {
281 uint32_t start = m->num_objects_in_base, end;
282 uint32_t cur_object;
283
284 if (cur_fanout)
285 start += ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
286 end = m->num_objects_in_base + ntohl(m->chunk_oid_fanout[cur_fanout]);
287
288 for (cur_object = start; cur_object < end; cur_object++) {
289 if ((preferred_pack != NO_PREFERRED_PACK) &&
290 (preferred_pack == nth_midxed_pack_int_id(m, cur_object))) {
291 /*
292 * Objects from preferred packs are added
293 * separately.
294 */
295 continue;
296 }
297
298 midx_fanout_grow(fanout, fanout->nr + 1);
299 nth_midxed_pack_midx_entry(m,
300 &fanout->entries[fanout->nr],
301 cur_object);
302 fanout->entries[fanout->nr].preferred = 0;
303 fanout->nr++;
304 }
305 }
306
307 static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
308 struct multi_pack_index *m,
309 uint32_t cur_fanout,
310 uint32_t preferred_pack)
311 {
312 if (m->base_midx)
313 midx_fanout_add_midx_fanout(fanout, m->base_midx, cur_fanout,
314 preferred_pack);
315 midx_fanout_add_midx_fanout_1(fanout, m, cur_fanout, preferred_pack);
316 }
317
318 static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
319 struct pack_info *info,
320 uint32_t cur_pack,
321 int preferred,
322 uint32_t cur_fanout)
323 {
324 struct packed_git *pack = info[cur_pack].p;
325 uint32_t start = 0, end;
326 uint32_t cur_object;
327
328 if (cur_fanout)
329 start = get_pack_fanout(pack, cur_fanout - 1);
330 end = get_pack_fanout(pack, cur_fanout);
331
332 for (cur_object = start; cur_object < end; cur_object++) {
333 midx_fanout_grow(fanout, fanout->nr + 1);
334 fill_pack_entry(cur_pack,
335 info[cur_pack].p,
336 cur_object,
337 &fanout->entries[fanout->nr],
338 preferred);
339 fanout->nr++;
340 }
341 }
342
343 static void midx_fanout_add(struct midx_fanout *fanout,
344 struct write_midx_context *ctx,
345 uint32_t start_pack,
346 uint32_t cur_fanout)
347 {
348 uint32_t cur_pack;
349
350 if (ctx->m && !ctx->incremental)
351 midx_fanout_add_midx_fanout(fanout, ctx->m, cur_fanout,
352 ctx->preferred_pack_idx);
353
354 for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++) {
355 int preferred = cur_pack == ctx->preferred_pack_idx;
356 midx_fanout_add_pack_fanout(fanout, ctx->info, cur_pack,
357 preferred, cur_fanout);
358 }
359
360 if (ctx->preferred_pack_idx != NO_PREFERRED_PACK &&
361 ctx->preferred_pack_idx < start_pack)
362 midx_fanout_add_pack_fanout(fanout, ctx->info,
363 ctx->preferred_pack_idx, 1,
364 cur_fanout);
365 }
366
367 static void midx_fanout_add_compact(struct midx_fanout *fanout,
368 struct write_midx_context *ctx,
369 uint32_t cur_fanout)
370 {
371 struct multi_pack_index *m = ctx->compact_to;
372
373 ASSERT(ctx->compact);
374
375 while (m && m != ctx->compact_from->base_midx) {
376 midx_fanout_add_midx_fanout_1(fanout, m, cur_fanout,
377 NO_PREFERRED_PACK);
378 m = m->base_midx;
379 }
380 }
381
382 /*
383 * It is possible to artificially get into a state where there are many
384 * duplicate copies of objects. That can create high memory pressure if
385 * we are to create a list of all objects before de-duplication. To reduce
386 * this memory pressure without a significant performance drop, automatically
387 * group objects by the first byte of their object id. Use the IDX fanout
388 * tables to group the data, copy to a local array, then sort.
389 *
390 * Copy only the de-duplicated entries (selected by most-recent modified time
391 * of a packfile containing the object).
392 */
393 static void compute_sorted_entries(struct write_midx_context *ctx,
394 uint32_t start_pack)
395 {
396 uint32_t cur_fanout, cur_pack, cur_object;
397 size_t alloc_objects, total_objects = 0;
398 struct midx_fanout fanout = { 0 };
399
400 if (ctx->compact)
401 ASSERT(!start_pack);
402
403 for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++)
404 total_objects = st_add(total_objects,
405 ctx->info[cur_pack].p->num_objects);
406
407 /*
408 * As we de-duplicate by fanout value, we expect the fanout
409 * slices to be evenly distributed, with some noise. Hence,
410 * allocate slightly more than one 256th.
411 */
412 alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16;
413
414 ALLOC_ARRAY(fanout.entries, fanout.alloc);
415 ALLOC_ARRAY(ctx->entries, alloc_objects);
416 ctx->entries_nr = 0;
417
418 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
419 fanout.nr = 0;
420
421 if (ctx->compact)
422 midx_fanout_add_compact(&fanout, ctx, cur_fanout);
423 else
424 midx_fanout_add(&fanout, ctx, start_pack, cur_fanout);
425 midx_fanout_sort(&fanout);
426
427 /*
428 * The batch is now sorted by OID and then mtime (descending).
429 * Take only the first duplicate.
430 */
431 for (cur_object = 0; cur_object < fanout.nr; cur_object++) {
432 if (cur_object && oideq(&fanout.entries[cur_object - 1].oid,
433 &fanout.entries[cur_object].oid))
434 continue;
435 if (ctx->incremental && ctx->base_midx &&
436 midx_has_oid(ctx->base_midx,
437 &fanout.entries[cur_object].oid))
438 continue;
439
440 ALLOC_GROW(ctx->entries, st_add(ctx->entries_nr, 1),
441 alloc_objects);
442 memcpy(&ctx->entries[ctx->entries_nr],
443 &fanout.entries[cur_object],
444 sizeof(struct pack_midx_entry));
445 ctx->entries_nr++;
446 }
447 }
448
449 free(fanout.entries);
450 }
451
452 static int write_midx_pack_names(struct hashfile *f, void *data)
453 {
454 struct write_midx_context *ctx = data;
455 uint32_t i;
456 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
457 size_t written = 0;
458
459 for (i = 0; i < ctx->nr; i++) {
460 size_t writelen;
461
462 if (ctx->info[i].expired)
463 continue;
464
465 if (ctx->version == MIDX_VERSION_V1 &&
466 i && strcmp(ctx->info[i].pack_name,
467 ctx->info[i - 1].pack_name) <= 0)
468 BUG("incorrect pack-file order: %s before %s",
469 ctx->info[i - 1].pack_name,
470 ctx->info[i].pack_name);
471
472 writelen = strlen(ctx->info[i].pack_name) + 1;
473 hashwrite(f, ctx->info[i].pack_name, writelen);
474 written += writelen;
475 }
476
477 /* add padding to be aligned */
478 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
479 if (i < MIDX_CHUNK_ALIGNMENT) {
480 memset(padding, 0, sizeof(padding));
481 hashwrite(f, padding, i);
482 }
483
484 return 0;
485 }
486
487 static int write_midx_bitmapped_packs(struct hashfile *f, void *data)
488 {
489 struct write_midx_context *ctx = data;
490 size_t i;
491
492 for (i = 0; i < ctx->nr; i++) {
493 struct pack_info *pack = &ctx->info[i];
494 if (pack->expired)
495 continue;
496
497 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN && pack->bitmap_nr)
498 BUG("pack '%s' has no bitmap position, but has %d bitmapped object(s)",
499 pack->pack_name, pack->bitmap_nr);
500
501 hashwrite_be32(f, pack->bitmap_pos);
502 hashwrite_be32(f, pack->bitmap_nr);
503 }
504 return 0;
505 }
506
507 static int write_midx_oid_fanout(struct hashfile *f,
508 void *data)
509 {
510 struct write_midx_context *ctx = data;
511 struct pack_midx_entry *list = ctx->entries;
512 struct pack_midx_entry *last = ctx->entries + ctx->entries_nr;
513 uint32_t count = 0;
514 uint32_t i;
515
516 /*
517 * Write the first-level table (the list is sorted,
518 * but we use a 256-entry lookup to be able to avoid
519 * having to do eight extra binary search iterations).
520 */
521 for (i = 0; i < 256; i++) {
522 struct pack_midx_entry *next = list;
523
524 while (next < last && next->oid.hash[0] == i) {
525 count++;
526 next++;
527 }
528
529 hashwrite_be32(f, count);
530 list = next;
531 }
532
533 return 0;
534 }
535
536 static int write_midx_oid_lookup(struct hashfile *f,
537 void *data)
538 {
539 struct write_midx_context *ctx = data;
540 unsigned char hash_len = ctx->repo->hash_algo->rawsz;
541 struct pack_midx_entry *list = ctx->entries;
542 uint32_t i;
543
544 for (i = 0; i < ctx->entries_nr; i++) {
545 struct pack_midx_entry *obj = list++;
546
547 if (i < ctx->entries_nr - 1) {
548 struct pack_midx_entry *next = list;
549 if (oidcmp(&obj->oid, &next->oid) >= 0)
550 BUG("OIDs not in order: %s >= %s",
551 oid_to_hex(&obj->oid),
552 oid_to_hex(&next->oid));
553 }
554
555 hashwrite(f, obj->oid.hash, (int)hash_len);
556 }
557
558 return 0;
559 }
560
561 static int write_midx_object_offsets(struct hashfile *f,
562 void *data)
563 {
564 struct write_midx_context *ctx = data;
565 struct pack_midx_entry *list = ctx->entries;
566 uint32_t i, nr_large_offset = 0;
567
568 for (i = 0; i < ctx->entries_nr; i++) {
569 struct pack_midx_entry *obj = list++;
570
571 if (midx_pack_perm(ctx, obj->pack_int_id) == PACK_EXPIRED)
572 BUG("object %s is in an expired pack with int-id %d",
573 oid_to_hex(&obj->oid),
574 obj->pack_int_id);
575
576 hashwrite_be32(f, midx_pack_perm(ctx, obj->pack_int_id));
577
578 if (ctx->large_offsets_needed && obj->offset >> 31)
579 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
580 else if (!ctx->large_offsets_needed && obj->offset >> 32)
581 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
582 oid_to_hex(&obj->oid),
583 obj->offset);
584 else
585 hashwrite_be32(f, (uint32_t)obj->offset);
586 }
587
588 return 0;
589 }
590
591 static int write_midx_large_offsets(struct hashfile *f,
592 void *data)
593 {
594 struct write_midx_context *ctx = data;
595 struct pack_midx_entry *list = ctx->entries;
596 struct pack_midx_entry *end = ctx->entries + ctx->entries_nr;
597 uint32_t nr_large_offset = ctx->num_large_offsets;
598
599 while (nr_large_offset) {
600 struct pack_midx_entry *obj;
601 uint64_t offset;
602
603 if (list >= end)
604 BUG("too many large-offset objects");
605
606 obj = list++;
607 offset = obj->offset;
608
609 if (!(offset >> 31))
610 continue;
611
612 hashwrite_be64(f, offset);
613
614 nr_large_offset--;
615 }
616
617 return 0;
618 }
619
620 static int write_midx_revindex(struct hashfile *f,
621 void *data)
622 {
623 struct write_midx_context *ctx = data;
624 uint32_t i, nr_base;
625
626 if (ctx->incremental && ctx->base_midx)
627 nr_base = ctx->base_midx->num_objects +
628 ctx->base_midx->num_objects_in_base;
629 else
630 nr_base = 0;
631
632 for (i = 0; i < ctx->entries_nr; i++)
633 hashwrite_be32(f, ctx->pack_order[i] + nr_base);
634
635 return 0;
636 }
637
638 struct midx_pack_order_data {
639 uint32_t nr;
640 uint32_t pack;
641 off_t offset;
642 };
643
644 static int midx_pack_order_cmp(const void *va, const void *vb)
645 {
646 const struct midx_pack_order_data *a = va, *b = vb;
647 if (a->pack < b->pack)
648 return -1;
649 else if (a->pack > b->pack)
650 return 1;
651 else if (a->offset < b->offset)
652 return -1;
653 else if (a->offset > b->offset)
654 return 1;
655 else
656 return 0;
657 }
658
659 static uint32_t *midx_pack_order(struct write_midx_context *ctx)
660 {
661 struct midx_pack_order_data *data;
662 uint32_t *pack_order, base_objects = 0;
663 uint32_t i;
664
665 trace2_region_enter("midx", "midx_pack_order", ctx->repo);
666
667 if (ctx->incremental && ctx->base_midx)
668 base_objects = ctx->base_midx->num_objects +
669 ctx->base_midx->num_objects_in_base;
670
671 ALLOC_ARRAY(pack_order, ctx->entries_nr);
672 ALLOC_ARRAY(data, ctx->entries_nr);
673
674 for (i = 0; i < ctx->entries_nr; i++) {
675 struct pack_midx_entry *e = &ctx->entries[i];
676 data[i].nr = i;
677 data[i].pack = midx_pack_perm(ctx, e->pack_int_id);
678 if (!e->preferred || ctx->compact)
679 data[i].pack |= (1U << 31);
680 data[i].offset = e->offset;
681 }
682
683 QSORT(data, ctx->entries_nr, midx_pack_order_cmp);
684
685 for (i = 0; i < ctx->entries_nr; i++) {
686 struct pack_midx_entry *e = &ctx->entries[data[i].nr];
687 struct pack_info *pack = &ctx->info[midx_pack_perm(ctx, e->pack_int_id)];
688 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
689 pack->bitmap_pos = i + base_objects;
690 pack->bitmap_nr++;
691 pack_order[i] = data[i].nr;
692 }
693 for (i = 0; i < ctx->nr; i++) {
694 struct pack_info *pack = &ctx->info[i];
695 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
696 pack->bitmap_pos = 0;
697 }
698 free(data);
699
700 trace2_region_leave("midx", "midx_pack_order", ctx->repo);
701
702 return pack_order;
703 }
704
705 static void write_midx_reverse_index(struct write_midx_context *ctx,
706 unsigned char *midx_hash)
707 {
708 struct strbuf buf = STRBUF_INIT;
709 char *tmp_file;
710
711 trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
712
713 if (ctx->incremental)
714 get_split_midx_filename_ext(ctx->source, &buf,
715 midx_hash, MIDX_EXT_REV);
716 else
717 get_midx_filename_ext(ctx->source, &buf,
718 midx_hash, MIDX_EXT_REV);
719
720 tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order,
721 ctx->entries_nr, midx_hash, WRITE_REV);
722
723 if (finalize_object_file(ctx->repo, tmp_file, buf.buf))
724 die(_("cannot store reverse index file"));
725
726 strbuf_release(&buf);
727 free(tmp_file);
728
729 trace2_region_leave("midx", "write_midx_reverse_index", ctx->repo);
730 }
731
732 static void prepare_midx_packing_data(struct packing_data *pdata,
733 struct write_midx_context *ctx)
734 {
735 uint32_t i;
736
737 trace2_region_enter("midx", "prepare_midx_packing_data", ctx->repo);
738
739 memset(pdata, 0, sizeof(struct packing_data));
740 prepare_packing_data(ctx->repo, pdata);
741
742 for (i = 0; i < ctx->entries_nr; i++) {
743 uint32_t pos = ctx->pack_order[i];
744 struct pack_midx_entry *from = &ctx->entries[pos];
745 struct object_entry *to = packlist_alloc(pdata, &from->oid);
746
747 oe_set_in_pack(pdata, to,
748 ctx->info[midx_pack_perm(ctx, from->pack_int_id)].p);
749 }
750
751 trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo);
752 }
753
754 static int add_ref_to_pending(const struct reference *ref, void *cb_data)
755 {
756 struct rev_info *revs = (struct rev_info*)cb_data;
757 const struct object_id *maybe_peeled = ref->oid;
758 struct object_id peeled;
759 struct object *object;
760
761 if ((ref->flags & REF_ISSYMREF) && (ref->flags & REF_ISBROKEN)) {
762 warning("symbolic ref is dangling: %s", ref->name);
763 return 0;
764 }
765
766 if (!reference_get_peeled_oid(revs->repo, ref, &peeled))
767 maybe_peeled = &peeled;
768
769 object = parse_object_or_die(revs->repo, maybe_peeled, ref->name);
770 if (object->type != OBJ_COMMIT)
771 return 0;
772
773 add_pending_object(revs, object, "");
774 if (bitmap_is_preferred_refname(revs->repo, ref->name))
775 object->flags |= NEEDS_BITMAP;
776 return 0;
777 }
778
779 struct bitmap_commit_cb {
780 struct commit_stack *commits;
781 struct write_midx_context *ctx;
782 };
783
784 static const struct object_id *bitmap_oid_access(size_t index,
785 const void *_entries)
786 {
787 const struct pack_midx_entry *entries = _entries;
788 return &entries[index].oid;
789 }
790
791 static void bitmap_show_commit(struct commit *commit, void *_data)
792 {
793 struct bitmap_commit_cb *data = _data;
794 int pos = oid_pos(&commit->object.oid, data->ctx->entries,
795 data->ctx->entries_nr,
796 bitmap_oid_access);
797 if (pos < 0)
798 return;
799
800 commit_stack_push(data->commits, commit);
801 }
802
803 static int read_refs_snapshot(const char *refs_snapshot,
804 struct rev_info *revs)
805 {
806 struct strbuf buf = STRBUF_INIT;
807 struct object_id oid;
808 FILE *f = xfopen(refs_snapshot, "r");
809
810 while (strbuf_getline(&buf, f) != EOF) {
811 struct object *object;
812 int preferred = 0;
813 char *hex = buf.buf;
814 const char *end = NULL;
815
816 if (buf.len && *buf.buf == '+') {
817 preferred = 1;
818 hex = &buf.buf[1];
819 }
820
821 if (parse_oid_hex_algop(hex, &oid, &end, revs->repo->hash_algo) < 0)
822 die(_("could not parse line: %s"), buf.buf);
823 if (*end)
824 die(_("malformed line: %s"), buf.buf);
825
826 object = parse_object_or_die(revs->repo, &oid, NULL);
827 if (preferred)
828 object->flags |= NEEDS_BITMAP;
829
830 add_pending_object(revs, object, "");
831 }
832
833 fclose(f);
834 strbuf_release(&buf);
835 return 0;
836 }
837
838 static void find_commits_for_midx_bitmap(struct commit_stack *commits,
839 const char *refs_snapshot,
840 struct write_midx_context *ctx)
841 {
842 struct rev_info revs;
843 struct bitmap_commit_cb cb = { .commits = commits, .ctx = ctx };
844
845 trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo);
846
847 repo_init_revisions(ctx->repo, &revs, NULL);
848 if (refs_snapshot) {
849 read_refs_snapshot(refs_snapshot, &revs);
850 } else {
851 setup_revisions(0, NULL, &revs, NULL);
852 refs_for_each_ref(get_main_ref_store(ctx->repo),
853 add_ref_to_pending, &revs);
854 }
855
856 /*
857 * Skipping promisor objects here is intentional, since it only excludes
858 * them from the list of reachable commits that we want to select from
859 * when computing the selection of MIDX'd commits to receive bitmaps.
860 *
861 * Reachability bitmaps do require that their objects be closed under
862 * reachability, but fetching any objects missing from promisors at this
863 * point is too late. But, if one of those objects can be reached from
864 * an another object that is included in the bitmap, then we will
865 * complain later that we don't have reachability closure (and fail
866 * appropriately).
867 */
868 fetch_if_missing = 0;
869 revs.exclude_promisor_objects = 1;
870
871 if (prepare_revision_walk(&revs))
872 die(_("revision walk setup failed"));
873
874 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
875
876 release_revisions(&revs);
877
878 trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo);
879 }
880
881 static int write_midx_bitmap(struct write_midx_context *ctx,
882 const unsigned char *midx_hash,
883 struct packing_data *pdata,
884 struct commit **commits,
885 uint32_t commits_nr,
886 unsigned flags)
887 {
888 int ret;
889 uint16_t options = 0;
890 struct bitmap_writer writer;
891 struct pack_idx_entry **index;
892 struct strbuf bitmap_name = STRBUF_INIT;
893
894 trace2_region_enter("midx", "write_midx_bitmap", ctx->repo);
895
896 if (ctx->incremental)
897 get_split_midx_filename_ext(ctx->source, &bitmap_name,
898 midx_hash, MIDX_EXT_BITMAP);
899 else
900 get_midx_filename_ext(ctx->source, &bitmap_name,
901 midx_hash, MIDX_EXT_BITMAP);
902
903 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
904 options |= BITMAP_OPT_HASH_CACHE;
905
906 if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE)
907 options |= BITMAP_OPT_LOOKUP_TABLE;
908
909 /*
910 * Build the MIDX-order index based on pdata.objects (which is already
911 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
912 * this order).
913 */
914 ALLOC_ARRAY(index, pdata->nr_objects);
915 for (uint32_t i = 0; i < pdata->nr_objects; i++)
916 index[i] = &pdata->objects[i].idx;
917
918 bitmap_writer_init(&writer, ctx->repo, pdata,
919 ctx->incremental ? ctx->base_midx : NULL);
920 bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
921 bitmap_writer_build_type_index(&writer, index);
922
923 /*
924 * bitmap_writer_finish expects objects in lex order, but pack_order
925 * gives us exactly that. use it directly instead of re-sorting the
926 * array.
927 *
928 * This changes the order of objects in 'index' between
929 * bitmap_writer_build_type_index and bitmap_writer_finish.
930 *
931 * The same re-ordering takes place in the single-pack bitmap code via
932 * write_idx_file(), which is called by finish_tmp_packfile(), which
933 * happens between bitmap_writer_build_type_index() and
934 * bitmap_writer_finish().
935 */
936 for (uint32_t i = 0; i < pdata->nr_objects; i++)
937 index[ctx->pack_order[i]] = &pdata->objects[i].idx;
938
939 bitmap_writer_select_commits(&writer, commits, commits_nr);
940 ret = bitmap_writer_build(&writer);
941 if (ret < 0)
942 goto cleanup;
943
944 bitmap_writer_set_checksum(&writer, midx_hash);
945 bitmap_writer_finish(&writer, index, bitmap_name.buf, options);
946
947 cleanup:
948 free(index);
949 strbuf_release(&bitmap_name);
950 bitmap_writer_free(&writer);
951
952 trace2_region_leave("midx", "write_midx_bitmap", ctx->repo);
953
954 return ret;
955 }
956
957 static int fill_pack_from_midx(struct pack_info *info,
958 struct multi_pack_index *m,
959 uint32_t pack_int_id)
960 {
961 if (prepare_midx_pack(m, pack_int_id))
962 return error(_("could not load pack %d"), pack_int_id);
963
964 fill_pack_info(info,
965 m->packs[pack_int_id - m->num_packs_in_base],
966 m->pack_names[pack_int_id - m->num_packs_in_base],
967 pack_int_id);
968
969 return 0;
970 }
971
972 static int fill_packs_from_midx(struct write_midx_context *ctx)
973 {
974 struct multi_pack_index *m;
975
976 for (m = ctx->m; m; m = m->base_midx) {
977 uint32_t i;
978
979 for (i = m->num_packs_in_base;
980 i < m->num_packs_in_base + m->num_packs; i++) {
981 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
982
983 if (fill_pack_from_midx(&ctx->info[ctx->nr], m, i) < 0)
984 return -1;
985
986 ctx->nr++;
987 }
988 }
989 return 0;
990 }
991
992 static uint32_t compactible_packs_between(const struct multi_pack_index *from,
993 const struct multi_pack_index *to)
994 {
995 uint32_t nr;
996
997 ASSERT(from && to);
998
999 if (unsigned_add_overflows(to->num_packs, to->num_packs_in_base))
1000 die(_("too many packs, unable to compact"));
1001
1002 nr = to->num_packs + to->num_packs_in_base;
1003 if (nr < from->num_packs_in_base)
1004 BUG("unexpected number of packs in base during compaction: "
1005 "%"PRIu32" < %"PRIu32, nr, from->num_packs_in_base);
1006
1007 return nr - from->num_packs_in_base;
1008 }
1009
1010 static int fill_packs_from_midx_range(struct write_midx_context *ctx,
1011 int bitmap_order)
1012 {
1013 struct multi_pack_index *m = ctx->compact_to;
1014 uint32_t packs_nr;
1015
1016 ASSERT(ctx->compact && !ctx->nr);
1017 ASSERT(ctx->compact_from);
1018 ASSERT(ctx->compact_to);
1019
1020 packs_nr = compactible_packs_between(ctx->compact_from,
1021 ctx->compact_to);
1022
1023 ALLOC_GROW(ctx->info, packs_nr, ctx->alloc);
1024
1025 while (m != ctx->compact_from->base_midx) {
1026 uint32_t pack_int_id, preferred_pack_id;
1027 uint32_t i;
1028
1029 if (bitmap_order) {
1030 if (midx_preferred_pack(m, &preferred_pack_id) < 0)
1031 die(_("could not determine preferred pack"));
1032 } else {
1033 preferred_pack_id = m->num_packs_in_base;
1034 }
1035
1036 pack_int_id = m->num_packs_in_base - ctx->compact_from->num_packs_in_base;
1037
1038 if (fill_pack_from_midx(&ctx->info[pack_int_id++], m,
1039 preferred_pack_id) < 0)
1040 return -1;
1041
1042 for (i = m->num_packs_in_base;
1043 i < m->num_packs_in_base + m->num_packs; i++) {
1044 if (preferred_pack_id == i)
1045 continue;
1046
1047 if (fill_pack_from_midx(&ctx->info[pack_int_id++], m,
1048 i) < 0)
1049 return -1;
1050 }
1051
1052 ctx->nr += m->num_packs;
1053 m = m->base_midx;
1054 }
1055
1056 ASSERT(ctx->nr == packs_nr);
1057
1058 return 0;
1059 }
1060
1061 static struct {
1062 const char *non_split;
1063 const char *split;
1064 } midx_exts[] = {
1065 {NULL, MIDX_EXT_MIDX},
1066 {MIDX_EXT_BITMAP, MIDX_EXT_BITMAP},
1067 {MIDX_EXT_REV, MIDX_EXT_REV},
1068 };
1069
1070 static int link_midx_to_chain(struct multi_pack_index *m)
1071 {
1072 struct strbuf from = STRBUF_INIT;
1073 struct strbuf to = STRBUF_INIT;
1074 int ret = 0;
1075 size_t i;
1076
1077 if (!m || m->has_chain) {
1078 /*
1079 * Either no MIDX previously existed, or it was already
1080 * part of a MIDX chain. In both cases, we have nothing
1081 * to link, so return early.
1082 */
1083 goto done;
1084 }
1085
1086 for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
1087 const unsigned char *hash = midx_get_checksum_hash(m);
1088
1089 get_midx_filename_ext(m->source, &from,
1090 hash, midx_exts[i].non_split);
1091 get_split_midx_filename_ext(m->source, &to, hash,
1092 midx_exts[i].split);
1093
1094 if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
1095 ret = error_errno(_("unable to link '%s' to '%s'"),
1096 from.buf, to.buf);
1097 goto done;
1098 }
1099
1100 strbuf_reset(&from);
1101 strbuf_reset(&to);
1102 }
1103
1104 done:
1105 strbuf_release(&from);
1106 strbuf_release(&to);
1107 return ret;
1108 }
1109
1110 static void clear_midx_files(struct odb_source *source,
1111 const struct strvec *hashes, unsigned incremental)
1112 {
1113 /*
1114 * if incremental:
1115 * - remove all non-incremental MIDX files
1116 * - remove any incremental MIDX files not in the current one
1117 *
1118 * if non-incremental:
1119 * - remove all incremental MIDX files
1120 * - remove any non-incremental MIDX files not matching the current
1121 * hash
1122 */
1123 struct strbuf buf = STRBUF_INIT;
1124 const char *exts[] = { MIDX_EXT_BITMAP, MIDX_EXT_REV, MIDX_EXT_MIDX };
1125 uint32_t i;
1126
1127 for (i = 0; i < ARRAY_SIZE(exts); i++) {
1128 clear_incremental_midx_files_ext(source, exts[i], hashes);
1129 if (hashes) {
1130 for (size_t j = 0; j < hashes->nr; j++)
1131 clear_midx_files_ext(source, exts[i],
1132 hashes->v[j]);
1133 }
1134 }
1135
1136 if (incremental)
1137 get_midx_filename(source, &buf);
1138 else
1139 get_midx_chain_filename(source, &buf);
1140
1141 if (unlink(buf.buf) && errno != ENOENT)
1142 die_errno(_("failed to clear multi-pack-index at %s"), buf.buf);
1143
1144 strbuf_release(&buf);
1145 }
1146
1147 static bool midx_needs_update(struct multi_pack_index *midx, struct write_midx_context *ctx)
1148 {
1149 struct strset packs = STRSET_INIT;
1150 struct strbuf buf = STRBUF_INIT;
1151 bool needed = true;
1152
1153 /*
1154 * Ensure that we have a valid checksum before consulting the
1155 * existing MIDX in order to determine if we can avoid an
1156 * update.
1157 *
1158 * This is necessary because the given MIDX is loaded directly
1159 * from the object store (because we still compare our proposed
1160 * update to any on-disk MIDX regardless of whether or not we
1161 * have assigned "ctx.m") and is thus not guaranteed to have a
1162 * valid checksum.
1163 */
1164 if (!midx_checksum_valid(midx))
1165 goto out;
1166
1167 /*
1168 * If the version differs, we need to update.
1169 */
1170 if (midx->version != ctx->version)
1171 goto out;
1172
1173 /*
1174 * Ignore incremental updates for now. The assumption is that any
1175 * incremental update would be either empty (in which case we will bail
1176 * out later) or it would actually cover at least one new pack.
1177 */
1178 if (ctx->incremental)
1179 goto out;
1180
1181 if (ctx->compact)
1182 goto out; /* Compaction always requires an update. */
1183
1184 /*
1185 * Otherwise, we need to verify that the packs covered by the existing
1186 * MIDX match the packs that we already have. The logic to do so is way
1187 * more complicated than it has any right to be. This is because:
1188 *
1189 * - We cannot assume any ordering.
1190 *
1191 * - The MIDX packs may not be loaded at all, and loading them would
1192 * be wasteful. So we need to use the pack names tracked by the
1193 * MIDX itself.
1194 *
1195 * - The MIDX pack names are tracking the ".idx" files, whereas the
1196 * packs themselves are tracking the ".pack" files. So we need to
1197 * strip suffixes.
1198 */
1199 if (ctx->nr != midx->num_packs + midx->num_packs_in_base)
1200 goto out;
1201
1202 for (uint32_t i = 0; i < ctx->nr; i++) {
1203 strbuf_reset(&buf);
1204 strbuf_addstr(&buf, pack_basename(ctx->info[i].p));
1205 strbuf_strip_suffix(&buf, ".pack");
1206
1207 if (!strset_add(&packs, buf.buf))
1208 BUG("same pack added twice?");
1209 }
1210
1211 for (struct multi_pack_index *m = midx; m; m = m->base_midx) {
1212 for (uint32_t i = 0; i < m->num_packs; i++) {
1213 strbuf_reset(&buf);
1214 strbuf_addstr(&buf, m->pack_names[i]);
1215 strbuf_strip_suffix(&buf, ".idx");
1216
1217 if (!strset_contains(&packs, buf.buf))
1218 goto out;
1219 strset_remove(&packs, buf.buf);
1220 }
1221 }
1222
1223 needed = false;
1224
1225 out:
1226 strbuf_release(&buf);
1227 strset_clear(&packs);
1228 return needed;
1229 }
1230
1231 static int midx_hashcmp(const struct multi_pack_index *a,
1232 const struct multi_pack_index *b,
1233 const struct git_hash_algo *algop)
1234 {
1235 return hashcmp(midx_get_checksum_hash(a), midx_get_checksum_hash(b),
1236 algop);
1237 }
1238
1239 struct write_midx_opts {
1240 struct odb_source *source; /* non-optional */
1241
1242 struct string_list *packs_to_include;
1243 struct string_list *packs_to_drop;
1244
1245 struct multi_pack_index *compact_from;
1246 struct multi_pack_index *compact_to;
1247
1248 const char *preferred_pack_name;
1249 const char *refs_snapshot;
1250 const char *incremental_base;
1251 unsigned flags;
1252 };
1253
1254 static int write_midx_internal(struct write_midx_opts *opts)
1255 {
1256 struct repository *r = opts->source->odb->repo;
1257 struct strbuf midx_name = STRBUF_INIT;
1258 unsigned char midx_hash[GIT_MAX_RAWSZ];
1259 uint32_t start_pack;
1260 struct hashfile *f = NULL;
1261 struct lock_file lk = LOCK_INIT;
1262 struct tempfile *incr;
1263 struct write_midx_context ctx = {
1264 .preferred_pack_idx = NO_PREFERRED_PACK,
1265 };
1266 struct multi_pack_index *midx_to_free = NULL;
1267 int bitmapped_packs_concat_len = 0;
1268 int pack_name_concat_len = 0;
1269 int dropped_packs = 0;
1270 int result = -1;
1271 struct strvec keep_hashes = STRVEC_INIT;
1272 struct chunkfile *cf;
1273
1274 trace2_region_enter("midx", "write_midx_internal", r);
1275
1276 ctx.repo = r;
1277 ctx.source = opts->source;
1278
1279 ctx.version = ((opts->flags & MIDX_WRITE_COMPACT)
1280 ? MIDX_VERSION_V2
1281 : MIDX_VERSION_V1);
1282 repo_config_get_int(ctx.repo, "midx.version", &ctx.version);
1283 if (ctx.version != MIDX_VERSION_V1 && ctx.version != MIDX_VERSION_V2)
1284 die(_("unknown MIDX version: %d"), ctx.version);
1285
1286 ctx.incremental = !!(opts->flags & MIDX_WRITE_INCREMENTAL);
1287 ctx.compact = !!(opts->flags & MIDX_WRITE_COMPACT);
1288
1289 if (ctx.compact) {
1290 if (ctx.version != MIDX_VERSION_V2)
1291 die(_("cannot perform MIDX compaction with v1 format"));
1292 if (!opts->compact_from)
1293 BUG("expected non-NULL 'from' MIDX during compaction");
1294 if (!opts->compact_to)
1295 BUG("expected non-NULL 'to' MIDX during compaction");
1296
1297 ctx.compact_from = opts->compact_from;
1298 ctx.compact_to = opts->compact_to;
1299 }
1300
1301 if (ctx.incremental)
1302 strbuf_addf(&midx_name,
1303 "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
1304 opts->source->path);
1305 else
1306 get_midx_filename(opts->source, &midx_name);
1307 if (safe_create_leading_directories(r, midx_name.buf))
1308 die_errno(_("unable to create leading directories of %s"),
1309 midx_name.buf);
1310
1311 if (!opts->packs_to_include || ctx.incremental) {
1312 struct multi_pack_index *m = get_multi_pack_index(opts->source);
1313 if (m && !midx_checksum_valid(m)) {
1314 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1315 m = NULL;
1316 }
1317
1318 if (m) {
1319 /*
1320 * Only reference an existing MIDX when not filtering
1321 * which packs to include, since all packs and objects
1322 * are copied blindly from an existing MIDX if one is
1323 * present.
1324 */
1325 if (ctx.incremental)
1326 ctx.base_midx = m;
1327 if (!opts->packs_to_include)
1328 ctx.m = m;
1329 }
1330 }
1331
1332 /*
1333 * If compacting MIDX layer(s) in the range [from, to], then the
1334 * compacted MIDX will share the same base MIDX as 'from',
1335 * unless a custom --base is specified (see below).
1336 */
1337 if (ctx.compact)
1338 ctx.base_midx = ctx.compact_from->base_midx;
1339
1340 if (opts->incremental_base) {
1341 if (!strcmp(opts->incremental_base, "none")) {
1342 ctx.base_midx = NULL;
1343 } else {
1344 while (ctx.base_midx) {
1345 const char *cmp = midx_get_checksum_hex(ctx.base_midx);
1346 if (!strcmp(opts->incremental_base, cmp))
1347 break;
1348
1349 ctx.base_midx = ctx.base_midx->base_midx;
1350 }
1351
1352 if (!ctx.base_midx) {
1353 error(_("could not find base MIDX '%s'"),
1354 opts->incremental_base);
1355 goto cleanup;
1356 }
1357 }
1358 }
1359
1360 ctx.nr = 0;
1361 ctx.alloc = ctx.m ? ctx.m->num_packs + ctx.m->num_packs_in_base : 16;
1362 ctx.info = NULL;
1363 ALLOC_ARRAY(ctx.info, ctx.alloc);
1364
1365 if (ctx.incremental) {
1366 struct multi_pack_index *m = ctx.base_midx;
1367 while (m) {
1368 if (opts->flags & MIDX_WRITE_BITMAP && load_midx_revindex(m)) {
1369 error(_("could not load reverse index for MIDX %s"),
1370 midx_get_checksum_hex(m));
1371 goto cleanup;
1372 }
1373 ctx.num_multi_pack_indexes_before++;
1374 m = m->base_midx;
1375 }
1376 } else if (ctx.m && !ctx.compact && fill_packs_from_midx(&ctx)) {
1377 goto cleanup;
1378 }
1379
1380 start_pack = ctx.nr;
1381
1382 ctx.pack_paths_checked = 0;
1383 if (opts->flags & MIDX_PROGRESS)
1384 ctx.progress = start_delayed_progress(r,
1385 _("Adding packfiles to multi-pack-index"), 0);
1386 else
1387 ctx.progress = NULL;
1388
1389 if (ctx.compact) {
1390 int bitmap_order = 0;
1391 if (opts->preferred_pack_name)
1392 bitmap_order |= 1;
1393 else if (opts->flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))
1394 bitmap_order |= 1;
1395
1396 fill_packs_from_midx_range(&ctx, bitmap_order);
1397 } else {
1398 ctx.to_include = opts->packs_to_include;
1399 for_each_file_in_pack_dir(opts->source->path, add_pack_to_midx, &ctx);
1400 }
1401 stop_progress(&ctx.progress);
1402
1403 if (!opts->packs_to_drop) {
1404 /*
1405 * If there is no MIDX then either it doesn't exist, or we're
1406 * doing a geometric repack. Try to load it from the source to
1407 * tell these two cases apart.
1408 */
1409 struct multi_pack_index *midx = ctx.m;
1410 if (!midx)
1411 midx = midx_to_free = load_multi_pack_index(ctx.source);
1412
1413 if (midx && !midx_needs_update(midx, &ctx)) {
1414 struct bitmap_index *bitmap_git;
1415 int bitmap_exists;
1416 int want_bitmap = opts->flags & MIDX_WRITE_BITMAP;
1417
1418 bitmap_git = prepare_midx_bitmap_git(midx);
1419 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
1420 free_bitmap_index(bitmap_git);
1421
1422 if (bitmap_exists || !want_bitmap) {
1423 /*
1424 * The correct MIDX already exists, and so does a
1425 * corresponding bitmap (or one wasn't requested).
1426 */
1427 if (!want_bitmap)
1428 clear_midx_files_ext(ctx.source, "bitmap", NULL);
1429 result = 0;
1430 goto cleanup;
1431 }
1432 }
1433
1434 close_midx(midx_to_free);
1435 midx_to_free = NULL;
1436 }
1437
1438 if (ctx.incremental && !ctx.nr) {
1439 result = 0;
1440 goto cleanup; /* nothing to do */
1441 }
1442
1443 if (opts->preferred_pack_name) {
1444 ctx.preferred_pack_idx = NO_PREFERRED_PACK;
1445
1446 for (size_t i = 0; i < ctx.nr; i++) {
1447 if (!cmp_idx_or_pack_name(opts->preferred_pack_name,
1448 ctx.info[i].pack_name)) {
1449 ctx.preferred_pack_idx = i;
1450 break;
1451 }
1452 }
1453
1454 if (ctx.preferred_pack_idx == NO_PREFERRED_PACK)
1455 warning(_("unknown preferred pack: '%s'"),
1456 opts->preferred_pack_name);
1457 } else if (ctx.nr &&
1458 (opts->flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1459 struct packed_git *oldest = ctx.info[0].p;
1460 ctx.preferred_pack_idx = 0;
1461
1462 /*
1463 * Attempt opening the pack index to populate num_objects.
1464 * Ignore failures as they can be expected and are not
1465 * fatal during this selection time.
1466 */
1467 open_pack_index(oldest);
1468
1469 if (opts->packs_to_drop && opts->packs_to_drop->nr)
1470 BUG("cannot write a MIDX bitmap during expiration");
1471
1472 /*
1473 * set a preferred pack when writing a bitmap to ensure that
1474 * the pack from which the first object is selected in pseudo
1475 * pack-order has all of its objects selected from that pack
1476 * (and not another pack containing a duplicate)
1477 */
1478 for (size_t i = 1; i < ctx.nr; i++) {
1479 struct packed_git *p = ctx.info[i].p;
1480
1481 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1482 oldest = p;
1483 open_pack_index(oldest);
1484 ctx.preferred_pack_idx = i;
1485 }
1486 }
1487
1488 if (!oldest->num_objects) {
1489 /*
1490 * If all packs are empty; unset the preferred index.
1491 * This is acceptable since there will be no duplicate
1492 * objects to resolve, so the preferred value doesn't
1493 * matter.
1494 */
1495 ctx.preferred_pack_idx = NO_PREFERRED_PACK;
1496 }
1497 } else {
1498 /*
1499 * otherwise don't mark any pack as preferred to avoid
1500 * interfering with expiration logic below
1501 */
1502 ctx.preferred_pack_idx = NO_PREFERRED_PACK;
1503 }
1504
1505 if (ctx.preferred_pack_idx != NO_PREFERRED_PACK) {
1506 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1507
1508 if (open_pack_index(preferred))
1509 die(_("failed to open preferred pack %s"),
1510 ctx.info[ctx.preferred_pack_idx].pack_name);
1511
1512 if (!preferred->num_objects) {
1513 error(_("cannot select preferred pack %s with no objects"),
1514 preferred->pack_name);
1515 goto cleanup;
1516 }
1517 }
1518
1519 compute_sorted_entries(&ctx, start_pack);
1520
1521 ctx.large_offsets_needed = 0;
1522 for (size_t i = 0; i < ctx.entries_nr; i++) {
1523 if (ctx.entries[i].offset > 0x7fffffff)
1524 ctx.num_large_offsets++;
1525 if (ctx.entries[i].offset > 0xffffffff)
1526 ctx.large_offsets_needed = 1;
1527 }
1528
1529 if (ctx.compact) {
1530 if (ctx.version != MIDX_VERSION_V2)
1531 BUG("performing MIDX compaction with v1 MIDX");
1532 } else {
1533 QSORT(ctx.info, ctx.nr, pack_info_compare);
1534 }
1535
1536 if (opts->packs_to_drop && opts->packs_to_drop->nr) {
1537 size_t drop_index = 0;
1538 int missing_drops = 0;
1539
1540 ASSERT(!ctx.compact);
1541
1542 for (size_t i = 0;
1543 i < ctx.nr && drop_index < opts->packs_to_drop->nr; i++) {
1544 int cmp = strcmp(ctx.info[i].pack_name,
1545 opts->packs_to_drop->items[drop_index].string);
1546
1547 if (!cmp) {
1548 drop_index++;
1549 ctx.info[i].expired = 1;
1550 } else if (cmp > 0) {
1551 error(_("did not see pack-file %s to drop"),
1552 opts->packs_to_drop->items[drop_index].string);
1553 drop_index++;
1554 missing_drops++;
1555 i--;
1556 } else {
1557 ctx.info[i].expired = 0;
1558 }
1559 }
1560
1561 if (missing_drops)
1562 goto cleanup;
1563 }
1564
1565 /*
1566 * pack_perm stores a permutation between pack-int-ids from the
1567 * previous multi-pack-index to the new one we are writing:
1568 *
1569 * pack_perm[old_id] = new_id
1570 */
1571 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1572 for (size_t i = 0; i < ctx.nr; i++) {
1573 uint32_t from = ctx.info[i].orig_pack_int_id;
1574 uint32_t to;
1575
1576 if (ctx.info[i].expired) {
1577 to = PACK_EXPIRED;
1578 dropped_packs++;
1579 } else {
1580 to = i - dropped_packs;
1581 }
1582
1583 if (ctx.compact)
1584 from -= ctx.compact_from->num_packs_in_base;
1585
1586 ctx.pack_perm[from] = to;
1587 }
1588
1589 for (size_t i = 0; i < ctx.nr; i++) {
1590 if (ctx.info[i].expired)
1591 continue;
1592 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
1593 bitmapped_packs_concat_len += 2 * sizeof(uint32_t);
1594 }
1595
1596 /* Check that the preferred pack wasn't expired (if given). */
1597 if (opts->preferred_pack_name) {
1598 struct pack_info *preferred = bsearch(opts->preferred_pack_name,
1599 ctx.info, ctx.nr,
1600 sizeof(*ctx.info),
1601 idx_or_pack_name_cmp);
1602 if (preferred) {
1603 uint32_t perm = midx_pack_perm(&ctx, preferred->orig_pack_int_id);
1604 if (perm == PACK_EXPIRED)
1605 warning(_("preferred pack '%s' is expired"),
1606 opts->preferred_pack_name);
1607 }
1608 }
1609
1610 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1611 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1612 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1613
1614 if (ctx.nr - dropped_packs == 0) {
1615 error(_("no pack files to index."));
1616 goto cleanup;
1617 }
1618
1619 if (!ctx.entries_nr) {
1620 if (opts->flags & MIDX_WRITE_BITMAP)
1621 warning(_("refusing to write multi-pack .bitmap without any objects"));
1622 opts->flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1623 }
1624
1625 if (ctx.incremental) {
1626 if (!(opts->flags & MIDX_WRITE_NO_CHAIN)) {
1627 struct strbuf lock_name = STRBUF_INIT;
1628
1629 get_midx_chain_filename(opts->source, &lock_name);
1630 hold_lock_file_for_update(&lk, lock_name.buf,
1631 LOCK_DIE_ON_ERROR);
1632 strbuf_release(&lock_name);
1633 }
1634
1635 incr = mks_tempfile_m(midx_name.buf, 0444);
1636 if (!incr) {
1637 error(_("unable to create temporary MIDX layer"));
1638 goto cleanup;
1639 }
1640
1641 if (adjust_shared_perm(r, get_tempfile_path(incr))) {
1642 error(_("unable to adjust shared permissions for '%s'"),
1643 get_tempfile_path(incr));
1644 goto cleanup;
1645 }
1646
1647 f = hashfd(r->hash_algo, get_tempfile_fd(incr),
1648 get_tempfile_path(incr));
1649 } else {
1650 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
1651 f = hashfd(r->hash_algo, get_lock_file_fd(&lk),
1652 get_lock_file_path(&lk));
1653 }
1654
1655 cf = init_chunkfile(f);
1656
1657 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1658 write_midx_pack_names);
1659 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1660 write_midx_oid_fanout);
1661 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
1662 st_mult(ctx.entries_nr, r->hash_algo->rawsz),
1663 write_midx_oid_lookup);
1664 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
1665 st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
1666 write_midx_object_offsets);
1667
1668 if (ctx.large_offsets_needed)
1669 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
1670 st_mult(ctx.num_large_offsets,
1671 MIDX_CHUNK_LARGE_OFFSET_WIDTH),
1672 write_midx_large_offsets);
1673
1674 if (opts->flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) {
1675 ctx.pack_order = midx_pack_order(&ctx);
1676 add_chunk(cf, MIDX_CHUNKID_REVINDEX,
1677 st_mult(ctx.entries_nr, sizeof(uint32_t)),
1678 write_midx_revindex);
1679 add_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
1680 bitmapped_packs_concat_len,
1681 write_midx_bitmapped_packs);
1682 }
1683
1684 write_midx_header(r->hash_algo, f, get_num_chunks(cf),
1685 ctx.nr - dropped_packs, ctx.version);
1686 write_chunkfile(cf, &ctx);
1687
1688 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
1689 CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1690 free_chunkfile(cf);
1691
1692 if (opts->flags & MIDX_WRITE_REV_INDEX &&
1693 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1694 write_midx_reverse_index(&ctx, midx_hash);
1695
1696 if (opts->flags & MIDX_WRITE_BITMAP) {
1697 struct packing_data pdata;
1698 struct commit_stack commits = COMMIT_STACK_INIT;
1699
1700 if (!ctx.entries_nr)
1701 BUG("cannot write a bitmap without any objects");
1702
1703 prepare_midx_packing_data(&pdata, &ctx);
1704
1705 find_commits_for_midx_bitmap(&commits, opts->refs_snapshot, &ctx);
1706
1707 /*
1708 * The previous steps translated the information from
1709 * 'entries' into information suitable for constructing
1710 * bitmaps. We no longer need that array, so clear it to
1711 * reduce memory pressure.
1712 */
1713 FREE_AND_NULL(ctx.entries);
1714 ctx.entries_nr = 0;
1715
1716 if (write_midx_bitmap(&ctx, midx_hash, &pdata, commits.items,
1717 commits.nr, opts->flags) < 0) {
1718 error(_("could not write multi-pack bitmap"));
1719 clear_packing_data(&pdata);
1720 commit_stack_clear(&commits);
1721 goto cleanup;
1722 }
1723
1724 clear_packing_data(&pdata);
1725 commit_stack_clear(&commits);
1726 }
1727 /*
1728 * NOTE: Do not use ctx.entries beyond this point, since it might
1729 * have been freed in the previous if block.
1730 */
1731
1732 if (ctx.num_multi_pack_indexes_before == UINT32_MAX)
1733 die(_("too many multi-pack-indexes"));
1734
1735 if (!is_lock_file_locked(&lk))
1736 printf("%s\n", hash_to_hex_algop(midx_hash, r->hash_algo));
1737 else if (opts->flags & MIDX_WRITE_NO_CHAIN)
1738 BUG("lockfile held with MIDX_WRITE_NO_CHAIN set?");
1739
1740 if (ctx.incremental) {
1741 struct strbuf final_midx_name = STRBUF_INIT;
1742 struct multi_pack_index *m = ctx.base_midx;
1743 struct multi_pack_index **layers = NULL;
1744 size_t layers_nr = 0, layers_alloc = 0;
1745
1746 if (is_lock_file_locked(&lk)){
1747 FILE *chainf = fdopen_lock_file(&lk, "w");
1748 if (!chainf) {
1749 error_errno(_("unable to open multi-pack-index chain file"));
1750 goto cleanup;
1751 }
1752 }
1753
1754 if (link_midx_to_chain(ctx.base_midx) < 0)
1755 goto cleanup;
1756
1757 get_split_midx_filename_ext(opts->source, &final_midx_name,
1758 midx_hash, MIDX_EXT_MIDX);
1759
1760 if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
1761 error_errno(_("unable to rename new multi-pack-index layer"));
1762 goto cleanup;
1763 }
1764
1765 strbuf_release(&final_midx_name);
1766
1767 if (ctx.compact) {
1768 struct multi_pack_index *mp;
1769
1770 for (mp = ctx.base_midx; mp; mp = mp->base_midx) {
1771 ALLOC_GROW(layers, layers_nr + 1, layers_alloc);
1772 layers[layers_nr++] = mp;
1773 }
1774 while (layers_nr)
1775 strvec_push(&keep_hashes,
1776 midx_get_checksum_hex(layers[--layers_nr]));
1777
1778 strvec_push(&keep_hashes,
1779 hash_to_hex_algop(midx_hash,
1780 r->hash_algo));
1781
1782 for (mp = ctx.m;
1783 mp && midx_hashcmp(mp, ctx.compact_to,
1784 r->hash_algo);
1785 mp = mp->base_midx) {
1786 ALLOC_GROW(layers, layers_nr + 1, layers_alloc);
1787 layers[layers_nr++] = mp;
1788 }
1789 while (layers_nr)
1790 strvec_push(&keep_hashes,
1791 midx_get_checksum_hex(layers[--layers_nr]));
1792 } else {
1793 for (; m; m = m->base_midx) {
1794 ALLOC_GROW(layers, layers_nr + 1, layers_alloc);
1795 layers[layers_nr++] = m;
1796 }
1797 while (layers_nr)
1798 strvec_push(&keep_hashes,
1799 midx_get_checksum_hex(layers[--layers_nr]));
1800
1801 strvec_push(&keep_hashes,
1802 hash_to_hex_algop(midx_hash,
1803 r->hash_algo));
1804 }
1805
1806 free(layers);
1807
1808 if (is_lock_file_locked(&lk))
1809 for (size_t i = 0; i < keep_hashes.nr; i++)
1810 fprintf(get_lock_file_fp(&lk), "%s\n",
1811 keep_hashes.v[i]);
1812 } else {
1813 strvec_push(&keep_hashes,
1814 hash_to_hex_algop(midx_hash, r->hash_algo));
1815 }
1816
1817 if (ctx.m || ctx.base_midx)
1818 odb_close(ctx.repo->objects);
1819
1820 if (is_lock_file_locked(&lk)) {
1821 if (commit_lock_file(&lk) < 0)
1822 die_errno(_("could not write multi-pack-index"));
1823
1824 clear_midx_files(opts->source, &keep_hashes, ctx.incremental);
1825 }
1826 result = 0;
1827
1828 cleanup:
1829 for (size_t i = 0; i < ctx.nr; i++) {
1830 if (ctx.info[i].p) {
1831 close_pack(ctx.info[i].p);
1832 free(ctx.info[i].p);
1833 }
1834 free(ctx.info[i].pack_name);
1835 }
1836
1837 free(ctx.info);
1838 free(ctx.entries);
1839 free(ctx.pack_perm);
1840 free(ctx.pack_order);
1841 strvec_clear(&keep_hashes);
1842 strbuf_release(&midx_name);
1843 close_midx(midx_to_free);
1844
1845 trace2_region_leave("midx", "write_midx_internal", r);
1846
1847 return result;
1848 }
1849
1850 int write_midx_file(struct odb_source *source,
1851 const char *preferred_pack_name,
1852 const char *refs_snapshot,
1853 unsigned flags)
1854 {
1855 struct write_midx_opts opts = {
1856 .source = source,
1857 .preferred_pack_name = preferred_pack_name,
1858 .refs_snapshot = refs_snapshot,
1859 .flags = flags,
1860 };
1861
1862 return write_midx_internal(&opts);
1863 }
1864
1865 int write_midx_file_only(struct odb_source *source,
1866 struct string_list *packs_to_include,
1867 const char *preferred_pack_name,
1868 const char *refs_snapshot,
1869 const char *incremental_base,
1870 unsigned flags)
1871 {
1872 struct write_midx_opts opts = {
1873 .source = source,
1874 .packs_to_include = packs_to_include,
1875 .preferred_pack_name = preferred_pack_name,
1876 .refs_snapshot = refs_snapshot,
1877 .incremental_base = incremental_base,
1878 .flags = flags,
1879 };
1880
1881 return write_midx_internal(&opts);
1882 }
1883
1884 int write_midx_file_compact(struct odb_source *source,
1885 struct multi_pack_index *from,
1886 struct multi_pack_index *to,
1887 const char *incremental_base,
1888 unsigned flags)
1889 {
1890 struct write_midx_opts opts = {
1891 .source = source,
1892 .compact_from = from,
1893 .compact_to = to,
1894 .incremental_base = incremental_base,
1895 .flags = flags | MIDX_WRITE_COMPACT,
1896 };
1897
1898 return write_midx_internal(&opts);
1899 }
1900
1901 int expire_midx_packs(struct odb_source *source, unsigned flags)
1902 {
1903 uint32_t i, *count, result = 0;
1904 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1905 struct multi_pack_index *m = get_multi_pack_index(source);
1906 struct progress *progress = NULL;
1907
1908 if (!m)
1909 return 0;
1910
1911 if (m->base_midx)
1912 die(_("cannot expire packs from an incremental multi-pack-index"));
1913
1914 CALLOC_ARRAY(count, m->num_packs);
1915
1916 if (flags & MIDX_PROGRESS)
1917 progress = start_delayed_progress(
1918 source->odb->repo,
1919 _("Counting referenced objects"),
1920 m->num_objects);
1921 for (i = 0; i < m->num_objects; i++) {
1922 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1923 count[pack_int_id]++;
1924 display_progress(progress, i + 1);
1925 }
1926 stop_progress(&progress);
1927
1928 if (flags & MIDX_PROGRESS)
1929 progress = start_delayed_progress(
1930 source->odb->repo,
1931 _("Finding and deleting unreferenced packfiles"),
1932 m->num_packs);
1933 for (i = 0; i < m->num_packs; i++) {
1934 char *pack_name;
1935 display_progress(progress, i + 1);
1936
1937 if (count[i])
1938 continue;
1939
1940 if (prepare_midx_pack(m, i))
1941 continue;
1942
1943 if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
1944 continue;
1945
1946 pack_name = xstrdup(m->packs[i]->pack_name);
1947 close_pack(m->packs[i]);
1948
1949 string_list_insert(&packs_to_drop, m->pack_names[i]);
1950 unlink_pack_path(pack_name, 0);
1951 free(pack_name);
1952 }
1953 stop_progress(&progress);
1954
1955 free(count);
1956
1957 if (packs_to_drop.nr) {
1958 struct write_midx_opts opts = {
1959 .source = source,
1960 .packs_to_drop = &packs_to_drop,
1961 .flags = flags & MIDX_PROGRESS,
1962 };
1963 result = write_midx_internal(&opts);
1964 }
1965
1966 string_list_clear(&packs_to_drop, 0);
1967
1968 return result;
1969 }
1970
1971 struct repack_info {
1972 timestamp_t mtime;
1973 uint32_t referenced_objects;
1974 uint32_t pack_int_id;
1975 };
1976
1977 static int compare_by_mtime(const void *a_, const void *b_)
1978 {
1979 const struct repack_info *a, *b;
1980
1981 a = (const struct repack_info *)a_;
1982 b = (const struct repack_info *)b_;
1983
1984 if (a->mtime < b->mtime)
1985 return -1;
1986 if (a->mtime > b->mtime)
1987 return 1;
1988 return 0;
1989 }
1990
1991 static int want_included_pack(struct multi_pack_index *m,
1992 int pack_kept_objects,
1993 uint32_t pack_int_id)
1994 {
1995 struct packed_git *p;
1996 if (prepare_midx_pack(m, pack_int_id))
1997 return 0;
1998 p = m->packs[pack_int_id];
1999 if (!pack_kept_objects && p->pack_keep)
2000 return 0;
2001 if (p->is_cruft)
2002 return 0;
2003 if (open_pack_index(p) || !p->num_objects)
2004 return 0;
2005 return 1;
2006 }
2007
2008 static void fill_included_packs_all(struct repository *r,
2009 struct multi_pack_index *m,
2010 unsigned char *include_pack)
2011 {
2012 uint32_t i;
2013 int pack_kept_objects = 0;
2014
2015 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
2016
2017 for (i = 0; i < m->num_packs; i++) {
2018 if (!want_included_pack(m, pack_kept_objects, i))
2019 continue;
2020
2021 include_pack[i] = 1;
2022 }
2023 }
2024
2025 static void fill_included_packs_batch(struct repository *r,
2026 struct multi_pack_index *m,
2027 unsigned char *include_pack,
2028 size_t batch_size)
2029 {
2030 uint32_t i;
2031 size_t total_size;
2032 struct repack_info *pack_info;
2033 int pack_kept_objects = 0;
2034
2035 CALLOC_ARRAY(pack_info, m->num_packs);
2036
2037 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
2038
2039 for (i = 0; i < m->num_packs; i++) {
2040 pack_info[i].pack_int_id = i;
2041
2042 if (prepare_midx_pack(m, i))
2043 continue;
2044
2045 pack_info[i].mtime = m->packs[i]->mtime;
2046 }
2047
2048 for (i = 0; i < m->num_objects; i++) {
2049 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
2050 pack_info[pack_int_id].referenced_objects++;
2051 }
2052
2053 QSORT(pack_info, m->num_packs, compare_by_mtime);
2054
2055 total_size = 0;
2056 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
2057 uint32_t pack_int_id = pack_info[i].pack_int_id;
2058 struct packed_git *p = m->packs[pack_int_id];
2059 uint64_t expected_size;
2060
2061 if (!want_included_pack(m, pack_kept_objects, pack_int_id))
2062 continue;
2063
2064 /*
2065 * Use shifted integer arithmetic to calculate the
2066 * expected pack size to ~4 significant digits without
2067 * overflow for packsizes less that 1PB.
2068 */
2069 expected_size = (uint64_t)pack_info[i].referenced_objects << 14;
2070 expected_size /= p->num_objects;
2071 expected_size = u64_mult(expected_size, p->pack_size);
2072 expected_size = u64_add(expected_size, 1u << 13) >> 14;
2073
2074 if (expected_size >= batch_size)
2075 continue;
2076
2077 if (unsigned_add_overflows(total_size, (size_t)expected_size))
2078 total_size = SIZE_MAX;
2079 else
2080 total_size += expected_size;
2081
2082 include_pack[pack_int_id] = 1;
2083 }
2084
2085 free(pack_info);
2086 }
2087
2088 int midx_repack(struct odb_source *source, size_t batch_size, unsigned flags)
2089 {
2090 struct repository *r = source->odb->repo;
2091 int result = 0;
2092 uint32_t i, packs_to_repack = 0;
2093 unsigned char *include_pack;
2094 struct child_process cmd = CHILD_PROCESS_INIT;
2095 FILE *cmd_in;
2096 struct multi_pack_index *m = get_multi_pack_index(source);
2097 struct write_midx_opts opts = {
2098 .source = source,
2099 .flags = flags,
2100 };
2101
2102 /*
2103 * When updating the default for these configuration
2104 * variables in builtin/repack.c, these must be adjusted
2105 * to match.
2106 */
2107 int delta_base_offset = 1;
2108 int use_delta_islands = 0;
2109
2110 if (!m)
2111 return 0;
2112 if (m->base_midx)
2113 die(_("cannot repack an incremental multi-pack-index"));
2114
2115 CALLOC_ARRAY(include_pack, m->num_packs);
2116
2117 if (batch_size)
2118 fill_included_packs_batch(r, m, include_pack, batch_size);
2119 else
2120 fill_included_packs_all(r, m, include_pack);
2121
2122 for (i = 0; i < m->num_packs; i++) {
2123 if (include_pack[i])
2124 packs_to_repack++;
2125 }
2126 if (packs_to_repack <= 1)
2127 goto cleanup;
2128
2129 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
2130 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
2131
2132 strvec_push(&cmd.args, "pack-objects");
2133
2134 strvec_pushf(&cmd.args, "%s/pack/pack", source->path);
2135
2136 if (delta_base_offset)
2137 strvec_push(&cmd.args, "--delta-base-offset");
2138 if (use_delta_islands)
2139 strvec_push(&cmd.args, "--delta-islands");
2140
2141 if (flags & MIDX_PROGRESS)
2142 strvec_push(&cmd.args, "--progress");
2143 else
2144 strvec_push(&cmd.args, "-q");
2145
2146 cmd.git_cmd = 1;
2147 cmd.in = cmd.out = -1;
2148
2149 if (start_command(&cmd)) {
2150 error(_("could not start pack-objects"));
2151 result = 1;
2152 goto cleanup;
2153 }
2154
2155 cmd_in = xfdopen(cmd.in, "w");
2156
2157 for (i = 0; i < m->num_objects; i++) {
2158 struct object_id oid;
2159 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
2160
2161 if (!include_pack[pack_int_id])
2162 continue;
2163
2164 nth_midxed_object_oid(&oid, m, i);
2165 fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
2166 }
2167 fclose(cmd_in);
2168
2169 if (finish_command(&cmd)) {
2170 error(_("could not finish pack-objects"));
2171 result = 1;
2172 goto cleanup;
2173 }
2174
2175 result = write_midx_internal(&opts);
2176
2177 cleanup:
2178 free(include_pack);
2179 return result;
2180 }