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