sparse-checkout: free duplicate hashmap entries
[git.git] / midx-write.c
blob2fa120c213b37c50e51801caa329b7160ee3e9b9
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 refs_for_each_ref(get_main_ref_store(the_repository),
759 add_ref_to_pending, &revs);
763 * Skipping promisor objects here is intentional, since it only excludes
764 * them from the list of reachable commits that we want to select from
765 * when computing the selection of MIDX'd commits to receive bitmaps.
767 * Reachability bitmaps do require that their objects be closed under
768 * reachability, but fetching any objects missing from promisors at this
769 * point is too late. But, if one of those objects can be reached from
770 * an another object that is included in the bitmap, then we will
771 * complain later that we don't have reachability closure (and fail
772 * appropriately).
774 fetch_if_missing = 0;
775 revs.exclude_promisor_objects = 1;
777 if (prepare_revision_walk(&revs))
778 die(_("revision walk setup failed"));
780 traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb);
781 if (indexed_commits_nr_p)
782 *indexed_commits_nr_p = cb.commits_nr;
784 release_revisions(&revs);
786 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
787 the_repository);
789 return cb.commits;
792 static int write_midx_bitmap(const char *midx_name,
793 const unsigned char *midx_hash,
794 struct packing_data *pdata,
795 struct commit **commits,
796 uint32_t commits_nr,
797 uint32_t *pack_order,
798 unsigned flags)
800 int ret, i;
801 uint16_t options = 0;
802 struct bitmap_writer writer;
803 struct pack_idx_entry **index;
804 char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
805 hash_to_hex(midx_hash));
807 trace2_region_enter("midx", "write_midx_bitmap", the_repository);
809 if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
810 options |= BITMAP_OPT_HASH_CACHE;
812 if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE)
813 options |= BITMAP_OPT_LOOKUP_TABLE;
816 * Build the MIDX-order index based on pdata.objects (which is already
817 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
818 * this order).
820 ALLOC_ARRAY(index, pdata->nr_objects);
821 for (i = 0; i < pdata->nr_objects; i++)
822 index[i] = &pdata->objects[i].idx;
824 bitmap_writer_init(&writer);
825 bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
826 bitmap_writer_build_type_index(&writer, pdata, index,
827 pdata->nr_objects);
830 * bitmap_writer_finish expects objects in lex order, but pack_order
831 * gives us exactly that. use it directly instead of re-sorting the
832 * array.
834 * This changes the order of objects in 'index' between
835 * bitmap_writer_build_type_index and bitmap_writer_finish.
837 * The same re-ordering takes place in the single-pack bitmap code via
838 * write_idx_file(), which is called by finish_tmp_packfile(), which
839 * happens between bitmap_writer_build_type_index() and
840 * bitmap_writer_finish().
842 for (i = 0; i < pdata->nr_objects; i++)
843 index[pack_order[i]] = &pdata->objects[i].idx;
845 bitmap_writer_select_commits(&writer, commits, commits_nr);
846 ret = bitmap_writer_build(&writer, pdata);
847 if (ret < 0)
848 goto cleanup;
850 bitmap_writer_set_checksum(&writer, midx_hash);
851 bitmap_writer_finish(&writer, index, pdata->nr_objects, bitmap_name,
852 options);
854 cleanup:
855 free(index);
856 free(bitmap_name);
857 bitmap_writer_free(&writer);
859 trace2_region_leave("midx", "write_midx_bitmap", the_repository);
861 return ret;
864 static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
865 const char *object_dir)
867 struct multi_pack_index *result = NULL;
868 struct multi_pack_index *cur;
869 char *obj_dir_real = real_pathdup(object_dir, 1);
870 struct strbuf cur_path_real = STRBUF_INIT;
872 /* Ensure the given object_dir is local, or a known alternate. */
873 find_odb(r, obj_dir_real);
875 for (cur = get_multi_pack_index(r); cur; cur = cur->next) {
876 strbuf_realpath(&cur_path_real, cur->object_dir, 1);
877 if (!strcmp(obj_dir_real, cur_path_real.buf)) {
878 result = cur;
879 goto cleanup;
883 cleanup:
884 free(obj_dir_real);
885 strbuf_release(&cur_path_real);
886 return result;
889 static int write_midx_internal(const char *object_dir,
890 struct string_list *packs_to_include,
891 struct string_list *packs_to_drop,
892 const char *preferred_pack_name,
893 const char *refs_snapshot,
894 unsigned flags)
896 struct strbuf midx_name = STRBUF_INIT;
897 unsigned char midx_hash[GIT_MAX_RAWSZ];
898 uint32_t i;
899 struct hashfile *f = NULL;
900 struct lock_file lk;
901 struct write_midx_context ctx = { 0 };
902 int bitmapped_packs_concat_len = 0;
903 int pack_name_concat_len = 0;
904 int dropped_packs = 0;
905 int result = 0;
906 struct chunkfile *cf;
908 trace2_region_enter("midx", "write_midx_internal", the_repository);
910 get_midx_filename(&midx_name, object_dir);
911 if (safe_create_leading_directories(midx_name.buf))
912 die_errno(_("unable to create leading directories of %s"),
913 midx_name.buf);
915 if (!packs_to_include) {
917 * Only reference an existing MIDX when not filtering which
918 * packs to include, since all packs and objects are copied
919 * blindly from an existing MIDX if one is present.
921 ctx.m = lookup_multi_pack_index(the_repository, object_dir);
924 if (ctx.m && !midx_checksum_valid(ctx.m)) {
925 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
926 ctx.m = NULL;
929 ctx.nr = 0;
930 ctx.alloc = ctx.m ? ctx.m->num_packs : 16;
931 ctx.info = NULL;
932 ALLOC_ARRAY(ctx.info, ctx.alloc);
934 if (ctx.m) {
935 for (i = 0; i < ctx.m->num_packs; i++) {
936 ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
938 if (flags & MIDX_WRITE_REV_INDEX) {
940 * If generating a reverse index, need to have
941 * packed_git's loaded to compare their
942 * mtimes and object count.
944 if (prepare_midx_pack(the_repository, ctx.m, i)) {
945 error(_("could not load pack"));
946 result = 1;
947 goto cleanup;
950 if (open_pack_index(ctx.m->packs[i]))
951 die(_("could not open index for %s"),
952 ctx.m->packs[i]->pack_name);
955 fill_pack_info(&ctx.info[ctx.nr++], ctx.m->packs[i],
956 ctx.m->pack_names[i], i);
960 ctx.pack_paths_checked = 0;
961 if (flags & MIDX_PROGRESS)
962 ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
963 else
964 ctx.progress = NULL;
966 ctx.to_include = packs_to_include;
968 for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx);
969 stop_progress(&ctx.progress);
971 if ((ctx.m && ctx.nr == ctx.m->num_packs) &&
972 !(packs_to_include || packs_to_drop)) {
973 struct bitmap_index *bitmap_git;
974 int bitmap_exists;
975 int want_bitmap = flags & MIDX_WRITE_BITMAP;
977 bitmap_git = prepare_midx_bitmap_git(ctx.m);
978 bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git);
979 free_bitmap_index(bitmap_git);
981 if (bitmap_exists || !want_bitmap) {
983 * The correct MIDX already exists, and so does a
984 * corresponding bitmap (or one wasn't requested).
986 if (!want_bitmap)
987 clear_midx_files_ext(object_dir, ".bitmap",
988 NULL);
989 goto cleanup;
993 if (preferred_pack_name) {
994 ctx.preferred_pack_idx = -1;
996 for (i = 0; i < ctx.nr; i++) {
997 if (!cmp_idx_or_pack_name(preferred_pack_name,
998 ctx.info[i].pack_name)) {
999 ctx.preferred_pack_idx = i;
1000 break;
1004 if (ctx.preferred_pack_idx == -1)
1005 warning(_("unknown preferred pack: '%s'"),
1006 preferred_pack_name);
1007 } else if (ctx.nr &&
1008 (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1009 struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1010 ctx.preferred_pack_idx = 0;
1012 if (packs_to_drop && packs_to_drop->nr)
1013 BUG("cannot write a MIDX bitmap during expiration");
1016 * set a preferred pack when writing a bitmap to ensure that
1017 * the pack from which the first object is selected in pseudo
1018 * pack-order has all of its objects selected from that pack
1019 * (and not another pack containing a duplicate)
1021 for (i = 1; i < ctx.nr; i++) {
1022 struct packed_git *p = ctx.info[i].p;
1024 if (!oldest->num_objects || p->mtime < oldest->mtime) {
1025 oldest = p;
1026 ctx.preferred_pack_idx = i;
1030 if (!oldest->num_objects) {
1032 * If all packs are empty; unset the preferred index.
1033 * This is acceptable since there will be no duplicate
1034 * objects to resolve, so the preferred value doesn't
1035 * matter.
1037 ctx.preferred_pack_idx = -1;
1039 } else {
1041 * otherwise don't mark any pack as preferred to avoid
1042 * interfering with expiration logic below
1044 ctx.preferred_pack_idx = -1;
1047 if (ctx.preferred_pack_idx > -1) {
1048 struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
1049 if (!preferred->num_objects) {
1050 error(_("cannot select preferred pack %s with no objects"),
1051 preferred->pack_name);
1052 result = 1;
1053 goto cleanup;
1057 ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr,
1058 ctx.preferred_pack_idx);
1060 ctx.large_offsets_needed = 0;
1061 for (i = 0; i < ctx.entries_nr; i++) {
1062 if (ctx.entries[i].offset > 0x7fffffff)
1063 ctx.num_large_offsets++;
1064 if (ctx.entries[i].offset > 0xffffffff)
1065 ctx.large_offsets_needed = 1;
1068 QSORT(ctx.info, ctx.nr, pack_info_compare);
1070 if (packs_to_drop && packs_to_drop->nr) {
1071 int drop_index = 0;
1072 int missing_drops = 0;
1074 for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) {
1075 int cmp = strcmp(ctx.info[i].pack_name,
1076 packs_to_drop->items[drop_index].string);
1078 if (!cmp) {
1079 drop_index++;
1080 ctx.info[i].expired = 1;
1081 } else if (cmp > 0) {
1082 error(_("did not see pack-file %s to drop"),
1083 packs_to_drop->items[drop_index].string);
1084 drop_index++;
1085 missing_drops++;
1086 i--;
1087 } else {
1088 ctx.info[i].expired = 0;
1092 if (missing_drops) {
1093 result = 1;
1094 goto cleanup;
1099 * pack_perm stores a permutation between pack-int-ids from the
1100 * previous multi-pack-index to the new one we are writing:
1102 * pack_perm[old_id] = new_id
1104 ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
1105 for (i = 0; i < ctx.nr; i++) {
1106 if (ctx.info[i].expired) {
1107 dropped_packs++;
1108 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
1109 } else {
1110 ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
1114 for (i = 0; i < ctx.nr; i++) {
1115 if (ctx.info[i].expired)
1116 continue;
1117 pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1;
1118 bitmapped_packs_concat_len += 2 * sizeof(uint32_t);
1121 /* Check that the preferred pack wasn't expired (if given). */
1122 if (preferred_pack_name) {
1123 struct pack_info *preferred = bsearch(preferred_pack_name,
1124 ctx.info, ctx.nr,
1125 sizeof(*ctx.info),
1126 idx_or_pack_name_cmp);
1127 if (preferred) {
1128 uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1129 if (perm == PACK_EXPIRED)
1130 warning(_("preferred pack '%s' is expired"),
1131 preferred_pack_name);
1135 if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
1136 pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
1137 (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
1139 hold_lock_file_for_update(&lk, midx_name.buf, LOCK_DIE_ON_ERROR);
1140 f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
1142 if (ctx.nr - dropped_packs == 0) {
1143 error(_("no pack files to index."));
1144 result = 1;
1145 goto cleanup;
1148 if (!ctx.entries_nr) {
1149 if (flags & MIDX_WRITE_BITMAP)
1150 warning(_("refusing to write multi-pack .bitmap without any objects"));
1151 flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP);
1154 cf = init_chunkfile(f);
1156 add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len,
1157 write_midx_pack_names);
1158 add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
1159 write_midx_oid_fanout);
1160 add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
1161 st_mult(ctx.entries_nr, the_hash_algo->rawsz),
1162 write_midx_oid_lookup);
1163 add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
1164 st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
1165 write_midx_object_offsets);
1167 if (ctx.large_offsets_needed)
1168 add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS,
1169 st_mult(ctx.num_large_offsets,
1170 MIDX_CHUNK_LARGE_OFFSET_WIDTH),
1171 write_midx_large_offsets);
1173 if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) {
1174 ctx.pack_order = midx_pack_order(&ctx);
1175 add_chunk(cf, MIDX_CHUNKID_REVINDEX,
1176 st_mult(ctx.entries_nr, sizeof(uint32_t)),
1177 write_midx_revindex);
1178 add_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
1179 bitmapped_packs_concat_len,
1180 write_midx_bitmapped_packs);
1183 write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
1184 write_chunkfile(cf, &ctx);
1186 finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
1187 CSUM_FSYNC | CSUM_HASH_IN_STREAM);
1188 free_chunkfile(cf);
1190 if (flags & MIDX_WRITE_REV_INDEX &&
1191 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1192 write_midx_reverse_index(midx_name.buf, midx_hash, &ctx);
1194 if (flags & MIDX_WRITE_BITMAP) {
1195 struct packing_data pdata;
1196 struct commit **commits;
1197 uint32_t commits_nr;
1199 if (!ctx.entries_nr)
1200 BUG("cannot write a bitmap without any objects");
1202 prepare_midx_packing_data(&pdata, &ctx);
1204 commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, &ctx);
1207 * The previous steps translated the information from
1208 * 'entries' into information suitable for constructing
1209 * bitmaps. We no longer need that array, so clear it to
1210 * reduce memory pressure.
1212 FREE_AND_NULL(ctx.entries);
1213 ctx.entries_nr = 0;
1215 if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
1216 commits, commits_nr, ctx.pack_order,
1217 flags) < 0) {
1218 error(_("could not write multi-pack bitmap"));
1219 result = 1;
1220 clear_packing_data(&pdata);
1221 free(commits);
1222 goto cleanup;
1225 clear_packing_data(&pdata);
1226 free(commits);
1229 * NOTE: Do not use ctx.entries beyond this point, since it might
1230 * have been freed in the previous if block.
1233 if (ctx.m)
1234 close_object_store(the_repository->objects);
1236 if (commit_lock_file(&lk) < 0)
1237 die_errno(_("could not write multi-pack-index"));
1239 clear_midx_files_ext(object_dir, ".bitmap", midx_hash);
1240 clear_midx_files_ext(object_dir, ".rev", midx_hash);
1242 cleanup:
1243 for (i = 0; i < ctx.nr; i++) {
1244 if (ctx.info[i].p) {
1245 close_pack(ctx.info[i].p);
1246 free(ctx.info[i].p);
1248 free(ctx.info[i].pack_name);
1251 free(ctx.info);
1252 free(ctx.entries);
1253 free(ctx.pack_perm);
1254 free(ctx.pack_order);
1255 strbuf_release(&midx_name);
1257 trace2_region_leave("midx", "write_midx_internal", the_repository);
1259 return result;
1262 int write_midx_file(const char *object_dir,
1263 const char *preferred_pack_name,
1264 const char *refs_snapshot,
1265 unsigned flags)
1267 return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
1268 refs_snapshot, flags);
1271 int write_midx_file_only(const char *object_dir,
1272 struct string_list *packs_to_include,
1273 const char *preferred_pack_name,
1274 const char *refs_snapshot,
1275 unsigned flags)
1277 return write_midx_internal(object_dir, packs_to_include, NULL,
1278 preferred_pack_name, refs_snapshot, flags);
1281 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
1283 uint32_t i, *count, result = 0;
1284 struct string_list packs_to_drop = STRING_LIST_INIT_DUP;
1285 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1286 struct progress *progress = NULL;
1288 if (!m)
1289 return 0;
1291 CALLOC_ARRAY(count, m->num_packs);
1293 if (flags & MIDX_PROGRESS)
1294 progress = start_delayed_progress(_("Counting referenced objects"),
1295 m->num_objects);
1296 for (i = 0; i < m->num_objects; i++) {
1297 int pack_int_id = nth_midxed_pack_int_id(m, i);
1298 count[pack_int_id]++;
1299 display_progress(progress, i + 1);
1301 stop_progress(&progress);
1303 if (flags & MIDX_PROGRESS)
1304 progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1305 m->num_packs);
1306 for (i = 0; i < m->num_packs; i++) {
1307 char *pack_name;
1308 display_progress(progress, i + 1);
1310 if (count[i])
1311 continue;
1313 if (prepare_midx_pack(r, m, i))
1314 continue;
1316 if (m->packs[i]->pack_keep || m->packs[i]->is_cruft)
1317 continue;
1319 pack_name = xstrdup(m->packs[i]->pack_name);
1320 close_pack(m->packs[i]);
1322 string_list_insert(&packs_to_drop, m->pack_names[i]);
1323 unlink_pack_path(pack_name, 0);
1324 free(pack_name);
1326 stop_progress(&progress);
1328 free(count);
1330 if (packs_to_drop.nr)
1331 result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
1333 string_list_clear(&packs_to_drop, 0);
1335 return result;
1338 struct repack_info {
1339 timestamp_t mtime;
1340 uint32_t referenced_objects;
1341 uint32_t pack_int_id;
1344 static int compare_by_mtime(const void *a_, const void *b_)
1346 const struct repack_info *a, *b;
1348 a = (const struct repack_info *)a_;
1349 b = (const struct repack_info *)b_;
1351 if (a->mtime < b->mtime)
1352 return -1;
1353 if (a->mtime > b->mtime)
1354 return 1;
1355 return 0;
1358 static int want_included_pack(struct repository *r,
1359 struct multi_pack_index *m,
1360 int pack_kept_objects,
1361 uint32_t pack_int_id)
1363 struct packed_git *p;
1364 if (prepare_midx_pack(r, m, pack_int_id))
1365 return 0;
1366 p = m->packs[pack_int_id];
1367 if (!pack_kept_objects && p->pack_keep)
1368 return 0;
1369 if (p->is_cruft)
1370 return 0;
1371 if (open_pack_index(p) || !p->num_objects)
1372 return 0;
1373 return 1;
1376 static void fill_included_packs_all(struct repository *r,
1377 struct multi_pack_index *m,
1378 unsigned char *include_pack)
1380 uint32_t i;
1381 int pack_kept_objects = 0;
1383 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1385 for (i = 0; i < m->num_packs; i++) {
1386 if (!want_included_pack(r, m, pack_kept_objects, i))
1387 continue;
1389 include_pack[i] = 1;
1393 static void fill_included_packs_batch(struct repository *r,
1394 struct multi_pack_index *m,
1395 unsigned char *include_pack,
1396 size_t batch_size)
1398 uint32_t i;
1399 size_t total_size;
1400 struct repack_info *pack_info;
1401 int pack_kept_objects = 0;
1403 CALLOC_ARRAY(pack_info, m->num_packs);
1405 repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects);
1407 for (i = 0; i < m->num_packs; i++) {
1408 pack_info[i].pack_int_id = i;
1410 if (prepare_midx_pack(r, m, i))
1411 continue;
1413 pack_info[i].mtime = m->packs[i]->mtime;
1416 for (i = 0; i < m->num_objects; i++) {
1417 uint32_t pack_int_id = nth_midxed_pack_int_id(m, i);
1418 pack_info[pack_int_id].referenced_objects++;
1421 QSORT(pack_info, m->num_packs, compare_by_mtime);
1423 total_size = 0;
1424 for (i = 0; total_size < batch_size && i < m->num_packs; i++) {
1425 int pack_int_id = pack_info[i].pack_int_id;
1426 struct packed_git *p = m->packs[pack_int_id];
1427 size_t expected_size;
1429 if (!want_included_pack(r, m, pack_kept_objects, pack_int_id))
1430 continue;
1432 expected_size = st_mult(p->pack_size,
1433 pack_info[i].referenced_objects);
1434 expected_size /= p->num_objects;
1436 if (expected_size >= batch_size)
1437 continue;
1439 total_size += expected_size;
1440 include_pack[pack_int_id] = 1;
1443 free(pack_info);
1446 int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags)
1448 int result = 0;
1449 uint32_t i, packs_to_repack = 0;
1450 unsigned char *include_pack;
1451 struct child_process cmd = CHILD_PROCESS_INIT;
1452 FILE *cmd_in;
1453 struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
1456 * When updating the default for these configuration
1457 * variables in builtin/repack.c, these must be adjusted
1458 * to match.
1460 int delta_base_offset = 1;
1461 int use_delta_islands = 0;
1463 if (!m)
1464 return 0;
1466 CALLOC_ARRAY(include_pack, m->num_packs);
1468 if (batch_size)
1469 fill_included_packs_batch(r, m, include_pack, batch_size);
1470 else
1471 fill_included_packs_all(r, m, include_pack);
1473 for (i = 0; i < m->num_packs; i++) {
1474 if (include_pack[i])
1475 packs_to_repack++;
1477 if (packs_to_repack <= 1)
1478 goto cleanup;
1480 repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset);
1481 repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands);
1483 strvec_pushl(&cmd.args, "pack-objects", "--stdin-packs", "--non-empty",
1484 NULL);
1486 strvec_pushf(&cmd.args, "%s/pack/pack", object_dir);
1488 if (delta_base_offset)
1489 strvec_push(&cmd.args, "--delta-base-offset");
1490 if (use_delta_islands)
1491 strvec_push(&cmd.args, "--delta-islands");
1493 if (flags & MIDX_PROGRESS)
1494 strvec_push(&cmd.args, "--progress");
1495 else
1496 strvec_push(&cmd.args, "-q");
1498 cmd.git_cmd = 1;
1499 cmd.in = cmd.out = -1;
1501 if (start_command(&cmd)) {
1502 error(_("could not start pack-objects"));
1503 result = 1;
1504 goto cleanup;
1507 cmd_in = xfdopen(cmd.in, "w");
1508 for (i = 0; i < m->num_packs; i++) {
1509 struct packed_git *p = m->packs[i];
1510 if (!p)
1511 continue;
1513 if (include_pack[i])
1514 fprintf(cmd_in, "%s\n", pack_basename(p));
1515 else
1516 fprintf(cmd_in, "^%s\n", pack_basename(p));
1518 fclose(cmd_in);
1520 if (finish_command(&cmd)) {
1521 error(_("could not finish pack-objects"));
1522 result = 1;
1523 goto cleanup;
1526 result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
1528 cleanup:
1529 free(include_pack);
1530 return result;