t0410: make test description clearer
[git/gitster.git] / midx-write.c
blobb3a5f6c51669349dc26edf94b5c25b8fea033322
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "abspath.h"
5 #include "config.h"
6 #include "hex.h"
7 #include "lockfile.h"
8 #include "packfile.h"
9 #include "object-file.h"
10 #include "hash-lookup.h"
11 #include "midx.h"
12 #include "progress.h"
13 #include "trace2.h"
14 #include "run-command.h"
15 #include "chunk-format.h"
16 #include "pack-bitmap.h"
17 #include "refs.h"
18 #include "revision.h"
19 #include "list-objects.h"
20 #include "path.h"
21 #include "pack-revindex.h"
23 #define PACK_EXPIRED UINT_MAX
24 #define BITMAP_POS_UNKNOWN (~((uint32_t)0))
25 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
26 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
28 extern int midx_checksum_valid(struct multi_pack_index *m);
29 extern void clear_midx_files_ext(const char *object_dir, const char *ext,
30 const char *keep_hash);
31 extern void clear_incremental_midx_files_ext(const char *object_dir,
32 const char *ext,
33 const char **keep_hashes,
34 uint32_t hashes_nr);
35 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
36 const char *idx_name);
38 static size_t write_midx_header(struct hashfile *f,
39 unsigned char num_chunks,
40 uint32_t num_packs)
42 hashwrite_be32(f, MIDX_SIGNATURE);
43 hashwrite_u8(f, MIDX_VERSION);
44 hashwrite_u8(f, oid_version(the_hash_algo));
45 hashwrite_u8(f, num_chunks);
46 hashwrite_u8(f, 0); /* unused */
47 hashwrite_be32(f, num_packs);
49 return MIDX_HEADER_SIZE;
52 struct pack_info {
53 uint32_t orig_pack_int_id;
54 char *pack_name;
55 struct packed_git *p;
57 uint32_t bitmap_pos;
58 uint32_t bitmap_nr;
60 unsigned expired : 1;
63 static void fill_pack_info(struct pack_info *info,
64 struct packed_git *p, const char *pack_name,
65 uint32_t orig_pack_int_id)
67 memset(info, 0, sizeof(struct pack_info));
69 info->orig_pack_int_id = orig_pack_int_id;
70 info->pack_name = xstrdup(pack_name);
71 info->p = p;
72 info->bitmap_pos = BITMAP_POS_UNKNOWN;
75 static int pack_info_compare(const void *_a, const void *_b)
77 struct pack_info *a = (struct pack_info *)_a;
78 struct pack_info *b = (struct pack_info *)_b;
79 return strcmp(a->pack_name, b->pack_name);
82 static int idx_or_pack_name_cmp(const void *_va, const void *_vb)
84 const char *pack_name = _va;
85 const struct pack_info *compar = _vb;
87 return cmp_idx_or_pack_name(pack_name, compar->pack_name);
90 struct write_midx_context {
91 struct pack_info *info;
92 size_t nr;
93 size_t alloc;
94 struct multi_pack_index *m;
95 struct multi_pack_index *base_midx;
96 struct progress *progress;
97 unsigned pack_paths_checked;
99 struct pack_midx_entry *entries;
100 size_t entries_nr;
102 uint32_t *pack_perm;
103 uint32_t *pack_order;
104 unsigned large_offsets_needed:1;
105 uint32_t num_large_offsets;
107 int preferred_pack_idx;
109 int incremental;
110 uint32_t num_multi_pack_indexes_before;
112 struct string_list *to_include;
115 static int should_include_pack(const struct write_midx_context *ctx,
116 const char *file_name)
119 * Note that at most one of ctx->m and ctx->to_include are set,
120 * so we are testing midx_contains_pack() and
121 * string_list_has_string() independently (guarded by the
122 * appropriate NULL checks).
124 * We could support passing to_include while reusing an existing
125 * MIDX, but don't currently since the reuse process drags
126 * forward all packs from an existing MIDX (without checking
127 * whether or not they appear in the to_include list).
129 * If we added support for that, these next two conditional
130 * should be performed independently (likely checking
131 * to_include before the existing MIDX).
133 if (ctx->m && midx_contains_pack(ctx->m, file_name))
134 return 0;
135 else if (ctx->base_midx && midx_contains_pack(ctx->base_midx,
136 file_name))
137 return 0;
138 else if (ctx->to_include &&
139 !string_list_has_string(ctx->to_include, file_name))
140 return 0;
141 return 1;
144 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
145 const char *file_name, void *data)
147 struct write_midx_context *ctx = data;
148 struct packed_git *p;
150 if (ends_with(file_name, ".idx")) {
151 display_progress(ctx->progress, ++ctx->pack_paths_checked);
153 if (!should_include_pack(ctx, file_name))
154 return;
156 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
157 p = add_packed_git(full_path, full_path_len, 0);
158 if (!p) {
159 warning(_("failed to add packfile '%s'"),
160 full_path);
161 return;
164 if (open_pack_index(p)) {
165 warning(_("failed to open pack-index '%s'"),
166 full_path);
167 close_pack(p);
168 free(p);
169 return;
172 fill_pack_info(&ctx->info[ctx->nr], p, file_name, ctx->nr);
173 ctx->nr++;
177 struct pack_midx_entry {
178 struct object_id oid;
179 uint32_t pack_int_id;
180 time_t pack_mtime;
181 uint64_t offset;
182 unsigned preferred : 1;
185 static int midx_oid_compare(const void *_a, const void *_b)
187 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
188 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
189 int cmp = oidcmp(&a->oid, &b->oid);
191 if (cmp)
192 return cmp;
194 /* Sort objects in a preferred pack first when multiple copies exist. */
195 if (a->preferred > b->preferred)
196 return -1;
197 if (a->preferred < b->preferred)
198 return 1;
200 if (a->pack_mtime > b->pack_mtime)
201 return -1;
202 else if (a->pack_mtime < b->pack_mtime)
203 return 1;
205 return a->pack_int_id - b->pack_int_id;
208 static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
209 struct pack_midx_entry *e,
210 uint32_t pos)
212 if (pos >= m->num_objects + m->num_objects_in_base)
213 return 1;
215 nth_midxed_object_oid(&e->oid, m, pos);
216 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
217 e->offset = nth_midxed_offset(m, pos);
219 /* consider objects in midx to be from "old" packs */
220 e->pack_mtime = 0;
221 return 0;
224 static void fill_pack_entry(uint32_t pack_int_id,
225 struct packed_git *p,
226 uint32_t cur_object,
227 struct pack_midx_entry *entry,
228 int preferred)
230 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
231 die(_("failed to locate object %d in packfile"), cur_object);
233 entry->pack_int_id = pack_int_id;
234 entry->pack_mtime = p->mtime;
236 entry->offset = nth_packed_object_offset(p, cur_object);
237 entry->preferred = !!preferred;
240 struct midx_fanout {
241 struct pack_midx_entry *entries;
242 size_t nr, alloc;
245 static void midx_fanout_grow(struct midx_fanout *fanout, size_t nr)
247 if (nr < fanout->nr)
248 BUG("negative growth in midx_fanout_grow() (%"PRIuMAX" < %"PRIuMAX")",
249 (uintmax_t)nr, (uintmax_t)fanout->nr);
250 ALLOC_GROW(fanout->entries, nr, fanout->alloc);
253 static void midx_fanout_sort(struct midx_fanout *fanout)
255 QSORT(fanout->entries, fanout->nr, midx_oid_compare);
258 static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
259 struct multi_pack_index *m,
260 uint32_t cur_fanout,
261 int preferred_pack)
263 uint32_t start = m->num_objects_in_base, end;
264 uint32_t cur_object;
266 if (m->base_midx)
267 midx_fanout_add_midx_fanout(fanout, m->base_midx, cur_fanout,
268 preferred_pack);
270 if (cur_fanout)
271 start += ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
272 end = m->num_objects_in_base + ntohl(m->chunk_oid_fanout[cur_fanout]);
274 for (cur_object = start; cur_object < end; cur_object++) {
275 if ((preferred_pack > -1) &&
276 (preferred_pack == nth_midxed_pack_int_id(m, cur_object))) {
278 * Objects from preferred packs are added
279 * separately.
281 continue;
284 midx_fanout_grow(fanout, fanout->nr + 1);
285 nth_midxed_pack_midx_entry(m,
286 &fanout->entries[fanout->nr],
287 cur_object);
288 fanout->entries[fanout->nr].preferred = 0;
289 fanout->nr++;
293 static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
294 struct pack_info *info,
295 uint32_t cur_pack,
296 int preferred,
297 uint32_t cur_fanout)
299 struct packed_git *pack = info[cur_pack].p;
300 uint32_t start = 0, end;
301 uint32_t cur_object;
303 if (cur_fanout)
304 start = get_pack_fanout(pack, cur_fanout - 1);
305 end = get_pack_fanout(pack, cur_fanout);
307 for (cur_object = start; cur_object < end; cur_object++) {
308 midx_fanout_grow(fanout, fanout->nr + 1);
309 fill_pack_entry(cur_pack,
310 info[cur_pack].p,
311 cur_object,
312 &fanout->entries[fanout->nr],
313 preferred);
314 fanout->nr++;
319 * It is possible to artificially get into a state where there are many
320 * duplicate copies of objects. That can create high memory pressure if
321 * we are to create a list of all objects before de-duplication. To reduce
322 * this memory pressure without a significant performance drop, automatically
323 * group objects by the first byte of their object id. Use the IDX fanout
324 * tables to group the data, copy to a local array, then sort.
326 * Copy only the de-duplicated entries (selected by most-recent modified time
327 * of a packfile containing the object).
329 static void compute_sorted_entries(struct write_midx_context *ctx,
330 uint32_t start_pack)
332 uint32_t cur_fanout, cur_pack, cur_object;
333 size_t alloc_objects, total_objects = 0;
334 struct midx_fanout fanout = { 0 };
336 for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++)
337 total_objects = st_add(total_objects,
338 ctx->info[cur_pack].p->num_objects);
341 * As we de-duplicate by fanout value, we expect the fanout
342 * slices to be evenly distributed, with some noise. Hence,
343 * allocate slightly more than one 256th.
345 alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16;
347 ALLOC_ARRAY(fanout.entries, fanout.alloc);
348 ALLOC_ARRAY(ctx->entries, alloc_objects);
349 ctx->entries_nr = 0;
351 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
352 fanout.nr = 0;
354 if (ctx->m && !ctx->incremental)
355 midx_fanout_add_midx_fanout(&fanout, ctx->m, cur_fanout,
356 ctx->preferred_pack_idx);
358 for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++) {
359 int preferred = cur_pack == ctx->preferred_pack_idx;
360 midx_fanout_add_pack_fanout(&fanout,
361 ctx->info, cur_pack,
362 preferred, cur_fanout);
365 if (-1 < ctx->preferred_pack_idx && ctx->preferred_pack_idx < start_pack)
366 midx_fanout_add_pack_fanout(&fanout, ctx->info,
367 ctx->preferred_pack_idx, 1,
368 cur_fanout);
370 midx_fanout_sort(&fanout);
373 * The batch is now sorted by OID and then mtime (descending).
374 * Take only the first duplicate.
376 for (cur_object = 0; cur_object < fanout.nr; cur_object++) {
377 if (cur_object && oideq(&fanout.entries[cur_object - 1].oid,
378 &fanout.entries[cur_object].oid))
379 continue;
380 if (ctx->incremental && ctx->base_midx &&
381 midx_has_oid(ctx->base_midx,
382 &fanout.entries[cur_object].oid))
383 continue;
385 ALLOC_GROW(ctx->entries, st_add(ctx->entries_nr, 1),
386 alloc_objects);
387 memcpy(&ctx->entries[ctx->entries_nr],
388 &fanout.entries[cur_object],
389 sizeof(struct pack_midx_entry));
390 ctx->entries_nr++;
394 free(fanout.entries);
397 static int write_midx_pack_names(struct hashfile *f, void *data)
399 struct write_midx_context *ctx = data;
400 uint32_t i;
401 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
402 size_t written = 0;
404 for (i = 0; i < ctx->nr; i++) {
405 size_t writelen;
407 if (ctx->info[i].expired)
408 continue;
410 if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0)
411 BUG("incorrect pack-file order: %s before %s",
412 ctx->info[i - 1].pack_name,
413 ctx->info[i].pack_name);
415 writelen = strlen(ctx->info[i].pack_name) + 1;
416 hashwrite(f, ctx->info[i].pack_name, writelen);
417 written += writelen;
420 /* add padding to be aligned */
421 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
422 if (i < MIDX_CHUNK_ALIGNMENT) {
423 memset(padding, 0, sizeof(padding));
424 hashwrite(f, padding, i);
427 return 0;
430 static int write_midx_bitmapped_packs(struct hashfile *f, void *data)
432 struct write_midx_context *ctx = data;
433 size_t i;
435 for (i = 0; i < ctx->nr; i++) {
436 struct pack_info *pack = &ctx->info[i];
437 if (pack->expired)
438 continue;
440 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN && pack->bitmap_nr)
441 BUG("pack '%s' has no bitmap position, but has %d bitmapped object(s)",
442 pack->pack_name, pack->bitmap_nr);
444 hashwrite_be32(f, pack->bitmap_pos);
445 hashwrite_be32(f, pack->bitmap_nr);
447 return 0;
450 static int write_midx_oid_fanout(struct hashfile *f,
451 void *data)
453 struct write_midx_context *ctx = data;
454 struct pack_midx_entry *list = ctx->entries;
455 struct pack_midx_entry *last = ctx->entries + ctx->entries_nr;
456 uint32_t count = 0;
457 uint32_t i;
460 * Write the first-level table (the list is sorted,
461 * but we use a 256-entry lookup to be able to avoid
462 * having to do eight extra binary search iterations).
464 for (i = 0; i < 256; i++) {
465 struct pack_midx_entry *next = list;
467 while (next < last && next->oid.hash[0] == i) {
468 count++;
469 next++;
472 hashwrite_be32(f, count);
473 list = next;
476 return 0;
479 static int write_midx_oid_lookup(struct hashfile *f,
480 void *data)
482 struct write_midx_context *ctx = data;
483 unsigned char hash_len = the_hash_algo->rawsz;
484 struct pack_midx_entry *list = ctx->entries;
485 uint32_t i;
487 for (i = 0; i < ctx->entries_nr; i++) {
488 struct pack_midx_entry *obj = list++;
490 if (i < ctx->entries_nr - 1) {
491 struct pack_midx_entry *next = list;
492 if (oidcmp(&obj->oid, &next->oid) >= 0)
493 BUG("OIDs not in order: %s >= %s",
494 oid_to_hex(&obj->oid),
495 oid_to_hex(&next->oid));
498 hashwrite(f, obj->oid.hash, (int)hash_len);
501 return 0;
504 static int write_midx_object_offsets(struct hashfile *f,
505 void *data)
507 struct write_midx_context *ctx = data;
508 struct pack_midx_entry *list = ctx->entries;
509 uint32_t i, nr_large_offset = 0;
511 for (i = 0; i < ctx->entries_nr; i++) {
512 struct pack_midx_entry *obj = list++;
514 if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
515 BUG("object %s is in an expired pack with int-id %d",
516 oid_to_hex(&obj->oid),
517 obj->pack_int_id);
519 hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
521 if (ctx->large_offsets_needed && obj->offset >> 31)
522 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
523 else if (!ctx->large_offsets_needed && obj->offset >> 32)
524 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
525 oid_to_hex(&obj->oid),
526 obj->offset);
527 else
528 hashwrite_be32(f, (uint32_t)obj->offset);
531 return 0;
534 static int write_midx_large_offsets(struct hashfile *f,
535 void *data)
537 struct write_midx_context *ctx = data;
538 struct pack_midx_entry *list = ctx->entries;
539 struct pack_midx_entry *end = ctx->entries + ctx->entries_nr;
540 uint32_t nr_large_offset = ctx->num_large_offsets;
542 while (nr_large_offset) {
543 struct pack_midx_entry *obj;
544 uint64_t offset;
546 if (list >= end)
547 BUG("too many large-offset objects");
549 obj = list++;
550 offset = obj->offset;
552 if (!(offset >> 31))
553 continue;
555 hashwrite_be64(f, offset);
557 nr_large_offset--;
560 return 0;
563 static int write_midx_revindex(struct hashfile *f,
564 void *data)
566 struct write_midx_context *ctx = data;
567 uint32_t i, nr_base;
569 if (ctx->incremental && ctx->base_midx)
570 nr_base = ctx->base_midx->num_objects +
571 ctx->base_midx->num_objects_in_base;
572 else
573 nr_base = 0;
575 for (i = 0; i < ctx->entries_nr; i++)
576 hashwrite_be32(f, ctx->pack_order[i] + nr_base);
578 return 0;
581 struct midx_pack_order_data {
582 uint32_t nr;
583 uint32_t pack;
584 off_t offset;
587 static int midx_pack_order_cmp(const void *va, const void *vb)
589 const struct midx_pack_order_data *a = va, *b = vb;
590 if (a->pack < b->pack)
591 return -1;
592 else if (a->pack > b->pack)
593 return 1;
594 else if (a->offset < b->offset)
595 return -1;
596 else if (a->offset > b->offset)
597 return 1;
598 else
599 return 0;
602 static uint32_t *midx_pack_order(struct write_midx_context *ctx)
604 struct midx_pack_order_data *data;
605 uint32_t *pack_order, base_objects = 0;
606 uint32_t i;
608 trace2_region_enter("midx", "midx_pack_order", the_repository);
610 if (ctx->incremental && ctx->base_midx)
611 base_objects = ctx->base_midx->num_objects +
612 ctx->base_midx->num_objects_in_base;
614 ALLOC_ARRAY(pack_order, ctx->entries_nr);
615 ALLOC_ARRAY(data, ctx->entries_nr);
617 for (i = 0; i < ctx->entries_nr; i++) {
618 struct pack_midx_entry *e = &ctx->entries[i];
619 data[i].nr = i;
620 data[i].pack = ctx->pack_perm[e->pack_int_id];
621 if (!e->preferred)
622 data[i].pack |= (1U << 31);
623 data[i].offset = e->offset;
626 QSORT(data, ctx->entries_nr, midx_pack_order_cmp);
628 for (i = 0; i < ctx->entries_nr; i++) {
629 struct pack_midx_entry *e = &ctx->entries[data[i].nr];
630 struct pack_info *pack = &ctx->info[ctx->pack_perm[e->pack_int_id]];
631 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
632 pack->bitmap_pos = i + base_objects;
633 pack->bitmap_nr++;
634 pack_order[i] = data[i].nr;
636 for (i = 0; i < ctx->nr; i++) {
637 struct pack_info *pack = &ctx->info[ctx->pack_perm[i]];
638 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
639 pack->bitmap_pos = 0;
641 free(data);
643 trace2_region_leave("midx", "midx_pack_order", the_repository);
645 return pack_order;
648 static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
649 struct write_midx_context *ctx)
651 struct strbuf buf = STRBUF_INIT;
652 char *tmp_file;
654 trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
656 strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
658 tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
659 midx_hash, WRITE_REV);
661 if (finalize_object_file(tmp_file, buf.buf))
662 die(_("cannot store reverse index file"));
664 strbuf_release(&buf);
665 free(tmp_file);
667 trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
670 static void prepare_midx_packing_data(struct packing_data *pdata,
671 struct write_midx_context *ctx)
673 uint32_t i;
675 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
677 memset(pdata, 0, sizeof(struct packing_data));
678 prepare_packing_data(the_repository, pdata);
680 for (i = 0; i < ctx->entries_nr; i++) {
681 uint32_t pos = ctx->pack_order[i];
682 struct pack_midx_entry *from = &ctx->entries[pos];
683 struct object_entry *to = packlist_alloc(pdata, &from->oid);
685 oe_set_in_pack(pdata, to,
686 ctx->info[ctx->pack_perm[from->pack_int_id]].p);
689 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
692 static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
693 const struct object_id *oid,
694 int flag, void *cb_data)
696 struct rev_info *revs = (struct rev_info*)cb_data;
697 struct object_id peeled;
698 struct object *object;
700 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
701 warning("symbolic ref is dangling: %s", refname);
702 return 0;
705 if (!peel_iterated_oid(the_repository, oid, &peeled))
706 oid = &peeled;
708 object = parse_object_or_die(oid, refname);
709 if (object->type != OBJ_COMMIT)
710 return 0;
712 add_pending_object(revs, object, "");
713 if (bitmap_is_preferred_refname(revs->repo, refname))
714 object->flags |= NEEDS_BITMAP;
715 return 0;
718 struct bitmap_commit_cb {
719 struct commit **commits;
720 size_t commits_nr, commits_alloc;
722 struct write_midx_context *ctx;
725 static const struct object_id *bitmap_oid_access(size_t index,
726 const void *_entries)
728 const struct pack_midx_entry *entries = _entries;
729 return &entries[index].oid;
732 static void bitmap_show_commit(struct commit *commit, void *_data)
734 struct bitmap_commit_cb *data = _data;
735 int pos = oid_pos(&commit->object.oid, data->ctx->entries,
736 data->ctx->entries_nr,
737 bitmap_oid_access);
738 if (pos < 0)
739 return;
741 ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc);
742 data->commits[data->commits_nr++] = commit;
745 static int read_refs_snapshot(const char *refs_snapshot,
746 struct rev_info *revs)
748 struct strbuf buf = STRBUF_INIT;
749 struct object_id oid;
750 FILE *f = xfopen(refs_snapshot, "r");
752 while (strbuf_getline(&buf, f) != EOF) {
753 struct object *object;
754 int preferred = 0;
755 char *hex = buf.buf;
756 const char *end = NULL;
758 if (buf.len && *buf.buf == '+') {
759 preferred = 1;
760 hex = &buf.buf[1];
763 if (parse_oid_hex(hex, &oid, &end) < 0)
764 die(_("could not parse line: %s"), buf.buf);
765 if (*end)
766 die(_("malformed line: %s"), buf.buf);
768 object = parse_object_or_die(&oid, NULL);
769 if (preferred)
770 object->flags |= NEEDS_BITMAP;
772 add_pending_object(revs, object, "");
775 fclose(f);
776 strbuf_release(&buf);
777 return 0;
779 static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
780 const char *refs_snapshot,
781 struct write_midx_context *ctx)
783 struct rev_info revs;
784 struct bitmap_commit_cb cb = {0};
786 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
787 the_repository);
789 cb.ctx = ctx;
791 repo_init_revisions(the_repository, &revs, NULL);
792 if (refs_snapshot) {
793 read_refs_snapshot(refs_snapshot, &revs);
794 } else {
795 setup_revisions(0, NULL, &revs, NULL);
796 refs_for_each_ref(get_main_ref_store(the_repository),
797 add_ref_to_pending, &revs);
801 * Skipping promisor objects here is intentional, since it only excludes
802 * them from the list of reachable commits that we want to select from
803 * when computing the selection of MIDX'd commits to receive bitmaps.
805 * Reachability bitmaps do require that their objects be closed under
806 * reachability, but fetching any objects missing from promisors at this
807 * point is too late. But, if one of those objects can be reached from
808 * an another object that is included in the bitmap, then we will
809 * complain later that we don't have reachability closure (and fail
810 * appropriately).
812 fetch_if_missing = 0;
813 revs.exclude_promisor_objects = 1;
815 if (prepare_revision_walk(&revs))
816 die(_("revision walk setup failed"));
818 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
819 if (indexed_commits_nr_p)
820 *indexed_commits_nr_p = cb.commits_nr;
822 release_revisions(&revs);
824 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
825 the_repository);
827 return cb.commits;
830 static int write_midx_bitmap(const char *midx_name,
831 const unsigned char *midx_hash,
832 struct packing_data *pdata,
833 struct commit **commits,
834 uint32_t commits_nr,
835 uint32_t *pack_order,
836 unsigned flags)
838 int ret, i;
839 uint16_t options = 0;
840 struct bitmap_writer writer;
841 struct pack_idx_entry **index;
842 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
843 hash_to_hex(midx_hash));
845 trace2_region_enter("midx", "write_midx_bitmap", the_repository);
847 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
848 options |= BITMAP_OPT_HASH_CACHE;
850 if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE)
851 options |= BITMAP_OPT_LOOKUP_TABLE;
854 * Build the MIDX-order index based on pdata.objects (which is already
855 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
856 * this order).
858 ALLOC_ARRAY(index, pdata->nr_objects);
859 for (i = 0; i < pdata->nr_objects; i++)
860 index[i] = &pdata->objects[i].idx;
862 bitmap_writer_init(&writer, the_repository, pdata);
863 bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
864 bitmap_writer_build_type_index(&writer, index);
867 * bitmap_writer_finish expects objects in lex order, but pack_order
868 * gives us exactly that. use it directly instead of re-sorting the
869 * array.
871 * This changes the order of objects in 'index' between
872 * bitmap_writer_build_type_index and bitmap_writer_finish.
874 * The same re-ordering takes place in the single-pack bitmap code via
875 * write_idx_file(), which is called by finish_tmp_packfile(), which
876 * happens between bitmap_writer_build_type_index() and
877 * bitmap_writer_finish().
879 for (i = 0; i < pdata->nr_objects; i++)
880 index[pack_order[i]] = &pdata->objects[i].idx;
882 bitmap_writer_select_commits(&writer, commits, commits_nr);
883 ret = bitmap_writer_build(&writer);
884 if (ret < 0)
885 goto cleanup;
887 bitmap_writer_set_checksum(&writer, midx_hash);
888 bitmap_writer_finish(&writer, index, bitmap_name, options);
890 cleanup:
891 free(index);
892 free(bitmap_name);
893 bitmap_writer_free(&writer);
895 trace2_region_leave("midx", "write_midx_bitmap", the_repository);
897 return ret;
900 static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
901 const char *object_dir)
903 struct multi_pack_index *result = NULL;
904 struct multi_pack_index *cur;
905 char *obj_dir_real = real_pathdup(object_dir, 1);
906 struct strbuf cur_path_real = STRBUF_INIT;
908 /* Ensure the given object_dir is local, or a known alternate. */
909 find_odb(r, obj_dir_real);
911 for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
912 strbuf_realpath(&cur_path_real, cur->object_dir, 1);
913 if (!strcmp(obj_dir_real, cur_path_real.buf)) {
914 result = cur;
915 goto cleanup;
919 cleanup:
920 free(obj_dir_real);
921 strbuf_release(&cur_path_real);
922 return result;
925 static int fill_packs_from_midx(struct write_midx_context *ctx,
926 const char *preferred_pack_name, uint32_t flags)
928 struct multi_pack_index *m;
930 for (m = ctx->m; m; m = m->base_midx) {
931 uint32_t i;
933 for (i = 0; i < m->num_packs; i++) {
934 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
937 * If generating a reverse index, need to have
938 * packed_git's loaded to compare their
939 * mtimes and object count.
941 * If a preferred pack is specified, need to
942 * have packed_git's loaded to ensure the chosen
943 * preferred pack has a non-zero object count.
945 if (flags & MIDX_WRITE_REV_INDEX ||
946 preferred_pack_name) {
947 if (prepare_midx_pack(the_repository, m,
948 m->num_packs_in_base + i)) {
949 error(_("could not load pack"));
950 return 1;
953 if (open_pack_index(m->packs[i]))
954 die(_("could not open index for %s"),
955 m->packs[i]->pack_name);
958 fill_pack_info(&ctx->info[ctx->nr++], m->packs[i],
959 m->pack_names[i],
960 m->num_packs_in_base + i);
963 return 0;
966 static struct {
967 const char *non_split;
968 const char *split;
969 } midx_exts[] = {
970 {NULL, MIDX_EXT_MIDX},
971 {MIDX_EXT_BITMAP, MIDX_EXT_BITMAP},
972 {MIDX_EXT_REV, MIDX_EXT_REV},
975 static int link_midx_to_chain(struct multi_pack_index *m)
977 struct strbuf from = STRBUF_INIT;
978 struct strbuf to = STRBUF_INIT;
979 int ret = 0;
980 size_t i;
982 if (!m || m->has_chain) {
984 * Either no MIDX previously existed, or it was already
985 * part of a MIDX chain. In both cases, we have nothing
986 * to link, so return early.
988 goto done;
991 for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
992 const unsigned char *hash = get_midx_checksum(m);
994 get_midx_filename_ext(&from, m->object_dir, hash,
995 midx_exts[i].non_split);
996 get_split_midx_filename_ext(&to, m->object_dir, hash,
997 midx_exts[i].split);
999 if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
1000 ret = error_errno(_("unable to link '%s' to '%s'"),
1001 from.buf, to.buf);
1002 goto done;
1005 strbuf_reset(&from);
1006 strbuf_reset(&to);
1009 done:
1010 strbuf_release(&from);
1011 strbuf_release(&to);
1012 return ret;
1015 static void clear_midx_files(const char *object_dir,
1016 const char **hashes,
1017 uint32_t hashes_nr,
1018 unsigned incremental)
1021 * if incremental:
1022 * - remove all non-incremental MIDX files
1023 * - remove any incremental MIDX files not in the current one
1025 * if non-incremental:
1026 * - remove all incremental MIDX files
1027 * - remove any non-incremental MIDX files not matching the current
1028 * hash
1030 struct strbuf buf = STRBUF_INIT;
1031 const char *exts[] = { MIDX_EXT_BITMAP, MIDX_EXT_REV, MIDX_EXT_MIDX };
1032 uint32_t i, j;
1034 for (i = 0; i < ARRAY_SIZE(exts); i++) {
1035 clear_incremental_midx_files_ext(object_dir, exts[i],
1036 hashes, hashes_nr);
1037 for (j = 0; j < hashes_nr; j++)
1038 clear_midx_files_ext(object_dir, exts[i], hashes[j]);
1041 if (incremental)
1042 get_midx_filename(&buf, object_dir);
1043 else
1044 get_midx_chain_filename(&buf, object_dir);
1046 if (unlink(buf.buf) && errno != ENOENT)
1047 die_errno(_("failed to clear multi-pack-index at %s"), buf.buf);
1049 strbuf_release(&buf);
1052 static int write_midx_internal(const char *object_dir,
1053 struct string_list *packs_to_include,
1054 struct string_list *packs_to_drop,
1055 const char *preferred_pack_name,
1056 const char *refs_snapshot,
1057 unsigned flags)
1059 struct strbuf midx_name = STRBUF_INIT;
1060 unsigned char midx_hash[GIT_MAX_RAWSZ];
1061 uint32_t i, start_pack;
1062 struct hashfile *f = NULL;
1063 struct lock_file lk;
1064 struct tempfile *incr;
1065 struct write_midx_context ctx = { 0 };
1066 int bitmapped_packs_concat_len = 0;
1067 int pack_name_concat_len = 0;
1068 int dropped_packs = 0;
1069 int result = 0;
1070 const char **keep_hashes = NULL;
1071 struct chunkfile *cf;
1073 trace2_region_enter("midx", "write_midx_internal", the_repository);
1075 ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
1076 if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
1077 die(_("cannot write incremental MIDX with bitmap"));
1079 if (ctx.incremental)
1080 strbuf_addf(&midx_name,
1081 "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
1082 object_dir);
1083 else
1084 get_midx_filename(&midx_name, object_dir);
1085 if (safe_create_leading_directories(midx_name.buf))
1086 die_errno(_("unable to create leading directories of %s"),
1087 midx_name.buf);
1089 if (!packs_to_include || ctx.incremental) {
1090 struct multi_pack_index *m = lookup_multi_pack_index(the_repository,
1091 object_dir);
1092 if (m && !midx_checksum_valid(m)) {
1093 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1094 m = NULL;
1097 if (m) {
1099 * Only reference an existing MIDX when not filtering
1100 * which packs to include, since all packs and objects
1101 * are copied blindly from an existing MIDX if one is
1102 * present.
1104 if (ctx.incremental)
1105 ctx.base_midx = m;
1106 else if (!packs_to_include)
1107 ctx.m = m;
1111 ctx.nr = 0;
1112 ctx.alloc = ctx.m ? ctx.m->num_packs + ctx.m->num_packs_in_base : 16;
1113 ctx.info = NULL;
1114 ALLOC_ARRAY(ctx.info, ctx.alloc);
1116 if (ctx.incremental) {
1117 struct multi_pack_index *m = ctx.base_midx;
1118 while (m) {
1119 ctx.num_multi_pack_indexes_before++;
1120 m = m->base_midx;
1122 } else if (ctx.m && fill_packs_from_midx(&ctx, preferred_pack_name,
1123 flags) < 0) {
1124 goto cleanup;
1127 start_pack = ctx.nr;
1129 ctx.pack_paths_checked = 0;
1130 if (flags & MIDX_PROGRESS)
1131 ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
1132 else
1133 ctx.progress = NULL;
1135 ctx.to_include = packs_to_include;
1137 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
1138 stop_progress(&ctx.progress);
1140 if ((ctx.m && ctx.nr == ctx.m->num_packs + ctx.m->num_packs_in_base) &&
1141 !ctx.incremental &&
1142 !(packs_to_include || packs_to_drop)) {
1143 struct bitmap_index *bitmap_git;
1144 int bitmap_exists;
1145 int want_bitmap = flags & MIDX_WRITE_BITMAP;
1147 bitmap_git = prepare_midx_bitmap_git(ctx.m);
1148 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
1149 free_bitmap_index(bitmap_git);
1151 if (bitmap_exists || !want_bitmap) {
1153 * The correct MIDX already exists, and so does a
1154 * corresponding bitmap (or one wasn't requested).
1156 if (!want_bitmap)
1157 clear_midx_files_ext(object_dir, "bitmap", NULL);
1158 goto cleanup;
1162 if (ctx.incremental && !ctx.nr)
1163 goto cleanup; /* nothing to do */
1165 if (preferred_pack_name) {
1166 ctx.preferred_pack_idx = -1;
1168 for (i = 0; i < ctx.nr; i++) {
1169 if (!cmp_idx_or_pack_name(preferred_pack_name,
1170 ctx.info[i].pack_name)) {
1171 ctx.preferred_pack_idx = i;
1172 break;
1176 if (ctx.preferred_pack_idx == -1)
1177 warning(_("unknown preferred pack: '%s'"),
1178 preferred_pack_name);
1179 } else if (ctx.nr &&
1180 (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1181 struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1182 ctx.preferred_pack_idx = 0;
1184 if (packs_to_drop && packs_to_drop->nr)
1185 BUG("cannot write a MIDX bitmap during expiration");
1188 * set a preferred pack when writing a bitmap to ensure that
1189 * the pack from which the first object is selected in pseudo
1190 * pack-order has all of its objects selected from that pack
1191 * (and not another pack containing a duplicate)
1193 for (i = 1; i < ctx.nr; i++) {
1194 struct packed_git *p = ctx.info[i].p;
1196 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1197 oldest = p;
1198 ctx.preferred_pack_idx = i;
1202 if (!oldest->num_objects) {
1204 * If all packs are empty; unset the preferred index.
1205 * This is acceptable since there will be no duplicate
1206 * objects to resolve, so the preferred value doesn't
1207 * matter.
1209 ctx.preferred_pack_idx = -1;
1211 } else {
1213 * otherwise don't mark any pack as preferred to avoid
1214 * interfering with expiration logic below
1216 ctx.preferred_pack_idx = -1;
1219 if (ctx.preferred_pack_idx > -1) {
1220 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1221 if (!preferred->num_objects) {
1222 error(_("cannot select preferred pack %s with no objects"),
1223 preferred->pack_name);
1224 result = 1;
1225 goto cleanup;
1229 compute_sorted_entries(&ctx, start_pack);
1231 ctx.large_offsets_needed = 0;
1232 for (i = 0; i < ctx.entries_nr; i++) {
1233 if (ctx.entries[i].offset > 0x7fffffff)
1234 ctx.num_large_offsets++;
1235 if (ctx.entries[i].offset > 0xffffffff)
1236 ctx.large_offsets_needed = 1;
1239 QSORT(ctx.info, ctx.nr, pack_info_compare);
1241 if (packs_to_drop && packs_to_drop->nr) {
1242 int drop_index = 0;
1243 int missing_drops = 0;
1245 for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1246 int cmp = strcmp(ctx.info[i].pack_name,
1247 packs_to_drop->items[drop_index].string);
1249 if (!cmp) {
1250 drop_index++;
1251 ctx.info[i].expired = 1;
1252 } else if (cmp > 0) {
1253 error(_("did not see pack-file %s to drop"),
1254 packs_to_drop->items[drop_index].string);
1255 drop_index++;
1256 missing_drops++;
1257 i--;
1258 } else {
1259 ctx.info[i].expired = 0;
1263 if (missing_drops) {
1264 result = 1;
1265 goto cleanup;
1270 * pack_perm stores a permutation between pack-int-ids from the
1271 * previous multi-pack-index to the new one we are writing:
1273 * pack_perm[old_id] = new_id
1275 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1276 for (i = 0; i < ctx.nr; i++) {
1277 if (ctx.info[i].expired) {
1278 dropped_packs++;
1279 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
1280 } else {
1281 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
1285 for (i = 0; i < ctx.nr; i++) {
1286 if (ctx.info[i].expired)
1287 continue;
1288 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
1289 bitmapped_packs_concat_len += 2 * sizeof(uint32_t);
1292 /* Check that the preferred pack wasn't expired (if given). */
1293 if (preferred_pack_name) {
1294 struct pack_info *preferred = bsearch(preferred_pack_name,
1295 ctx.info, ctx.nr,
1296 sizeof(*ctx.info),
1297 idx_or_pack_name_cmp);
1298 if (preferred) {
1299 uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1300 if (perm == PACK_EXPIRED)
1301 warning(_("preferred pack '%s' is expired"),
1302 preferred_pack_name);
1306 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1307 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1308 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1310 if (ctx.nr - dropped_packs == 0) {
1311 error(_("no pack files to index."));
1312 result = 1;
1313 goto cleanup;
1316 if (!ctx.entries_nr) {
1317 if (flags & MIDX_WRITE_BITMAP)
1318 warning(_("refusing to write multi-pack .bitmap without any objects"));
1319 flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1322 if (ctx.incremental) {
1323 struct strbuf lock_name = STRBUF_INIT;
1325 get_midx_chain_filename(&lock_name, object_dir);
1326 hold_lock_file_for_update(&lk, lock_name.buf, LOCK_DIE_ON_ERROR);
1327 strbuf_release(&lock_name);
1329 incr = mks_tempfile_m(midx_name.buf, 0444);
1330 if (!incr) {
1331 error(_("unable to create temporary MIDX layer"));
1332 return -1;
1335 if (adjust_shared_perm(get_tempfile_path(incr))) {
1336 error(_("unable to adjust shared permissions for '%s'"),
1337 get_tempfile_path(incr));
1338 return -1;
1341 f = hashfd(get_tempfile_fd(incr), get_tempfile_path(incr));
1342 } else {
1343 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
1344 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
1347 cf = init_chunkfile(f);
1349 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1350 write_midx_pack_names);
1351 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1352 write_midx_oid_fanout);
1353 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
1354 st_mult(ctx.entries_nr, the_hash_algo->rawsz),
1355 write_midx_oid_lookup);
1356 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
1357 st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
1358 write_midx_object_offsets);
1360 if (ctx.large_offsets_needed)
1361 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
1362 st_mult(ctx.num_large_offsets,
1363 MIDX_CHUNK_LARGE_OFFSET_WIDTH),
1364 write_midx_large_offsets);
1366 if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) {
1367 ctx.pack_order = midx_pack_order(&ctx);
1368 add_chunk(cf, MIDX_CHUNKID_REVINDEX,
1369 st_mult(ctx.entries_nr, sizeof(uint32_t)),
1370 write_midx_revindex);
1371 add_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
1372 bitmapped_packs_concat_len,
1373 write_midx_bitmapped_packs);
1376 write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1377 write_chunkfile(cf, &ctx);
1379 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
1380 CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1381 free_chunkfile(cf);
1383 if (flags & MIDX_WRITE_REV_INDEX &&
1384 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1385 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
1387 if (flags & MIDX_WRITE_BITMAP) {
1388 struct packing_data pdata;
1389 struct commit **commits;
1390 uint32_t commits_nr;
1392 if (!ctx.entries_nr)
1393 BUG("cannot write a bitmap without any objects");
1395 prepare_midx_packing_data(&pdata, &ctx);
1397 commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1400 * The previous steps translated the information from
1401 * 'entries' into information suitable for constructing
1402 * bitmaps. We no longer need that array, so clear it to
1403 * reduce memory pressure.
1405 FREE_AND_NULL(ctx.entries);
1406 ctx.entries_nr = 0;
1408 if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1409 commits, commits_nr, ctx.pack_order,
1410 flags) < 0) {
1411 error(_("could not write multi-pack bitmap"));
1412 result = 1;
1413 clear_packing_data(&pdata);
1414 free(commits);
1415 goto cleanup;
1418 clear_packing_data(&pdata);
1419 free(commits);
1422 * NOTE: Do not use ctx.entries beyond this point, since it might
1423 * have been freed in the previous if block.
1426 CALLOC_ARRAY(keep_hashes, ctx.num_multi_pack_indexes_before + 1);
1428 if (ctx.incremental) {
1429 FILE *chainf = fdopen_lock_file(&lk, "w");
1430 struct strbuf final_midx_name = STRBUF_INIT;
1431 struct multi_pack_index *m = ctx.base_midx;
1433 if (!chainf) {
1434 error_errno(_("unable to open multi-pack-index chain file"));
1435 return -1;
1438 if (link_midx_to_chain(ctx.base_midx) < 0)
1439 return -1;
1441 get_split_midx_filename_ext(&final_midx_name, object_dir,
1442 midx_hash, MIDX_EXT_MIDX);
1444 if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
1445 error_errno(_("unable to rename new multi-pack-index layer"));
1446 return -1;
1449 strbuf_release(&final_midx_name);
1451 keep_hashes[ctx.num_multi_pack_indexes_before] =
1452 xstrdup(hash_to_hex(midx_hash));
1454 for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
1455 uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
1457 keep_hashes[j] = xstrdup(hash_to_hex(get_midx_checksum(m)));
1458 m = m->base_midx;
1461 for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1462 fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
1463 } else {
1464 keep_hashes[ctx.num_multi_pack_indexes_before] =
1465 xstrdup(hash_to_hex(midx_hash));
1468 if (ctx.m || ctx.base_midx)
1469 close_object_store(the_repository->objects);
1471 if (commit_lock_file(&lk) < 0)
1472 die_errno(_("could not write multi-pack-index"));
1474 clear_midx_files(object_dir, keep_hashes,
1475 ctx.num_multi_pack_indexes_before + 1,
1476 ctx.incremental);
1478 cleanup:
1479 for (i = 0; i < ctx.nr; i++) {
1480 if (ctx.info[i].p) {
1481 close_pack(ctx.info[i].p);
1482 free(ctx.info[i].p);
1484 free(ctx.info[i].pack_name);
1487 free(ctx.info);
1488 free(ctx.entries);
1489 free(ctx.pack_perm);
1490 free(ctx.pack_order);
1491 if (keep_hashes) {
1492 for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1493 free((char *)keep_hashes[i]);
1494 free(keep_hashes);
1496 strbuf_release(&midx_name);
1498 trace2_region_leave("midx", "write_midx_internal", the_repository);
1500 return result;
1503 int write_midx_file(const char *object_dir,
1504 const char *preferred_pack_name,
1505 const char *refs_snapshot,
1506 unsigned flags)
1508 return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
1509 refs_snapshot, flags);
1512 int write_midx_file_only(const char *object_dir,
1513 struct string_list *packs_to_include,
1514 const char *preferred_pack_name,
1515 const char *refs_snapshot,
1516 unsigned flags)
1518 return write_midx_internal(object_dir, packs_to_include, NULL,
1519 preferred_pack_name, refs_snapshot, flags);
1522 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
1524 uint32_t i, *count, result = 0;
1525 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1526 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1527 struct progress *progress = NULL;
1529 if (!m)
1530 return 0;
1532 if (m->base_midx)
1533 die(_("cannot expire packs from an incremental multi-pack-index"));
1535 CALLOC_ARRAY(count, m->num_packs);
1537 if (flags & MIDX_PROGRESS)
1538 progress = start_delayed_progress(_("Counting referenced objects"),
1539 m->num_objects);
1540 for (i = 0; i < m->num_objects; i++) {
1541 int pack_int_id = nth_midxed_pack_int_id(m, i);
1542 count[pack_int_id]++;
1543 display_progress(progress, i + 1);
1545 stop_progress(&progress);
1547 if (flags & MIDX_PROGRESS)
1548 progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1549 m->num_packs);
1550 for (i = 0; i < m->num_packs; i++) {
1551 char *pack_name;
1552 display_progress(progress, i + 1);
1554 if (count[i])
1555 continue;
1557 if (prepare_midx_pack(r, m, i))
1558 continue;
1560 if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
1561 continue;
1563 pack_name = xstrdup(m->packs[i]->pack_name);
1564 close_pack(m->packs[i]);
1566 string_list_insert(&packs_to_drop, m->pack_names[i]);
1567 unlink_pack_path(pack_name, 0);
1568 free(pack_name);
1570 stop_progress(&progress);
1572 free(count);
1574 if (packs_to_drop.nr)
1575 result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
1577 string_list_clear(&packs_to_drop, 0);
1579 return result;
1582 struct repack_info {
1583 timestamp_t mtime;
1584 uint32_t referenced_objects;
1585 uint32_t pack_int_id;
1588 static int compare_by_mtime(const void *a_, const void *b_)
1590 const struct repack_info *a, *b;
1592 a = (const struct repack_info *)a_;
1593 b = (const struct repack_info *)b_;
1595 if (a->mtime < b->mtime)
1596 return -1;
1597 if (a->mtime > b->mtime)
1598 return 1;
1599 return 0;
1602 static int want_included_pack(struct repository *r,
1603 struct multi_pack_index *m,
1604 int pack_kept_objects,
1605 uint32_t pack_int_id)
1607 struct packed_git *p;
1608 if (prepare_midx_pack(r, m, pack_int_id))
1609 return 0;
1610 p = m->packs[pack_int_id];
1611 if (!pack_kept_objects && p->pack_keep)
1612 return 0;
1613 if (p->is_cruft)
1614 return 0;
1615 if (open_pack_index(p) || !p->num_objects)
1616 return 0;
1617 return 1;
1620 static void fill_included_packs_all(struct repository *r,
1621 struct multi_pack_index *m,
1622 unsigned char *include_pack)
1624 uint32_t i;
1625 int pack_kept_objects = 0;
1627 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1629 for (i = 0; i < m->num_packs; i++) {
1630 if (!want_included_pack(r, m, pack_kept_objects, i))
1631 continue;
1633 include_pack[i] = 1;
1637 static void fill_included_packs_batch(struct repository *r,
1638 struct multi_pack_index *m,
1639 unsigned char *include_pack,
1640 size_t batch_size)
1642 uint32_t i;
1643 size_t total_size;
1644 struct repack_info *pack_info;
1645 int pack_kept_objects = 0;
1647 CALLOC_ARRAY(pack_info, m->num_packs);
1649 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1651 for (i = 0; i < m->num_packs; i++) {
1652 pack_info[i].pack_int_id = i;
1654 if (prepare_midx_pack(r, m, i))
1655 continue;
1657 pack_info[i].mtime = m->packs[i]->mtime;
1660 for (i = 0; i < m->num_objects; i++) {
1661 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1662 pack_info[pack_int_id].referenced_objects++;
1665 QSORT(pack_info, m->num_packs, compare_by_mtime);
1667 total_size = 0;
1668 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1669 int pack_int_id = pack_info[i].pack_int_id;
1670 struct packed_git *p = m->packs[pack_int_id];
1671 size_t expected_size;
1673 if (!want_included_pack(r, m, pack_kept_objects, pack_int_id))
1674 continue;
1676 expected_size = st_mult(p->pack_size,
1677 pack_info[i].referenced_objects);
1678 expected_size /= p->num_objects;
1680 if (expected_size >= batch_size)
1681 continue;
1683 total_size += expected_size;
1684 include_pack[pack_int_id] = 1;
1687 free(pack_info);
1690 int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
1692 int result = 0;
1693 uint32_t i, packs_to_repack = 0;
1694 unsigned char *include_pack;
1695 struct child_process cmd = CHILD_PROCESS_INIT;
1696 FILE *cmd_in;
1697 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1700 * When updating the default for these configuration
1701 * variables in builtin/repack.c, these must be adjusted
1702 * to match.
1704 int delta_base_offset = 1;
1705 int use_delta_islands = 0;
1707 if (!m)
1708 return 0;
1709 if (m->base_midx)
1710 die(_("cannot repack an incremental multi-pack-index"));
1712 CALLOC_ARRAY(include_pack, m->num_packs);
1714 if (batch_size)
1715 fill_included_packs_batch(r, m, include_pack, batch_size);
1716 else
1717 fill_included_packs_all(r, m, include_pack);
1719 for (i = 0; i < m->num_packs; i++) {
1720 if (include_pack[i])
1721 packs_to_repack++;
1723 if (packs_to_repack <= 1)
1724 goto cleanup;
1726 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
1727 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
1729 strvec_push(&cmd.args, "pack-objects");
1731 strvec_pushf(&cmd.args, "%s/pack/pack", object_dir);
1733 if (delta_base_offset)
1734 strvec_push(&cmd.args, "--delta-base-offset");
1735 if (use_delta_islands)
1736 strvec_push(&cmd.args, "--delta-islands");
1738 if (flags & MIDX_PROGRESS)
1739 strvec_push(&cmd.args, "--progress");
1740 else
1741 strvec_push(&cmd.args, "-q");
1743 cmd.git_cmd = 1;
1744 cmd.in = cmd.out = -1;
1746 if (start_command(&cmd)) {
1747 error(_("could not start pack-objects"));
1748 result = 1;
1749 goto cleanup;
1752 cmd_in = xfdopen(cmd.in, "w");
1754 for (i = 0; i < m->num_objects; i++) {
1755 struct object_id oid;
1756 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1758 if (!include_pack[pack_int_id])
1759 continue;
1761 nth_midxed_object_oid(&oid, m, i);
1762 fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
1764 fclose(cmd_in);
1766 if (finish_command(&cmd)) {
1767 error(_("could not finish pack-objects"));
1768 result = 1;
1769 goto cleanup;
1772 result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
1774 cleanup:
1775 free(include_pack);
1776 return result;