The eighteenth batch
[git.git] / builtin / repack.c
blob8bb875532b4db6e3284b42c8e77bbafa114fee9b
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "parse-options.h"
8 #include "path.h"
9 #include "run-command.h"
10 #include "server-info.h"
11 #include "strbuf.h"
12 #include "string-list.h"
13 #include "strvec.h"
14 #include "midx.h"
15 #include "packfile.h"
16 #include "prune-packed.h"
17 #include "object-store-ll.h"
18 #include "promisor-remote.h"
19 #include "shallow.h"
20 #include "pack.h"
21 #include "pack-bitmap.h"
22 #include "refs.h"
23 #include "list-objects-filter-options.h"
25 #define ALL_INTO_ONE 1
26 #define LOOSEN_UNREACHABLE 2
27 #define PACK_CRUFT 4
29 #define DELETE_PACK 1
30 #define RETAIN_PACK 2
32 static int pack_everything;
33 static int delta_base_offset = 1;
34 static int pack_kept_objects = -1;
35 static int write_bitmaps = -1;
36 static int use_delta_islands;
37 static int run_update_server_info = 1;
38 static char *packdir, *packtmp_name, *packtmp;
40 static const char *const git_repack_usage[] = {
41 N_("git repack [<options>]"),
42 NULL
45 static const char incremental_bitmap_conflict_error[] = N_(
46 "Incremental repacks are incompatible with bitmap indexes. Use\n"
47 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
50 struct pack_objects_args {
51 char *window;
52 char *window_memory;
53 char *depth;
54 char *threads;
55 unsigned long max_pack_size;
56 int no_reuse_delta;
57 int no_reuse_object;
58 int quiet;
59 int local;
60 struct list_objects_filter_options filter_options;
63 static int repack_config(const char *var, const char *value,
64 const struct config_context *ctx, void *cb)
66 struct pack_objects_args *cruft_po_args = cb;
67 if (!strcmp(var, "repack.usedeltabaseoffset")) {
68 delta_base_offset = git_config_bool(var, value);
69 return 0;
71 if (!strcmp(var, "repack.packkeptobjects")) {
72 pack_kept_objects = git_config_bool(var, value);
73 return 0;
75 if (!strcmp(var, "repack.writebitmaps") ||
76 !strcmp(var, "pack.writebitmaps")) {
77 write_bitmaps = git_config_bool(var, value);
78 return 0;
80 if (!strcmp(var, "repack.usedeltaislands")) {
81 use_delta_islands = git_config_bool(var, value);
82 return 0;
84 if (strcmp(var, "repack.updateserverinfo") == 0) {
85 run_update_server_info = git_config_bool(var, value);
86 return 0;
88 if (!strcmp(var, "repack.cruftwindow"))
89 return git_config_string(&cruft_po_args->window, var, value);
90 if (!strcmp(var, "repack.cruftwindowmemory"))
91 return git_config_string(&cruft_po_args->window_memory, var, value);
92 if (!strcmp(var, "repack.cruftdepth"))
93 return git_config_string(&cruft_po_args->depth, var, value);
94 if (!strcmp(var, "repack.cruftthreads"))
95 return git_config_string(&cruft_po_args->threads, var, value);
96 return git_default_config(var, value, ctx, cb);
99 struct existing_packs {
100 struct string_list kept_packs;
101 struct string_list non_kept_packs;
102 struct string_list cruft_packs;
105 #define EXISTING_PACKS_INIT { \
106 .kept_packs = STRING_LIST_INIT_DUP, \
107 .non_kept_packs = STRING_LIST_INIT_DUP, \
108 .cruft_packs = STRING_LIST_INIT_DUP, \
111 static int has_existing_non_kept_packs(const struct existing_packs *existing)
113 return existing->non_kept_packs.nr || existing->cruft_packs.nr;
116 static void pack_mark_for_deletion(struct string_list_item *item)
118 item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
121 static void pack_unmark_for_deletion(struct string_list_item *item)
123 item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
126 static int pack_is_marked_for_deletion(struct string_list_item *item)
128 return (uintptr_t)item->util & DELETE_PACK;
131 static void pack_mark_retained(struct string_list_item *item)
133 item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
136 static int pack_is_retained(struct string_list_item *item)
138 return (uintptr_t)item->util & RETAIN_PACK;
141 static void mark_packs_for_deletion_1(struct string_list *names,
142 struct string_list *list)
144 struct string_list_item *item;
145 const int hexsz = the_hash_algo->hexsz;
147 for_each_string_list_item(item, list) {
148 char *sha1;
149 size_t len = strlen(item->string);
150 if (len < hexsz)
151 continue;
152 sha1 = item->string + len - hexsz;
154 if (pack_is_retained(item)) {
155 pack_unmark_for_deletion(item);
156 } else if (!string_list_has_string(names, sha1)) {
158 * Mark this pack for deletion, which ensures
159 * that this pack won't be included in a MIDX
160 * (if `--write-midx` was given) and that we
161 * will actually delete this pack (if `-d` was
162 * given).
164 pack_mark_for_deletion(item);
169 static void retain_cruft_pack(struct existing_packs *existing,
170 struct packed_git *cruft)
172 struct strbuf buf = STRBUF_INIT;
173 struct string_list_item *item;
175 strbuf_addstr(&buf, pack_basename(cruft));
176 strbuf_strip_suffix(&buf, ".pack");
178 item = string_list_lookup(&existing->cruft_packs, buf.buf);
179 if (!item)
180 BUG("could not find cruft pack '%s'", pack_basename(cruft));
182 pack_mark_retained(item);
183 strbuf_release(&buf);
186 static void mark_packs_for_deletion(struct existing_packs *existing,
187 struct string_list *names)
190 mark_packs_for_deletion_1(names, &existing->non_kept_packs);
191 mark_packs_for_deletion_1(names, &existing->cruft_packs);
194 static void remove_redundant_pack(const char *dir_name, const char *base_name)
196 struct strbuf buf = STRBUF_INIT;
197 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
198 strbuf_addf(&buf, "%s.pack", base_name);
199 if (m && midx_contains_pack(m, buf.buf))
200 clear_midx_file(the_repository);
201 strbuf_insertf(&buf, 0, "%s/", dir_name);
202 unlink_pack_path(buf.buf, 1);
203 strbuf_release(&buf);
206 static void remove_redundant_packs_1(struct string_list *packs)
208 struct string_list_item *item;
209 for_each_string_list_item(item, packs) {
210 if (!pack_is_marked_for_deletion(item))
211 continue;
212 remove_redundant_pack(packdir, item->string);
216 static void remove_redundant_existing_packs(struct existing_packs *existing)
218 remove_redundant_packs_1(&existing->non_kept_packs);
219 remove_redundant_packs_1(&existing->cruft_packs);
222 static void existing_packs_release(struct existing_packs *existing)
224 string_list_clear(&existing->kept_packs, 0);
225 string_list_clear(&existing->non_kept_packs, 0);
226 string_list_clear(&existing->cruft_packs, 0);
230 * Adds all packs hex strings (pack-$HASH) to either packs->non_kept
231 * or packs->kept based on whether each pack has a corresponding
232 * .keep file or not. Packs without a .keep file are not to be kept
233 * if we are going to pack everything into one file.
235 static void collect_pack_filenames(struct existing_packs *existing,
236 const struct string_list *extra_keep)
238 struct packed_git *p;
239 struct strbuf buf = STRBUF_INIT;
241 for (p = get_all_packs(the_repository); p; p = p->next) {
242 int i;
243 const char *base;
245 if (!p->pack_local)
246 continue;
248 base = pack_basename(p);
250 for (i = 0; i < extra_keep->nr; i++)
251 if (!fspathcmp(base, extra_keep->items[i].string))
252 break;
254 strbuf_reset(&buf);
255 strbuf_addstr(&buf, base);
256 strbuf_strip_suffix(&buf, ".pack");
258 if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
259 string_list_append(&existing->kept_packs, buf.buf);
260 else if (p->is_cruft)
261 string_list_append(&existing->cruft_packs, buf.buf);
262 else
263 string_list_append(&existing->non_kept_packs, buf.buf);
266 string_list_sort(&existing->kept_packs);
267 string_list_sort(&existing->non_kept_packs);
268 string_list_sort(&existing->cruft_packs);
269 strbuf_release(&buf);
272 static void prepare_pack_objects(struct child_process *cmd,
273 const struct pack_objects_args *args,
274 const char *out)
276 strvec_push(&cmd->args, "pack-objects");
277 if (args->window)
278 strvec_pushf(&cmd->args, "--window=%s", args->window);
279 if (args->window_memory)
280 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
281 if (args->depth)
282 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
283 if (args->threads)
284 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
285 if (args->max_pack_size)
286 strvec_pushf(&cmd->args, "--max-pack-size=%lu", args->max_pack_size);
287 if (args->no_reuse_delta)
288 strvec_pushf(&cmd->args, "--no-reuse-delta");
289 if (args->no_reuse_object)
290 strvec_pushf(&cmd->args, "--no-reuse-object");
291 if (args->local)
292 strvec_push(&cmd->args, "--local");
293 if (args->quiet)
294 strvec_push(&cmd->args, "--quiet");
295 if (delta_base_offset)
296 strvec_push(&cmd->args, "--delta-base-offset");
297 strvec_push(&cmd->args, out);
298 cmd->git_cmd = 1;
299 cmd->out = -1;
303 * Write oid to the given struct child_process's stdin, starting it first if
304 * necessary.
306 static int write_oid(const struct object_id *oid,
307 struct packed_git *pack UNUSED,
308 uint32_t pos UNUSED, void *data)
310 struct child_process *cmd = data;
312 if (cmd->in == -1) {
313 if (start_command(cmd))
314 die(_("could not start pack-objects to repack promisor objects"));
317 if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
318 write_in_full(cmd->in, "\n", 1) < 0)
319 die(_("failed to feed promisor objects to pack-objects"));
320 return 0;
323 static struct {
324 const char *name;
325 unsigned optional:1;
326 } exts[] = {
327 {".pack"},
328 {".rev", 1},
329 {".mtimes", 1},
330 {".bitmap", 1},
331 {".promisor", 1},
332 {".idx"},
335 struct generated_pack_data {
336 struct tempfile *tempfiles[ARRAY_SIZE(exts)];
339 static struct generated_pack_data *populate_pack_exts(const char *name)
341 struct stat statbuf;
342 struct strbuf path = STRBUF_INIT;
343 struct generated_pack_data *data = xcalloc(1, sizeof(*data));
344 int i;
346 for (i = 0; i < ARRAY_SIZE(exts); i++) {
347 strbuf_reset(&path);
348 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
350 if (stat(path.buf, &statbuf))
351 continue;
353 data->tempfiles[i] = register_tempfile(path.buf);
356 strbuf_release(&path);
357 return data;
360 static int has_pack_ext(const struct generated_pack_data *data,
361 const char *ext)
363 int i;
364 for (i = 0; i < ARRAY_SIZE(exts); i++) {
365 if (strcmp(exts[i].name, ext))
366 continue;
367 return !!data->tempfiles[i];
369 BUG("unknown pack extension: '%s'", ext);
372 static void repack_promisor_objects(const struct pack_objects_args *args,
373 struct string_list *names)
375 struct child_process cmd = CHILD_PROCESS_INIT;
376 FILE *out;
377 struct strbuf line = STRBUF_INIT;
379 prepare_pack_objects(&cmd, args, packtmp);
380 cmd.in = -1;
383 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
384 * hints may result in suboptimal deltas in the resulting pack. See if
385 * the OIDs can be sent with fake paths such that pack-objects can use a
386 * {type -> existing pack order} ordering when computing deltas instead
387 * of a {type -> size} ordering, which may produce better deltas.
389 for_each_packed_object(write_oid, &cmd,
390 FOR_EACH_OBJECT_PROMISOR_ONLY);
392 if (cmd.in == -1) {
393 /* No packed objects; cmd was never started */
394 child_process_clear(&cmd);
395 return;
398 close(cmd.in);
400 out = xfdopen(cmd.out, "r");
401 while (strbuf_getline_lf(&line, out) != EOF) {
402 struct string_list_item *item;
403 char *promisor_name;
405 if (line.len != the_hash_algo->hexsz)
406 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
407 item = string_list_append(names, line.buf);
410 * pack-objects creates the .pack and .idx files, but not the
411 * .promisor file. Create the .promisor file, which is empty.
413 * NEEDSWORK: fetch-pack sometimes generates non-empty
414 * .promisor files containing the ref names and associated
415 * hashes at the point of generation of the corresponding
416 * packfile, but this would not preserve their contents. Maybe
417 * concatenate the contents of all .promisor files instead of
418 * just creating a new empty file.
420 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
421 line.buf);
422 write_promisor_file(promisor_name, NULL, 0);
424 item->util = populate_pack_exts(item->string);
426 free(promisor_name);
428 fclose(out);
429 if (finish_command(&cmd))
430 die(_("could not finish pack-objects to repack promisor objects"));
433 struct pack_geometry {
434 struct packed_git **pack;
435 uint32_t pack_nr, pack_alloc;
436 uint32_t split;
438 int split_factor;
441 static uint32_t geometry_pack_weight(struct packed_git *p)
443 if (open_pack_index(p))
444 die(_("cannot open index for %s"), p->pack_name);
445 return p->num_objects;
448 static int geometry_cmp(const void *va, const void *vb)
450 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
451 bw = geometry_pack_weight(*(struct packed_git **)vb);
453 if (aw < bw)
454 return -1;
455 if (aw > bw)
456 return 1;
457 return 0;
460 static void init_pack_geometry(struct pack_geometry *geometry,
461 struct existing_packs *existing,
462 const struct pack_objects_args *args)
464 struct packed_git *p;
465 struct strbuf buf = STRBUF_INIT;
467 for (p = get_all_packs(the_repository); p; p = p->next) {
468 if (args->local && !p->pack_local)
470 * When asked to only repack local packfiles we skip
471 * over any packfiles that are borrowed from alternate
472 * object directories.
474 continue;
476 if (!pack_kept_objects) {
478 * Any pack that has its pack_keep bit set will
479 * appear in existing->kept_packs below, but
480 * this saves us from doing a more expensive
481 * check.
483 if (p->pack_keep)
484 continue;
487 * The pack may be kept via the --keep-pack
488 * option; check 'existing->kept_packs' to
489 * determine whether to ignore it.
491 strbuf_reset(&buf);
492 strbuf_addstr(&buf, pack_basename(p));
493 strbuf_strip_suffix(&buf, ".pack");
495 if (string_list_has_string(&existing->kept_packs, buf.buf))
496 continue;
498 if (p->is_cruft)
499 continue;
501 ALLOC_GROW(geometry->pack,
502 geometry->pack_nr + 1,
503 geometry->pack_alloc);
505 geometry->pack[geometry->pack_nr] = p;
506 geometry->pack_nr++;
509 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
510 strbuf_release(&buf);
513 static void split_pack_geometry(struct pack_geometry *geometry)
515 uint32_t i;
516 uint32_t split;
517 off_t total_size = 0;
519 if (!geometry->pack_nr) {
520 geometry->split = geometry->pack_nr;
521 return;
525 * First, count the number of packs (in descending order of size) which
526 * already form a geometric progression.
528 for (i = geometry->pack_nr - 1; i > 0; i--) {
529 struct packed_git *ours = geometry->pack[i];
530 struct packed_git *prev = geometry->pack[i - 1];
532 if (unsigned_mult_overflows(geometry->split_factor,
533 geometry_pack_weight(prev)))
534 die(_("pack %s too large to consider in geometric "
535 "progression"),
536 prev->pack_name);
538 if (geometry_pack_weight(ours) <
539 geometry->split_factor * geometry_pack_weight(prev))
540 break;
543 split = i;
545 if (split) {
547 * Move the split one to the right, since the top element in the
548 * last-compared pair can't be in the progression. Only do this
549 * when we split in the middle of the array (otherwise if we got
550 * to the end, then the split is in the right place).
552 split++;
556 * Then, anything to the left of 'split' must be in a new pack. But,
557 * creating that new pack may cause packs in the heavy half to no longer
558 * form a geometric progression.
560 * Compute an expected size of the new pack, and then determine how many
561 * packs in the heavy half need to be joined into it (if any) to restore
562 * the geometric progression.
564 for (i = 0; i < split; i++) {
565 struct packed_git *p = geometry->pack[i];
567 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
568 die(_("pack %s too large to roll up"), p->pack_name);
569 total_size += geometry_pack_weight(p);
571 for (i = split; i < geometry->pack_nr; i++) {
572 struct packed_git *ours = geometry->pack[i];
574 if (unsigned_mult_overflows(geometry->split_factor,
575 total_size))
576 die(_("pack %s too large to roll up"), ours->pack_name);
578 if (geometry_pack_weight(ours) <
579 geometry->split_factor * total_size) {
580 if (unsigned_add_overflows(total_size,
581 geometry_pack_weight(ours)))
582 die(_("pack %s too large to roll up"),
583 ours->pack_name);
585 split++;
586 total_size += geometry_pack_weight(ours);
587 } else
588 break;
591 geometry->split = split;
594 static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
596 uint32_t i;
598 if (!geometry) {
600 * No geometry means either an all-into-one repack (in which
601 * case there is only one pack left and it is the largest) or an
602 * incremental one.
604 * If repacking incrementally, then we could check the size of
605 * all packs to determine which should be preferred, but leave
606 * this for later.
608 return NULL;
610 if (geometry->split == geometry->pack_nr)
611 return NULL;
614 * The preferred pack is the largest pack above the split line. In
615 * other words, it is the largest pack that does not get rolled up in
616 * the geometric repack.
618 for (i = geometry->pack_nr; i > geometry->split; i--)
620 * A pack that is not local would never be included in a
621 * multi-pack index. We thus skip over any non-local packs.
623 if (geometry->pack[i - 1]->pack_local)
624 return geometry->pack[i - 1];
626 return NULL;
629 static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
630 struct string_list *names,
631 struct existing_packs *existing)
633 struct strbuf buf = STRBUF_INIT;
634 uint32_t i;
636 for (i = 0; i < geometry->split; i++) {
637 struct packed_git *p = geometry->pack[i];
638 if (string_list_has_string(names, hash_to_hex(p->hash)))
639 continue;
641 strbuf_reset(&buf);
642 strbuf_addstr(&buf, pack_basename(p));
643 strbuf_strip_suffix(&buf, ".pack");
645 if ((p->pack_keep) ||
646 (string_list_has_string(&existing->kept_packs, buf.buf)))
647 continue;
649 remove_redundant_pack(packdir, buf.buf);
652 strbuf_release(&buf);
655 static void free_pack_geometry(struct pack_geometry *geometry)
657 if (!geometry)
658 return;
660 free(geometry->pack);
663 struct midx_snapshot_ref_data {
664 struct tempfile *f;
665 struct oidset seen;
666 int preferred;
669 static int midx_snapshot_ref_one(const char *refname UNUSED,
670 const char *referent UNUSED,
671 const struct object_id *oid,
672 int flag UNUSED, void *_data)
674 struct midx_snapshot_ref_data *data = _data;
675 struct object_id peeled;
677 if (!peel_iterated_oid(the_repository, oid, &peeled))
678 oid = &peeled;
680 if (oidset_insert(&data->seen, oid))
681 return 0; /* already seen */
683 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
684 return 0;
686 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
687 oid_to_hex(oid));
689 return 0;
692 static void midx_snapshot_refs(struct tempfile *f)
694 struct midx_snapshot_ref_data data;
695 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
697 data.f = f;
698 data.preferred = 0;
699 oidset_init(&data.seen, 0);
701 if (!fdopen_tempfile(f, "w"))
702 die(_("could not open tempfile %s for writing"),
703 get_tempfile_path(f));
705 if (preferred) {
706 struct string_list_item *item;
708 data.preferred = 1;
709 for_each_string_list_item(item, preferred)
710 refs_for_each_ref_in(get_main_ref_store(the_repository),
711 item->string,
712 midx_snapshot_ref_one, &data);
713 data.preferred = 0;
716 refs_for_each_ref(get_main_ref_store(the_repository),
717 midx_snapshot_ref_one, &data);
719 if (close_tempfile_gently(f)) {
720 int save_errno = errno;
721 delete_tempfile(&f);
722 errno = save_errno;
723 die_errno(_("could not close refs snapshot tempfile"));
726 oidset_clear(&data.seen);
729 static void midx_included_packs(struct string_list *include,
730 struct existing_packs *existing,
731 struct string_list *names,
732 struct pack_geometry *geometry)
734 struct string_list_item *item;
735 struct strbuf buf = STRBUF_INIT;
737 for_each_string_list_item(item, &existing->kept_packs) {
738 strbuf_reset(&buf);
739 strbuf_addf(&buf, "%s.idx", item->string);
740 string_list_insert(include, buf.buf);
743 for_each_string_list_item(item, names) {
744 strbuf_reset(&buf);
745 strbuf_addf(&buf, "pack-%s.idx", item->string);
746 string_list_insert(include, buf.buf);
749 if (geometry->split_factor) {
750 uint32_t i;
752 for (i = geometry->split; i < geometry->pack_nr; i++) {
753 struct packed_git *p = geometry->pack[i];
756 * The multi-pack index never refers to packfiles part
757 * of an alternate object database, so we skip these.
758 * While git-multi-pack-index(1) would silently ignore
759 * them anyway, this allows us to skip executing the
760 * command completely when we have only non-local
761 * packfiles.
763 if (!p->pack_local)
764 continue;
766 strbuf_reset(&buf);
767 strbuf_addstr(&buf, pack_basename(p));
768 strbuf_strip_suffix(&buf, ".pack");
769 strbuf_addstr(&buf, ".idx");
771 string_list_insert(include, buf.buf);
773 } else {
774 for_each_string_list_item(item, &existing->non_kept_packs) {
775 if (pack_is_marked_for_deletion(item))
776 continue;
778 strbuf_reset(&buf);
779 strbuf_addf(&buf, "%s.idx", item->string);
780 string_list_insert(include, buf.buf);
784 for_each_string_list_item(item, &existing->cruft_packs) {
786 * When doing a --geometric repack, there is no need to check
787 * for deleted packs, since we're by definition not doing an
788 * ALL_INTO_ONE repack (hence no packs will be deleted).
789 * Otherwise we must check for and exclude any packs which are
790 * enqueued for deletion.
792 * So we could omit the conditional below in the --geometric
793 * case, but doing so is unnecessary since no packs are marked
794 * as pending deletion (since we only call
795 * `mark_packs_for_deletion()` when doing an all-into-one
796 * repack).
798 if (pack_is_marked_for_deletion(item))
799 continue;
801 strbuf_reset(&buf);
802 strbuf_addf(&buf, "%s.idx", item->string);
803 string_list_insert(include, buf.buf);
806 strbuf_release(&buf);
809 static int write_midx_included_packs(struct string_list *include,
810 struct pack_geometry *geometry,
811 struct string_list *names,
812 const char *refs_snapshot,
813 int show_progress, int write_bitmaps)
815 struct child_process cmd = CHILD_PROCESS_INIT;
816 struct string_list_item *item;
817 struct packed_git *preferred = get_preferred_pack(geometry);
818 FILE *in;
819 int ret;
821 if (!include->nr)
822 return 0;
824 cmd.in = -1;
825 cmd.git_cmd = 1;
827 strvec_push(&cmd.args, "multi-pack-index");
828 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
830 if (show_progress)
831 strvec_push(&cmd.args, "--progress");
832 else
833 strvec_push(&cmd.args, "--no-progress");
835 if (write_bitmaps)
836 strvec_push(&cmd.args, "--bitmap");
838 if (preferred)
839 strvec_pushf(&cmd.args, "--preferred-pack=%s",
840 pack_basename(preferred));
841 else if (names->nr) {
842 /* The largest pack was repacked, meaning that either
843 * one or two packs exist depending on whether the
844 * repository has a cruft pack or not.
846 * Select the non-cruft one as preferred to encourage
847 * pack-reuse among packs containing reachable objects
848 * over unreachable ones.
850 * (Note we could write multiple packs here if
851 * `--max-pack-size` was given, but any one of them
852 * will suffice, so pick the first one.)
854 for_each_string_list_item(item, names) {
855 struct generated_pack_data *data = item->util;
856 if (has_pack_ext(data, ".mtimes"))
857 continue;
859 strvec_pushf(&cmd.args, "--preferred-pack=pack-%s.pack",
860 item->string);
861 break;
863 } else {
865 * No packs were kept, and no packs were written. The
866 * only thing remaining are .keep packs (unless
867 * --pack-kept-objects was given).
869 * Set the `--preferred-pack` arbitrarily here.
874 if (refs_snapshot)
875 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
877 ret = start_command(&cmd);
878 if (ret)
879 return ret;
881 in = xfdopen(cmd.in, "w");
882 for_each_string_list_item(item, include)
883 fprintf(in, "%s\n", item->string);
884 fclose(in);
886 return finish_command(&cmd);
889 static void remove_redundant_bitmaps(struct string_list *include,
890 const char *packdir)
892 struct strbuf path = STRBUF_INIT;
893 struct string_list_item *item;
894 size_t packdir_len;
896 strbuf_addstr(&path, packdir);
897 strbuf_addch(&path, '/');
898 packdir_len = path.len;
901 * Remove any pack bitmaps corresponding to packs which are now
902 * included in the MIDX.
904 for_each_string_list_item(item, include) {
905 strbuf_addstr(&path, item->string);
906 strbuf_strip_suffix(&path, ".idx");
907 strbuf_addstr(&path, ".bitmap");
909 if (unlink(path.buf) && errno != ENOENT)
910 warning_errno(_("could not remove stale bitmap: %s"),
911 path.buf);
913 strbuf_setlen(&path, packdir_len);
915 strbuf_release(&path);
918 static int finish_pack_objects_cmd(struct child_process *cmd,
919 struct string_list *names,
920 int local)
922 FILE *out;
923 struct strbuf line = STRBUF_INIT;
925 out = xfdopen(cmd->out, "r");
926 while (strbuf_getline_lf(&line, out) != EOF) {
927 struct string_list_item *item;
929 if (line.len != the_hash_algo->hexsz)
930 die(_("repack: Expecting full hex object ID lines only "
931 "from pack-objects."));
933 * Avoid putting packs written outside of the repository in the
934 * list of names.
936 if (local) {
937 item = string_list_append(names, line.buf);
938 item->util = populate_pack_exts(line.buf);
941 fclose(out);
943 strbuf_release(&line);
945 return finish_command(cmd);
948 static int write_filtered_pack(const struct pack_objects_args *args,
949 const char *destination,
950 const char *pack_prefix,
951 struct existing_packs *existing,
952 struct string_list *names)
954 struct child_process cmd = CHILD_PROCESS_INIT;
955 struct string_list_item *item;
956 FILE *in;
957 int ret;
958 const char *caret;
959 const char *scratch;
960 int local = skip_prefix(destination, packdir, &scratch);
962 prepare_pack_objects(&cmd, args, destination);
964 strvec_push(&cmd.args, "--stdin-packs");
966 if (!pack_kept_objects)
967 strvec_push(&cmd.args, "--honor-pack-keep");
968 for_each_string_list_item(item, &existing->kept_packs)
969 strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
971 cmd.in = -1;
973 ret = start_command(&cmd);
974 if (ret)
975 return ret;
978 * Here 'names' contains only the pack(s) that were just
979 * written, which is exactly the packs we want to keep. Also
980 * 'existing_kept_packs' already contains the packs in
981 * 'keep_pack_list'.
983 in = xfdopen(cmd.in, "w");
984 for_each_string_list_item(item, names)
985 fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
986 for_each_string_list_item(item, &existing->non_kept_packs)
987 fprintf(in, "%s.pack\n", item->string);
988 for_each_string_list_item(item, &existing->cruft_packs)
989 fprintf(in, "%s.pack\n", item->string);
990 caret = pack_kept_objects ? "" : "^";
991 for_each_string_list_item(item, &existing->kept_packs)
992 fprintf(in, "%s%s.pack\n", caret, item->string);
993 fclose(in);
995 return finish_pack_objects_cmd(&cmd, names, local);
998 static int existing_cruft_pack_cmp(const void *va, const void *vb)
1000 struct packed_git *a = *(struct packed_git **)va;
1001 struct packed_git *b = *(struct packed_git **)vb;
1003 if (a->pack_size < b->pack_size)
1004 return -1;
1005 if (a->pack_size > b->pack_size)
1006 return 1;
1007 return 0;
1010 static void collapse_small_cruft_packs(FILE *in, size_t max_size,
1011 struct existing_packs *existing)
1013 struct packed_git **existing_cruft, *p;
1014 struct strbuf buf = STRBUF_INIT;
1015 size_t total_size = 0;
1016 size_t existing_cruft_nr = 0;
1017 size_t i;
1019 ALLOC_ARRAY(existing_cruft, existing->cruft_packs.nr);
1021 for (p = get_all_packs(the_repository); p; p = p->next) {
1022 if (!(p->is_cruft && p->pack_local))
1023 continue;
1025 strbuf_reset(&buf);
1026 strbuf_addstr(&buf, pack_basename(p));
1027 strbuf_strip_suffix(&buf, ".pack");
1029 if (!string_list_has_string(&existing->cruft_packs, buf.buf))
1030 continue;
1032 if (existing_cruft_nr >= existing->cruft_packs.nr)
1033 BUG("too many cruft packs (found %"PRIuMAX", but knew "
1034 "of %"PRIuMAX")",
1035 (uintmax_t)existing_cruft_nr + 1,
1036 (uintmax_t)existing->cruft_packs.nr);
1037 existing_cruft[existing_cruft_nr++] = p;
1040 QSORT(existing_cruft, existing_cruft_nr, existing_cruft_pack_cmp);
1042 for (i = 0; i < existing_cruft_nr; i++) {
1043 size_t proposed;
1045 p = existing_cruft[i];
1046 proposed = st_add(total_size, p->pack_size);
1048 if (proposed <= max_size) {
1049 total_size = proposed;
1050 fprintf(in, "-%s\n", pack_basename(p));
1051 } else {
1052 retain_cruft_pack(existing, p);
1053 fprintf(in, "%s\n", pack_basename(p));
1057 for (i = 0; i < existing->non_kept_packs.nr; i++)
1058 fprintf(in, "-%s.pack\n",
1059 existing->non_kept_packs.items[i].string);
1061 strbuf_release(&buf);
1062 free(existing_cruft);
1065 static int write_cruft_pack(const struct pack_objects_args *args,
1066 const char *destination,
1067 const char *pack_prefix,
1068 const char *cruft_expiration,
1069 struct string_list *names,
1070 struct existing_packs *existing)
1072 struct child_process cmd = CHILD_PROCESS_INIT;
1073 struct string_list_item *item;
1074 FILE *in;
1075 int ret;
1076 const char *scratch;
1077 int local = skip_prefix(destination, packdir, &scratch);
1079 prepare_pack_objects(&cmd, args, destination);
1081 strvec_push(&cmd.args, "--cruft");
1082 if (cruft_expiration)
1083 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
1084 cruft_expiration);
1086 strvec_push(&cmd.args, "--honor-pack-keep");
1087 strvec_push(&cmd.args, "--non-empty");
1089 cmd.in = -1;
1091 ret = start_command(&cmd);
1092 if (ret)
1093 return ret;
1096 * names has a confusing double use: it both provides the list
1097 * of just-written new packs, and accepts the name of the cruft
1098 * pack we are writing.
1100 * By the time it is read here, it contains only the pack(s)
1101 * that were just written, which is exactly the set of packs we
1102 * want to consider kept.
1104 * If `--expire-to` is given, the double-use served by `names`
1105 * ensures that the pack written to `--expire-to` excludes any
1106 * objects contained in the cruft pack.
1108 in = xfdopen(cmd.in, "w");
1109 for_each_string_list_item(item, names)
1110 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
1111 if (args->max_pack_size && !cruft_expiration) {
1112 collapse_small_cruft_packs(in, args->max_pack_size, existing);
1113 } else {
1114 for_each_string_list_item(item, &existing->non_kept_packs)
1115 fprintf(in, "-%s.pack\n", item->string);
1116 for_each_string_list_item(item, &existing->cruft_packs)
1117 fprintf(in, "-%s.pack\n", item->string);
1119 for_each_string_list_item(item, &existing->kept_packs)
1120 fprintf(in, "%s.pack\n", item->string);
1121 fclose(in);
1123 return finish_pack_objects_cmd(&cmd, names, local);
1126 static const char *find_pack_prefix(const char *packdir, const char *packtmp)
1128 const char *pack_prefix;
1129 if (!skip_prefix(packtmp, packdir, &pack_prefix))
1130 die(_("pack prefix %s does not begin with objdir %s"),
1131 packtmp, packdir);
1132 if (*pack_prefix == '/')
1133 pack_prefix++;
1134 return pack_prefix;
1137 int cmd_repack(int argc, const char **argv, const char *prefix)
1139 struct child_process cmd = CHILD_PROCESS_INIT;
1140 struct string_list_item *item;
1141 struct string_list names = STRING_LIST_INIT_DUP;
1142 struct existing_packs existing = EXISTING_PACKS_INIT;
1143 struct pack_geometry geometry = { 0 };
1144 struct tempfile *refs_snapshot = NULL;
1145 int i, ext, ret;
1146 int show_progress;
1148 /* variables to be filled by option parsing */
1149 int delete_redundant = 0;
1150 const char *unpack_unreachable = NULL;
1151 int keep_unreachable = 0;
1152 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
1153 struct pack_objects_args po_args = {NULL};
1154 struct pack_objects_args cruft_po_args = {NULL};
1155 int write_midx = 0;
1156 const char *cruft_expiration = NULL;
1157 const char *expire_to = NULL;
1158 const char *filter_to = NULL;
1160 struct option builtin_repack_options[] = {
1161 OPT_BIT('a', NULL, &pack_everything,
1162 N_("pack everything in a single pack"), ALL_INTO_ONE),
1163 OPT_BIT('A', NULL, &pack_everything,
1164 N_("same as -a, and turn unreachable objects loose"),
1165 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
1166 OPT_BIT(0, "cruft", &pack_everything,
1167 N_("same as -a, pack unreachable cruft objects separately"),
1168 PACK_CRUFT),
1169 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
1170 N_("with --cruft, expire objects older than this")),
1171 OPT_MAGNITUDE(0, "max-cruft-size", &cruft_po_args.max_pack_size,
1172 N_("with --cruft, limit the size of new cruft packs")),
1173 OPT_BOOL('d', NULL, &delete_redundant,
1174 N_("remove redundant packs, and run git-prune-packed")),
1175 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
1176 N_("pass --no-reuse-delta to git-pack-objects")),
1177 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
1178 N_("pass --no-reuse-object to git-pack-objects")),
1179 OPT_NEGBIT('n', NULL, &run_update_server_info,
1180 N_("do not run git-update-server-info"), 1),
1181 OPT__QUIET(&po_args.quiet, N_("be quiet")),
1182 OPT_BOOL('l', "local", &po_args.local,
1183 N_("pass --local to git-pack-objects")),
1184 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
1185 N_("write bitmap index")),
1186 OPT_BOOL('i', "delta-islands", &use_delta_islands,
1187 N_("pass --delta-islands to git-pack-objects")),
1188 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
1189 N_("with -A, do not loosen objects older than this")),
1190 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
1191 N_("with -a, repack unreachable objects")),
1192 OPT_STRING(0, "window", &po_args.window, N_("n"),
1193 N_("size of the window used for delta compression")),
1194 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
1195 N_("same as the above, but limit memory size instead of entries count")),
1196 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
1197 N_("limits the maximum delta depth")),
1198 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
1199 N_("limits the maximum number of threads")),
1200 OPT_MAGNITUDE(0, "max-pack-size", &po_args.max_pack_size,
1201 N_("maximum size of each packfile")),
1202 OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options),
1203 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
1204 N_("repack objects in packs marked with .keep")),
1205 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
1206 N_("do not repack this pack")),
1207 OPT_INTEGER('g', "geometric", &geometry.split_factor,
1208 N_("find a geometric progression with factor <N>")),
1209 OPT_BOOL('m', "write-midx", &write_midx,
1210 N_("write a multi-pack index of the resulting packs")),
1211 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
1212 N_("pack prefix to store a pack containing pruned objects")),
1213 OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
1214 N_("pack prefix to store a pack containing filtered out objects")),
1215 OPT_END()
1218 list_objects_filter_init(&po_args.filter_options);
1220 git_config(repack_config, &cruft_po_args);
1222 argc = parse_options(argc, argv, prefix, builtin_repack_options,
1223 git_repack_usage, 0);
1225 if (delete_redundant && repository_format_precious_objects)
1226 die(_("cannot delete packs in a precious-objects repo"));
1228 die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
1229 keep_unreachable, "-k/--keep-unreachable",
1230 pack_everything & PACK_CRUFT, "--cruft");
1232 if (pack_everything & PACK_CRUFT)
1233 pack_everything |= ALL_INTO_ONE;
1235 if (write_bitmaps < 0) {
1236 if (!write_midx &&
1237 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
1238 write_bitmaps = 0;
1240 if (pack_kept_objects < 0)
1241 pack_kept_objects = write_bitmaps > 0 && !write_midx;
1243 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
1244 die(_(incremental_bitmap_conflict_error));
1246 if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1248 * When asked to do a local repack, but we have
1249 * packfiles that are inherited from an alternate, then
1250 * we cannot guarantee that the multi-pack-index would
1251 * have full coverage of all objects. We thus disable
1252 * writing bitmaps in that case.
1254 warning(_("disabling bitmap writing, as some objects are not being packed"));
1255 write_bitmaps = 0;
1258 if (write_midx && write_bitmaps) {
1259 struct strbuf path = STRBUF_INIT;
1261 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1262 "bitmap-ref-tips");
1264 refs_snapshot = xmks_tempfile(path.buf);
1265 midx_snapshot_refs(refs_snapshot);
1267 strbuf_release(&path);
1270 packdir = mkpathdup("%s/pack", get_object_directory());
1271 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1272 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1274 collect_pack_filenames(&existing, &keep_pack_list);
1276 if (geometry.split_factor) {
1277 if (pack_everything)
1278 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1279 init_pack_geometry(&geometry, &existing, &po_args);
1280 split_pack_geometry(&geometry);
1283 prepare_pack_objects(&cmd, &po_args, packtmp);
1285 show_progress = !po_args.quiet && isatty(2);
1287 strvec_push(&cmd.args, "--keep-true-parents");
1288 if (!pack_kept_objects)
1289 strvec_push(&cmd.args, "--honor-pack-keep");
1290 for (i = 0; i < keep_pack_list.nr; i++)
1291 strvec_pushf(&cmd.args, "--keep-pack=%s",
1292 keep_pack_list.items[i].string);
1293 strvec_push(&cmd.args, "--non-empty");
1294 if (!geometry.split_factor) {
1296 * We need to grab all reachable objects, including those that
1297 * are reachable from reflogs and the index.
1299 * When repacking into a geometric progression of packs,
1300 * however, we ask 'git pack-objects --stdin-packs', and it is
1301 * not about packing objects based on reachability but about
1302 * repacking all the objects in specified packs and loose ones
1303 * (indeed, --stdin-packs is incompatible with these options).
1305 strvec_push(&cmd.args, "--all");
1306 strvec_push(&cmd.args, "--reflog");
1307 strvec_push(&cmd.args, "--indexed-objects");
1309 if (repo_has_promisor_remote(the_repository))
1310 strvec_push(&cmd.args, "--exclude-promisor-objects");
1311 if (!write_midx) {
1312 if (write_bitmaps > 0)
1313 strvec_push(&cmd.args, "--write-bitmap-index");
1314 else if (write_bitmaps < 0)
1315 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1317 if (use_delta_islands)
1318 strvec_push(&cmd.args, "--delta-islands");
1320 if (pack_everything & ALL_INTO_ONE) {
1321 repack_promisor_objects(&po_args, &names);
1323 if (has_existing_non_kept_packs(&existing) &&
1324 delete_redundant &&
1325 !(pack_everything & PACK_CRUFT)) {
1326 for_each_string_list_item(item, &names) {
1327 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1328 packtmp_name, item->string);
1330 if (unpack_unreachable) {
1331 strvec_pushf(&cmd.args,
1332 "--unpack-unreachable=%s",
1333 unpack_unreachable);
1334 } else if (pack_everything & LOOSEN_UNREACHABLE) {
1335 strvec_push(&cmd.args,
1336 "--unpack-unreachable");
1337 } else if (keep_unreachable) {
1338 strvec_push(&cmd.args, "--keep-unreachable");
1339 strvec_push(&cmd.args, "--pack-loose-unreachable");
1342 } else if (geometry.split_factor) {
1343 strvec_push(&cmd.args, "--stdin-packs");
1344 strvec_push(&cmd.args, "--unpacked");
1345 } else {
1346 strvec_push(&cmd.args, "--unpacked");
1347 strvec_push(&cmd.args, "--incremental");
1350 if (po_args.filter_options.choice)
1351 strvec_pushf(&cmd.args, "--filter=%s",
1352 expand_list_objects_filter_spec(&po_args.filter_options));
1353 else if (filter_to)
1354 die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
1356 if (geometry.split_factor)
1357 cmd.in = -1;
1358 else
1359 cmd.no_stdin = 1;
1361 ret = start_command(&cmd);
1362 if (ret)
1363 goto cleanup;
1365 if (geometry.split_factor) {
1366 FILE *in = xfdopen(cmd.in, "w");
1368 * The resulting pack should contain all objects in packs that
1369 * are going to be rolled up, but exclude objects in packs which
1370 * are being left alone.
1372 for (i = 0; i < geometry.split; i++)
1373 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1374 for (i = geometry.split; i < geometry.pack_nr; i++)
1375 fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
1376 fclose(in);
1379 ret = finish_pack_objects_cmd(&cmd, &names, 1);
1380 if (ret)
1381 goto cleanup;
1383 if (!names.nr && !po_args.quiet)
1384 printf_ln(_("Nothing new to pack."));
1386 if (pack_everything & PACK_CRUFT) {
1387 const char *pack_prefix = find_pack_prefix(packdir, packtmp);
1389 if (!cruft_po_args.window)
1390 cruft_po_args.window = po_args.window;
1391 if (!cruft_po_args.window_memory)
1392 cruft_po_args.window_memory = po_args.window_memory;
1393 if (!cruft_po_args.depth)
1394 cruft_po_args.depth = po_args.depth;
1395 if (!cruft_po_args.threads)
1396 cruft_po_args.threads = po_args.threads;
1397 if (!cruft_po_args.max_pack_size)
1398 cruft_po_args.max_pack_size = po_args.max_pack_size;
1400 cruft_po_args.local = po_args.local;
1401 cruft_po_args.quiet = po_args.quiet;
1403 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1404 cruft_expiration, &names,
1405 &existing);
1406 if (ret)
1407 goto cleanup;
1409 if (delete_redundant && expire_to) {
1411 * If `--expire-to` is given with `-d`, it's possible
1412 * that we're about to prune some objects. With cruft
1413 * packs, pruning is implicit: any objects from existing
1414 * packs that weren't picked up by new packs are removed
1415 * when their packs are deleted.
1417 * Generate an additional cruft pack, with one twist:
1418 * `names` now includes the name of the cruft pack
1419 * written in the previous step. So the contents of
1420 * _this_ cruft pack exclude everything contained in the
1421 * existing cruft pack (that is, all of the unreachable
1422 * objects which are no older than
1423 * `--cruft-expiration`).
1425 * To make this work, cruft_expiration must become NULL
1426 * so that this cruft pack doesn't actually prune any
1427 * objects. If it were non-NULL, this call would always
1428 * generate an empty pack (since every object not in the
1429 * cruft pack generated above will have an mtime older
1430 * than the expiration).
1432 ret = write_cruft_pack(&cruft_po_args, expire_to,
1433 pack_prefix,
1434 NULL,
1435 &names,
1436 &existing);
1437 if (ret)
1438 goto cleanup;
1442 if (po_args.filter_options.choice) {
1443 if (!filter_to)
1444 filter_to = packtmp;
1446 ret = write_filtered_pack(&po_args,
1447 filter_to,
1448 find_pack_prefix(packdir, packtmp),
1449 &existing,
1450 &names);
1451 if (ret)
1452 goto cleanup;
1455 string_list_sort(&names);
1457 close_object_store(the_repository->objects);
1460 * Ok we have prepared all new packfiles.
1462 for_each_string_list_item(item, &names) {
1463 struct generated_pack_data *data = item->util;
1465 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1466 char *fname;
1468 fname = mkpathdup("%s/pack-%s%s",
1469 packdir, item->string, exts[ext].name);
1471 if (data->tempfiles[ext]) {
1472 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1473 struct stat statbuffer;
1475 if (!stat(fname_old, &statbuffer)) {
1476 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1477 chmod(fname_old, statbuffer.st_mode);
1480 if (rename_tempfile(&data->tempfiles[ext], fname))
1481 die_errno(_("renaming pack to '%s' failed"), fname);
1482 } else if (!exts[ext].optional)
1483 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1484 exts[ext].name, packtmp, item->string);
1485 else if (unlink(fname) < 0 && errno != ENOENT)
1486 die_errno(_("could not unlink: %s"), fname);
1488 free(fname);
1491 /* End of pack replacement. */
1493 if (delete_redundant && pack_everything & ALL_INTO_ONE)
1494 mark_packs_for_deletion(&existing, &names);
1496 if (write_midx) {
1497 struct string_list include = STRING_LIST_INIT_DUP;
1498 midx_included_packs(&include, &existing, &names, &geometry);
1500 ret = write_midx_included_packs(&include, &geometry, &names,
1501 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1502 show_progress, write_bitmaps > 0);
1504 if (!ret && write_bitmaps)
1505 remove_redundant_bitmaps(&include, packdir);
1507 string_list_clear(&include, 0);
1509 if (ret)
1510 goto cleanup;
1513 reprepare_packed_git(the_repository);
1515 if (delete_redundant) {
1516 int opts = 0;
1517 remove_redundant_existing_packs(&existing);
1519 if (geometry.split_factor)
1520 geometry_remove_redundant_packs(&geometry, &names,
1521 &existing);
1522 if (show_progress)
1523 opts |= PRUNE_PACKED_VERBOSE;
1524 prune_packed_objects(opts);
1526 if (!keep_unreachable &&
1527 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1528 unpack_unreachable) &&
1529 is_repository_shallow(the_repository))
1530 prune_shallow(PRUNE_QUICK);
1533 if (run_update_server_info)
1534 update_server_info(0);
1536 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1537 unsigned flags = 0;
1538 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
1539 flags |= MIDX_WRITE_INCREMENTAL;
1540 write_midx_file(get_object_directory(), NULL, NULL, flags);
1543 cleanup:
1544 string_list_clear(&names, 1);
1545 existing_packs_release(&existing);
1546 free_pack_geometry(&geometry);
1547 list_objects_filter_release(&po_args.filter_options);
1549 return ret;