t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / midx-write.c
blob1ef62c4f4bebf6b837b015d97f28e3dda359725c
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 const 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);
666 trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
669 static void prepare_midx_packing_data(struct packing_data *pdata,
670 struct write_midx_context *ctx)
672 uint32_t i;
674 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
676 memset(pdata, 0, sizeof(struct packing_data));
677 prepare_packing_data(the_repository, pdata);
679 for (i = 0; i < ctx->entries_nr; i++) {
680 uint32_t pos = ctx->pack_order[i];
681 struct pack_midx_entry *from = &ctx->entries[pos];
682 struct object_entry *to = packlist_alloc(pdata, &from->oid);
684 oe_set_in_pack(pdata, to,
685 ctx->info[ctx->pack_perm[from->pack_int_id]].p);
688 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
691 static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
692 const struct object_id *oid,
693 int flag, void *cb_data)
695 struct rev_info *revs = (struct rev_info*)cb_data;
696 struct object_id peeled;
697 struct object *object;
699 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
700 warning("symbolic ref is dangling: %s", refname);
701 return 0;
704 if (!peel_iterated_oid(the_repository, oid, &peeled))
705 oid = &peeled;
707 object = parse_object_or_die(oid, refname);
708 if (object->type != OBJ_COMMIT)
709 return 0;
711 add_pending_object(revs, object, "");
712 if (bitmap_is_preferred_refname(revs->repo, refname))
713 object->flags |= NEEDS_BITMAP;
714 return 0;
717 struct bitmap_commit_cb {
718 struct commit **commits;
719 size_t commits_nr, commits_alloc;
721 struct write_midx_context *ctx;
724 static const struct object_id *bitmap_oid_access(size_t index,
725 const void *_entries)
727 const struct pack_midx_entry *entries = _entries;
728 return &entries[index].oid;
731 static void bitmap_show_commit(struct commit *commit, void *_data)
733 struct bitmap_commit_cb *data = _data;
734 int pos = oid_pos(&commit->object.oid, data->ctx->entries,
735 data->ctx->entries_nr,
736 bitmap_oid_access);
737 if (pos < 0)
738 return;
740 ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc);
741 data->commits[data->commits_nr++] = commit;
744 static int read_refs_snapshot(const char *refs_snapshot,
745 struct rev_info *revs)
747 struct strbuf buf = STRBUF_INIT;
748 struct object_id oid;
749 FILE *f = xfopen(refs_snapshot, "r");
751 while (strbuf_getline(&buf, f) != EOF) {
752 struct object *object;
753 int preferred = 0;
754 char *hex = buf.buf;
755 const char *end = NULL;
757 if (buf.len && *buf.buf == '+') {
758 preferred = 1;
759 hex = &buf.buf[1];
762 if (parse_oid_hex(hex, &oid, &end) < 0)
763 die(_("could not parse line: %s"), buf.buf);
764 if (*end)
765 die(_("malformed line: %s"), buf.buf);
767 object = parse_object_or_die(&oid, NULL);
768 if (preferred)
769 object->flags |= NEEDS_BITMAP;
771 add_pending_object(revs, object, "");
774 fclose(f);
775 strbuf_release(&buf);
776 return 0;
778 static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
779 const char *refs_snapshot,
780 struct write_midx_context *ctx)
782 struct rev_info revs;
783 struct bitmap_commit_cb cb = {0};
785 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
786 the_repository);
788 cb.ctx = ctx;
790 repo_init_revisions(the_repository, &revs, NULL);
791 if (refs_snapshot) {
792 read_refs_snapshot(refs_snapshot, &revs);
793 } else {
794 setup_revisions(0, NULL, &revs, NULL);
795 refs_for_each_ref(get_main_ref_store(the_repository),
796 add_ref_to_pending, &revs);
800 * Skipping promisor objects here is intentional, since it only excludes
801 * them from the list of reachable commits that we want to select from
802 * when computing the selection of MIDX'd commits to receive bitmaps.
804 * Reachability bitmaps do require that their objects be closed under
805 * reachability, but fetching any objects missing from promisors at this
806 * point is too late. But, if one of those objects can be reached from
807 * an another object that is included in the bitmap, then we will
808 * complain later that we don't have reachability closure (and fail
809 * appropriately).
811 fetch_if_missing = 0;
812 revs.exclude_promisor_objects = 1;
814 if (prepare_revision_walk(&revs))
815 die(_("revision walk setup failed"));
817 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
818 if (indexed_commits_nr_p)
819 *indexed_commits_nr_p = cb.commits_nr;
821 release_revisions(&revs);
823 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
824 the_repository);
826 return cb.commits;
829 static int write_midx_bitmap(const char *midx_name,
830 const unsigned char *midx_hash,
831 struct packing_data *pdata,
832 struct commit **commits,
833 uint32_t commits_nr,
834 uint32_t *pack_order,
835 unsigned flags)
837 int ret, i;
838 uint16_t options = 0;
839 struct bitmap_writer writer;
840 struct pack_idx_entry **index;
841 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
842 hash_to_hex(midx_hash));
844 trace2_region_enter("midx", "write_midx_bitmap", the_repository);
846 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
847 options |= BITMAP_OPT_HASH_CACHE;
849 if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE)
850 options |= BITMAP_OPT_LOOKUP_TABLE;
853 * Build the MIDX-order index based on pdata.objects (which is already
854 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
855 * this order).
857 ALLOC_ARRAY(index, pdata->nr_objects);
858 for (i = 0; i < pdata->nr_objects; i++)
859 index[i] = &pdata->objects[i].idx;
861 bitmap_writer_init(&writer, the_repository, pdata);
862 bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
863 bitmap_writer_build_type_index(&writer, index);
866 * bitmap_writer_finish expects objects in lex order, but pack_order
867 * gives us exactly that. use it directly instead of re-sorting the
868 * array.
870 * This changes the order of objects in 'index' between
871 * bitmap_writer_build_type_index and bitmap_writer_finish.
873 * The same re-ordering takes place in the single-pack bitmap code via
874 * write_idx_file(), which is called by finish_tmp_packfile(), which
875 * happens between bitmap_writer_build_type_index() and
876 * bitmap_writer_finish().
878 for (i = 0; i < pdata->nr_objects; i++)
879 index[pack_order[i]] = &pdata->objects[i].idx;
881 bitmap_writer_select_commits(&writer, commits, commits_nr);
882 ret = bitmap_writer_build(&writer);
883 if (ret < 0)
884 goto cleanup;
886 bitmap_writer_set_checksum(&writer, midx_hash);
887 bitmap_writer_finish(&writer, index, bitmap_name, options);
889 cleanup:
890 free(index);
891 free(bitmap_name);
892 bitmap_writer_free(&writer);
894 trace2_region_leave("midx", "write_midx_bitmap", the_repository);
896 return ret;
899 static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
900 const char *object_dir)
902 struct multi_pack_index *result = NULL;
903 struct multi_pack_index *cur;
904 char *obj_dir_real = real_pathdup(object_dir, 1);
905 struct strbuf cur_path_real = STRBUF_INIT;
907 /* Ensure the given object_dir is local, or a known alternate. */
908 find_odb(r, obj_dir_real);
910 for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
911 strbuf_realpath(&cur_path_real, cur->object_dir, 1);
912 if (!strcmp(obj_dir_real, cur_path_real.buf)) {
913 result = cur;
914 goto cleanup;
918 cleanup:
919 free(obj_dir_real);
920 strbuf_release(&cur_path_real);
921 return result;
924 static int fill_packs_from_midx(struct write_midx_context *ctx,
925 const char *preferred_pack_name, uint32_t flags)
927 struct multi_pack_index *m;
929 for (m = ctx->m; m; m = m->base_midx) {
930 uint32_t i;
932 for (i = 0; i < m->num_packs; i++) {
933 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
936 * If generating a reverse index, need to have
937 * packed_git's loaded to compare their
938 * mtimes and object count.
940 * If a preferred pack is specified, need to
941 * have packed_git's loaded to ensure the chosen
942 * preferred pack has a non-zero object count.
944 if (flags & MIDX_WRITE_REV_INDEX ||
945 preferred_pack_name) {
946 if (prepare_midx_pack(the_repository, m,
947 m->num_packs_in_base + i)) {
948 error(_("could not load pack"));
949 return 1;
952 if (open_pack_index(m->packs[i]))
953 die(_("could not open index for %s"),
954 m->packs[i]->pack_name);
957 fill_pack_info(&ctx->info[ctx->nr++], m->packs[i],
958 m->pack_names[i],
959 m->num_packs_in_base + i);
962 return 0;
965 static struct {
966 const char *non_split;
967 const char *split;
968 } midx_exts[] = {
969 {NULL, MIDX_EXT_MIDX},
970 {MIDX_EXT_BITMAP, MIDX_EXT_BITMAP},
971 {MIDX_EXT_REV, MIDX_EXT_REV},
974 static int link_midx_to_chain(struct multi_pack_index *m)
976 struct strbuf from = STRBUF_INIT;
977 struct strbuf to = STRBUF_INIT;
978 int ret = 0;
979 size_t i;
981 if (!m || m->has_chain) {
983 * Either no MIDX previously existed, or it was already
984 * part of a MIDX chain. In both cases, we have nothing
985 * to link, so return early.
987 goto done;
990 for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
991 const unsigned char *hash = get_midx_checksum(m);
993 get_midx_filename_ext(&from, m->object_dir, hash,
994 midx_exts[i].non_split);
995 get_split_midx_filename_ext(&to, m->object_dir, hash,
996 midx_exts[i].split);
998 if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
999 ret = error_errno(_("unable to link '%s' to '%s'"),
1000 from.buf, to.buf);
1001 goto done;
1004 strbuf_reset(&from);
1005 strbuf_reset(&to);
1008 done:
1009 strbuf_release(&from);
1010 strbuf_release(&to);
1011 return ret;
1014 static void clear_midx_files(const char *object_dir,
1015 const char **hashes,
1016 uint32_t hashes_nr,
1017 unsigned incremental)
1020 * if incremental:
1021 * - remove all non-incremental MIDX files
1022 * - remove any incremental MIDX files not in the current one
1024 * if non-incremental:
1025 * - remove all incremental MIDX files
1026 * - remove any non-incremental MIDX files not matching the current
1027 * hash
1029 struct strbuf buf = STRBUF_INIT;
1030 const char *exts[] = { MIDX_EXT_BITMAP, MIDX_EXT_REV, MIDX_EXT_MIDX };
1031 uint32_t i, j;
1033 for (i = 0; i < ARRAY_SIZE(exts); i++) {
1034 clear_incremental_midx_files_ext(object_dir, exts[i],
1035 hashes, hashes_nr);
1036 for (j = 0; j < hashes_nr; j++)
1037 clear_midx_files_ext(object_dir, exts[i], hashes[j]);
1040 if (incremental)
1041 get_midx_filename(&buf, object_dir);
1042 else
1043 get_midx_chain_filename(&buf, object_dir);
1045 if (unlink(buf.buf) && errno != ENOENT)
1046 die_errno(_("failed to clear multi-pack-index at %s"), buf.buf);
1048 strbuf_release(&buf);
1051 static int write_midx_internal(const char *object_dir,
1052 struct string_list *packs_to_include,
1053 struct string_list *packs_to_drop,
1054 const char *preferred_pack_name,
1055 const char *refs_snapshot,
1056 unsigned flags)
1058 struct strbuf midx_name = STRBUF_INIT;
1059 unsigned char midx_hash[GIT_MAX_RAWSZ];
1060 uint32_t i, start_pack;
1061 struct hashfile *f = NULL;
1062 struct lock_file lk;
1063 struct tempfile *incr;
1064 struct write_midx_context ctx = { 0 };
1065 int bitmapped_packs_concat_len = 0;
1066 int pack_name_concat_len = 0;
1067 int dropped_packs = 0;
1068 int result = 0;
1069 const char **keep_hashes = NULL;
1070 struct chunkfile *cf;
1072 trace2_region_enter("midx", "write_midx_internal", the_repository);
1074 ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
1075 if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
1076 die(_("cannot write incremental MIDX with bitmap"));
1078 if (ctx.incremental)
1079 strbuf_addf(&midx_name,
1080 "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
1081 object_dir);
1082 else
1083 get_midx_filename(&midx_name, object_dir);
1084 if (safe_create_leading_directories(midx_name.buf))
1085 die_errno(_("unable to create leading directories of %s"),
1086 midx_name.buf);
1088 if (!packs_to_include || ctx.incremental) {
1089 struct multi_pack_index *m = lookup_multi_pack_index(the_repository,
1090 object_dir);
1091 if (m && !midx_checksum_valid(m)) {
1092 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1093 m = NULL;
1096 if (m) {
1098 * Only reference an existing MIDX when not filtering
1099 * which packs to include, since all packs and objects
1100 * are copied blindly from an existing MIDX if one is
1101 * present.
1103 if (ctx.incremental)
1104 ctx.base_midx = m;
1105 else if (!packs_to_include)
1106 ctx.m = m;
1110 ctx.nr = 0;
1111 ctx.alloc = ctx.m ? ctx.m->num_packs + ctx.m->num_packs_in_base : 16;
1112 ctx.info = NULL;
1113 ALLOC_ARRAY(ctx.info, ctx.alloc);
1115 if (ctx.incremental) {
1116 struct multi_pack_index *m = ctx.base_midx;
1117 while (m) {
1118 ctx.num_multi_pack_indexes_before++;
1119 m = m->base_midx;
1121 } else if (ctx.m && fill_packs_from_midx(&ctx, preferred_pack_name,
1122 flags) < 0) {
1123 goto cleanup;
1126 start_pack = ctx.nr;
1128 ctx.pack_paths_checked = 0;
1129 if (flags & MIDX_PROGRESS)
1130 ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
1131 else
1132 ctx.progress = NULL;
1134 ctx.to_include = packs_to_include;
1136 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
1137 stop_progress(&ctx.progress);
1139 if ((ctx.m && ctx.nr == ctx.m->num_packs + ctx.m->num_packs_in_base) &&
1140 !ctx.incremental &&
1141 !(packs_to_include || packs_to_drop)) {
1142 struct bitmap_index *bitmap_git;
1143 int bitmap_exists;
1144 int want_bitmap = flags & MIDX_WRITE_BITMAP;
1146 bitmap_git = prepare_midx_bitmap_git(ctx.m);
1147 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
1148 free_bitmap_index(bitmap_git);
1150 if (bitmap_exists || !want_bitmap) {
1152 * The correct MIDX already exists, and so does a
1153 * corresponding bitmap (or one wasn't requested).
1155 if (!want_bitmap)
1156 clear_midx_files_ext(object_dir, "bitmap", NULL);
1157 goto cleanup;
1161 if (ctx.incremental && !ctx.nr)
1162 goto cleanup; /* nothing to do */
1164 if (preferred_pack_name) {
1165 ctx.preferred_pack_idx = -1;
1167 for (i = 0; i < ctx.nr; i++) {
1168 if (!cmp_idx_or_pack_name(preferred_pack_name,
1169 ctx.info[i].pack_name)) {
1170 ctx.preferred_pack_idx = i;
1171 break;
1175 if (ctx.preferred_pack_idx == -1)
1176 warning(_("unknown preferred pack: '%s'"),
1177 preferred_pack_name);
1178 } else if (ctx.nr &&
1179 (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1180 struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1181 ctx.preferred_pack_idx = 0;
1183 if (packs_to_drop && packs_to_drop->nr)
1184 BUG("cannot write a MIDX bitmap during expiration");
1187 * set a preferred pack when writing a bitmap to ensure that
1188 * the pack from which the first object is selected in pseudo
1189 * pack-order has all of its objects selected from that pack
1190 * (and not another pack containing a duplicate)
1192 for (i = 1; i < ctx.nr; i++) {
1193 struct packed_git *p = ctx.info[i].p;
1195 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1196 oldest = p;
1197 ctx.preferred_pack_idx = i;
1201 if (!oldest->num_objects) {
1203 * If all packs are empty; unset the preferred index.
1204 * This is acceptable since there will be no duplicate
1205 * objects to resolve, so the preferred value doesn't
1206 * matter.
1208 ctx.preferred_pack_idx = -1;
1210 } else {
1212 * otherwise don't mark any pack as preferred to avoid
1213 * interfering with expiration logic below
1215 ctx.preferred_pack_idx = -1;
1218 if (ctx.preferred_pack_idx > -1) {
1219 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1220 if (!preferred->num_objects) {
1221 error(_("cannot select preferred pack %s with no objects"),
1222 preferred->pack_name);
1223 result = 1;
1224 goto cleanup;
1228 compute_sorted_entries(&ctx, start_pack);
1230 ctx.large_offsets_needed = 0;
1231 for (i = 0; i < ctx.entries_nr; i++) {
1232 if (ctx.entries[i].offset > 0x7fffffff)
1233 ctx.num_large_offsets++;
1234 if (ctx.entries[i].offset > 0xffffffff)
1235 ctx.large_offsets_needed = 1;
1238 QSORT(ctx.info, ctx.nr, pack_info_compare);
1240 if (packs_to_drop && packs_to_drop->nr) {
1241 int drop_index = 0;
1242 int missing_drops = 0;
1244 for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1245 int cmp = strcmp(ctx.info[i].pack_name,
1246 packs_to_drop->items[drop_index].string);
1248 if (!cmp) {
1249 drop_index++;
1250 ctx.info[i].expired = 1;
1251 } else if (cmp > 0) {
1252 error(_("did not see pack-file %s to drop"),
1253 packs_to_drop->items[drop_index].string);
1254 drop_index++;
1255 missing_drops++;
1256 i--;
1257 } else {
1258 ctx.info[i].expired = 0;
1262 if (missing_drops) {
1263 result = 1;
1264 goto cleanup;
1269 * pack_perm stores a permutation between pack-int-ids from the
1270 * previous multi-pack-index to the new one we are writing:
1272 * pack_perm[old_id] = new_id
1274 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1275 for (i = 0; i < ctx.nr; i++) {
1276 if (ctx.info[i].expired) {
1277 dropped_packs++;
1278 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
1279 } else {
1280 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
1284 for (i = 0; i < ctx.nr; i++) {
1285 if (ctx.info[i].expired)
1286 continue;
1287 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
1288 bitmapped_packs_concat_len += 2 * sizeof(uint32_t);
1291 /* Check that the preferred pack wasn't expired (if given). */
1292 if (preferred_pack_name) {
1293 struct pack_info *preferred = bsearch(preferred_pack_name,
1294 ctx.info, ctx.nr,
1295 sizeof(*ctx.info),
1296 idx_or_pack_name_cmp);
1297 if (preferred) {
1298 uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1299 if (perm == PACK_EXPIRED)
1300 warning(_("preferred pack '%s' is expired"),
1301 preferred_pack_name);
1305 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1306 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1307 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1309 if (ctx.nr - dropped_packs == 0) {
1310 error(_("no pack files to index."));
1311 result = 1;
1312 goto cleanup;
1315 if (!ctx.entries_nr) {
1316 if (flags & MIDX_WRITE_BITMAP)
1317 warning(_("refusing to write multi-pack .bitmap without any objects"));
1318 flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1321 if (ctx.incremental) {
1322 struct strbuf lock_name = STRBUF_INIT;
1324 get_midx_chain_filename(&lock_name, object_dir);
1325 hold_lock_file_for_update(&lk, lock_name.buf, LOCK_DIE_ON_ERROR);
1326 strbuf_release(&lock_name);
1328 incr = mks_tempfile_m(midx_name.buf, 0444);
1329 if (!incr) {
1330 error(_("unable to create temporary MIDX layer"));
1331 return -1;
1334 if (adjust_shared_perm(get_tempfile_path(incr))) {
1335 error(_("unable to adjust shared permissions for '%s'"),
1336 get_tempfile_path(incr));
1337 return -1;
1340 f = hashfd(get_tempfile_fd(incr), get_tempfile_path(incr));
1341 } else {
1342 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
1343 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
1346 cf = init_chunkfile(f);
1348 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1349 write_midx_pack_names);
1350 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1351 write_midx_oid_fanout);
1352 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
1353 st_mult(ctx.entries_nr, the_hash_algo->rawsz),
1354 write_midx_oid_lookup);
1355 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
1356 st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
1357 write_midx_object_offsets);
1359 if (ctx.large_offsets_needed)
1360 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
1361 st_mult(ctx.num_large_offsets,
1362 MIDX_CHUNK_LARGE_OFFSET_WIDTH),
1363 write_midx_large_offsets);
1365 if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) {
1366 ctx.pack_order = midx_pack_order(&ctx);
1367 add_chunk(cf, MIDX_CHUNKID_REVINDEX,
1368 st_mult(ctx.entries_nr, sizeof(uint32_t)),
1369 write_midx_revindex);
1370 add_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
1371 bitmapped_packs_concat_len,
1372 write_midx_bitmapped_packs);
1375 write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1376 write_chunkfile(cf, &ctx);
1378 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
1379 CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1380 free_chunkfile(cf);
1382 if (flags & MIDX_WRITE_REV_INDEX &&
1383 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1384 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
1386 if (flags & MIDX_WRITE_BITMAP) {
1387 struct packing_data pdata;
1388 struct commit **commits;
1389 uint32_t commits_nr;
1391 if (!ctx.entries_nr)
1392 BUG("cannot write a bitmap without any objects");
1394 prepare_midx_packing_data(&pdata, &ctx);
1396 commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1399 * The previous steps translated the information from
1400 * 'entries' into information suitable for constructing
1401 * bitmaps. We no longer need that array, so clear it to
1402 * reduce memory pressure.
1404 FREE_AND_NULL(ctx.entries);
1405 ctx.entries_nr = 0;
1407 if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1408 commits, commits_nr, ctx.pack_order,
1409 flags) < 0) {
1410 error(_("could not write multi-pack bitmap"));
1411 result = 1;
1412 clear_packing_data(&pdata);
1413 free(commits);
1414 goto cleanup;
1417 clear_packing_data(&pdata);
1418 free(commits);
1421 * NOTE: Do not use ctx.entries beyond this point, since it might
1422 * have been freed in the previous if block.
1425 CALLOC_ARRAY(keep_hashes, ctx.num_multi_pack_indexes_before + 1);
1427 if (ctx.incremental) {
1428 FILE *chainf = fdopen_lock_file(&lk, "w");
1429 struct strbuf final_midx_name = STRBUF_INIT;
1430 struct multi_pack_index *m = ctx.base_midx;
1432 if (!chainf) {
1433 error_errno(_("unable to open multi-pack-index chain file"));
1434 return -1;
1437 if (link_midx_to_chain(ctx.base_midx) < 0)
1438 return -1;
1440 get_split_midx_filename_ext(&final_midx_name, object_dir,
1441 midx_hash, MIDX_EXT_MIDX);
1443 if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
1444 error_errno(_("unable to rename new multi-pack-index layer"));
1445 return -1;
1448 keep_hashes[ctx.num_multi_pack_indexes_before] =
1449 xstrdup(hash_to_hex(midx_hash));
1451 for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
1452 uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
1454 keep_hashes[j] = xstrdup(hash_to_hex(get_midx_checksum(m)));
1455 m = m->base_midx;
1458 for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1459 fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
1460 } else {
1461 keep_hashes[ctx.num_multi_pack_indexes_before] =
1462 xstrdup(hash_to_hex(midx_hash));
1465 if (ctx.m || ctx.base_midx)
1466 close_object_store(the_repository->objects);
1468 if (commit_lock_file(&lk) < 0)
1469 die_errno(_("could not write multi-pack-index"));
1471 clear_midx_files(object_dir, keep_hashes,
1472 ctx.num_multi_pack_indexes_before + 1,
1473 ctx.incremental);
1475 cleanup:
1476 for (i = 0; i < ctx.nr; i++) {
1477 if (ctx.info[i].p) {
1478 close_pack(ctx.info[i].p);
1479 free(ctx.info[i].p);
1481 free(ctx.info[i].pack_name);
1484 free(ctx.info);
1485 free(ctx.entries);
1486 free(ctx.pack_perm);
1487 free(ctx.pack_order);
1488 if (keep_hashes) {
1489 for (i = 0; i < ctx.num_multi_pack_indexes_before + 1; i++)
1490 free((char *)keep_hashes[i]);
1491 free(keep_hashes);
1493 strbuf_release(&midx_name);
1495 trace2_region_leave("midx", "write_midx_internal", the_repository);
1497 return result;
1500 int write_midx_file(const char *object_dir,
1501 const char *preferred_pack_name,
1502 const char *refs_snapshot,
1503 unsigned flags)
1505 return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
1506 refs_snapshot, flags);
1509 int write_midx_file_only(const char *object_dir,
1510 struct string_list *packs_to_include,
1511 const char *preferred_pack_name,
1512 const char *refs_snapshot,
1513 unsigned flags)
1515 return write_midx_internal(object_dir, packs_to_include, NULL,
1516 preferred_pack_name, refs_snapshot, flags);
1519 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
1521 uint32_t i, *count, result = 0;
1522 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1523 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1524 struct progress *progress = NULL;
1526 if (!m)
1527 return 0;
1529 if (m->base_midx)
1530 die(_("cannot expire packs from an incremental multi-pack-index"));
1532 CALLOC_ARRAY(count, m->num_packs);
1534 if (flags & MIDX_PROGRESS)
1535 progress = start_delayed_progress(_("Counting referenced objects"),
1536 m->num_objects);
1537 for (i = 0; i < m->num_objects; i++) {
1538 int pack_int_id = nth_midxed_pack_int_id(m, i);
1539 count[pack_int_id]++;
1540 display_progress(progress, i + 1);
1542 stop_progress(&progress);
1544 if (flags & MIDX_PROGRESS)
1545 progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1546 m->num_packs);
1547 for (i = 0; i < m->num_packs; i++) {
1548 char *pack_name;
1549 display_progress(progress, i + 1);
1551 if (count[i])
1552 continue;
1554 if (prepare_midx_pack(r, m, i))
1555 continue;
1557 if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
1558 continue;
1560 pack_name = xstrdup(m->packs[i]->pack_name);
1561 close_pack(m->packs[i]);
1563 string_list_insert(&packs_to_drop, m->pack_names[i]);
1564 unlink_pack_path(pack_name, 0);
1565 free(pack_name);
1567 stop_progress(&progress);
1569 free(count);
1571 if (packs_to_drop.nr)
1572 result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
1574 string_list_clear(&packs_to_drop, 0);
1576 return result;
1579 struct repack_info {
1580 timestamp_t mtime;
1581 uint32_t referenced_objects;
1582 uint32_t pack_int_id;
1585 static int compare_by_mtime(const void *a_, const void *b_)
1587 const struct repack_info *a, *b;
1589 a = (const struct repack_info *)a_;
1590 b = (const struct repack_info *)b_;
1592 if (a->mtime < b->mtime)
1593 return -1;
1594 if (a->mtime > b->mtime)
1595 return 1;
1596 return 0;
1599 static int want_included_pack(struct repository *r,
1600 struct multi_pack_index *m,
1601 int pack_kept_objects,
1602 uint32_t pack_int_id)
1604 struct packed_git *p;
1605 if (prepare_midx_pack(r, m, pack_int_id))
1606 return 0;
1607 p = m->packs[pack_int_id];
1608 if (!pack_kept_objects && p->pack_keep)
1609 return 0;
1610 if (p->is_cruft)
1611 return 0;
1612 if (open_pack_index(p) || !p->num_objects)
1613 return 0;
1614 return 1;
1617 static void fill_included_packs_all(struct repository *r,
1618 struct multi_pack_index *m,
1619 unsigned char *include_pack)
1621 uint32_t i;
1622 int pack_kept_objects = 0;
1624 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1626 for (i = 0; i < m->num_packs; i++) {
1627 if (!want_included_pack(r, m, pack_kept_objects, i))
1628 continue;
1630 include_pack[i] = 1;
1634 static void fill_included_packs_batch(struct repository *r,
1635 struct multi_pack_index *m,
1636 unsigned char *include_pack,
1637 size_t batch_size)
1639 uint32_t i;
1640 size_t total_size;
1641 struct repack_info *pack_info;
1642 int pack_kept_objects = 0;
1644 CALLOC_ARRAY(pack_info, m->num_packs);
1646 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1648 for (i = 0; i < m->num_packs; i++) {
1649 pack_info[i].pack_int_id = i;
1651 if (prepare_midx_pack(r, m, i))
1652 continue;
1654 pack_info[i].mtime = m->packs[i]->mtime;
1657 for (i = 0; i < m->num_objects; i++) {
1658 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1659 pack_info[pack_int_id].referenced_objects++;
1662 QSORT(pack_info, m->num_packs, compare_by_mtime);
1664 total_size = 0;
1665 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1666 int pack_int_id = pack_info[i].pack_int_id;
1667 struct packed_git *p = m->packs[pack_int_id];
1668 size_t expected_size;
1670 if (!want_included_pack(r, m, pack_kept_objects, pack_int_id))
1671 continue;
1673 expected_size = st_mult(p->pack_size,
1674 pack_info[i].referenced_objects);
1675 expected_size /= p->num_objects;
1677 if (expected_size >= batch_size)
1678 continue;
1680 total_size += expected_size;
1681 include_pack[pack_int_id] = 1;
1684 free(pack_info);
1687 int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
1689 int result = 0;
1690 uint32_t i, packs_to_repack = 0;
1691 unsigned char *include_pack;
1692 struct child_process cmd = CHILD_PROCESS_INIT;
1693 FILE *cmd_in;
1694 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1697 * When updating the default for these configuration
1698 * variables in builtin/repack.c, these must be adjusted
1699 * to match.
1701 int delta_base_offset = 1;
1702 int use_delta_islands = 0;
1704 if (!m)
1705 return 0;
1706 if (m->base_midx)
1707 die(_("cannot repack an incremental multi-pack-index"));
1709 CALLOC_ARRAY(include_pack, m->num_packs);
1711 if (batch_size)
1712 fill_included_packs_batch(r, m, include_pack, batch_size);
1713 else
1714 fill_included_packs_all(r, m, include_pack);
1716 for (i = 0; i < m->num_packs; i++) {
1717 if (include_pack[i])
1718 packs_to_repack++;
1720 if (packs_to_repack <= 1)
1721 goto cleanup;
1723 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
1724 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
1726 strvec_push(&cmd.args, "pack-objects");
1728 strvec_pushf(&cmd.args, "%s/pack/pack", object_dir);
1730 if (delta_base_offset)
1731 strvec_push(&cmd.args, "--delta-base-offset");
1732 if (use_delta_islands)
1733 strvec_push(&cmd.args, "--delta-islands");
1735 if (flags & MIDX_PROGRESS)
1736 strvec_push(&cmd.args, "--progress");
1737 else
1738 strvec_push(&cmd.args, "-q");
1740 cmd.git_cmd = 1;
1741 cmd.in = cmd.out = -1;
1743 if (start_command(&cmd)) {
1744 error(_("could not start pack-objects"));
1745 result = 1;
1746 goto cleanup;
1749 cmd_in = xfdopen(cmd.in, "w");
1751 for (i = 0; i < m->num_objects; i++) {
1752 struct object_id oid;
1753 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1755 if (!include_pack[pack_int_id])
1756 continue;
1758 nth_midxed_object_oid(&oid, m, i);
1759 fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
1761 fclose(cmd_in);
1763 if (finish_command(&cmd)) {
1764 error(_("could not finish pack-objects"));
1765 result = 1;
1766 goto cleanup;
1769 result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
1771 cleanup:
1772 free(include_pack);
1773 return result;