Merge branch 'rj/add-p-typo-reaction'
[git/gitster.git] / midx-write.c
blob65e69d2de787cf784bb1736c7d834ce1f8aeebaf
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "packfile.h"
7 #include "object-file.h"
8 #include "hash-lookup.h"
9 #include "midx.h"
10 #include "progress.h"
11 #include "trace2.h"
12 #include "run-command.h"
13 #include "chunk-format.h"
14 #include "pack-bitmap.h"
15 #include "refs.h"
16 #include "revision.h"
17 #include "list-objects.h"
19 #define PACK_EXPIRED UINT_MAX
20 #define BITMAP_POS_UNKNOWN (~((uint32_t)0))
21 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
22 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
24 extern int midx_checksum_valid(struct multi_pack_index *m);
25 extern void clear_midx_files_ext(const char *object_dir, const char *ext,
26 unsigned char *keep_hash);
27 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
28 const char *idx_name);
30 static size_t write_midx_header(struct hashfile *f,
31 unsigned char num_chunks,
32 uint32_t num_packs)
34 hashwrite_be32(f, MIDX_SIGNATURE);
35 hashwrite_u8(f, MIDX_VERSION);
36 hashwrite_u8(f, oid_version(the_hash_algo));
37 hashwrite_u8(f, num_chunks);
38 hashwrite_u8(f, 0); /* unused */
39 hashwrite_be32(f, num_packs);
41 return MIDX_HEADER_SIZE;
44 struct pack_info {
45 uint32_t orig_pack_int_id;
46 char *pack_name;
47 struct packed_git *p;
49 uint32_t bitmap_pos;
50 uint32_t bitmap_nr;
52 unsigned expired : 1;
55 static void fill_pack_info(struct pack_info *info,
56 struct packed_git *p, const char *pack_name,
57 uint32_t orig_pack_int_id)
59 memset(info, 0, sizeof(struct pack_info));
61 info->orig_pack_int_id = orig_pack_int_id;
62 info->pack_name = xstrdup(pack_name);
63 info->p = p;
64 info->bitmap_pos = BITMAP_POS_UNKNOWN;
67 static int pack_info_compare(const void *_a, const void *_b)
69 struct pack_info *a = (struct pack_info *)_a;
70 struct pack_info *b = (struct pack_info *)_b;
71 return strcmp(a->pack_name, b->pack_name);
74 static int idx_or_pack_name_cmp(const void *_va, const void *_vb)
76 const char *pack_name = _va;
77 const struct pack_info *compar = _vb;
79 return cmp_idx_or_pack_name(pack_name, compar->pack_name);
82 struct write_midx_context {
83 struct pack_info *info;
84 size_t nr;
85 size_t alloc;
86 struct multi_pack_index *m;
87 struct progress *progress;
88 unsigned pack_paths_checked;
90 struct pack_midx_entry *entries;
91 size_t entries_nr;
93 uint32_t *pack_perm;
94 uint32_t *pack_order;
95 unsigned large_offsets_needed:1;
96 uint32_t num_large_offsets;
98 int preferred_pack_idx;
100 struct string_list *to_include;
103 static void add_pack_to_midx(const char *full_path, size_t full_path_len,
104 const char *file_name, void *data)
106 struct write_midx_context *ctx = data;
107 struct packed_git *p;
109 if (ends_with(file_name, ".idx")) {
110 display_progress(ctx->progress, ++ctx->pack_paths_checked);
112 * Note that at most one of ctx->m and ctx->to_include are set,
113 * so we are testing midx_contains_pack() and
114 * string_list_has_string() independently (guarded by the
115 * appropriate NULL checks).
117 * We could support passing to_include while reusing an existing
118 * MIDX, but don't currently since the reuse process drags
119 * forward all packs from an existing MIDX (without checking
120 * whether or not they appear in the to_include list).
122 * If we added support for that, these next two conditional
123 * should be performed independently (likely checking
124 * to_include before the existing MIDX).
126 if (ctx->m && midx_contains_pack(ctx->m, file_name))
127 return;
128 else if (ctx->to_include &&
129 !string_list_has_string(ctx->to_include, file_name))
130 return;
132 ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
134 p = add_packed_git(full_path, full_path_len, 0);
135 if (!p) {
136 warning(_("failed to add packfile '%s'"),
137 full_path);
138 return;
141 if (open_pack_index(p)) {
142 warning(_("failed to open pack-index '%s'"),
143 full_path);
144 close_pack(p);
145 free(p);
146 return;
149 fill_pack_info(&ctx->info[ctx->nr], p, file_name, ctx->nr);
150 ctx->nr++;
154 struct pack_midx_entry {
155 struct object_id oid;
156 uint32_t pack_int_id;
157 time_t pack_mtime;
158 uint64_t offset;
159 unsigned preferred : 1;
162 static int midx_oid_compare(const void *_a, const void *_b)
164 const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a;
165 const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b;
166 int cmp = oidcmp(&a->oid, &b->oid);
168 if (cmp)
169 return cmp;
171 /* Sort objects in a preferred pack first when multiple copies exist. */
172 if (a->preferred > b->preferred)
173 return -1;
174 if (a->preferred < b->preferred)
175 return 1;
177 if (a->pack_mtime > b->pack_mtime)
178 return -1;
179 else if (a->pack_mtime < b->pack_mtime)
180 return 1;
182 return a->pack_int_id - b->pack_int_id;
185 static int nth_midxed_pack_midx_entry(struct multi_pack_index *m,
186 struct pack_midx_entry *e,
187 uint32_t pos)
189 if (pos >= m->num_objects)
190 return 1;
192 nth_midxed_object_oid(&e->oid, m, pos);
193 e->pack_int_id = nth_midxed_pack_int_id(m, pos);
194 e->offset = nth_midxed_offset(m, pos);
196 /* consider objects in midx to be from "old" packs */
197 e->pack_mtime = 0;
198 return 0;
201 static void fill_pack_entry(uint32_t pack_int_id,
202 struct packed_git *p,
203 uint32_t cur_object,
204 struct pack_midx_entry *entry,
205 int preferred)
207 if (nth_packed_object_id(&entry->oid, p, cur_object) < 0)
208 die(_("failed to locate object %d in packfile"), cur_object);
210 entry->pack_int_id = pack_int_id;
211 entry->pack_mtime = p->mtime;
213 entry->offset = nth_packed_object_offset(p, cur_object);
214 entry->preferred = !!preferred;
217 struct midx_fanout {
218 struct pack_midx_entry *entries;
219 size_t nr, alloc;
222 static void midx_fanout_grow(struct midx_fanout *fanout, size_t nr)
224 if (nr < fanout->nr)
225 BUG("negative growth in midx_fanout_grow() (%"PRIuMAX" < %"PRIuMAX")",
226 (uintmax_t)nr, (uintmax_t)fanout->nr);
227 ALLOC_GROW(fanout->entries, nr, fanout->alloc);
230 static void midx_fanout_sort(struct midx_fanout *fanout)
232 QSORT(fanout->entries, fanout->nr, midx_oid_compare);
235 static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
236 struct multi_pack_index *m,
237 uint32_t cur_fanout,
238 int preferred_pack)
240 uint32_t start = 0, end;
241 uint32_t cur_object;
243 if (cur_fanout)
244 start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]);
245 end = ntohl(m->chunk_oid_fanout[cur_fanout]);
247 for (cur_object = start; cur_object < end; cur_object++) {
248 if ((preferred_pack > -1) &&
249 (preferred_pack == nth_midxed_pack_int_id(m, cur_object))) {
251 * Objects from preferred packs are added
252 * separately.
254 continue;
257 midx_fanout_grow(fanout, fanout->nr + 1);
258 nth_midxed_pack_midx_entry(m,
259 &fanout->entries[fanout->nr],
260 cur_object);
261 fanout->entries[fanout->nr].preferred = 0;
262 fanout->nr++;
266 static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout,
267 struct pack_info *info,
268 uint32_t cur_pack,
269 int preferred,
270 uint32_t cur_fanout)
272 struct packed_git *pack = info[cur_pack].p;
273 uint32_t start = 0, end;
274 uint32_t cur_object;
276 if (cur_fanout)
277 start = get_pack_fanout(pack, cur_fanout - 1);
278 end = get_pack_fanout(pack, cur_fanout);
280 for (cur_object = start; cur_object < end; cur_object++) {
281 midx_fanout_grow(fanout, fanout->nr + 1);
282 fill_pack_entry(cur_pack,
283 info[cur_pack].p,
284 cur_object,
285 &fanout->entries[fanout->nr],
286 preferred);
287 fanout->nr++;
292 * It is possible to artificially get into a state where there are many
293 * duplicate copies of objects. That can create high memory pressure if
294 * we are to create a list of all objects before de-duplication. To reduce
295 * this memory pressure without a significant performance drop, automatically
296 * group objects by the first byte of their object id. Use the IDX fanout
297 * tables to group the data, copy to a local array, then sort.
299 * Copy only the de-duplicated entries (selected by most-recent modified time
300 * of a packfile containing the object).
302 static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m,
303 struct pack_info *info,
304 uint32_t nr_packs,
305 size_t *nr_objects,
306 int preferred_pack)
308 uint32_t cur_fanout, cur_pack, cur_object;
309 size_t alloc_objects, total_objects = 0;
310 struct midx_fanout fanout = { 0 };
311 struct pack_midx_entry *deduplicated_entries = NULL;
312 uint32_t start_pack = m ? m->num_packs : 0;
314 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++)
315 total_objects = st_add(total_objects,
316 info[cur_pack].p->num_objects);
319 * As we de-duplicate by fanout value, we expect the fanout
320 * slices to be evenly distributed, with some noise. Hence,
321 * allocate slightly more than one 256th.
323 alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16;
325 ALLOC_ARRAY(fanout.entries, fanout.alloc);
326 ALLOC_ARRAY(deduplicated_entries, alloc_objects);
327 *nr_objects = 0;
329 for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) {
330 fanout.nr = 0;
332 if (m)
333 midx_fanout_add_midx_fanout(&fanout, m, cur_fanout,
334 preferred_pack);
336 for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) {
337 int preferred = cur_pack == preferred_pack;
338 midx_fanout_add_pack_fanout(&fanout,
339 info, cur_pack,
340 preferred, cur_fanout);
343 if (-1 < preferred_pack && preferred_pack < start_pack)
344 midx_fanout_add_pack_fanout(&fanout, info,
345 preferred_pack, 1,
346 cur_fanout);
348 midx_fanout_sort(&fanout);
351 * The batch is now sorted by OID and then mtime (descending).
352 * Take only the first duplicate.
354 for (cur_object = 0; cur_object < fanout.nr; cur_object++) {
355 if (cur_object && oideq(&fanout.entries[cur_object - 1].oid,
356 &fanout.entries[cur_object].oid))
357 continue;
359 ALLOC_GROW(deduplicated_entries, st_add(*nr_objects, 1),
360 alloc_objects);
361 memcpy(&deduplicated_entries[*nr_objects],
362 &fanout.entries[cur_object],
363 sizeof(struct pack_midx_entry));
364 (*nr_objects)++;
368 free(fanout.entries);
369 return deduplicated_entries;
372 static int write_midx_pack_names(struct hashfile *f, void *data)
374 struct write_midx_context *ctx = data;
375 uint32_t i;
376 unsigned char padding[MIDX_CHUNK_ALIGNMENT];
377 size_t written = 0;
379 for (i = 0; i < ctx->nr; i++) {
380 size_t writelen;
382 if (ctx->info[i].expired)
383 continue;
385 if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0)
386 BUG("incorrect pack-file order: %s before %s",
387 ctx->info[i - 1].pack_name,
388 ctx->info[i].pack_name);
390 writelen = strlen(ctx->info[i].pack_name) + 1;
391 hashwrite(f, ctx->info[i].pack_name, writelen);
392 written += writelen;
395 /* add padding to be aligned */
396 i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT);
397 if (i < MIDX_CHUNK_ALIGNMENT) {
398 memset(padding, 0, sizeof(padding));
399 hashwrite(f, padding, i);
402 return 0;
405 static int write_midx_bitmapped_packs(struct hashfile *f, void *data)
407 struct write_midx_context *ctx = data;
408 size_t i;
410 for (i = 0; i < ctx->nr; i++) {
411 struct pack_info *pack = &ctx->info[i];
412 if (pack->expired)
413 continue;
415 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN && pack->bitmap_nr)
416 BUG("pack '%s' has no bitmap position, but has %d bitmapped object(s)",
417 pack->pack_name, pack->bitmap_nr);
419 hashwrite_be32(f, pack->bitmap_pos);
420 hashwrite_be32(f, pack->bitmap_nr);
422 return 0;
425 static int write_midx_oid_fanout(struct hashfile *f,
426 void *data)
428 struct write_midx_context *ctx = data;
429 struct pack_midx_entry *list = ctx->entries;
430 struct pack_midx_entry *last = ctx->entries + ctx->entries_nr;
431 uint32_t count = 0;
432 uint32_t i;
435 * Write the first-level table (the list is sorted,
436 * but we use a 256-entry lookup to be able to avoid
437 * having to do eight extra binary search iterations).
439 for (i = 0; i < 256; i++) {
440 struct pack_midx_entry *next = list;
442 while (next < last && next->oid.hash[0] == i) {
443 count++;
444 next++;
447 hashwrite_be32(f, count);
448 list = next;
451 return 0;
454 static int write_midx_oid_lookup(struct hashfile *f,
455 void *data)
457 struct write_midx_context *ctx = data;
458 unsigned char hash_len = the_hash_algo->rawsz;
459 struct pack_midx_entry *list = ctx->entries;
460 uint32_t i;
462 for (i = 0; i < ctx->entries_nr; i++) {
463 struct pack_midx_entry *obj = list++;
465 if (i < ctx->entries_nr - 1) {
466 struct pack_midx_entry *next = list;
467 if (oidcmp(&obj->oid, &next->oid) >= 0)
468 BUG("OIDs not in order: %s >= %s",
469 oid_to_hex(&obj->oid),
470 oid_to_hex(&next->oid));
473 hashwrite(f, obj->oid.hash, (int)hash_len);
476 return 0;
479 static int write_midx_object_offsets(struct hashfile *f,
480 void *data)
482 struct write_midx_context *ctx = data;
483 struct pack_midx_entry *list = ctx->entries;
484 uint32_t i, nr_large_offset = 0;
486 for (i = 0; i < ctx->entries_nr; i++) {
487 struct pack_midx_entry *obj = list++;
489 if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
490 BUG("object %s is in an expired pack with int-id %d",
491 oid_to_hex(&obj->oid),
492 obj->pack_int_id);
494 hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
496 if (ctx->large_offsets_needed && obj->offset >> 31)
497 hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
498 else if (!ctx->large_offsets_needed && obj->offset >> 32)
499 BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
500 oid_to_hex(&obj->oid),
501 obj->offset);
502 else
503 hashwrite_be32(f, (uint32_t)obj->offset);
506 return 0;
509 static int write_midx_large_offsets(struct hashfile *f,
510 void *data)
512 struct write_midx_context *ctx = data;
513 struct pack_midx_entry *list = ctx->entries;
514 struct pack_midx_entry *end = ctx->entries + ctx->entries_nr;
515 uint32_t nr_large_offset = ctx->num_large_offsets;
517 while (nr_large_offset) {
518 struct pack_midx_entry *obj;
519 uint64_t offset;
521 if (list >= end)
522 BUG("too many large-offset objects");
524 obj = list++;
525 offset = obj->offset;
527 if (!(offset >> 31))
528 continue;
530 hashwrite_be64(f, offset);
532 nr_large_offset--;
535 return 0;
538 static int write_midx_revindex(struct hashfile *f,
539 void *data)
541 struct write_midx_context *ctx = data;
542 uint32_t i;
544 for (i = 0; i < ctx->entries_nr; i++)
545 hashwrite_be32(f, ctx->pack_order[i]);
547 return 0;
550 struct midx_pack_order_data {
551 uint32_t nr;
552 uint32_t pack;
553 off_t offset;
556 static int midx_pack_order_cmp(const void *va, const void *vb)
558 const struct midx_pack_order_data *a = va, *b = vb;
559 if (a->pack < b->pack)
560 return -1;
561 else if (a->pack > b->pack)
562 return 1;
563 else if (a->offset < b->offset)
564 return -1;
565 else if (a->offset > b->offset)
566 return 1;
567 else
568 return 0;
571 static uint32_t *midx_pack_order(struct write_midx_context *ctx)
573 struct midx_pack_order_data *data;
574 uint32_t *pack_order;
575 uint32_t i;
577 trace2_region_enter("midx", "midx_pack_order", the_repository);
579 ALLOC_ARRAY(data, ctx->entries_nr);
580 for (i = 0; i < ctx->entries_nr; i++) {
581 struct pack_midx_entry *e = &ctx->entries[i];
582 data[i].nr = i;
583 data[i].pack = ctx->pack_perm[e->pack_int_id];
584 if (!e->preferred)
585 data[i].pack |= (1U << 31);
586 data[i].offset = e->offset;
589 QSORT(data, ctx->entries_nr, midx_pack_order_cmp);
591 ALLOC_ARRAY(pack_order, ctx->entries_nr);
592 for (i = 0; i < ctx->entries_nr; i++) {
593 struct pack_midx_entry *e = &ctx->entries[data[i].nr];
594 struct pack_info *pack = &ctx->info[ctx->pack_perm[e->pack_int_id]];
595 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
596 pack->bitmap_pos = i;
597 pack->bitmap_nr++;
598 pack_order[i] = data[i].nr;
600 for (i = 0; i < ctx->nr; i++) {
601 struct pack_info *pack = &ctx->info[ctx->pack_perm[i]];
602 if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
603 pack->bitmap_pos = 0;
605 free(data);
607 trace2_region_leave("midx", "midx_pack_order", the_repository);
609 return pack_order;
612 static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
613 struct write_midx_context *ctx)
615 struct strbuf buf = STRBUF_INIT;
616 const char *tmp_file;
618 trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
620 strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
622 tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
623 midx_hash, WRITE_REV);
625 if (finalize_object_file(tmp_file, buf.buf))
626 die(_("cannot store reverse index file"));
628 strbuf_release(&buf);
630 trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
633 static void prepare_midx_packing_data(struct packing_data *pdata,
634 struct write_midx_context *ctx)
636 uint32_t i;
638 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
640 memset(pdata, 0, sizeof(struct packing_data));
641 prepare_packing_data(the_repository, pdata);
643 for (i = 0; i < ctx->entries_nr; i++) {
644 struct pack_midx_entry *from = &ctx->entries[ctx->pack_order[i]];
645 struct object_entry *to = packlist_alloc(pdata, &from->oid);
647 oe_set_in_pack(pdata, to,
648 ctx->info[ctx->pack_perm[from->pack_int_id]].p);
651 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
654 static int add_ref_to_pending(const char *refname,
655 const struct object_id *oid,
656 int flag, void *cb_data)
658 struct rev_info *revs = (struct rev_info*)cb_data;
659 struct object_id peeled;
660 struct object *object;
662 if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
663 warning("symbolic ref is dangling: %s", refname);
664 return 0;
667 if (!peel_iterated_oid(oid, &peeled))
668 oid = &peeled;
670 object = parse_object_or_die(oid, refname);
671 if (object->type != OBJ_COMMIT)
672 return 0;
674 add_pending_object(revs, object, "");
675 if (bitmap_is_preferred_refname(revs->repo, refname))
676 object->flags |= NEEDS_BITMAP;
677 return 0;
680 struct bitmap_commit_cb {
681 struct commit **commits;
682 size_t commits_nr, commits_alloc;
684 struct write_midx_context *ctx;
687 static const struct object_id *bitmap_oid_access(size_t index,
688 const void *_entries)
690 const struct pack_midx_entry *entries = _entries;
691 return &entries[index].oid;
694 static void bitmap_show_commit(struct commit *commit, void *_data)
696 struct bitmap_commit_cb *data = _data;
697 int pos = oid_pos(&commit->object.oid, data->ctx->entries,
698 data->ctx->entries_nr,
699 bitmap_oid_access);
700 if (pos < 0)
701 return;
703 ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc);
704 data->commits[data->commits_nr++] = commit;
707 static int read_refs_snapshot(const char *refs_snapshot,
708 struct rev_info *revs)
710 struct strbuf buf = STRBUF_INIT;
711 struct object_id oid;
712 FILE *f = xfopen(refs_snapshot, "r");
714 while (strbuf_getline(&buf, f) != EOF) {
715 struct object *object;
716 int preferred = 0;
717 char *hex = buf.buf;
718 const char *end = NULL;
720 if (buf.len && *buf.buf == '+') {
721 preferred = 1;
722 hex = &buf.buf[1];
725 if (parse_oid_hex(hex, &oid, &end) < 0)
726 die(_("could not parse line: %s"), buf.buf);
727 if (*end)
728 die(_("malformed line: %s"), buf.buf);
730 object = parse_object_or_die(&oid, NULL);
731 if (preferred)
732 object->flags |= NEEDS_BITMAP;
734 add_pending_object(revs, object, "");
737 fclose(f);
738 strbuf_release(&buf);
739 return 0;
741 static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
742 const char *refs_snapshot,
743 struct write_midx_context *ctx)
745 struct rev_info revs;
746 struct bitmap_commit_cb cb = {0};
748 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
749 the_repository);
751 cb.ctx = ctx;
753 repo_init_revisions(the_repository, &revs, NULL);
754 if (refs_snapshot) {
755 read_refs_snapshot(refs_snapshot, &revs);
756 } else {
757 setup_revisions(0, NULL, &revs, NULL);
758 for_each_ref(add_ref_to_pending, &revs);
762 * Skipping promisor objects here is intentional, since it only excludes
763 * them from the list of reachable commits that we want to select from
764 * when computing the selection of MIDX'd commits to receive bitmaps.
766 * Reachability bitmaps do require that their objects be closed under
767 * reachability, but fetching any objects missing from promisors at this
768 * point is too late. But, if one of those objects can be reached from
769 * an another object that is included in the bitmap, then we will
770 * complain later that we don't have reachability closure (and fail
771 * appropriately).
773 fetch_if_missing = 0;
774 revs.exclude_promisor_objects = 1;
776 if (prepare_revision_walk(&revs))
777 die(_("revision walk setup failed"));
779 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
780 if (indexed_commits_nr_p)
781 *indexed_commits_nr_p = cb.commits_nr;
783 release_revisions(&revs);
785 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
786 the_repository);
788 return cb.commits;
791 static int write_midx_bitmap(const char *midx_name,
792 const unsigned char *midx_hash,
793 struct packing_data *pdata,
794 struct commit **commits,
795 uint32_t commits_nr,
796 uint32_t *pack_order,
797 unsigned flags)
799 int ret, i;
800 uint16_t options = 0;
801 struct pack_idx_entry **index;
802 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
803 hash_to_hex(midx_hash));
805 trace2_region_enter("midx", "write_midx_bitmap", the_repository);
807 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
808 options |= BITMAP_OPT_HASH_CACHE;
810 if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE)
811 options |= BITMAP_OPT_LOOKUP_TABLE;
814 * Build the MIDX-order index based on pdata.objects (which is already
815 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
816 * this order).
818 ALLOC_ARRAY(index, pdata->nr_objects);
819 for (i = 0; i < pdata->nr_objects; i++)
820 index[i] = &pdata->objects[i].idx;
822 bitmap_writer_show_progress(flags & MIDX_PROGRESS);
823 bitmap_writer_build_type_index(pdata, index, pdata->nr_objects);
826 * bitmap_writer_finish expects objects in lex order, but pack_order
827 * gives us exactly that. use it directly instead of re-sorting the
828 * array.
830 * This changes the order of objects in 'index' between
831 * bitmap_writer_build_type_index and bitmap_writer_finish.
833 * The same re-ordering takes place in the single-pack bitmap code via
834 * write_idx_file(), which is called by finish_tmp_packfile(), which
835 * happens between bitmap_writer_build_type_index() and
836 * bitmap_writer_finish().
838 for (i = 0; i < pdata->nr_objects; i++)
839 index[pack_order[i]] = &pdata->objects[i].idx;
841 bitmap_writer_select_commits(commits, commits_nr, -1);
842 ret = bitmap_writer_build(pdata);
843 if (ret < 0)
844 goto cleanup;
846 bitmap_writer_set_checksum(midx_hash);
847 bitmap_writer_finish(index, pdata->nr_objects, bitmap_name, options);
849 cleanup:
850 free(index);
851 free(bitmap_name);
853 trace2_region_leave("midx", "write_midx_bitmap", the_repository);
855 return ret;
858 static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
859 const char *object_dir)
861 struct multi_pack_index *result = NULL;
862 struct multi_pack_index *cur;
863 char *obj_dir_real = real_pathdup(object_dir, 1);
864 struct strbuf cur_path_real = STRBUF_INIT;
866 /* Ensure the given object_dir is local, or a known alternate. */
867 find_odb(r, obj_dir_real);
869 for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
870 strbuf_realpath(&cur_path_real, cur->object_dir, 1);
871 if (!strcmp(obj_dir_real, cur_path_real.buf)) {
872 result = cur;
873 goto cleanup;
877 cleanup:
878 free(obj_dir_real);
879 strbuf_release(&cur_path_real);
880 return result;
883 static int write_midx_internal(const char *object_dir,
884 struct string_list *packs_to_include,
885 struct string_list *packs_to_drop,
886 const char *preferred_pack_name,
887 const char *refs_snapshot,
888 unsigned flags)
890 struct strbuf midx_name = STRBUF_INIT;
891 unsigned char midx_hash[GIT_MAX_RAWSZ];
892 uint32_t i;
893 struct hashfile *f = NULL;
894 struct lock_file lk;
895 struct write_midx_context ctx = { 0 };
896 int bitmapped_packs_concat_len = 0;
897 int pack_name_concat_len = 0;
898 int dropped_packs = 0;
899 int result = 0;
900 struct chunkfile *cf;
902 trace2_region_enter("midx", "write_midx_internal", the_repository);
904 get_midx_filename(&midx_name, object_dir);
905 if (safe_create_leading_directories(midx_name.buf))
906 die_errno(_("unable to create leading directories of %s"),
907 midx_name.buf);
909 if (!packs_to_include) {
911 * Only reference an existing MIDX when not filtering which
912 * packs to include, since all packs and objects are copied
913 * blindly from an existing MIDX if one is present.
915 ctx.m = lookup_multi_pack_index(the_repository, object_dir);
918 if (ctx.m && !midx_checksum_valid(ctx.m)) {
919 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
920 ctx.m = NULL;
923 ctx.nr = 0;
924 ctx.alloc = ctx.m ? ctx.m->num_packs : 16;
925 ctx.info = NULL;
926 ALLOC_ARRAY(ctx.info, ctx.alloc);
928 if (ctx.m) {
929 for (i = 0; i < ctx.m->num_packs; i++) {
930 ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
932 if (flags & MIDX_WRITE_REV_INDEX) {
934 * If generating a reverse index, need to have
935 * packed_git's loaded to compare their
936 * mtimes and object count.
938 if (prepare_midx_pack(the_repository, ctx.m, i)) {
939 error(_("could not load pack"));
940 result = 1;
941 goto cleanup;
944 if (open_pack_index(ctx.m->packs[i]))
945 die(_("could not open index for %s"),
946 ctx.m->packs[i]->pack_name);
949 fill_pack_info(&ctx.info[ctx.nr++], ctx.m->packs[i],
950 ctx.m->pack_names[i], i);
954 ctx.pack_paths_checked = 0;
955 if (flags & MIDX_PROGRESS)
956 ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
957 else
958 ctx.progress = NULL;
960 ctx.to_include = packs_to_include;
962 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
963 stop_progress(&ctx.progress);
965 if ((ctx.m && ctx.nr == ctx.m->num_packs) &&
966 !(packs_to_include || packs_to_drop)) {
967 struct bitmap_index *bitmap_git;
968 int bitmap_exists;
969 int want_bitmap = flags & MIDX_WRITE_BITMAP;
971 bitmap_git = prepare_midx_bitmap_git(ctx.m);
972 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
973 free_bitmap_index(bitmap_git);
975 if (bitmap_exists || !want_bitmap) {
977 * The correct MIDX already exists, and so does a
978 * corresponding bitmap (or one wasn't requested).
980 if (!want_bitmap)
981 clear_midx_files_ext(object_dir, ".bitmap",
982 NULL);
983 goto cleanup;
987 if (preferred_pack_name) {
988 ctx.preferred_pack_idx = -1;
990 for (i = 0; i < ctx.nr; i++) {
991 if (!cmp_idx_or_pack_name(preferred_pack_name,
992 ctx.info[i].pack_name)) {
993 ctx.preferred_pack_idx = i;
994 break;
998 if (ctx.preferred_pack_idx == -1)
999 warning(_("unknown preferred pack: '%s'"),
1000 preferred_pack_name);
1001 } else if (ctx.nr &&
1002 (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1003 struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1004 ctx.preferred_pack_idx = 0;
1006 if (packs_to_drop && packs_to_drop->nr)
1007 BUG("cannot write a MIDX bitmap during expiration");
1010 * set a preferred pack when writing a bitmap to ensure that
1011 * the pack from which the first object is selected in pseudo
1012 * pack-order has all of its objects selected from that pack
1013 * (and not another pack containing a duplicate)
1015 for (i = 1; i < ctx.nr; i++) {
1016 struct packed_git *p = ctx.info[i].p;
1018 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1019 oldest = p;
1020 ctx.preferred_pack_idx = i;
1024 if (!oldest->num_objects) {
1026 * If all packs are empty; unset the preferred index.
1027 * This is acceptable since there will be no duplicate
1028 * objects to resolve, so the preferred value doesn't
1029 * matter.
1031 ctx.preferred_pack_idx = -1;
1033 } else {
1035 * otherwise don't mark any pack as preferred to avoid
1036 * interfering with expiration logic below
1038 ctx.preferred_pack_idx = -1;
1041 if (ctx.preferred_pack_idx > -1) {
1042 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1043 if (!preferred->num_objects) {
1044 error(_("cannot select preferred pack %s with no objects"),
1045 preferred->pack_name);
1046 result = 1;
1047 goto cleanup;
1051 ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr,
1052 ctx.preferred_pack_idx);
1054 ctx.large_offsets_needed = 0;
1055 for (i = 0; i < ctx.entries_nr; i++) {
1056 if (ctx.entries[i].offset > 0x7fffffff)
1057 ctx.num_large_offsets++;
1058 if (ctx.entries[i].offset > 0xffffffff)
1059 ctx.large_offsets_needed = 1;
1062 QSORT(ctx.info, ctx.nr, pack_info_compare);
1064 if (packs_to_drop && packs_to_drop->nr) {
1065 int drop_index = 0;
1066 int missing_drops = 0;
1068 for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1069 int cmp = strcmp(ctx.info[i].pack_name,
1070 packs_to_drop->items[drop_index].string);
1072 if (!cmp) {
1073 drop_index++;
1074 ctx.info[i].expired = 1;
1075 } else if (cmp > 0) {
1076 error(_("did not see pack-file %s to drop"),
1077 packs_to_drop->items[drop_index].string);
1078 drop_index++;
1079 missing_drops++;
1080 i--;
1081 } else {
1082 ctx.info[i].expired = 0;
1086 if (missing_drops) {
1087 result = 1;
1088 goto cleanup;
1093 * pack_perm stores a permutation between pack-int-ids from the
1094 * previous multi-pack-index to the new one we are writing:
1096 * pack_perm[old_id] = new_id
1098 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1099 for (i = 0; i < ctx.nr; i++) {
1100 if (ctx.info[i].expired) {
1101 dropped_packs++;
1102 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
1103 } else {
1104 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
1108 for (i = 0; i < ctx.nr; i++) {
1109 if (ctx.info[i].expired)
1110 continue;
1111 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
1112 bitmapped_packs_concat_len += 2 * sizeof(uint32_t);
1115 /* Check that the preferred pack wasn't expired (if given). */
1116 if (preferred_pack_name) {
1117 struct pack_info *preferred = bsearch(preferred_pack_name,
1118 ctx.info, ctx.nr,
1119 sizeof(*ctx.info),
1120 idx_or_pack_name_cmp);
1121 if (preferred) {
1122 uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1123 if (perm == PACK_EXPIRED)
1124 warning(_("preferred pack '%s' is expired"),
1125 preferred_pack_name);
1129 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1130 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1131 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1133 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
1134 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
1136 if (ctx.nr - dropped_packs == 0) {
1137 error(_("no pack files to index."));
1138 result = 1;
1139 goto cleanup;
1142 if (!ctx.entries_nr) {
1143 if (flags & MIDX_WRITE_BITMAP)
1144 warning(_("refusing to write multi-pack .bitmap without any objects"));
1145 flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1148 cf = init_chunkfile(f);
1150 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1151 write_midx_pack_names);
1152 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1153 write_midx_oid_fanout);
1154 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
1155 st_mult(ctx.entries_nr, the_hash_algo->rawsz),
1156 write_midx_oid_lookup);
1157 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
1158 st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
1159 write_midx_object_offsets);
1161 if (ctx.large_offsets_needed)
1162 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
1163 st_mult(ctx.num_large_offsets,
1164 MIDX_CHUNK_LARGE_OFFSET_WIDTH),
1165 write_midx_large_offsets);
1167 if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) {
1168 ctx.pack_order = midx_pack_order(&ctx);
1169 add_chunk(cf, MIDX_CHUNKID_REVINDEX,
1170 st_mult(ctx.entries_nr, sizeof(uint32_t)),
1171 write_midx_revindex);
1172 add_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
1173 bitmapped_packs_concat_len,
1174 write_midx_bitmapped_packs);
1177 write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1178 write_chunkfile(cf, &ctx);
1180 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
1181 CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1182 free_chunkfile(cf);
1184 if (flags & MIDX_WRITE_REV_INDEX &&
1185 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1186 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
1188 if (flags & MIDX_WRITE_BITMAP) {
1189 struct packing_data pdata;
1190 struct commit **commits;
1191 uint32_t commits_nr;
1193 if (!ctx.entries_nr)
1194 BUG("cannot write a bitmap without any objects");
1196 prepare_midx_packing_data(&pdata, &ctx);
1198 commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1201 * The previous steps translated the information from
1202 * 'entries' into information suitable for constructing
1203 * bitmaps. We no longer need that array, so clear it to
1204 * reduce memory pressure.
1206 FREE_AND_NULL(ctx.entries);
1207 ctx.entries_nr = 0;
1209 if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1210 commits, commits_nr, ctx.pack_order,
1211 flags) < 0) {
1212 error(_("could not write multi-pack bitmap"));
1213 result = 1;
1214 clear_packing_data(&pdata);
1215 free(commits);
1216 goto cleanup;
1219 clear_packing_data(&pdata);
1220 free(commits);
1223 * NOTE: Do not use ctx.entries beyond this point, since it might
1224 * have been freed in the previous if block.
1227 if (ctx.m)
1228 close_object_store(the_repository->objects);
1230 if (commit_lock_file(&lk) < 0)
1231 die_errno(_("could not write multi-pack-index"));
1233 clear_midx_files_ext(object_dir, ".bitmap", midx_hash);
1234 clear_midx_files_ext(object_dir, ".rev", midx_hash);
1236 cleanup:
1237 for (i = 0; i < ctx.nr; i++) {
1238 if (ctx.info[i].p) {
1239 close_pack(ctx.info[i].p);
1240 free(ctx.info[i].p);
1242 free(ctx.info[i].pack_name);
1245 free(ctx.info);
1246 free(ctx.entries);
1247 free(ctx.pack_perm);
1248 free(ctx.pack_order);
1249 strbuf_release(&midx_name);
1251 trace2_region_leave("midx", "write_midx_internal", the_repository);
1253 return result;
1256 int write_midx_file(const char *object_dir,
1257 const char *preferred_pack_name,
1258 const char *refs_snapshot,
1259 unsigned flags)
1261 return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
1262 refs_snapshot, flags);
1265 int write_midx_file_only(const char *object_dir,
1266 struct string_list *packs_to_include,
1267 const char *preferred_pack_name,
1268 const char *refs_snapshot,
1269 unsigned flags)
1271 return write_midx_internal(object_dir, packs_to_include, NULL,
1272 preferred_pack_name, refs_snapshot, flags);
1275 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
1277 uint32_t i, *count, result = 0;
1278 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1279 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1280 struct progress *progress = NULL;
1282 if (!m)
1283 return 0;
1285 CALLOC_ARRAY(count, m->num_packs);
1287 if (flags & MIDX_PROGRESS)
1288 progress = start_delayed_progress(_("Counting referenced objects"),
1289 m->num_objects);
1290 for (i = 0; i < m->num_objects; i++) {
1291 int pack_int_id = nth_midxed_pack_int_id(m, i);
1292 count[pack_int_id]++;
1293 display_progress(progress, i + 1);
1295 stop_progress(&progress);
1297 if (flags & MIDX_PROGRESS)
1298 progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1299 m->num_packs);
1300 for (i = 0; i < m->num_packs; i++) {
1301 char *pack_name;
1302 display_progress(progress, i + 1);
1304 if (count[i])
1305 continue;
1307 if (prepare_midx_pack(r, m, i))
1308 continue;
1310 if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
1311 continue;
1313 pack_name = xstrdup(m->packs[i]->pack_name);
1314 close_pack(m->packs[i]);
1316 string_list_insert(&packs_to_drop, m->pack_names[i]);
1317 unlink_pack_path(pack_name, 0);
1318 free(pack_name);
1320 stop_progress(&progress);
1322 free(count);
1324 if (packs_to_drop.nr)
1325 result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
1327 string_list_clear(&packs_to_drop, 0);
1329 return result;
1332 struct repack_info {
1333 timestamp_t mtime;
1334 uint32_t referenced_objects;
1335 uint32_t pack_int_id;
1338 static int compare_by_mtime(const void *a_, const void *b_)
1340 const struct repack_info *a, *b;
1342 a = (const struct repack_info *)a_;
1343 b = (const struct repack_info *)b_;
1345 if (a->mtime < b->mtime)
1346 return -1;
1347 if (a->mtime > b->mtime)
1348 return 1;
1349 return 0;
1352 static int want_included_pack(struct repository *r,
1353 struct multi_pack_index *m,
1354 int pack_kept_objects,
1355 uint32_t pack_int_id)
1357 struct packed_git *p;
1358 if (prepare_midx_pack(r, m, pack_int_id))
1359 return 0;
1360 p = m->packs[pack_int_id];
1361 if (!pack_kept_objects && p->pack_keep)
1362 return 0;
1363 if (p->is_cruft)
1364 return 0;
1365 if (open_pack_index(p) || !p->num_objects)
1366 return 0;
1367 return 1;
1370 static void fill_included_packs_all(struct repository *r,
1371 struct multi_pack_index *m,
1372 unsigned char *include_pack)
1374 uint32_t i;
1375 int pack_kept_objects = 0;
1377 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1379 for (i = 0; i < m->num_packs; i++) {
1380 if (!want_included_pack(r, m, pack_kept_objects, i))
1381 continue;
1383 include_pack[i] = 1;
1387 static void fill_included_packs_batch(struct repository *r,
1388 struct multi_pack_index *m,
1389 unsigned char *include_pack,
1390 size_t batch_size)
1392 uint32_t i;
1393 size_t total_size;
1394 struct repack_info *pack_info;
1395 int pack_kept_objects = 0;
1397 CALLOC_ARRAY(pack_info, m->num_packs);
1399 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1401 for (i = 0; i < m->num_packs; i++) {
1402 pack_info[i].pack_int_id = i;
1404 if (prepare_midx_pack(r, m, i))
1405 continue;
1407 pack_info[i].mtime = m->packs[i]->mtime;
1410 for (i = 0; i < m->num_objects; i++) {
1411 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1412 pack_info[pack_int_id].referenced_objects++;
1415 QSORT(pack_info, m->num_packs, compare_by_mtime);
1417 total_size = 0;
1418 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1419 int pack_int_id = pack_info[i].pack_int_id;
1420 struct packed_git *p = m->packs[pack_int_id];
1421 size_t expected_size;
1423 if (!want_included_pack(r, m, pack_kept_objects, pack_int_id))
1424 continue;
1426 expected_size = st_mult(p->pack_size,
1427 pack_info[i].referenced_objects);
1428 expected_size /= p->num_objects;
1430 if (expected_size >= batch_size)
1431 continue;
1433 total_size += expected_size;
1434 include_pack[pack_int_id] = 1;
1437 free(pack_info);
1440 int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
1442 int result = 0;
1443 uint32_t i, packs_to_repack = 0;
1444 unsigned char *include_pack;
1445 struct child_process cmd = CHILD_PROCESS_INIT;
1446 FILE *cmd_in;
1447 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1450 * When updating the default for these configuration
1451 * variables in builtin/repack.c, these must be adjusted
1452 * to match.
1454 int delta_base_offset = 1;
1455 int use_delta_islands = 0;
1457 if (!m)
1458 return 0;
1460 CALLOC_ARRAY(include_pack, m->num_packs);
1462 if (batch_size)
1463 fill_included_packs_batch(r, m, include_pack, batch_size);
1464 else
1465 fill_included_packs_all(r, m, include_pack);
1467 for (i = 0; i < m->num_packs; i++) {
1468 if (include_pack[i])
1469 packs_to_repack++;
1471 if (packs_to_repack <= 1)
1472 goto cleanup;
1474 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
1475 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
1477 strvec_pushl(&cmd.args, "pack-objects", "--stdin-packs", "--non-empty",
1478 NULL);
1480 strvec_pushf(&cmd.args, "%s/pack/pack", object_dir);
1482 if (delta_base_offset)
1483 strvec_push(&cmd.args, "--delta-base-offset");
1484 if (use_delta_islands)
1485 strvec_push(&cmd.args, "--delta-islands");
1487 if (flags & MIDX_PROGRESS)
1488 strvec_push(&cmd.args, "--progress");
1489 else
1490 strvec_push(&cmd.args, "-q");
1492 cmd.git_cmd = 1;
1493 cmd.in = cmd.out = -1;
1495 if (start_command(&cmd)) {
1496 error(_("could not start pack-objects"));
1497 result = 1;
1498 goto cleanup;
1501 cmd_in = xfdopen(cmd.in, "w");
1502 for (i = 0; i < m->num_packs; i++) {
1503 struct packed_git *p = m->packs[i];
1504 if (!p)
1505 continue;
1507 if (include_pack[i])
1508 fprintf(cmd_in, "%s\n", pack_basename(p));
1509 else
1510 fprintf(cmd_in, "^%s\n", pack_basename(p));
1512 fclose(cmd_in);
1514 if (finish_command(&cmd)) {
1515 error(_("could not finish pack-objects"));
1516 result = 1;
1517 goto cleanup;
1520 result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
1522 cleanup:
1523 free(include_pack);
1524 return result;