Merge branch 'ja/doc-placeholders-markup-rules' into HEAD
[alt-git.git] / midx.c
blob85e1c2cd1287b34e91d9dcccc74d330c93ff809c
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "config.h"
4 #include "csum-file.h"
5 #include "dir.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "lockfile.h"
9 #include "packfile.h"
10 #include "object-file.h"
11 #include "object-store-ll.h"
12 #include "hash-lookup.h"
13 #include "midx.h"
14 #include "progress.h"
15 #include "trace2.h"
16 #include "run-command.h"
17 #include "repository.h"
18 #include "chunk-format.h"
19 #include "pack.h"
20 #include "pack-bitmap.h"
21 #include "refs.h"
22 #include "revision.h"
23 #include "list-objects.h"
24 #include "pack-revindex.h"
26 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
27 #define MIDX_VERSION 1
28 #define MIDX_BYTE_FILE_VERSION 4
29 #define MIDX_BYTE_HASH_VERSION 5
30 #define MIDX_BYTE_NUM_CHUNKS 6
31 #define MIDX_BYTE_NUM_PACKS 8
32 #define MIDX_HEADER_SIZE 12
33 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
35 #define MIDX_CHUNK_ALIGNMENT 4
36 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
37 #define MIDX_CHUNKID_BITMAPPEDPACKS 0x42544d50 /* "BTMP" */
38 #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
39 #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
40 #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
41 #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
42 #define MIDX_CHUNKID_REVINDEX 0x52494458 /* "RIDX" */
43 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
44 #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
45 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
46 #define MIDX_CHUNK_BITMAPPED_PACKS_WIDTH (2 * sizeof(uint32_t))
47 #define MIDX_LARGE_OFFSET_NEEDED 0x80000000
49 #define PACK_EXPIRED UINT_MAX
51 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
53 return m->data + m->data_len - the_hash_algo->rawsz;
56 void get_midx_filename(struct strbuf *out, const char *object_dir)
58 strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
61 void get_midx_rev_filename(struct strbuf *out, struct multi_pack_index *m)
63 get_midx_filename(out, m->object_dir);
64 strbuf_addf(out, "-%s.rev", hash_to_hex(get_midx_checksum(m)));
67 static int midx_read_oid_fanout(const unsigned char *chunk_start,
68 size_t chunk_size, void *data)
70 int i;
71 struct multi_pack_index *m = data;
72 m->chunk_oid_fanout = (uint32_t *)chunk_start;
74 if (chunk_size != 4 * 256) {
75 error(_("multi-pack-index OID fanout is of the wrong size"));
76 return 1;
78 for (i = 0; i < 255; i++) {
79 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
80 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i+1]);
82 if (oid_fanout1 > oid_fanout2) {
83 error(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
84 i, oid_fanout1, oid_fanout2, i + 1);
85 return 1;
88 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
89 return 0;
92 static int midx_read_oid_lookup(const unsigned char *chunk_start,
93 size_t chunk_size, void *data)
95 struct multi_pack_index *m = data;
96 m->chunk_oid_lookup = chunk_start;
98 if (chunk_size != st_mult(m->hash_len, m->num_objects)) {
99 error(_("multi-pack-index OID lookup chunk is the wrong size"));
100 return 1;
102 return 0;
105 static int midx_read_object_offsets(const unsigned char *chunk_start,
106 size_t chunk_size, void *data)
108 struct multi_pack_index *m = data;
109 m->chunk_object_offsets = chunk_start;
111 if (chunk_size != st_mult(m->num_objects, MIDX_CHUNK_OFFSET_WIDTH)) {
112 error(_("multi-pack-index object offset chunk is the wrong size"));
113 return 1;
115 return 0;
118 struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
120 struct multi_pack_index *m = NULL;
121 int fd;
122 struct stat st;
123 size_t midx_size;
124 void *midx_map = NULL;
125 uint32_t hash_version;
126 struct strbuf midx_name = STRBUF_INIT;
127 uint32_t i;
128 const char *cur_pack_name;
129 struct chunkfile *cf = NULL;
131 get_midx_filename(&midx_name, object_dir);
133 fd = git_open(midx_name.buf);
135 if (fd < 0)
136 goto cleanup_fail;
137 if (fstat(fd, &st)) {
138 error_errno(_("failed to read %s"), midx_name.buf);
139 goto cleanup_fail;
142 midx_size = xsize_t(st.st_size);
144 if (midx_size < MIDX_MIN_SIZE) {
145 error(_("multi-pack-index file %s is too small"), midx_name.buf);
146 goto cleanup_fail;
149 strbuf_release(&midx_name);
151 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
152 close(fd);
154 FLEX_ALLOC_STR(m, object_dir, object_dir);
155 m->data = midx_map;
156 m->data_len = midx_size;
157 m->local = local;
159 m->signature = get_be32(m->data);
160 if (m->signature != MIDX_SIGNATURE)
161 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
162 m->signature, MIDX_SIGNATURE);
164 m->version = m->data[MIDX_BYTE_FILE_VERSION];
165 if (m->version != MIDX_VERSION)
166 die(_("multi-pack-index version %d not recognized"),
167 m->version);
169 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
170 if (hash_version != oid_version(the_hash_algo)) {
171 error(_("multi-pack-index hash version %u does not match version %u"),
172 hash_version, oid_version(the_hash_algo));
173 goto cleanup_fail;
175 m->hash_len = the_hash_algo->rawsz;
177 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
179 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
181 m->preferred_pack_idx = -1;
183 cf = init_chunkfile(NULL);
185 if (read_table_of_contents(cf, m->data, midx_size,
186 MIDX_HEADER_SIZE, m->num_chunks,
187 MIDX_CHUNK_ALIGNMENT))
188 goto cleanup_fail;
190 if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names, &m->chunk_pack_names_len))
191 die(_("multi-pack-index required pack-name chunk missing or corrupted"));
192 if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m))
193 die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
194 if (read_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, midx_read_oid_lookup, m))
195 die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
196 if (read_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, midx_read_object_offsets, m))
197 die(_("multi-pack-index required object offsets chunk missing or corrupted"));
199 pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets,
200 &m->chunk_large_offsets_len);
201 pair_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
202 (const unsigned char **)&m->chunk_bitmapped_packs,
203 &m->chunk_bitmapped_packs_len);
205 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
206 pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex,
207 &m->chunk_revindex_len);
209 CALLOC_ARRAY(m->pack_names, m->num_packs);
210 CALLOC_ARRAY(m->packs, m->num_packs);
212 cur_pack_name = (const char *)m->chunk_pack_names;
213 for (i = 0; i < m->num_packs; i++) {
214 const char *end;
215 size_t avail = m->chunk_pack_names_len -
216 (cur_pack_name - (const char *)m->chunk_pack_names);
218 m->pack_names[i] = cur_pack_name;
220 end = memchr(cur_pack_name, '\0', avail);
221 if (!end)
222 die(_("multi-pack-index pack-name chunk is too short"));
223 cur_pack_name = end + 1;
225 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
226 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
227 m->pack_names[i - 1],
228 m->pack_names[i]);
231 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
232 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
234 free_chunkfile(cf);
235 return m;
237 cleanup_fail:
238 free(m);
239 strbuf_release(&midx_name);
240 free_chunkfile(cf);
241 if (midx_map)
242 munmap(midx_map, midx_size);
243 if (0 <= fd)
244 close(fd);
245 return NULL;
248 void close_midx(struct multi_pack_index *m)
250 uint32_t i;
252 if (!m)
253 return;
255 close_midx(m->next);
257 munmap((unsigned char *)m->data, m->data_len);
259 for (i = 0; i < m->num_packs; i++) {
260 if (m->packs[i])
261 m->packs[i]->multi_pack_index = 0;
263 FREE_AND_NULL(m->packs);
264 FREE_AND_NULL(m->pack_names);
265 free(m);
268 int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
270 struct strbuf pack_name = STRBUF_INIT;
271 struct packed_git *p;
273 if (pack_int_id >= m->num_packs)
274 die(_("bad pack-int-id: %u (%u total packs)"),
275 pack_int_id, m->num_packs);
277 if (m->packs[pack_int_id])
278 return 0;
280 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
281 m->pack_names[pack_int_id]);
283 p = add_packed_git(pack_name.buf, pack_name.len, m->local);
284 strbuf_release(&pack_name);
286 if (!p)
287 return 1;
289 p->multi_pack_index = 1;
290 m->packs[pack_int_id] = p;
291 install_packed_git(r, p);
292 list_add_tail(&p->mru, &r->objects->packed_git_mru);
294 return 0;
297 int nth_bitmapped_pack(struct repository *r, struct multi_pack_index *m,
298 struct bitmapped_pack *bp, uint32_t pack_int_id)
300 if (!m->chunk_bitmapped_packs)
301 return error(_("MIDX does not contain the BTMP chunk"));
303 if (prepare_midx_pack(r, m, pack_int_id))
304 return error(_("could not load bitmapped pack %"PRIu32), pack_int_id);
306 bp->p = m->packs[pack_int_id];
307 bp->bitmap_pos = get_be32((char *)m->chunk_bitmapped_packs +
308 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * pack_int_id);
309 bp->bitmap_nr = get_be32((char *)m->chunk_bitmapped_packs +
310 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * pack_int_id +
311 sizeof(uint32_t));
312 bp->pack_int_id = pack_int_id;
314 return 0;
317 int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
319 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
320 the_hash_algo->rawsz, result);
323 struct object_id *nth_midxed_object_oid(struct object_id *oid,
324 struct multi_pack_index *m,
325 uint32_t n)
327 if (n >= m->num_objects)
328 return NULL;
330 oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n));
331 return oid;
334 off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
336 const unsigned char *offset_data;
337 uint32_t offset32;
339 offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
340 offset32 = get_be32(offset_data + sizeof(uint32_t));
342 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
343 if (sizeof(off_t) < sizeof(uint64_t))
344 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
346 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
347 if (offset32 >= m->chunk_large_offsets_len / sizeof(uint64_t))
348 die(_("multi-pack-index large offset out of bounds"));
349 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
352 return offset32;
355 uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
357 return get_be32(m->chunk_object_offsets +
358 (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
361 int fill_midx_entry(struct repository *r,
362 const struct object_id *oid,
363 struct pack_entry *e,
364 struct multi_pack_index *m)
366 uint32_t pos;
367 uint32_t pack_int_id;
368 struct packed_git *p;
370 if (!bsearch_midx(oid, m, &pos))
371 return 0;
373 if (pos >= m->num_objects)
374 return 0;
376 pack_int_id = nth_midxed_pack_int_id(m, pos);
378 if (prepare_midx_pack(r, m, pack_int_id))
379 return 0;
380 p = m->packs[pack_int_id];
383 * We are about to tell the caller where they can locate the
384 * requested object. We better make sure the packfile is
385 * still here and can be accessed before supplying that
386 * answer, as it may have been deleted since the MIDX was
387 * loaded!
389 if (!is_pack_valid(p))
390 return 0;
392 if (oidset_size(&p->bad_objects) &&
393 oidset_contains(&p->bad_objects, oid))
394 return 0;
396 e->offset = nth_midxed_offset(m, pos);
397 e->p = p;
399 return 1;
402 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
403 static int cmp_idx_or_pack_name(const char *idx_or_pack_name,
404 const char *idx_name)
406 /* Skip past any initial matching prefix. */
407 while (*idx_name && *idx_name == *idx_or_pack_name) {
408 idx_name++;
409 idx_or_pack_name++;
413 * If we didn't match completely, we may have matched "pack-1234." and
414 * be left with "idx" and "pack" respectively, which is also OK. We do
415 * not have to check for "idx" and "idx", because that would have been
416 * a complete match (and in that case these strcmps will be false, but
417 * we'll correctly return 0 from the final strcmp() below.
419 * Technically this matches "fooidx" and "foopack", but we'd never have
420 * such names in the first place.
422 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
423 return 0;
426 * This not only checks for a complete match, but also orders based on
427 * the first non-identical character, which means our ordering will
428 * match a raw strcmp(). That makes it OK to use this to binary search
429 * a naively-sorted list.
431 return strcmp(idx_or_pack_name, idx_name);
434 int midx_locate_pack(struct multi_pack_index *m, const char *idx_or_pack_name,
435 uint32_t *pos)
437 uint32_t first = 0, last = m->num_packs;
439 while (first < last) {
440 uint32_t mid = first + (last - first) / 2;
441 const char *current;
442 int cmp;
444 current = m->pack_names[mid];
445 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
446 if (!cmp) {
447 if (pos)
448 *pos = mid;
449 return 1;
451 if (cmp > 0) {
452 first = mid + 1;
453 continue;
455 last = mid;
458 return 0;
461 int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
463 return midx_locate_pack(m, idx_or_pack_name, NULL);
466 int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
468 if (m->preferred_pack_idx == -1) {
469 if (load_midx_revindex(m) < 0) {
470 m->preferred_pack_idx = -2;
471 return -1;
474 m->preferred_pack_idx =
475 nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0));
476 } else if (m->preferred_pack_idx == -2)
477 return -1; /* no revindex */
479 *pack_int_id = m->preferred_pack_idx;
480 return 0;
483 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
485 struct multi_pack_index *m;
486 struct multi_pack_index *m_search;
488 prepare_repo_settings(r);
489 if (!r->settings.core_multi_pack_index)
490 return 0;
492 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
493 if (!strcmp(object_dir, m_search->object_dir))
494 return 1;
496 m = load_multi_pack_index(object_dir, local);
498 if (m) {
499 struct multi_pack_index *mp = r->objects->multi_pack_index;
500 if (mp) {
501 m->next = mp->next;
502 mp->next = m;
503 } else
504 r->objects->multi_pack_index = m;
505 return 1;
508 return 0;
511 static size_t write_midx_header(struct hashfile *f,
512 unsigned char num_chunks,
513 uint32_t num_packs)
515 hashwrite_be32(f, MIDX_SIGNATURE);
516 hashwrite_u8(f, MIDX_VERSION);
517 hashwrite_u8(f, oid_version(the_hash_algo));
518 hashwrite_u8(f, num_chunks);
519 hashwrite_u8(f, 0); /* unused */
520 hashwrite_be32(f, num_packs);
522 return MIDX_HEADER_SIZE;
525 #define BITMAP_POS_UNKNOWN (~((uint32_t)0))
527 struct pack_info {
528 uint32_t orig_pack_int_id;
529 char *pack_name;
530 struct packed_git *p;
532 uint32_t bitmap_pos;
533 uint32_t bitmap_nr;
535 unsigned expired : 1;
538 static void fill_pack_info(struct pack_info *info,
539 struct packed_git *p, const char *pack_name,
540 uint32_t orig_pack_int_id)
542 memset(info, 0, sizeof(struct pack_info));
544 info->orig_pack_int_id = orig_pack_int_id;
545 info->pack_name = xstrdup(pack_name);
546 info->p = p;
547 info->bitmap_pos = BITMAP_POS_UNKNOWN;
550 static int pack_info_compare(const void *_a, const void *_b)
552 struct pack_info *a = (struct pack_info *)_a;
553 struct pack_info *b = (struct pack_info *)_b;
554 return strcmp(a->pack_name, b->pack_name);
557 static int idx_or_pack_name_cmp(const void *_va, const void *_vb)
559 const char *pack_name = _va;
560 const struct pack_info *compar = _vb;
562 return cmp_idx_or_pack_name(pack_name, compar->pack_name);
565 struct write_midx_context {
566 struct pack_info *info;
567 size_t nr;
568 size_t alloc;
569 struct multi_pack_index *m;
570 struct progress *progress;
571 unsigned pack_paths_checked;
573 struct pack_midx_entry *entries;
574 size_t entries_nr;
576 uint32_t *pack_perm;
577 uint32_t *pack_order;
578 unsigned large_offsets_needed:1;
579 uint32_t num_large_offsets;
581 int preferred_pack_idx;
583 struct string_list *to_include;
586 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
587 const char *file_name, void *data)
589 struct write_midx_context *ctx = data;
590 struct packed_git *p;
592 if (ends_with(file_name, ".idx")) {
593 display_progress(ctx->progress, ++ctx->pack_paths_checked);
595 * Note that at most one of ctx->m and ctx->to_include are set,
596 * so we are testing midx_contains_pack() and
597 * string_list_has_string() independently (guarded by the
598 * appropriate NULL checks).
600 * We could support passing to_include while reusing an existing
601 * MIDX, but don't currently since the reuse process drags
602 * forward all packs from an existing MIDX (without checking
603 * whether or not they appear in the to_include list).
605 * If we added support for that, these next two conditional
606 * should be performed independently (likely checking
607 * to_include before the existing MIDX).
609 if (ctx->m && midx_contains_pack(ctx->m, file_name))
610 return;
611 else if (ctx->to_include &&
612 !string_list_has_string(ctx->to_include, file_name))
613 return;
615 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
617 p = add_packed_git(full_path, full_path_len, 0);
618 if (!p) {
619 warning(_("failed to add packfile '%s'"),
620 full_path);
621 return;
624 if (open_pack_index(p)) {
625 warning(_("failed to open pack-index '%s'"),
626 full_path);
627 close_pack(p);
628 free(p);
629 return;
632 fill_pack_info(&ctx->info[ctx->nr], p, file_name, ctx->nr);
633 ctx->nr++;
637 struct pack_midx_entry {
638 struct object_id oid;
639 uint32_t pack_int_id;
640 time_t pack_mtime;
641 uint64_t offset;
642 unsigned preferred : 1;
645 static int midx_oid_compare(const void *_a, const void *_b)
647 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
648 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
649 int cmp = oidcmp(&a->oid, &b->oid);
651 if (cmp)
652 return cmp;
654 /* Sort objects in a preferred pack first when multiple copies exist. */
655 if (a->preferred > b->preferred)
656 return -1;
657 if (a->preferred < b->preferred)
658 return 1;
660 if (a->pack_mtime > b->pack_mtime)
661 return -1;
662 else if (a->pack_mtime < b->pack_mtime)
663 return 1;
665 return a->pack_int_id - b->pack_int_id;
668 static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
669 struct pack_midx_entry *e,
670 uint32_t pos)
672 if (pos >= m->num_objects)
673 return 1;
675 nth_midxed_object_oid(&e->oid, m, pos);
676 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
677 e->offset = nth_midxed_offset(m, pos);
679 /* consider objects in midx to be from "old" packs */
680 e->pack_mtime = 0;
681 return 0;
684 static void fill_pack_entry(uint32_t pack_int_id,
685 struct packed_git *p,
686 uint32_t cur_object,
687 struct pack_midx_entry *entry,
688 int preferred)
690 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
691 die(_("failed to locate object %d in packfile"), cur_object);
693 entry->pack_int_id = pack_int_id;
694 entry->pack_mtime = p->mtime;
696 entry->offset = nth_packed_object_offset(p, cur_object);
697 entry->preferred = !!preferred;
700 struct midx_fanout {
701 struct pack_midx_entry *entries;
702 size_t nr, alloc;
705 static void midx_fanout_grow(struct midx_fanout *fanout, size_t nr)
707 if (nr < fanout->nr)
708 BUG("negative growth in midx_fanout_grow() (%"PRIuMAX" < %"PRIuMAX")",
709 (uintmax_t)nr, (uintmax_t)fanout->nr);
710 ALLOC_GROW(fanout->entries, nr, fanout->alloc);
713 static void midx_fanout_sort(struct midx_fanout *fanout)
715 QSORT(fanout->entries, fanout->nr, midx_oid_compare);
718 static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
719 struct multi_pack_index *m,
720 uint32_t cur_fanout,
721 int preferred_pack)
723 uint32_t start = 0, end;
724 uint32_t cur_object;
726 if (cur_fanout)
727 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
728 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
730 for (cur_object = start; cur_object < end; cur_object++) {
731 if ((preferred_pack > -1) &&
732 (preferred_pack == nth_midxed_pack_int_id(m, cur_object))) {
734 * Objects from preferred packs are added
735 * separately.
737 continue;
740 midx_fanout_grow(fanout, fanout->nr + 1);
741 nth_midxed_pack_midx_entry(m,
742 &fanout->entries[fanout->nr],
743 cur_object);
744 fanout->entries[fanout->nr].preferred = 0;
745 fanout->nr++;
749 static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
750 struct pack_info *info,
751 uint32_t cur_pack,
752 int preferred,
753 uint32_t cur_fanout)
755 struct packed_git *pack = info[cur_pack].p;
756 uint32_t start = 0, end;
757 uint32_t cur_object;
759 if (cur_fanout)
760 start = get_pack_fanout(pack, cur_fanout - 1);
761 end = get_pack_fanout(pack, cur_fanout);
763 for (cur_object = start; cur_object < end; cur_object++) {
764 midx_fanout_grow(fanout, fanout->nr + 1);
765 fill_pack_entry(cur_pack,
766 info[cur_pack].p,
767 cur_object,
768 &fanout->entries[fanout->nr],
769 preferred);
770 fanout->nr++;
775 * It is possible to artificially get into a state where there are many
776 * duplicate copies of objects. That can create high memory pressure if
777 * we are to create a list of all objects before de-duplication. To reduce
778 * this memory pressure without a significant performance drop, automatically
779 * group objects by the first byte of their object id. Use the IDX fanout
780 * tables to group the data, copy to a local array, then sort.
782 * Copy only the de-duplicated entries (selected by most-recent modified time
783 * of a packfile containing the object).
785 static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
786 struct pack_info *info,
787 uint32_t nr_packs,
788 size_t *nr_objects,
789 int preferred_pack)
791 uint32_t cur_fanout, cur_pack, cur_object;
792 size_t alloc_objects, total_objects = 0;
793 struct midx_fanout fanout = { 0 };
794 struct pack_midx_entry *deduplicated_entries = NULL;
795 uint32_t start_pack = m ? m->num_packs : 0;
797 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
798 total_objects = st_add(total_objects,
799 info[cur_pack].p->num_objects);
802 * As we de-duplicate by fanout value, we expect the fanout
803 * slices to be evenly distributed, with some noise. Hence,
804 * allocate slightly more than one 256th.
806 alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16;
808 ALLOC_ARRAY(fanout.entries, fanout.alloc);
809 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
810 *nr_objects = 0;
812 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
813 fanout.nr = 0;
815 if (m)
816 midx_fanout_add_midx_fanout(&fanout, m, cur_fanout,
817 preferred_pack);
819 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
820 int preferred = cur_pack == preferred_pack;
821 midx_fanout_add_pack_fanout(&fanout,
822 info, cur_pack,
823 preferred, cur_fanout);
826 if (-1 < preferred_pack && preferred_pack < start_pack)
827 midx_fanout_add_pack_fanout(&fanout, info,
828 preferred_pack, 1,
829 cur_fanout);
831 midx_fanout_sort(&fanout);
834 * The batch is now sorted by OID and then mtime (descending).
835 * Take only the first duplicate.
837 for (cur_object = 0; cur_object < fanout.nr; cur_object++) {
838 if (cur_object && oideq(&fanout.entries[cur_object - 1].oid,
839 &fanout.entries[cur_object].oid))
840 continue;
842 ALLOC_GROW(deduplicated_entries, st_add(*nr_objects, 1),
843 alloc_objects);
844 memcpy(&deduplicated_entries[*nr_objects],
845 &fanout.entries[cur_object],
846 sizeof(struct pack_midx_entry));
847 (*nr_objects)++;
851 free(fanout.entries);
852 return deduplicated_entries;
855 static int write_midx_pack_names(struct hashfile *f, void *data)
857 struct write_midx_context *ctx = data;
858 uint32_t i;
859 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
860 size_t written = 0;
862 for (i = 0; i < ctx->nr; i++) {
863 size_t writelen;
865 if (ctx->info[i].expired)
866 continue;
868 if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0)
869 BUG("incorrect pack-file order: %s before %s",
870 ctx->info[i - 1].pack_name,
871 ctx->info[i].pack_name);
873 writelen = strlen(ctx->info[i].pack_name) + 1;
874 hashwrite(f, ctx->info[i].pack_name, writelen);
875 written += writelen;
878 /* add padding to be aligned */
879 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
880 if (i < MIDX_CHUNK_ALIGNMENT) {
881 memset(padding, 0, sizeof(padding));
882 hashwrite(f, padding, i);
885 return 0;
888 static int write_midx_bitmapped_packs(struct hashfile *f, void *data)
890 struct write_midx_context *ctx = data;
891 size_t i;
893 for (i = 0; i < ctx->nr; i++) {
894 struct pack_info *pack = &ctx->info[i];
895 if (pack->expired)
896 continue;
898 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN && pack->bitmap_nr)
899 BUG("pack '%s' has no bitmap position, but has %d bitmapped object(s)",
900 pack->pack_name, pack->bitmap_nr);
902 hashwrite_be32(f, pack->bitmap_pos);
903 hashwrite_be32(f, pack->bitmap_nr);
905 return 0;
908 static int write_midx_oid_fanout(struct hashfile *f,
909 void *data)
911 struct write_midx_context *ctx = data;
912 struct pack_midx_entry *list = ctx->entries;
913 struct pack_midx_entry *last = ctx->entries + ctx->entries_nr;
914 uint32_t count = 0;
915 uint32_t i;
918 * Write the first-level table (the list is sorted,
919 * but we use a 256-entry lookup to be able to avoid
920 * having to do eight extra binary search iterations).
922 for (i = 0; i < 256; i++) {
923 struct pack_midx_entry *next = list;
925 while (next < last && next->oid.hash[0] == i) {
926 count++;
927 next++;
930 hashwrite_be32(f, count);
931 list = next;
934 return 0;
937 static int write_midx_oid_lookup(struct hashfile *f,
938 void *data)
940 struct write_midx_context *ctx = data;
941 unsigned char hash_len = the_hash_algo->rawsz;
942 struct pack_midx_entry *list = ctx->entries;
943 uint32_t i;
945 for (i = 0; i < ctx->entries_nr; i++) {
946 struct pack_midx_entry *obj = list++;
948 if (i < ctx->entries_nr - 1) {
949 struct pack_midx_entry *next = list;
950 if (oidcmp(&obj->oid, &next->oid) >= 0)
951 BUG("OIDs not in order: %s >= %s",
952 oid_to_hex(&obj->oid),
953 oid_to_hex(&next->oid));
956 hashwrite(f, obj->oid.hash, (int)hash_len);
959 return 0;
962 static int write_midx_object_offsets(struct hashfile *f,
963 void *data)
965 struct write_midx_context *ctx = data;
966 struct pack_midx_entry *list = ctx->entries;
967 uint32_t i, nr_large_offset = 0;
969 for (i = 0; i < ctx->entries_nr; i++) {
970 struct pack_midx_entry *obj = list++;
972 if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
973 BUG("object %s is in an expired pack with int-id %d",
974 oid_to_hex(&obj->oid),
975 obj->pack_int_id);
977 hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
979 if (ctx->large_offsets_needed && obj->offset >> 31)
980 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
981 else if (!ctx->large_offsets_needed && obj->offset >> 32)
982 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
983 oid_to_hex(&obj->oid),
984 obj->offset);
985 else
986 hashwrite_be32(f, (uint32_t)obj->offset);
989 return 0;
992 static int write_midx_large_offsets(struct hashfile *f,
993 void *data)
995 struct write_midx_context *ctx = data;
996 struct pack_midx_entry *list = ctx->entries;
997 struct pack_midx_entry *end = ctx->entries + ctx->entries_nr;
998 uint32_t nr_large_offset = ctx->num_large_offsets;
1000 while (nr_large_offset) {
1001 struct pack_midx_entry *obj;
1002 uint64_t offset;
1004 if (list >= end)
1005 BUG("too many large-offset objects");
1007 obj = list++;
1008 offset = obj->offset;
1010 if (!(offset >> 31))
1011 continue;
1013 hashwrite_be64(f, offset);
1015 nr_large_offset--;
1018 return 0;
1021 static int write_midx_revindex(struct hashfile *f,
1022 void *data)
1024 struct write_midx_context *ctx = data;
1025 uint32_t i;
1027 for (i = 0; i < ctx->entries_nr; i++)
1028 hashwrite_be32(f, ctx->pack_order[i]);
1030 return 0;
1033 struct midx_pack_order_data {
1034 uint32_t nr;
1035 uint32_t pack;
1036 off_t offset;
1039 static int midx_pack_order_cmp(const void *va, const void *vb)
1041 const struct midx_pack_order_data *a = va, *b = vb;
1042 if (a->pack < b->pack)
1043 return -1;
1044 else if (a->pack > b->pack)
1045 return 1;
1046 else if (a->offset < b->offset)
1047 return -1;
1048 else if (a->offset > b->offset)
1049 return 1;
1050 else
1051 return 0;
1054 static uint32_t *midx_pack_order(struct write_midx_context *ctx)
1056 struct midx_pack_order_data *data;
1057 uint32_t *pack_order;
1058 uint32_t i;
1060 trace2_region_enter("midx", "midx_pack_order", the_repository);
1062 ALLOC_ARRAY(data, ctx->entries_nr);
1063 for (i = 0; i < ctx->entries_nr; i++) {
1064 struct pack_midx_entry *e = &ctx->entries[i];
1065 data[i].nr = i;
1066 data[i].pack = ctx->pack_perm[e->pack_int_id];
1067 if (!e->preferred)
1068 data[i].pack |= (1U << 31);
1069 data[i].offset = e->offset;
1072 QSORT(data, ctx->entries_nr, midx_pack_order_cmp);
1074 ALLOC_ARRAY(pack_order, ctx->entries_nr);
1075 for (i = 0; i < ctx->entries_nr; i++) {
1076 struct pack_midx_entry *e = &ctx->entries[data[i].nr];
1077 struct pack_info *pack = &ctx->info[ctx->pack_perm[e->pack_int_id]];
1078 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
1079 pack->bitmap_pos = i;
1080 pack->bitmap_nr++;
1081 pack_order[i] = data[i].nr;
1083 for (i = 0; i < ctx->nr; i++) {
1084 struct pack_info *pack = &ctx->info[ctx->pack_perm[i]];
1085 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
1086 pack->bitmap_pos = 0;
1088 free(data);
1090 trace2_region_leave("midx", "midx_pack_order", the_repository);
1092 return pack_order;
1095 static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
1096 struct write_midx_context *ctx)
1098 struct strbuf buf = STRBUF_INIT;
1099 const char *tmp_file;
1101 trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
1103 strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
1105 tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
1106 midx_hash, WRITE_REV);
1108 if (finalize_object_file(tmp_file, buf.buf))
1109 die(_("cannot store reverse index file"));
1111 strbuf_release(&buf);
1113 trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
1116 static void clear_midx_files_ext(const char *object_dir, const char *ext,
1117 unsigned char *keep_hash);
1119 static int midx_checksum_valid(struct multi_pack_index *m)
1121 return hashfile_checksum_valid(m->data, m->data_len);
1124 static void prepare_midx_packing_data(struct packing_data *pdata,
1125 struct write_midx_context *ctx)
1127 uint32_t i;
1129 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
1131 memset(pdata, 0, sizeof(struct packing_data));
1132 prepare_packing_data(the_repository, pdata);
1134 for (i = 0; i < ctx->entries_nr; i++) {
1135 struct pack_midx_entry *from = &ctx->entries[ctx->pack_order[i]];
1136 struct object_entry *to = packlist_alloc(pdata, &from->oid);
1138 oe_set_in_pack(pdata, to,
1139 ctx->info[ctx->pack_perm[from->pack_int_id]].p);
1142 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
1145 static int add_ref_to_pending(const char *refname,
1146 const struct object_id *oid,
1147 int flag, void *cb_data)
1149 struct rev_info *revs = (struct rev_info*)cb_data;
1150 struct object_id peeled;
1151 struct object *object;
1153 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
1154 warning("symbolic ref is dangling: %s", refname);
1155 return 0;
1158 if (!peel_iterated_oid(oid, &peeled))
1159 oid = &peeled;
1161 object = parse_object_or_die(oid, refname);
1162 if (object->type != OBJ_COMMIT)
1163 return 0;
1165 add_pending_object(revs, object, "");
1166 if (bitmap_is_preferred_refname(revs->repo, refname))
1167 object->flags |= NEEDS_BITMAP;
1168 return 0;
1171 struct bitmap_commit_cb {
1172 struct commit **commits;
1173 size_t commits_nr, commits_alloc;
1175 struct write_midx_context *ctx;
1178 static const struct object_id *bitmap_oid_access(size_t index,
1179 const void *_entries)
1181 const struct pack_midx_entry *entries = _entries;
1182 return &entries[index].oid;
1185 static void bitmap_show_commit(struct commit *commit, void *_data)
1187 struct bitmap_commit_cb *data = _data;
1188 int pos = oid_pos(&commit->object.oid, data->ctx->entries,
1189 data->ctx->entries_nr,
1190 bitmap_oid_access);
1191 if (pos < 0)
1192 return;
1194 ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc);
1195 data->commits[data->commits_nr++] = commit;
1198 static int read_refs_snapshot(const char *refs_snapshot,
1199 struct rev_info *revs)
1201 struct strbuf buf = STRBUF_INIT;
1202 struct object_id oid;
1203 FILE *f = xfopen(refs_snapshot, "r");
1205 while (strbuf_getline(&buf, f) != EOF) {
1206 struct object *object;
1207 int preferred = 0;
1208 char *hex = buf.buf;
1209 const char *end = NULL;
1211 if (buf.len && *buf.buf == '+') {
1212 preferred = 1;
1213 hex = &buf.buf[1];
1216 if (parse_oid_hex(hex, &oid, &end) < 0)
1217 die(_("could not parse line: %s"), buf.buf);
1218 if (*end)
1219 die(_("malformed line: %s"), buf.buf);
1221 object = parse_object_or_die(&oid, NULL);
1222 if (preferred)
1223 object->flags |= NEEDS_BITMAP;
1225 add_pending_object(revs, object, "");
1228 fclose(f);
1229 strbuf_release(&buf);
1230 return 0;
1233 static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
1234 const char *refs_snapshot,
1235 struct write_midx_context *ctx)
1237 struct rev_info revs;
1238 struct bitmap_commit_cb cb = {0};
1240 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
1241 the_repository);
1243 cb.ctx = ctx;
1245 repo_init_revisions(the_repository, &revs, NULL);
1246 if (refs_snapshot) {
1247 read_refs_snapshot(refs_snapshot, &revs);
1248 } else {
1249 setup_revisions(0, NULL, &revs, NULL);
1250 for_each_ref(add_ref_to_pending, &revs);
1254 * Skipping promisor objects here is intentional, since it only excludes
1255 * them from the list of reachable commits that we want to select from
1256 * when computing the selection of MIDX'd commits to receive bitmaps.
1258 * Reachability bitmaps do require that their objects be closed under
1259 * reachability, but fetching any objects missing from promisors at this
1260 * point is too late. But, if one of those objects can be reached from
1261 * an another object that is included in the bitmap, then we will
1262 * complain later that we don't have reachability closure (and fail
1263 * appropriately).
1265 fetch_if_missing = 0;
1266 revs.exclude_promisor_objects = 1;
1268 if (prepare_revision_walk(&revs))
1269 die(_("revision walk setup failed"));
1271 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
1272 if (indexed_commits_nr_p)
1273 *indexed_commits_nr_p = cb.commits_nr;
1275 release_revisions(&revs);
1277 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
1278 the_repository);
1280 return cb.commits;
1283 static int write_midx_bitmap(const char *midx_name,
1284 const unsigned char *midx_hash,
1285 struct packing_data *pdata,
1286 struct commit **commits,
1287 uint32_t commits_nr,
1288 uint32_t *pack_order,
1289 unsigned flags)
1291 int ret, i;
1292 uint16_t options = 0;
1293 struct pack_idx_entry **index;
1294 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
1295 hash_to_hex(midx_hash));
1297 trace2_region_enter("midx", "write_midx_bitmap", the_repository);
1299 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
1300 options |= BITMAP_OPT_HASH_CACHE;
1302 if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE)
1303 options |= BITMAP_OPT_LOOKUP_TABLE;
1306 * Build the MIDX-order index based on pdata.objects (which is already
1307 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
1308 * this order).
1310 ALLOC_ARRAY(index, pdata->nr_objects);
1311 for (i = 0; i < pdata->nr_objects; i++)
1312 index[i] = &pdata->objects[i].idx;
1314 bitmap_writer_show_progress(flags & MIDX_PROGRESS);
1315 bitmap_writer_build_type_index(pdata, index, pdata->nr_objects);
1318 * bitmap_writer_finish expects objects in lex order, but pack_order
1319 * gives us exactly that. use it directly instead of re-sorting the
1320 * array.
1322 * This changes the order of objects in 'index' between
1323 * bitmap_writer_build_type_index and bitmap_writer_finish.
1325 * The same re-ordering takes place in the single-pack bitmap code via
1326 * write_idx_file(), which is called by finish_tmp_packfile(), which
1327 * happens between bitmap_writer_build_type_index() and
1328 * bitmap_writer_finish().
1330 for (i = 0; i < pdata->nr_objects; i++)
1331 index[pack_order[i]] = &pdata->objects[i].idx;
1333 bitmap_writer_select_commits(commits, commits_nr, -1);
1334 ret = bitmap_writer_build(pdata);
1335 if (ret < 0)
1336 goto cleanup;
1338 bitmap_writer_set_checksum(midx_hash);
1339 bitmap_writer_finish(index, pdata->nr_objects, bitmap_name, options);
1341 cleanup:
1342 free(index);
1343 free(bitmap_name);
1345 trace2_region_leave("midx", "write_midx_bitmap", the_repository);
1347 return ret;
1350 static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
1351 const char *object_dir)
1353 struct multi_pack_index *result = NULL;
1354 struct multi_pack_index *cur;
1355 char *obj_dir_real = real_pathdup(object_dir, 1);
1356 struct strbuf cur_path_real = STRBUF_INIT;
1358 /* Ensure the given object_dir is local, or a known alternate. */
1359 find_odb(r, obj_dir_real);
1361 for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
1362 strbuf_realpath(&cur_path_real, cur->object_dir, 1);
1363 if (!strcmp(obj_dir_real, cur_path_real.buf)) {
1364 result = cur;
1365 goto cleanup;
1369 cleanup:
1370 free(obj_dir_real);
1371 strbuf_release(&cur_path_real);
1372 return result;
1375 static int write_midx_internal(const char *object_dir,
1376 struct string_list *packs_to_include,
1377 struct string_list *packs_to_drop,
1378 const char *preferred_pack_name,
1379 const char *refs_snapshot,
1380 unsigned flags)
1382 struct strbuf midx_name = STRBUF_INIT;
1383 unsigned char midx_hash[GIT_MAX_RAWSZ];
1384 uint32_t i;
1385 struct hashfile *f = NULL;
1386 struct lock_file lk;
1387 struct write_midx_context ctx = { 0 };
1388 int bitmapped_packs_concat_len = 0;
1389 int pack_name_concat_len = 0;
1390 int dropped_packs = 0;
1391 int result = 0;
1392 struct chunkfile *cf;
1394 trace2_region_enter("midx", "write_midx_internal", the_repository);
1396 get_midx_filename(&midx_name, object_dir);
1397 if (safe_create_leading_directories(midx_name.buf))
1398 die_errno(_("unable to create leading directories of %s"),
1399 midx_name.buf);
1401 if (!packs_to_include) {
1403 * Only reference an existing MIDX when not filtering which
1404 * packs to include, since all packs and objects are copied
1405 * blindly from an existing MIDX if one is present.
1407 ctx.m = lookup_multi_pack_index(the_repository, object_dir);
1410 if (ctx.m && !midx_checksum_valid(ctx.m)) {
1411 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1412 ctx.m = NULL;
1415 ctx.nr = 0;
1416 ctx.alloc = ctx.m ? ctx.m->num_packs : 16;
1417 ctx.info = NULL;
1418 ALLOC_ARRAY(ctx.info, ctx.alloc);
1420 if (ctx.m) {
1421 for (i = 0; i < ctx.m->num_packs; i++) {
1422 ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
1424 if (flags & MIDX_WRITE_REV_INDEX) {
1426 * If generating a reverse index, need to have
1427 * packed_git's loaded to compare their
1428 * mtimes and object count.
1430 if (prepare_midx_pack(the_repository, ctx.m, i)) {
1431 error(_("could not load pack"));
1432 result = 1;
1433 goto cleanup;
1436 if (open_pack_index(ctx.m->packs[i]))
1437 die(_("could not open index for %s"),
1438 ctx.m->packs[i]->pack_name);
1441 fill_pack_info(&ctx.info[ctx.nr++], ctx.m->packs[i],
1442 ctx.m->pack_names[i], i);
1446 ctx.pack_paths_checked = 0;
1447 if (flags & MIDX_PROGRESS)
1448 ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
1449 else
1450 ctx.progress = NULL;
1452 ctx.to_include = packs_to_include;
1454 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
1455 stop_progress(&ctx.progress);
1457 if ((ctx.m && ctx.nr == ctx.m->num_packs) &&
1458 !(packs_to_include || packs_to_drop)) {
1459 struct bitmap_index *bitmap_git;
1460 int bitmap_exists;
1461 int want_bitmap = flags & MIDX_WRITE_BITMAP;
1463 bitmap_git = prepare_midx_bitmap_git(ctx.m);
1464 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
1465 free_bitmap_index(bitmap_git);
1467 if (bitmap_exists || !want_bitmap) {
1469 * The correct MIDX already exists, and so does a
1470 * corresponding bitmap (or one wasn't requested).
1472 if (!want_bitmap)
1473 clear_midx_files_ext(object_dir, ".bitmap",
1474 NULL);
1475 goto cleanup;
1479 if (preferred_pack_name) {
1480 ctx.preferred_pack_idx = -1;
1482 for (i = 0; i < ctx.nr; i++) {
1483 if (!cmp_idx_or_pack_name(preferred_pack_name,
1484 ctx.info[i].pack_name)) {
1485 ctx.preferred_pack_idx = i;
1486 break;
1490 if (ctx.preferred_pack_idx == -1)
1491 warning(_("unknown preferred pack: '%s'"),
1492 preferred_pack_name);
1493 } else if (ctx.nr &&
1494 (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1495 struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1496 ctx.preferred_pack_idx = 0;
1498 if (packs_to_drop && packs_to_drop->nr)
1499 BUG("cannot write a MIDX bitmap during expiration");
1502 * set a preferred pack when writing a bitmap to ensure that
1503 * the pack from which the first object is selected in pseudo
1504 * pack-order has all of its objects selected from that pack
1505 * (and not another pack containing a duplicate)
1507 for (i = 1; i < ctx.nr; i++) {
1508 struct packed_git *p = ctx.info[i].p;
1510 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1511 oldest = p;
1512 ctx.preferred_pack_idx = i;
1516 if (!oldest->num_objects) {
1518 * If all packs are empty; unset the preferred index.
1519 * This is acceptable since there will be no duplicate
1520 * objects to resolve, so the preferred value doesn't
1521 * matter.
1523 ctx.preferred_pack_idx = -1;
1525 } else {
1527 * otherwise don't mark any pack as preferred to avoid
1528 * interfering with expiration logic below
1530 ctx.preferred_pack_idx = -1;
1533 if (ctx.preferred_pack_idx > -1) {
1534 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1535 if (!preferred->num_objects) {
1536 error(_("cannot select preferred pack %s with no objects"),
1537 preferred->pack_name);
1538 result = 1;
1539 goto cleanup;
1543 ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr,
1544 ctx.preferred_pack_idx);
1546 ctx.large_offsets_needed = 0;
1547 for (i = 0; i < ctx.entries_nr; i++) {
1548 if (ctx.entries[i].offset > 0x7fffffff)
1549 ctx.num_large_offsets++;
1550 if (ctx.entries[i].offset > 0xffffffff)
1551 ctx.large_offsets_needed = 1;
1554 QSORT(ctx.info, ctx.nr, pack_info_compare);
1556 if (packs_to_drop && packs_to_drop->nr) {
1557 int drop_index = 0;
1558 int missing_drops = 0;
1560 for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1561 int cmp = strcmp(ctx.info[i].pack_name,
1562 packs_to_drop->items[drop_index].string);
1564 if (!cmp) {
1565 drop_index++;
1566 ctx.info[i].expired = 1;
1567 } else if (cmp > 0) {
1568 error(_("did not see pack-file %s to drop"),
1569 packs_to_drop->items[drop_index].string);
1570 drop_index++;
1571 missing_drops++;
1572 i--;
1573 } else {
1574 ctx.info[i].expired = 0;
1578 if (missing_drops) {
1579 result = 1;
1580 goto cleanup;
1585 * pack_perm stores a permutation between pack-int-ids from the
1586 * previous multi-pack-index to the new one we are writing:
1588 * pack_perm[old_id] = new_id
1590 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1591 for (i = 0; i < ctx.nr; i++) {
1592 if (ctx.info[i].expired) {
1593 dropped_packs++;
1594 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
1595 } else {
1596 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
1600 for (i = 0; i < ctx.nr; i++) {
1601 if (ctx.info[i].expired)
1602 continue;
1603 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
1604 bitmapped_packs_concat_len += 2 * sizeof(uint32_t);
1607 /* Check that the preferred pack wasn't expired (if given). */
1608 if (preferred_pack_name) {
1609 struct pack_info *preferred = bsearch(preferred_pack_name,
1610 ctx.info, ctx.nr,
1611 sizeof(*ctx.info),
1612 idx_or_pack_name_cmp);
1613 if (preferred) {
1614 uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1615 if (perm == PACK_EXPIRED)
1616 warning(_("preferred pack '%s' is expired"),
1617 preferred_pack_name);
1621 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1622 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1623 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1625 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
1626 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
1628 if (ctx.nr - dropped_packs == 0) {
1629 error(_("no pack files to index."));
1630 result = 1;
1631 goto cleanup;
1634 if (!ctx.entries_nr) {
1635 if (flags & MIDX_WRITE_BITMAP)
1636 warning(_("refusing to write multi-pack .bitmap without any objects"));
1637 flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1640 cf = init_chunkfile(f);
1642 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1643 write_midx_pack_names);
1644 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1645 write_midx_oid_fanout);
1646 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
1647 st_mult(ctx.entries_nr, the_hash_algo->rawsz),
1648 write_midx_oid_lookup);
1649 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
1650 st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
1651 write_midx_object_offsets);
1653 if (ctx.large_offsets_needed)
1654 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
1655 st_mult(ctx.num_large_offsets,
1656 MIDX_CHUNK_LARGE_OFFSET_WIDTH),
1657 write_midx_large_offsets);
1659 if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) {
1660 ctx.pack_order = midx_pack_order(&ctx);
1661 add_chunk(cf, MIDX_CHUNKID_REVINDEX,
1662 st_mult(ctx.entries_nr, sizeof(uint32_t)),
1663 write_midx_revindex);
1664 add_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
1665 bitmapped_packs_concat_len,
1666 write_midx_bitmapped_packs);
1669 write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1670 write_chunkfile(cf, &ctx);
1672 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
1673 CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1674 free_chunkfile(cf);
1676 if (flags & MIDX_WRITE_REV_INDEX &&
1677 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1678 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
1680 if (flags & MIDX_WRITE_BITMAP) {
1681 struct packing_data pdata;
1682 struct commit **commits;
1683 uint32_t commits_nr;
1685 if (!ctx.entries_nr)
1686 BUG("cannot write a bitmap without any objects");
1688 prepare_midx_packing_data(&pdata, &ctx);
1690 commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1693 * The previous steps translated the information from
1694 * 'entries' into information suitable for constructing
1695 * bitmaps. We no longer need that array, so clear it to
1696 * reduce memory pressure.
1698 FREE_AND_NULL(ctx.entries);
1699 ctx.entries_nr = 0;
1701 if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1702 commits, commits_nr, ctx.pack_order,
1703 flags) < 0) {
1704 error(_("could not write multi-pack bitmap"));
1705 result = 1;
1706 clear_packing_data(&pdata);
1707 free(commits);
1708 goto cleanup;
1711 clear_packing_data(&pdata);
1712 free(commits);
1715 * NOTE: Do not use ctx.entries beyond this point, since it might
1716 * have been freed in the previous if block.
1719 if (ctx.m)
1720 close_object_store(the_repository->objects);
1722 if (commit_lock_file(&lk) < 0)
1723 die_errno(_("could not write multi-pack-index"));
1725 clear_midx_files_ext(object_dir, ".bitmap", midx_hash);
1726 clear_midx_files_ext(object_dir, ".rev", midx_hash);
1728 cleanup:
1729 for (i = 0; i < ctx.nr; i++) {
1730 if (ctx.info[i].p) {
1731 close_pack(ctx.info[i].p);
1732 free(ctx.info[i].p);
1734 free(ctx.info[i].pack_name);
1737 free(ctx.info);
1738 free(ctx.entries);
1739 free(ctx.pack_perm);
1740 free(ctx.pack_order);
1741 strbuf_release(&midx_name);
1743 trace2_region_leave("midx", "write_midx_internal", the_repository);
1745 return result;
1748 int write_midx_file(const char *object_dir,
1749 const char *preferred_pack_name,
1750 const char *refs_snapshot,
1751 unsigned flags)
1753 return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
1754 refs_snapshot, flags);
1757 int write_midx_file_only(const char *object_dir,
1758 struct string_list *packs_to_include,
1759 const char *preferred_pack_name,
1760 const char *refs_snapshot,
1761 unsigned flags)
1763 return write_midx_internal(object_dir, packs_to_include, NULL,
1764 preferred_pack_name, refs_snapshot, flags);
1767 struct clear_midx_data {
1768 char *keep;
1769 const char *ext;
1772 static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED,
1773 const char *file_name, void *_data)
1775 struct clear_midx_data *data = _data;
1777 if (!(starts_with(file_name, "multi-pack-index-") &&
1778 ends_with(file_name, data->ext)))
1779 return;
1780 if (data->keep && !strcmp(data->keep, file_name))
1781 return;
1783 if (unlink(full_path))
1784 die_errno(_("failed to remove %s"), full_path);
1787 static void clear_midx_files_ext(const char *object_dir, const char *ext,
1788 unsigned char *keep_hash)
1790 struct clear_midx_data data;
1791 memset(&data, 0, sizeof(struct clear_midx_data));
1793 if (keep_hash)
1794 data.keep = xstrfmt("multi-pack-index-%s%s",
1795 hash_to_hex(keep_hash), ext);
1796 data.ext = ext;
1798 for_each_file_in_pack_dir(object_dir,
1799 clear_midx_file_ext,
1800 &data);
1802 free(data.keep);
1805 void clear_midx_file(struct repository *r)
1807 struct strbuf midx = STRBUF_INIT;
1809 get_midx_filename(&midx, r->objects->odb->path);
1811 if (r->objects && r->objects->multi_pack_index) {
1812 close_midx(r->objects->multi_pack_index);
1813 r->objects->multi_pack_index = NULL;
1816 if (remove_path(midx.buf))
1817 die(_("failed to clear multi-pack-index at %s"), midx.buf);
1819 clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL);
1820 clear_midx_files_ext(r->objects->odb->path, ".rev", NULL);
1822 strbuf_release(&midx);
1825 static int verify_midx_error;
1827 __attribute__((format (printf, 1, 2)))
1828 static void midx_report(const char *fmt, ...)
1830 va_list ap;
1831 verify_midx_error = 1;
1832 va_start(ap, fmt);
1833 vfprintf(stderr, fmt, ap);
1834 fprintf(stderr, "\n");
1835 va_end(ap);
1838 struct pair_pos_vs_id
1840 uint32_t pos;
1841 uint32_t pack_int_id;
1844 static int compare_pair_pos_vs_id(const void *_a, const void *_b)
1846 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
1847 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
1849 return b->pack_int_id - a->pack_int_id;
1853 * Limit calls to display_progress() for performance reasons.
1854 * The interval here was arbitrarily chosen.
1856 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
1857 #define midx_display_sparse_progress(progress, n) \
1858 do { \
1859 uint64_t _n = (n); \
1860 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1861 display_progress(progress, _n); \
1862 } while (0)
1864 int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
1866 struct pair_pos_vs_id *pairs = NULL;
1867 uint32_t i;
1868 struct progress *progress = NULL;
1869 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
1870 verify_midx_error = 0;
1872 if (!m) {
1873 int result = 0;
1874 struct stat sb;
1875 struct strbuf filename = STRBUF_INIT;
1877 get_midx_filename(&filename, object_dir);
1879 if (!stat(filename.buf, &sb)) {
1880 error(_("multi-pack-index file exists, but failed to parse"));
1881 result = 1;
1883 strbuf_release(&filename);
1884 return result;
1887 if (!midx_checksum_valid(m))
1888 midx_report(_("incorrect checksum"));
1890 if (flags & MIDX_PROGRESS)
1891 progress = start_delayed_progress(_("Looking for referenced packfiles"),
1892 m->num_packs);
1893 for (i = 0; i < m->num_packs; i++) {
1894 if (prepare_midx_pack(r, m, i))
1895 midx_report("failed to load pack in position %d", i);
1897 display_progress(progress, i + 1);
1899 stop_progress(&progress);
1901 if (m->num_objects == 0) {
1902 midx_report(_("the midx contains no oid"));
1904 * Remaining tests assume that we have objects, so we can
1905 * return here.
1907 goto cleanup;
1910 if (flags & MIDX_PROGRESS)
1911 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1912 m->num_objects - 1);
1913 for (i = 0; i < m->num_objects - 1; i++) {
1914 struct object_id oid1, oid2;
1916 nth_midxed_object_oid(&oid1, m, i);
1917 nth_midxed_object_oid(&oid2, m, i + 1);
1919 if (oidcmp(&oid1, &oid2) >= 0)
1920 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1921 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
1923 midx_display_sparse_progress(progress, i + 1);
1925 stop_progress(&progress);
1928 * Create an array mapping each object to its packfile id. Sort it
1929 * to group the objects by packfile. Use this permutation to visit
1930 * each of the objects and only require 1 packfile to be open at a
1931 * time.
1933 ALLOC_ARRAY(pairs, m->num_objects);
1934 for (i = 0; i < m->num_objects; i++) {
1935 pairs[i].pos = i;
1936 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
1939 if (flags & MIDX_PROGRESS)
1940 progress = start_sparse_progress(_("Sorting objects by packfile"),
1941 m->num_objects);
1942 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
1943 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
1944 stop_progress(&progress);
1946 if (flags & MIDX_PROGRESS)
1947 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
1948 for (i = 0; i < m->num_objects; i++) {
1949 struct object_id oid;
1950 struct pack_entry e;
1951 off_t m_offset, p_offset;
1953 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
1954 m->packs[pairs[i-1].pack_int_id])
1956 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
1957 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
1960 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1962 if (!fill_midx_entry(r, &oid, &e, m)) {
1963 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1964 pairs[i].pos, oid_to_hex(&oid));
1965 continue;
1968 if (open_pack_index(e.p)) {
1969 midx_report(_("failed to load pack-index for packfile %s"),
1970 e.p->pack_name);
1971 break;
1974 m_offset = e.offset;
1975 p_offset = find_pack_entry_one(oid.hash, e.p);
1977 if (m_offset != p_offset)
1978 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
1979 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
1981 midx_display_sparse_progress(progress, i + 1);
1983 stop_progress(&progress);
1985 cleanup:
1986 free(pairs);
1987 close_midx(m);
1989 return verify_midx_error;
1992 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
1994 uint32_t i, *count, result = 0;
1995 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1996 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1997 struct progress *progress = NULL;
1999 if (!m)
2000 return 0;
2002 CALLOC_ARRAY(count, m->num_packs);
2004 if (flags & MIDX_PROGRESS)
2005 progress = start_delayed_progress(_("Counting referenced objects"),
2006 m->num_objects);
2007 for (i = 0; i < m->num_objects; i++) {
2008 int pack_int_id = nth_midxed_pack_int_id(m, i);
2009 count[pack_int_id]++;
2010 display_progress(progress, i + 1);
2012 stop_progress(&progress);
2014 if (flags & MIDX_PROGRESS)
2015 progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
2016 m->num_packs);
2017 for (i = 0; i < m->num_packs; i++) {
2018 char *pack_name;
2019 display_progress(progress, i + 1);
2021 if (count[i])
2022 continue;
2024 if (prepare_midx_pack(r, m, i))
2025 continue;
2027 if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
2028 continue;
2030 pack_name = xstrdup(m->packs[i]->pack_name);
2031 close_pack(m->packs[i]);
2033 string_list_insert(&packs_to_drop, m->pack_names[i]);
2034 unlink_pack_path(pack_name, 0);
2035 free(pack_name);
2037 stop_progress(&progress);
2039 free(count);
2041 if (packs_to_drop.nr)
2042 result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
2044 string_list_clear(&packs_to_drop, 0);
2046 return result;
2049 struct repack_info {
2050 timestamp_t mtime;
2051 uint32_t referenced_objects;
2052 uint32_t pack_int_id;
2055 static int compare_by_mtime(const void *a_, const void *b_)
2057 const struct repack_info *a, *b;
2059 a = (const struct repack_info *)a_;
2060 b = (const struct repack_info *)b_;
2062 if (a->mtime < b->mtime)
2063 return -1;
2064 if (a->mtime > b->mtime)
2065 return 1;
2066 return 0;
2069 static int fill_included_packs_all(struct repository *r,
2070 struct multi_pack_index *m,
2071 unsigned char *include_pack)
2073 uint32_t i, count = 0;
2074 int pack_kept_objects = 0;
2076 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
2078 for (i = 0; i < m->num_packs; i++) {
2079 if (prepare_midx_pack(r, m, i))
2080 continue;
2081 if (!pack_kept_objects && m->packs[i]->pack_keep)
2082 continue;
2083 if (m->packs[i]->is_cruft)
2084 continue;
2086 include_pack[i] = 1;
2087 count++;
2090 return count < 2;
2093 static int fill_included_packs_batch(struct repository *r,
2094 struct multi_pack_index *m,
2095 unsigned char *include_pack,
2096 size_t batch_size)
2098 uint32_t i, packs_to_repack;
2099 size_t total_size;
2100 struct repack_info *pack_info;
2101 int pack_kept_objects = 0;
2103 CALLOC_ARRAY(pack_info, m->num_packs);
2105 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
2107 for (i = 0; i < m->num_packs; i++) {
2108 pack_info[i].pack_int_id = i;
2110 if (prepare_midx_pack(r, m, i))
2111 continue;
2113 pack_info[i].mtime = m->packs[i]->mtime;
2116 for (i = 0; i < m->num_objects; i++) {
2117 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
2118 pack_info[pack_int_id].referenced_objects++;
2121 QSORT(pack_info, m->num_packs, compare_by_mtime);
2123 total_size = 0;
2124 packs_to_repack = 0;
2125 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
2126 int pack_int_id = pack_info[i].pack_int_id;
2127 struct packed_git *p = m->packs[pack_int_id];
2128 size_t expected_size;
2130 if (!p)
2131 continue;
2132 if (!pack_kept_objects && p->pack_keep)
2133 continue;
2134 if (p->is_cruft)
2135 continue;
2136 if (open_pack_index(p) || !p->num_objects)
2137 continue;
2139 expected_size = st_mult(p->pack_size,
2140 pack_info[i].referenced_objects);
2141 expected_size /= p->num_objects;
2143 if (expected_size >= batch_size)
2144 continue;
2146 packs_to_repack++;
2147 total_size += expected_size;
2148 include_pack[pack_int_id] = 1;
2151 free(pack_info);
2153 if (packs_to_repack < 2)
2154 return 1;
2156 return 0;
2159 int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
2161 int result = 0;
2162 uint32_t i;
2163 unsigned char *include_pack;
2164 struct child_process cmd = CHILD_PROCESS_INIT;
2165 FILE *cmd_in;
2166 struct strbuf base_name = STRBUF_INIT;
2167 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
2170 * When updating the default for these configuration
2171 * variables in builtin/repack.c, these must be adjusted
2172 * to match.
2174 int delta_base_offset = 1;
2175 int use_delta_islands = 0;
2177 if (!m)
2178 return 0;
2180 CALLOC_ARRAY(include_pack, m->num_packs);
2182 if (batch_size) {
2183 if (fill_included_packs_batch(r, m, include_pack, batch_size))
2184 goto cleanup;
2185 } else if (fill_included_packs_all(r, m, include_pack))
2186 goto cleanup;
2188 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
2189 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
2191 strvec_push(&cmd.args, "pack-objects");
2193 strbuf_addstr(&base_name, object_dir);
2194 strbuf_addstr(&base_name, "/pack/pack");
2195 strvec_push(&cmd.args, base_name.buf);
2197 if (delta_base_offset)
2198 strvec_push(&cmd.args, "--delta-base-offset");
2199 if (use_delta_islands)
2200 strvec_push(&cmd.args, "--delta-islands");
2202 if (flags & MIDX_PROGRESS)
2203 strvec_push(&cmd.args, "--progress");
2204 else
2205 strvec_push(&cmd.args, "-q");
2207 strbuf_release(&base_name);
2209 cmd.git_cmd = 1;
2210 cmd.in = cmd.out = -1;
2212 if (start_command(&cmd)) {
2213 error(_("could not start pack-objects"));
2214 result = 1;
2215 goto cleanup;
2218 cmd_in = xfdopen(cmd.in, "w");
2220 for (i = 0; i < m->num_objects; i++) {
2221 struct object_id oid;
2222 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
2224 if (!include_pack[pack_int_id])
2225 continue;
2227 nth_midxed_object_oid(&oid, m, i);
2228 fprintf(cmd_in, "%s\n", oid_to_hex(&oid));
2230 fclose(cmd_in);
2232 if (finish_command(&cmd)) {
2233 error(_("could not finish pack-objects"));
2234 result = 1;
2235 goto cleanup;
2238 result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
2240 cleanup:
2241 free(include_pack);
2242 return result;