upload-pack: drop separate v2 "haves" array
[alt-git.git] / builtin / repack.c
blobede36328a3cab9170bc59cb98dda097ea6eb24f7
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 const char *window;
52 const char *window_memory;
53 const char *depth;
54 const 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 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
318 xwrite(cmd->in, "\n", 1);
319 return 0;
322 static struct {
323 const char *name;
324 unsigned optional:1;
325 } exts[] = {
326 {".pack"},
327 {".rev", 1},
328 {".mtimes", 1},
329 {".bitmap", 1},
330 {".promisor", 1},
331 {".idx"},
334 struct generated_pack_data {
335 struct tempfile *tempfiles[ARRAY_SIZE(exts)];
338 static struct generated_pack_data *populate_pack_exts(const char *name)
340 struct stat statbuf;
341 struct strbuf path = STRBUF_INIT;
342 struct generated_pack_data *data = xcalloc(1, sizeof(*data));
343 int i;
345 for (i = 0; i < ARRAY_SIZE(exts); i++) {
346 strbuf_reset(&path);
347 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
349 if (stat(path.buf, &statbuf))
350 continue;
352 data->tempfiles[i] = register_tempfile(path.buf);
355 strbuf_release(&path);
356 return data;
359 static int has_pack_ext(const struct generated_pack_data *data,
360 const char *ext)
362 int i;
363 for (i = 0; i < ARRAY_SIZE(exts); i++) {
364 if (strcmp(exts[i].name, ext))
365 continue;
366 return !!data->tempfiles[i];
368 BUG("unknown pack extension: '%s'", ext);
371 static void repack_promisor_objects(const struct pack_objects_args *args,
372 struct string_list *names)
374 struct child_process cmd = CHILD_PROCESS_INIT;
375 FILE *out;
376 struct strbuf line = STRBUF_INIT;
378 prepare_pack_objects(&cmd, args, packtmp);
379 cmd.in = -1;
382 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
383 * hints may result in suboptimal deltas in the resulting pack. See if
384 * the OIDs can be sent with fake paths such that pack-objects can use a
385 * {type -> existing pack order} ordering when computing deltas instead
386 * of a {type -> size} ordering, which may produce better deltas.
388 for_each_packed_object(write_oid, &cmd,
389 FOR_EACH_OBJECT_PROMISOR_ONLY);
391 if (cmd.in == -1) {
392 /* No packed objects; cmd was never started */
393 child_process_clear(&cmd);
394 return;
397 close(cmd.in);
399 out = xfdopen(cmd.out, "r");
400 while (strbuf_getline_lf(&line, out) != EOF) {
401 struct string_list_item *item;
402 char *promisor_name;
404 if (line.len != the_hash_algo->hexsz)
405 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
406 item = string_list_append(names, line.buf);
409 * pack-objects creates the .pack and .idx files, but not the
410 * .promisor file. Create the .promisor file, which is empty.
412 * NEEDSWORK: fetch-pack sometimes generates non-empty
413 * .promisor files containing the ref names and associated
414 * hashes at the point of generation of the corresponding
415 * packfile, but this would not preserve their contents. Maybe
416 * concatenate the contents of all .promisor files instead of
417 * just creating a new empty file.
419 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
420 line.buf);
421 write_promisor_file(promisor_name, NULL, 0);
423 item->util = populate_pack_exts(item->string);
425 free(promisor_name);
427 fclose(out);
428 if (finish_command(&cmd))
429 die(_("could not finish pack-objects to repack promisor objects"));
432 struct pack_geometry {
433 struct packed_git **pack;
434 uint32_t pack_nr, pack_alloc;
435 uint32_t split;
437 int split_factor;
440 static uint32_t geometry_pack_weight(struct packed_git *p)
442 if (open_pack_index(p))
443 die(_("cannot open index for %s"), p->pack_name);
444 return p->num_objects;
447 static int geometry_cmp(const void *va, const void *vb)
449 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
450 bw = geometry_pack_weight(*(struct packed_git **)vb);
452 if (aw < bw)
453 return -1;
454 if (aw > bw)
455 return 1;
456 return 0;
459 static void init_pack_geometry(struct pack_geometry *geometry,
460 struct existing_packs *existing,
461 const struct pack_objects_args *args)
463 struct packed_git *p;
464 struct strbuf buf = STRBUF_INIT;
466 for (p = get_all_packs(the_repository); p; p = p->next) {
467 if (args->local && !p->pack_local)
469 * When asked to only repack local packfiles we skip
470 * over any packfiles that are borrowed from alternate
471 * object directories.
473 continue;
475 if (!pack_kept_objects) {
477 * Any pack that has its pack_keep bit set will
478 * appear in existing->kept_packs below, but
479 * this saves us from doing a more expensive
480 * check.
482 if (p->pack_keep)
483 continue;
486 * The pack may be kept via the --keep-pack
487 * option; check 'existing->kept_packs' to
488 * determine whether to ignore it.
490 strbuf_reset(&buf);
491 strbuf_addstr(&buf, pack_basename(p));
492 strbuf_strip_suffix(&buf, ".pack");
494 if (string_list_has_string(&existing->kept_packs, buf.buf))
495 continue;
497 if (p->is_cruft)
498 continue;
500 ALLOC_GROW(geometry->pack,
501 geometry->pack_nr + 1,
502 geometry->pack_alloc);
504 geometry->pack[geometry->pack_nr] = p;
505 geometry->pack_nr++;
508 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
509 strbuf_release(&buf);
512 static void split_pack_geometry(struct pack_geometry *geometry)
514 uint32_t i;
515 uint32_t split;
516 off_t total_size = 0;
518 if (!geometry->pack_nr) {
519 geometry->split = geometry->pack_nr;
520 return;
524 * First, count the number of packs (in descending order of size) which
525 * already form a geometric progression.
527 for (i = geometry->pack_nr - 1; i > 0; i--) {
528 struct packed_git *ours = geometry->pack[i];
529 struct packed_git *prev = geometry->pack[i - 1];
531 if (unsigned_mult_overflows(geometry->split_factor,
532 geometry_pack_weight(prev)))
533 die(_("pack %s too large to consider in geometric "
534 "progression"),
535 prev->pack_name);
537 if (geometry_pack_weight(ours) <
538 geometry->split_factor * geometry_pack_weight(prev))
539 break;
542 split = i;
544 if (split) {
546 * Move the split one to the right, since the top element in the
547 * last-compared pair can't be in the progression. Only do this
548 * when we split in the middle of the array (otherwise if we got
549 * to the end, then the split is in the right place).
551 split++;
555 * Then, anything to the left of 'split' must be in a new pack. But,
556 * creating that new pack may cause packs in the heavy half to no longer
557 * form a geometric progression.
559 * Compute an expected size of the new pack, and then determine how many
560 * packs in the heavy half need to be joined into it (if any) to restore
561 * the geometric progression.
563 for (i = 0; i < split; i++) {
564 struct packed_git *p = geometry->pack[i];
566 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
567 die(_("pack %s too large to roll up"), p->pack_name);
568 total_size += geometry_pack_weight(p);
570 for (i = split; i < geometry->pack_nr; i++) {
571 struct packed_git *ours = geometry->pack[i];
573 if (unsigned_mult_overflows(geometry->split_factor,
574 total_size))
575 die(_("pack %s too large to roll up"), ours->pack_name);
577 if (geometry_pack_weight(ours) <
578 geometry->split_factor * total_size) {
579 if (unsigned_add_overflows(total_size,
580 geometry_pack_weight(ours)))
581 die(_("pack %s too large to roll up"),
582 ours->pack_name);
584 split++;
585 total_size += geometry_pack_weight(ours);
586 } else
587 break;
590 geometry->split = split;
593 static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
595 uint32_t i;
597 if (!geometry) {
599 * No geometry means either an all-into-one repack (in which
600 * case there is only one pack left and it is the largest) or an
601 * incremental one.
603 * If repacking incrementally, then we could check the size of
604 * all packs to determine which should be preferred, but leave
605 * this for later.
607 return NULL;
609 if (geometry->split == geometry->pack_nr)
610 return NULL;
613 * The preferred pack is the largest pack above the split line. In
614 * other words, it is the largest pack that does not get rolled up in
615 * the geometric repack.
617 for (i = geometry->pack_nr; i > geometry->split; i--)
619 * A pack that is not local would never be included in a
620 * multi-pack index. We thus skip over any non-local packs.
622 if (geometry->pack[i - 1]->pack_local)
623 return geometry->pack[i - 1];
625 return NULL;
628 static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
629 struct string_list *names,
630 struct existing_packs *existing)
632 struct strbuf buf = STRBUF_INIT;
633 uint32_t i;
635 for (i = 0; i < geometry->split; i++) {
636 struct packed_git *p = geometry->pack[i];
637 if (string_list_has_string(names, hash_to_hex(p->hash)))
638 continue;
640 strbuf_reset(&buf);
641 strbuf_addstr(&buf, pack_basename(p));
642 strbuf_strip_suffix(&buf, ".pack");
644 if ((p->pack_keep) ||
645 (string_list_has_string(&existing->kept_packs, buf.buf)))
646 continue;
648 remove_redundant_pack(packdir, buf.buf);
651 strbuf_release(&buf);
654 static void free_pack_geometry(struct pack_geometry *geometry)
656 if (!geometry)
657 return;
659 free(geometry->pack);
662 struct midx_snapshot_ref_data {
663 struct tempfile *f;
664 struct oidset seen;
665 int preferred;
668 static int midx_snapshot_ref_one(const char *refname UNUSED,
669 const struct object_id *oid,
670 int flag UNUSED, void *_data)
672 struct midx_snapshot_ref_data *data = _data;
673 struct object_id peeled;
675 if (!peel_iterated_oid(oid, &peeled))
676 oid = &peeled;
678 if (oidset_insert(&data->seen, oid))
679 return 0; /* already seen */
681 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
682 return 0;
684 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
685 oid_to_hex(oid));
687 return 0;
690 static void midx_snapshot_refs(struct tempfile *f)
692 struct midx_snapshot_ref_data data;
693 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
695 data.f = f;
696 data.preferred = 0;
697 oidset_init(&data.seen, 0);
699 if (!fdopen_tempfile(f, "w"))
700 die(_("could not open tempfile %s for writing"),
701 get_tempfile_path(f));
703 if (preferred) {
704 struct string_list_item *item;
706 data.preferred = 1;
707 for_each_string_list_item(item, preferred)
708 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
709 data.preferred = 0;
712 for_each_ref(midx_snapshot_ref_one, &data);
714 if (close_tempfile_gently(f)) {
715 int save_errno = errno;
716 delete_tempfile(&f);
717 errno = save_errno;
718 die_errno(_("could not close refs snapshot tempfile"));
721 oidset_clear(&data.seen);
724 static void midx_included_packs(struct string_list *include,
725 struct existing_packs *existing,
726 struct string_list *names,
727 struct pack_geometry *geometry)
729 struct string_list_item *item;
731 for_each_string_list_item(item, &existing->kept_packs)
732 string_list_insert(include, xstrfmt("%s.idx", item->string));
733 for_each_string_list_item(item, names)
734 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
735 if (geometry->split_factor) {
736 struct strbuf buf = STRBUF_INIT;
737 uint32_t i;
738 for (i = geometry->split; i < geometry->pack_nr; i++) {
739 struct packed_git *p = geometry->pack[i];
742 * The multi-pack index never refers to packfiles part
743 * of an alternate object database, so we skip these.
744 * While git-multi-pack-index(1) would silently ignore
745 * them anyway, this allows us to skip executing the
746 * command completely when we have only non-local
747 * packfiles.
749 if (!p->pack_local)
750 continue;
752 strbuf_addstr(&buf, pack_basename(p));
753 strbuf_strip_suffix(&buf, ".pack");
754 strbuf_addstr(&buf, ".idx");
756 string_list_insert(include, strbuf_detach(&buf, NULL));
758 } else {
759 for_each_string_list_item(item, &existing->non_kept_packs) {
760 if (pack_is_marked_for_deletion(item))
761 continue;
762 string_list_insert(include, xstrfmt("%s.idx", item->string));
766 for_each_string_list_item(item, &existing->cruft_packs) {
768 * When doing a --geometric repack, there is no need to check
769 * for deleted packs, since we're by definition not doing an
770 * ALL_INTO_ONE repack (hence no packs will be deleted).
771 * Otherwise we must check for and exclude any packs which are
772 * enqueued for deletion.
774 * So we could omit the conditional below in the --geometric
775 * case, but doing so is unnecessary since no packs are marked
776 * as pending deletion (since we only call
777 * `mark_packs_for_deletion()` when doing an all-into-one
778 * repack).
780 if (pack_is_marked_for_deletion(item))
781 continue;
782 string_list_insert(include, xstrfmt("%s.idx", item->string));
786 static int write_midx_included_packs(struct string_list *include,
787 struct pack_geometry *geometry,
788 struct string_list *names,
789 const char *refs_snapshot,
790 int show_progress, int write_bitmaps)
792 struct child_process cmd = CHILD_PROCESS_INIT;
793 struct string_list_item *item;
794 struct packed_git *preferred = get_preferred_pack(geometry);
795 FILE *in;
796 int ret;
798 if (!include->nr)
799 return 0;
801 cmd.in = -1;
802 cmd.git_cmd = 1;
804 strvec_push(&cmd.args, "multi-pack-index");
805 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
807 if (show_progress)
808 strvec_push(&cmd.args, "--progress");
809 else
810 strvec_push(&cmd.args, "--no-progress");
812 if (write_bitmaps)
813 strvec_push(&cmd.args, "--bitmap");
815 if (preferred)
816 strvec_pushf(&cmd.args, "--preferred-pack=%s",
817 pack_basename(preferred));
818 else if (names->nr) {
819 /* The largest pack was repacked, meaning that either
820 * one or two packs exist depending on whether the
821 * repository has a cruft pack or not.
823 * Select the non-cruft one as preferred to encourage
824 * pack-reuse among packs containing reachable objects
825 * over unreachable ones.
827 * (Note we could write multiple packs here if
828 * `--max-pack-size` was given, but any one of them
829 * will suffice, so pick the first one.)
831 for_each_string_list_item(item, names) {
832 struct generated_pack_data *data = item->util;
833 if (has_pack_ext(data, ".mtimes"))
834 continue;
836 strvec_pushf(&cmd.args, "--preferred-pack=pack-%s.pack",
837 item->string);
838 break;
840 } else {
842 * No packs were kept, and no packs were written. The
843 * only thing remaining are .keep packs (unless
844 * --pack-kept-objects was given).
846 * Set the `--preferred-pack` arbitrarily here.
851 if (refs_snapshot)
852 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
854 ret = start_command(&cmd);
855 if (ret)
856 return ret;
858 in = xfdopen(cmd.in, "w");
859 for_each_string_list_item(item, include)
860 fprintf(in, "%s\n", item->string);
861 fclose(in);
863 return finish_command(&cmd);
866 static void remove_redundant_bitmaps(struct string_list *include,
867 const char *packdir)
869 struct strbuf path = STRBUF_INIT;
870 struct string_list_item *item;
871 size_t packdir_len;
873 strbuf_addstr(&path, packdir);
874 strbuf_addch(&path, '/');
875 packdir_len = path.len;
878 * Remove any pack bitmaps corresponding to packs which are now
879 * included in the MIDX.
881 for_each_string_list_item(item, include) {
882 strbuf_addstr(&path, item->string);
883 strbuf_strip_suffix(&path, ".idx");
884 strbuf_addstr(&path, ".bitmap");
886 if (unlink(path.buf) && errno != ENOENT)
887 warning_errno(_("could not remove stale bitmap: %s"),
888 path.buf);
890 strbuf_setlen(&path, packdir_len);
892 strbuf_release(&path);
895 static int finish_pack_objects_cmd(struct child_process *cmd,
896 struct string_list *names,
897 int local)
899 FILE *out;
900 struct strbuf line = STRBUF_INIT;
902 out = xfdopen(cmd->out, "r");
903 while (strbuf_getline_lf(&line, out) != EOF) {
904 struct string_list_item *item;
906 if (line.len != the_hash_algo->hexsz)
907 die(_("repack: Expecting full hex object ID lines only "
908 "from pack-objects."));
910 * Avoid putting packs written outside of the repository in the
911 * list of names.
913 if (local) {
914 item = string_list_append(names, line.buf);
915 item->util = populate_pack_exts(line.buf);
918 fclose(out);
920 strbuf_release(&line);
922 return finish_command(cmd);
925 static int write_filtered_pack(const struct pack_objects_args *args,
926 const char *destination,
927 const char *pack_prefix,
928 struct existing_packs *existing,
929 struct string_list *names)
931 struct child_process cmd = CHILD_PROCESS_INIT;
932 struct string_list_item *item;
933 FILE *in;
934 int ret;
935 const char *caret;
936 const char *scratch;
937 int local = skip_prefix(destination, packdir, &scratch);
939 prepare_pack_objects(&cmd, args, destination);
941 strvec_push(&cmd.args, "--stdin-packs");
943 if (!pack_kept_objects)
944 strvec_push(&cmd.args, "--honor-pack-keep");
945 for_each_string_list_item(item, &existing->kept_packs)
946 strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
948 cmd.in = -1;
950 ret = start_command(&cmd);
951 if (ret)
952 return ret;
955 * Here 'names' contains only the pack(s) that were just
956 * written, which is exactly the packs we want to keep. Also
957 * 'existing_kept_packs' already contains the packs in
958 * 'keep_pack_list'.
960 in = xfdopen(cmd.in, "w");
961 for_each_string_list_item(item, names)
962 fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
963 for_each_string_list_item(item, &existing->non_kept_packs)
964 fprintf(in, "%s.pack\n", item->string);
965 for_each_string_list_item(item, &existing->cruft_packs)
966 fprintf(in, "%s.pack\n", item->string);
967 caret = pack_kept_objects ? "" : "^";
968 for_each_string_list_item(item, &existing->kept_packs)
969 fprintf(in, "%s%s.pack\n", caret, item->string);
970 fclose(in);
972 return finish_pack_objects_cmd(&cmd, names, local);
975 static int existing_cruft_pack_cmp(const void *va, const void *vb)
977 struct packed_git *a = *(struct packed_git **)va;
978 struct packed_git *b = *(struct packed_git **)vb;
980 if (a->pack_size < b->pack_size)
981 return -1;
982 if (a->pack_size > b->pack_size)
983 return 1;
984 return 0;
987 static void collapse_small_cruft_packs(FILE *in, size_t max_size,
988 struct existing_packs *existing)
990 struct packed_git **existing_cruft, *p;
991 struct strbuf buf = STRBUF_INIT;
992 size_t total_size = 0;
993 size_t existing_cruft_nr = 0;
994 size_t i;
996 ALLOC_ARRAY(existing_cruft, existing->cruft_packs.nr);
998 for (p = get_all_packs(the_repository); p; p = p->next) {
999 if (!(p->is_cruft && p->pack_local))
1000 continue;
1002 strbuf_reset(&buf);
1003 strbuf_addstr(&buf, pack_basename(p));
1004 strbuf_strip_suffix(&buf, ".pack");
1006 if (!string_list_has_string(&existing->cruft_packs, buf.buf))
1007 continue;
1009 if (existing_cruft_nr >= existing->cruft_packs.nr)
1010 BUG("too many cruft packs (found %"PRIuMAX", but knew "
1011 "of %"PRIuMAX")",
1012 (uintmax_t)existing_cruft_nr + 1,
1013 (uintmax_t)existing->cruft_packs.nr);
1014 existing_cruft[existing_cruft_nr++] = p;
1017 QSORT(existing_cruft, existing_cruft_nr, existing_cruft_pack_cmp);
1019 for (i = 0; i < existing_cruft_nr; i++) {
1020 size_t proposed;
1022 p = existing_cruft[i];
1023 proposed = st_add(total_size, p->pack_size);
1025 if (proposed <= max_size) {
1026 total_size = proposed;
1027 fprintf(in, "-%s\n", pack_basename(p));
1028 } else {
1029 retain_cruft_pack(existing, p);
1030 fprintf(in, "%s\n", pack_basename(p));
1034 for (i = 0; i < existing->non_kept_packs.nr; i++)
1035 fprintf(in, "-%s.pack\n",
1036 existing->non_kept_packs.items[i].string);
1038 strbuf_release(&buf);
1039 free(existing_cruft);
1042 static int write_cruft_pack(const struct pack_objects_args *args,
1043 const char *destination,
1044 const char *pack_prefix,
1045 const char *cruft_expiration,
1046 struct string_list *names,
1047 struct existing_packs *existing)
1049 struct child_process cmd = CHILD_PROCESS_INIT;
1050 struct string_list_item *item;
1051 FILE *in;
1052 int ret;
1053 const char *scratch;
1054 int local = skip_prefix(destination, packdir, &scratch);
1056 prepare_pack_objects(&cmd, args, destination);
1058 strvec_push(&cmd.args, "--cruft");
1059 if (cruft_expiration)
1060 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
1061 cruft_expiration);
1063 strvec_push(&cmd.args, "--honor-pack-keep");
1064 strvec_push(&cmd.args, "--non-empty");
1066 cmd.in = -1;
1068 ret = start_command(&cmd);
1069 if (ret)
1070 return ret;
1073 * names has a confusing double use: it both provides the list
1074 * of just-written new packs, and accepts the name of the cruft
1075 * pack we are writing.
1077 * By the time it is read here, it contains only the pack(s)
1078 * that were just written, which is exactly the set of packs we
1079 * want to consider kept.
1081 * If `--expire-to` is given, the double-use served by `names`
1082 * ensures that the pack written to `--expire-to` excludes any
1083 * objects contained in the cruft pack.
1085 in = xfdopen(cmd.in, "w");
1086 for_each_string_list_item(item, names)
1087 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
1088 if (args->max_pack_size && !cruft_expiration) {
1089 collapse_small_cruft_packs(in, args->max_pack_size, existing);
1090 } else {
1091 for_each_string_list_item(item, &existing->non_kept_packs)
1092 fprintf(in, "-%s.pack\n", item->string);
1093 for_each_string_list_item(item, &existing->cruft_packs)
1094 fprintf(in, "-%s.pack\n", item->string);
1096 for_each_string_list_item(item, &existing->kept_packs)
1097 fprintf(in, "%s.pack\n", item->string);
1098 fclose(in);
1100 return finish_pack_objects_cmd(&cmd, names, local);
1103 static const char *find_pack_prefix(const char *packdir, const char *packtmp)
1105 const char *pack_prefix;
1106 if (!skip_prefix(packtmp, packdir, &pack_prefix))
1107 die(_("pack prefix %s does not begin with objdir %s"),
1108 packtmp, packdir);
1109 if (*pack_prefix == '/')
1110 pack_prefix++;
1111 return pack_prefix;
1114 int cmd_repack(int argc, const char **argv, const char *prefix)
1116 struct child_process cmd = CHILD_PROCESS_INIT;
1117 struct string_list_item *item;
1118 struct string_list names = STRING_LIST_INIT_DUP;
1119 struct existing_packs existing = EXISTING_PACKS_INIT;
1120 struct pack_geometry geometry = { 0 };
1121 struct tempfile *refs_snapshot = NULL;
1122 int i, ext, ret;
1123 int show_progress;
1125 /* variables to be filled by option parsing */
1126 int delete_redundant = 0;
1127 const char *unpack_unreachable = NULL;
1128 int keep_unreachable = 0;
1129 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
1130 struct pack_objects_args po_args = {NULL};
1131 struct pack_objects_args cruft_po_args = {NULL};
1132 int write_midx = 0;
1133 const char *cruft_expiration = NULL;
1134 const char *expire_to = NULL;
1135 const char *filter_to = NULL;
1137 struct option builtin_repack_options[] = {
1138 OPT_BIT('a', NULL, &pack_everything,
1139 N_("pack everything in a single pack"), ALL_INTO_ONE),
1140 OPT_BIT('A', NULL, &pack_everything,
1141 N_("same as -a, and turn unreachable objects loose"),
1142 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
1143 OPT_BIT(0, "cruft", &pack_everything,
1144 N_("same as -a, pack unreachable cruft objects separately"),
1145 PACK_CRUFT),
1146 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
1147 N_("with --cruft, expire objects older than this")),
1148 OPT_MAGNITUDE(0, "max-cruft-size", &cruft_po_args.max_pack_size,
1149 N_("with --cruft, limit the size of new cruft packs")),
1150 OPT_BOOL('d', NULL, &delete_redundant,
1151 N_("remove redundant packs, and run git-prune-packed")),
1152 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
1153 N_("pass --no-reuse-delta to git-pack-objects")),
1154 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
1155 N_("pass --no-reuse-object to git-pack-objects")),
1156 OPT_NEGBIT('n', NULL, &run_update_server_info,
1157 N_("do not run git-update-server-info"), 1),
1158 OPT__QUIET(&po_args.quiet, N_("be quiet")),
1159 OPT_BOOL('l', "local", &po_args.local,
1160 N_("pass --local to git-pack-objects")),
1161 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
1162 N_("write bitmap index")),
1163 OPT_BOOL('i', "delta-islands", &use_delta_islands,
1164 N_("pass --delta-islands to git-pack-objects")),
1165 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
1166 N_("with -A, do not loosen objects older than this")),
1167 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
1168 N_("with -a, repack unreachable objects")),
1169 OPT_STRING(0, "window", &po_args.window, N_("n"),
1170 N_("size of the window used for delta compression")),
1171 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
1172 N_("same as the above, but limit memory size instead of entries count")),
1173 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
1174 N_("limits the maximum delta depth")),
1175 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
1176 N_("limits the maximum number of threads")),
1177 OPT_MAGNITUDE(0, "max-pack-size", &po_args.max_pack_size,
1178 N_("maximum size of each packfile")),
1179 OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options),
1180 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
1181 N_("repack objects in packs marked with .keep")),
1182 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
1183 N_("do not repack this pack")),
1184 OPT_INTEGER('g', "geometric", &geometry.split_factor,
1185 N_("find a geometric progression with factor <N>")),
1186 OPT_BOOL('m', "write-midx", &write_midx,
1187 N_("write a multi-pack index of the resulting packs")),
1188 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
1189 N_("pack prefix to store a pack containing pruned objects")),
1190 OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
1191 N_("pack prefix to store a pack containing filtered out objects")),
1192 OPT_END()
1195 list_objects_filter_init(&po_args.filter_options);
1197 git_config(repack_config, &cruft_po_args);
1199 argc = parse_options(argc, argv, prefix, builtin_repack_options,
1200 git_repack_usage, 0);
1202 if (delete_redundant && repository_format_precious_objects)
1203 die(_("cannot delete packs in a precious-objects repo"));
1205 die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
1206 keep_unreachable, "-k/--keep-unreachable",
1207 pack_everything & PACK_CRUFT, "--cruft");
1209 if (pack_everything & PACK_CRUFT)
1210 pack_everything |= ALL_INTO_ONE;
1212 if (write_bitmaps < 0) {
1213 if (!write_midx &&
1214 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
1215 write_bitmaps = 0;
1216 } else if (write_bitmaps &&
1217 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
1218 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
1219 write_bitmaps = 0;
1221 if (pack_kept_objects < 0)
1222 pack_kept_objects = write_bitmaps > 0 && !write_midx;
1224 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
1225 die(_(incremental_bitmap_conflict_error));
1227 if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1229 * When asked to do a local repack, but we have
1230 * packfiles that are inherited from an alternate, then
1231 * we cannot guarantee that the multi-pack-index would
1232 * have full coverage of all objects. We thus disable
1233 * writing bitmaps in that case.
1235 warning(_("disabling bitmap writing, as some objects are not being packed"));
1236 write_bitmaps = 0;
1239 if (write_midx && write_bitmaps) {
1240 struct strbuf path = STRBUF_INIT;
1242 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1243 "bitmap-ref-tips");
1245 refs_snapshot = xmks_tempfile(path.buf);
1246 midx_snapshot_refs(refs_snapshot);
1248 strbuf_release(&path);
1251 packdir = mkpathdup("%s/pack", get_object_directory());
1252 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1253 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1255 collect_pack_filenames(&existing, &keep_pack_list);
1257 if (geometry.split_factor) {
1258 if (pack_everything)
1259 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1260 init_pack_geometry(&geometry, &existing, &po_args);
1261 split_pack_geometry(&geometry);
1264 prepare_pack_objects(&cmd, &po_args, packtmp);
1266 show_progress = !po_args.quiet && isatty(2);
1268 strvec_push(&cmd.args, "--keep-true-parents");
1269 if (!pack_kept_objects)
1270 strvec_push(&cmd.args, "--honor-pack-keep");
1271 for (i = 0; i < keep_pack_list.nr; i++)
1272 strvec_pushf(&cmd.args, "--keep-pack=%s",
1273 keep_pack_list.items[i].string);
1274 strvec_push(&cmd.args, "--non-empty");
1275 if (!geometry.split_factor) {
1277 * We need to grab all reachable objects, including those that
1278 * are reachable from reflogs and the index.
1280 * When repacking into a geometric progression of packs,
1281 * however, we ask 'git pack-objects --stdin-packs', and it is
1282 * not about packing objects based on reachability but about
1283 * repacking all the objects in specified packs and loose ones
1284 * (indeed, --stdin-packs is incompatible with these options).
1286 strvec_push(&cmd.args, "--all");
1287 strvec_push(&cmd.args, "--reflog");
1288 strvec_push(&cmd.args, "--indexed-objects");
1290 if (repo_has_promisor_remote(the_repository))
1291 strvec_push(&cmd.args, "--exclude-promisor-objects");
1292 if (!write_midx) {
1293 if (write_bitmaps > 0)
1294 strvec_push(&cmd.args, "--write-bitmap-index");
1295 else if (write_bitmaps < 0)
1296 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1298 if (use_delta_islands)
1299 strvec_push(&cmd.args, "--delta-islands");
1301 if (pack_everything & ALL_INTO_ONE) {
1302 repack_promisor_objects(&po_args, &names);
1304 if (has_existing_non_kept_packs(&existing) &&
1305 delete_redundant &&
1306 !(pack_everything & PACK_CRUFT)) {
1307 for_each_string_list_item(item, &names) {
1308 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1309 packtmp_name, item->string);
1311 if (unpack_unreachable) {
1312 strvec_pushf(&cmd.args,
1313 "--unpack-unreachable=%s",
1314 unpack_unreachable);
1315 } else if (pack_everything & LOOSEN_UNREACHABLE) {
1316 strvec_push(&cmd.args,
1317 "--unpack-unreachable");
1318 } else if (keep_unreachable) {
1319 strvec_push(&cmd.args, "--keep-unreachable");
1320 strvec_push(&cmd.args, "--pack-loose-unreachable");
1323 } else if (geometry.split_factor) {
1324 strvec_push(&cmd.args, "--stdin-packs");
1325 strvec_push(&cmd.args, "--unpacked");
1326 } else {
1327 strvec_push(&cmd.args, "--unpacked");
1328 strvec_push(&cmd.args, "--incremental");
1331 if (po_args.filter_options.choice)
1332 strvec_pushf(&cmd.args, "--filter=%s",
1333 expand_list_objects_filter_spec(&po_args.filter_options));
1334 else if (filter_to)
1335 die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
1337 if (geometry.split_factor)
1338 cmd.in = -1;
1339 else
1340 cmd.no_stdin = 1;
1342 ret = start_command(&cmd);
1343 if (ret)
1344 goto cleanup;
1346 if (geometry.split_factor) {
1347 FILE *in = xfdopen(cmd.in, "w");
1349 * The resulting pack should contain all objects in packs that
1350 * are going to be rolled up, but exclude objects in packs which
1351 * are being left alone.
1353 for (i = 0; i < geometry.split; i++)
1354 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1355 for (i = geometry.split; i < geometry.pack_nr; i++)
1356 fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
1357 fclose(in);
1360 ret = finish_pack_objects_cmd(&cmd, &names, 1);
1361 if (ret)
1362 goto cleanup;
1364 if (!names.nr && !po_args.quiet)
1365 printf_ln(_("Nothing new to pack."));
1367 if (pack_everything & PACK_CRUFT) {
1368 const char *pack_prefix = find_pack_prefix(packdir, packtmp);
1370 if (!cruft_po_args.window)
1371 cruft_po_args.window = po_args.window;
1372 if (!cruft_po_args.window_memory)
1373 cruft_po_args.window_memory = po_args.window_memory;
1374 if (!cruft_po_args.depth)
1375 cruft_po_args.depth = po_args.depth;
1376 if (!cruft_po_args.threads)
1377 cruft_po_args.threads = po_args.threads;
1378 if (!cruft_po_args.max_pack_size)
1379 cruft_po_args.max_pack_size = po_args.max_pack_size;
1381 cruft_po_args.local = po_args.local;
1382 cruft_po_args.quiet = po_args.quiet;
1384 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1385 cruft_expiration, &names,
1386 &existing);
1387 if (ret)
1388 goto cleanup;
1390 if (delete_redundant && expire_to) {
1392 * If `--expire-to` is given with `-d`, it's possible
1393 * that we're about to prune some objects. With cruft
1394 * packs, pruning is implicit: any objects from existing
1395 * packs that weren't picked up by new packs are removed
1396 * when their packs are deleted.
1398 * Generate an additional cruft pack, with one twist:
1399 * `names` now includes the name of the cruft pack
1400 * written in the previous step. So the contents of
1401 * _this_ cruft pack exclude everything contained in the
1402 * existing cruft pack (that is, all of the unreachable
1403 * objects which are no older than
1404 * `--cruft-expiration`).
1406 * To make this work, cruft_expiration must become NULL
1407 * so that this cruft pack doesn't actually prune any
1408 * objects. If it were non-NULL, this call would always
1409 * generate an empty pack (since every object not in the
1410 * cruft pack generated above will have an mtime older
1411 * than the expiration).
1413 ret = write_cruft_pack(&cruft_po_args, expire_to,
1414 pack_prefix,
1415 NULL,
1416 &names,
1417 &existing);
1418 if (ret)
1419 goto cleanup;
1423 if (po_args.filter_options.choice) {
1424 if (!filter_to)
1425 filter_to = packtmp;
1427 ret = write_filtered_pack(&po_args,
1428 filter_to,
1429 find_pack_prefix(packdir, packtmp),
1430 &existing,
1431 &names);
1432 if (ret)
1433 goto cleanup;
1436 string_list_sort(&names);
1438 close_object_store(the_repository->objects);
1441 * Ok we have prepared all new packfiles.
1443 for_each_string_list_item(item, &names) {
1444 struct generated_pack_data *data = item->util;
1446 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1447 char *fname;
1449 fname = mkpathdup("%s/pack-%s%s",
1450 packdir, item->string, exts[ext].name);
1452 if (data->tempfiles[ext]) {
1453 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1454 struct stat statbuffer;
1456 if (!stat(fname_old, &statbuffer)) {
1457 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1458 chmod(fname_old, statbuffer.st_mode);
1461 if (rename_tempfile(&data->tempfiles[ext], fname))
1462 die_errno(_("renaming pack to '%s' failed"), fname);
1463 } else if (!exts[ext].optional)
1464 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1465 exts[ext].name, packtmp, item->string);
1466 else if (unlink(fname) < 0 && errno != ENOENT)
1467 die_errno(_("could not unlink: %s"), fname);
1469 free(fname);
1472 /* End of pack replacement. */
1474 if (delete_redundant && pack_everything & ALL_INTO_ONE)
1475 mark_packs_for_deletion(&existing, &names);
1477 if (write_midx) {
1478 struct string_list include = STRING_LIST_INIT_NODUP;
1479 midx_included_packs(&include, &existing, &names, &geometry);
1481 ret = write_midx_included_packs(&include, &geometry, &names,
1482 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1483 show_progress, write_bitmaps > 0);
1485 if (!ret && write_bitmaps)
1486 remove_redundant_bitmaps(&include, packdir);
1488 string_list_clear(&include, 0);
1490 if (ret)
1491 goto cleanup;
1494 reprepare_packed_git(the_repository);
1496 if (delete_redundant) {
1497 int opts = 0;
1498 remove_redundant_existing_packs(&existing);
1500 if (geometry.split_factor)
1501 geometry_remove_redundant_packs(&geometry, &names,
1502 &existing);
1503 if (show_progress)
1504 opts |= PRUNE_PACKED_VERBOSE;
1505 prune_packed_objects(opts);
1507 if (!keep_unreachable &&
1508 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1509 unpack_unreachable) &&
1510 is_repository_shallow(the_repository))
1511 prune_shallow(PRUNE_QUICK);
1514 if (run_update_server_info)
1515 update_server_info(0);
1517 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1518 unsigned flags = 0;
1519 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
1520 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
1521 write_midx_file(get_object_directory(), NULL, NULL, flags);
1524 cleanup:
1525 string_list_clear(&names, 1);
1526 existing_packs_release(&existing);
1527 free_pack_geometry(&geometry);
1528 list_objects_filter_release(&po_args.filter_options);
1530 return ret;