t0410: make test description clearer
[git/gitster.git] / builtin / repack.c
blobd6bb37e84ae2e3715c48791b97f6798177629684
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "parse-options.h"
9 #include "path.h"
10 #include "run-command.h"
11 #include "server-info.h"
12 #include "strbuf.h"
13 #include "string-list.h"
14 #include "strvec.h"
15 #include "midx.h"
16 #include "packfile.h"
17 #include "prune-packed.h"
18 #include "object-store-ll.h"
19 #include "promisor-remote.h"
20 #include "shallow.h"
21 #include "pack.h"
22 #include "pack-bitmap.h"
23 #include "refs.h"
24 #include "list-objects-filter-options.h"
26 #define ALL_INTO_ONE 1
27 #define LOOSEN_UNREACHABLE 2
28 #define PACK_CRUFT 4
30 #define DELETE_PACK 1
31 #define RETAIN_PACK 2
33 static int pack_everything;
34 static int delta_base_offset = 1;
35 static int pack_kept_objects = -1;
36 static int write_bitmaps = -1;
37 static int use_delta_islands;
38 static int run_update_server_info = 1;
39 static char *packdir, *packtmp_name, *packtmp;
41 static const char *const git_repack_usage[] = {
42 N_("git repack [<options>]"),
43 NULL
46 static const char incremental_bitmap_conflict_error[] = N_(
47 "Incremental repacks are incompatible with bitmap indexes. Use\n"
48 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
51 struct pack_objects_args {
52 char *window;
53 char *window_memory;
54 char *depth;
55 char *threads;
56 unsigned long max_pack_size;
57 int no_reuse_delta;
58 int no_reuse_object;
59 int quiet;
60 int local;
61 struct list_objects_filter_options filter_options;
64 static int repack_config(const char *var, const char *value,
65 const struct config_context *ctx, void *cb)
67 struct pack_objects_args *cruft_po_args = cb;
68 if (!strcmp(var, "repack.usedeltabaseoffset")) {
69 delta_base_offset = git_config_bool(var, value);
70 return 0;
72 if (!strcmp(var, "repack.packkeptobjects")) {
73 pack_kept_objects = git_config_bool(var, value);
74 return 0;
76 if (!strcmp(var, "repack.writebitmaps") ||
77 !strcmp(var, "pack.writebitmaps")) {
78 write_bitmaps = git_config_bool(var, value);
79 return 0;
81 if (!strcmp(var, "repack.usedeltaislands")) {
82 use_delta_islands = git_config_bool(var, value);
83 return 0;
85 if (strcmp(var, "repack.updateserverinfo") == 0) {
86 run_update_server_info = git_config_bool(var, value);
87 return 0;
89 if (!strcmp(var, "repack.cruftwindow")) {
90 free(cruft_po_args->window);
91 return git_config_string(&cruft_po_args->window, var, value);
93 if (!strcmp(var, "repack.cruftwindowmemory")) {
94 free(cruft_po_args->window_memory);
95 return git_config_string(&cruft_po_args->window_memory, var, value);
97 if (!strcmp(var, "repack.cruftdepth")) {
98 free(cruft_po_args->depth);
99 return git_config_string(&cruft_po_args->depth, var, value);
101 if (!strcmp(var, "repack.cruftthreads")) {
102 free(cruft_po_args->threads);
103 return git_config_string(&cruft_po_args->threads, var, value);
105 return git_default_config(var, value, ctx, cb);
108 static void pack_objects_args_release(struct pack_objects_args *args)
110 free(args->window);
111 free(args->window_memory);
112 free(args->depth);
113 free(args->threads);
114 list_objects_filter_release(&args->filter_options);
117 struct existing_packs {
118 struct string_list kept_packs;
119 struct string_list non_kept_packs;
120 struct string_list cruft_packs;
123 #define EXISTING_PACKS_INIT { \
124 .kept_packs = STRING_LIST_INIT_DUP, \
125 .non_kept_packs = STRING_LIST_INIT_DUP, \
126 .cruft_packs = STRING_LIST_INIT_DUP, \
129 static int has_existing_non_kept_packs(const struct existing_packs *existing)
131 return existing->non_kept_packs.nr || existing->cruft_packs.nr;
134 static void pack_mark_for_deletion(struct string_list_item *item)
136 item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
139 static void pack_unmark_for_deletion(struct string_list_item *item)
141 item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
144 static int pack_is_marked_for_deletion(struct string_list_item *item)
146 return (uintptr_t)item->util & DELETE_PACK;
149 static void pack_mark_retained(struct string_list_item *item)
151 item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
154 static int pack_is_retained(struct string_list_item *item)
156 return (uintptr_t)item->util & RETAIN_PACK;
159 static void mark_packs_for_deletion_1(struct string_list *names,
160 struct string_list *list)
162 struct string_list_item *item;
163 const int hexsz = the_hash_algo->hexsz;
165 for_each_string_list_item(item, list) {
166 char *sha1;
167 size_t len = strlen(item->string);
168 if (len < hexsz)
169 continue;
170 sha1 = item->string + len - hexsz;
172 if (pack_is_retained(item)) {
173 pack_unmark_for_deletion(item);
174 } else if (!string_list_has_string(names, sha1)) {
176 * Mark this pack for deletion, which ensures
177 * that this pack won't be included in a MIDX
178 * (if `--write-midx` was given) and that we
179 * will actually delete this pack (if `-d` was
180 * given).
182 pack_mark_for_deletion(item);
187 static void retain_cruft_pack(struct existing_packs *existing,
188 struct packed_git *cruft)
190 struct strbuf buf = STRBUF_INIT;
191 struct string_list_item *item;
193 strbuf_addstr(&buf, pack_basename(cruft));
194 strbuf_strip_suffix(&buf, ".pack");
196 item = string_list_lookup(&existing->cruft_packs, buf.buf);
197 if (!item)
198 BUG("could not find cruft pack '%s'", pack_basename(cruft));
200 pack_mark_retained(item);
201 strbuf_release(&buf);
204 static void mark_packs_for_deletion(struct existing_packs *existing,
205 struct string_list *names)
208 mark_packs_for_deletion_1(names, &existing->non_kept_packs);
209 mark_packs_for_deletion_1(names, &existing->cruft_packs);
212 static void remove_redundant_pack(const char *dir_name, const char *base_name)
214 struct strbuf buf = STRBUF_INIT;
215 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
216 strbuf_addf(&buf, "%s.pack", base_name);
217 if (m && midx_contains_pack(m, buf.buf))
218 clear_midx_file(the_repository);
219 strbuf_insertf(&buf, 0, "%s/", dir_name);
220 unlink_pack_path(buf.buf, 1);
221 strbuf_release(&buf);
224 static void remove_redundant_packs_1(struct string_list *packs)
226 struct string_list_item *item;
227 for_each_string_list_item(item, packs) {
228 if (!pack_is_marked_for_deletion(item))
229 continue;
230 remove_redundant_pack(packdir, item->string);
234 static void remove_redundant_existing_packs(struct existing_packs *existing)
236 remove_redundant_packs_1(&existing->non_kept_packs);
237 remove_redundant_packs_1(&existing->cruft_packs);
240 static void existing_packs_release(struct existing_packs *existing)
242 string_list_clear(&existing->kept_packs, 0);
243 string_list_clear(&existing->non_kept_packs, 0);
244 string_list_clear(&existing->cruft_packs, 0);
248 * Adds all packs hex strings (pack-$HASH) to either packs->non_kept
249 * or packs->kept based on whether each pack has a corresponding
250 * .keep file or not. Packs without a .keep file are not to be kept
251 * if we are going to pack everything into one file.
253 static void collect_pack_filenames(struct existing_packs *existing,
254 const struct string_list *extra_keep)
256 struct packed_git *p;
257 struct strbuf buf = STRBUF_INIT;
259 for (p = get_all_packs(the_repository); p; p = p->next) {
260 int i;
261 const char *base;
263 if (!p->pack_local)
264 continue;
266 base = pack_basename(p);
268 for (i = 0; i < extra_keep->nr; i++)
269 if (!fspathcmp(base, extra_keep->items[i].string))
270 break;
272 strbuf_reset(&buf);
273 strbuf_addstr(&buf, base);
274 strbuf_strip_suffix(&buf, ".pack");
276 if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
277 string_list_append(&existing->kept_packs, buf.buf);
278 else if (p->is_cruft)
279 string_list_append(&existing->cruft_packs, buf.buf);
280 else
281 string_list_append(&existing->non_kept_packs, buf.buf);
284 string_list_sort(&existing->kept_packs);
285 string_list_sort(&existing->non_kept_packs);
286 string_list_sort(&existing->cruft_packs);
287 strbuf_release(&buf);
290 static void prepare_pack_objects(struct child_process *cmd,
291 const struct pack_objects_args *args,
292 const char *out)
294 strvec_push(&cmd->args, "pack-objects");
295 if (args->window)
296 strvec_pushf(&cmd->args, "--window=%s", args->window);
297 if (args->window_memory)
298 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
299 if (args->depth)
300 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
301 if (args->threads)
302 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
303 if (args->max_pack_size)
304 strvec_pushf(&cmd->args, "--max-pack-size=%lu", args->max_pack_size);
305 if (args->no_reuse_delta)
306 strvec_pushf(&cmd->args, "--no-reuse-delta");
307 if (args->no_reuse_object)
308 strvec_pushf(&cmd->args, "--no-reuse-object");
309 if (args->local)
310 strvec_push(&cmd->args, "--local");
311 if (args->quiet)
312 strvec_push(&cmd->args, "--quiet");
313 if (delta_base_offset)
314 strvec_push(&cmd->args, "--delta-base-offset");
315 strvec_push(&cmd->args, out);
316 cmd->git_cmd = 1;
317 cmd->out = -1;
321 * Write oid to the given struct child_process's stdin, starting it first if
322 * necessary.
324 static int write_oid(const struct object_id *oid,
325 struct packed_git *pack UNUSED,
326 uint32_t pos UNUSED, void *data)
328 struct child_process *cmd = data;
330 if (cmd->in == -1) {
331 if (start_command(cmd))
332 die(_("could not start pack-objects to repack promisor objects"));
335 if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
336 write_in_full(cmd->in, "\n", 1) < 0)
337 die(_("failed to feed promisor objects to pack-objects"));
338 return 0;
341 static struct {
342 const char *name;
343 unsigned optional:1;
344 } exts[] = {
345 {".pack"},
346 {".rev", 1},
347 {".mtimes", 1},
348 {".bitmap", 1},
349 {".promisor", 1},
350 {".idx"},
353 struct generated_pack_data {
354 struct tempfile *tempfiles[ARRAY_SIZE(exts)];
357 static struct generated_pack_data *populate_pack_exts(const char *name)
359 struct stat statbuf;
360 struct strbuf path = STRBUF_INIT;
361 struct generated_pack_data *data = xcalloc(1, sizeof(*data));
362 int i;
364 for (i = 0; i < ARRAY_SIZE(exts); i++) {
365 strbuf_reset(&path);
366 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
368 if (stat(path.buf, &statbuf))
369 continue;
371 data->tempfiles[i] = register_tempfile(path.buf);
374 strbuf_release(&path);
375 return data;
378 static int has_pack_ext(const struct generated_pack_data *data,
379 const char *ext)
381 int i;
382 for (i = 0; i < ARRAY_SIZE(exts); i++) {
383 if (strcmp(exts[i].name, ext))
384 continue;
385 return !!data->tempfiles[i];
387 BUG("unknown pack extension: '%s'", ext);
390 static void repack_promisor_objects(const struct pack_objects_args *args,
391 struct string_list *names)
393 struct child_process cmd = CHILD_PROCESS_INIT;
394 FILE *out;
395 struct strbuf line = STRBUF_INIT;
397 prepare_pack_objects(&cmd, args, packtmp);
398 cmd.in = -1;
401 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
402 * hints may result in suboptimal deltas in the resulting pack. See if
403 * the OIDs can be sent with fake paths such that pack-objects can use a
404 * {type -> existing pack order} ordering when computing deltas instead
405 * of a {type -> size} ordering, which may produce better deltas.
407 for_each_packed_object(write_oid, &cmd,
408 FOR_EACH_OBJECT_PROMISOR_ONLY);
410 if (cmd.in == -1) {
411 /* No packed objects; cmd was never started */
412 child_process_clear(&cmd);
413 return;
416 close(cmd.in);
418 out = xfdopen(cmd.out, "r");
419 while (strbuf_getline_lf(&line, out) != EOF) {
420 struct string_list_item *item;
421 char *promisor_name;
423 if (line.len != the_hash_algo->hexsz)
424 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
425 item = string_list_append(names, line.buf);
428 * pack-objects creates the .pack and .idx files, but not the
429 * .promisor file. Create the .promisor file, which is empty.
431 * NEEDSWORK: fetch-pack sometimes generates non-empty
432 * .promisor files containing the ref names and associated
433 * hashes at the point of generation of the corresponding
434 * packfile, but this would not preserve their contents. Maybe
435 * concatenate the contents of all .promisor files instead of
436 * just creating a new empty file.
438 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
439 line.buf);
440 write_promisor_file(promisor_name, NULL, 0);
442 item->util = populate_pack_exts(item->string);
444 free(promisor_name);
447 fclose(out);
448 if (finish_command(&cmd))
449 die(_("could not finish pack-objects to repack promisor objects"));
450 strbuf_release(&line);
453 struct pack_geometry {
454 struct packed_git **pack;
455 uint32_t pack_nr, pack_alloc;
456 uint32_t split;
458 int split_factor;
461 static uint32_t geometry_pack_weight(struct packed_git *p)
463 if (open_pack_index(p))
464 die(_("cannot open index for %s"), p->pack_name);
465 return p->num_objects;
468 static int geometry_cmp(const void *va, const void *vb)
470 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
471 bw = geometry_pack_weight(*(struct packed_git **)vb);
473 if (aw < bw)
474 return -1;
475 if (aw > bw)
476 return 1;
477 return 0;
480 static void init_pack_geometry(struct pack_geometry *geometry,
481 struct existing_packs *existing,
482 const struct pack_objects_args *args)
484 struct packed_git *p;
485 struct strbuf buf = STRBUF_INIT;
487 for (p = get_all_packs(the_repository); p; p = p->next) {
488 if (args->local && !p->pack_local)
490 * When asked to only repack local packfiles we skip
491 * over any packfiles that are borrowed from alternate
492 * object directories.
494 continue;
496 if (!pack_kept_objects) {
498 * Any pack that has its pack_keep bit set will
499 * appear in existing->kept_packs below, but
500 * this saves us from doing a more expensive
501 * check.
503 if (p->pack_keep)
504 continue;
507 * The pack may be kept via the --keep-pack
508 * option; check 'existing->kept_packs' to
509 * determine whether to ignore it.
511 strbuf_reset(&buf);
512 strbuf_addstr(&buf, pack_basename(p));
513 strbuf_strip_suffix(&buf, ".pack");
515 if (string_list_has_string(&existing->kept_packs, buf.buf))
516 continue;
518 if (p->is_cruft)
519 continue;
521 ALLOC_GROW(geometry->pack,
522 geometry->pack_nr + 1,
523 geometry->pack_alloc);
525 geometry->pack[geometry->pack_nr] = p;
526 geometry->pack_nr++;
529 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
530 strbuf_release(&buf);
533 static void split_pack_geometry(struct pack_geometry *geometry)
535 uint32_t i;
536 uint32_t split;
537 off_t total_size = 0;
539 if (!geometry->pack_nr) {
540 geometry->split = geometry->pack_nr;
541 return;
545 * First, count the number of packs (in descending order of size) which
546 * already form a geometric progression.
548 for (i = geometry->pack_nr - 1; i > 0; i--) {
549 struct packed_git *ours = geometry->pack[i];
550 struct packed_git *prev = geometry->pack[i - 1];
552 if (unsigned_mult_overflows(geometry->split_factor,
553 geometry_pack_weight(prev)))
554 die(_("pack %s too large to consider in geometric "
555 "progression"),
556 prev->pack_name);
558 if (geometry_pack_weight(ours) <
559 geometry->split_factor * geometry_pack_weight(prev))
560 break;
563 split = i;
565 if (split) {
567 * Move the split one to the right, since the top element in the
568 * last-compared pair can't be in the progression. Only do this
569 * when we split in the middle of the array (otherwise if we got
570 * to the end, then the split is in the right place).
572 split++;
576 * Then, anything to the left of 'split' must be in a new pack. But,
577 * creating that new pack may cause packs in the heavy half to no longer
578 * form a geometric progression.
580 * Compute an expected size of the new pack, and then determine how many
581 * packs in the heavy half need to be joined into it (if any) to restore
582 * the geometric progression.
584 for (i = 0; i < split; i++) {
585 struct packed_git *p = geometry->pack[i];
587 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
588 die(_("pack %s too large to roll up"), p->pack_name);
589 total_size += geometry_pack_weight(p);
591 for (i = split; i < geometry->pack_nr; i++) {
592 struct packed_git *ours = geometry->pack[i];
594 if (unsigned_mult_overflows(geometry->split_factor,
595 total_size))
596 die(_("pack %s too large to roll up"), ours->pack_name);
598 if (geometry_pack_weight(ours) <
599 geometry->split_factor * total_size) {
600 if (unsigned_add_overflows(total_size,
601 geometry_pack_weight(ours)))
602 die(_("pack %s too large to roll up"),
603 ours->pack_name);
605 split++;
606 total_size += geometry_pack_weight(ours);
607 } else
608 break;
611 geometry->split = split;
614 static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
616 uint32_t i;
618 if (!geometry) {
620 * No geometry means either an all-into-one repack (in which
621 * case there is only one pack left and it is the largest) or an
622 * incremental one.
624 * If repacking incrementally, then we could check the size of
625 * all packs to determine which should be preferred, but leave
626 * this for later.
628 return NULL;
630 if (geometry->split == geometry->pack_nr)
631 return NULL;
634 * The preferred pack is the largest pack above the split line. In
635 * other words, it is the largest pack that does not get rolled up in
636 * the geometric repack.
638 for (i = geometry->pack_nr; i > geometry->split; i--)
640 * A pack that is not local would never be included in a
641 * multi-pack index. We thus skip over any non-local packs.
643 if (geometry->pack[i - 1]->pack_local)
644 return geometry->pack[i - 1];
646 return NULL;
649 static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
650 struct string_list *names,
651 struct existing_packs *existing)
653 struct strbuf buf = STRBUF_INIT;
654 uint32_t i;
656 for (i = 0; i < geometry->split; i++) {
657 struct packed_git *p = geometry->pack[i];
658 if (string_list_has_string(names, hash_to_hex(p->hash)))
659 continue;
661 strbuf_reset(&buf);
662 strbuf_addstr(&buf, pack_basename(p));
663 strbuf_strip_suffix(&buf, ".pack");
665 if ((p->pack_keep) ||
666 (string_list_has_string(&existing->kept_packs, buf.buf)))
667 continue;
669 remove_redundant_pack(packdir, buf.buf);
672 strbuf_release(&buf);
675 static void free_pack_geometry(struct pack_geometry *geometry)
677 if (!geometry)
678 return;
680 free(geometry->pack);
683 struct midx_snapshot_ref_data {
684 struct tempfile *f;
685 struct oidset seen;
686 int preferred;
689 static int midx_snapshot_ref_one(const char *refname UNUSED,
690 const char *referent UNUSED,
691 const struct object_id *oid,
692 int flag UNUSED, void *_data)
694 struct midx_snapshot_ref_data *data = _data;
695 struct object_id peeled;
697 if (!peel_iterated_oid(the_repository, oid, &peeled))
698 oid = &peeled;
700 if (oidset_insert(&data->seen, oid))
701 return 0; /* already seen */
703 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
704 return 0;
706 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
707 oid_to_hex(oid));
709 return 0;
712 static void midx_snapshot_refs(struct tempfile *f)
714 struct midx_snapshot_ref_data data;
715 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
717 data.f = f;
718 data.preferred = 0;
719 oidset_init(&data.seen, 0);
721 if (!fdopen_tempfile(f, "w"))
722 die(_("could not open tempfile %s for writing"),
723 get_tempfile_path(f));
725 if (preferred) {
726 struct string_list_item *item;
728 data.preferred = 1;
729 for_each_string_list_item(item, preferred)
730 refs_for_each_ref_in(get_main_ref_store(the_repository),
731 item->string,
732 midx_snapshot_ref_one, &data);
733 data.preferred = 0;
736 refs_for_each_ref(get_main_ref_store(the_repository),
737 midx_snapshot_ref_one, &data);
739 if (close_tempfile_gently(f)) {
740 int save_errno = errno;
741 delete_tempfile(&f);
742 errno = save_errno;
743 die_errno(_("could not close refs snapshot tempfile"));
746 oidset_clear(&data.seen);
749 static void midx_included_packs(struct string_list *include,
750 struct existing_packs *existing,
751 struct string_list *names,
752 struct pack_geometry *geometry)
754 struct string_list_item *item;
755 struct strbuf buf = STRBUF_INIT;
757 for_each_string_list_item(item, &existing->kept_packs) {
758 strbuf_reset(&buf);
759 strbuf_addf(&buf, "%s.idx", item->string);
760 string_list_insert(include, buf.buf);
763 for_each_string_list_item(item, names) {
764 strbuf_reset(&buf);
765 strbuf_addf(&buf, "pack-%s.idx", item->string);
766 string_list_insert(include, buf.buf);
769 if (geometry->split_factor) {
770 uint32_t i;
772 for (i = geometry->split; i < geometry->pack_nr; i++) {
773 struct packed_git *p = geometry->pack[i];
776 * The multi-pack index never refers to packfiles part
777 * of an alternate object database, so we skip these.
778 * While git-multi-pack-index(1) would silently ignore
779 * them anyway, this allows us to skip executing the
780 * command completely when we have only non-local
781 * packfiles.
783 if (!p->pack_local)
784 continue;
786 strbuf_reset(&buf);
787 strbuf_addstr(&buf, pack_basename(p));
788 strbuf_strip_suffix(&buf, ".pack");
789 strbuf_addstr(&buf, ".idx");
791 string_list_insert(include, buf.buf);
793 } else {
794 for_each_string_list_item(item, &existing->non_kept_packs) {
795 if (pack_is_marked_for_deletion(item))
796 continue;
798 strbuf_reset(&buf);
799 strbuf_addf(&buf, "%s.idx", item->string);
800 string_list_insert(include, buf.buf);
804 for_each_string_list_item(item, &existing->cruft_packs) {
806 * When doing a --geometric repack, there is no need to check
807 * for deleted packs, since we're by definition not doing an
808 * ALL_INTO_ONE repack (hence no packs will be deleted).
809 * Otherwise we must check for and exclude any packs which are
810 * enqueued for deletion.
812 * So we could omit the conditional below in the --geometric
813 * case, but doing so is unnecessary since no packs are marked
814 * as pending deletion (since we only call
815 * `mark_packs_for_deletion()` when doing an all-into-one
816 * repack).
818 if (pack_is_marked_for_deletion(item))
819 continue;
821 strbuf_reset(&buf);
822 strbuf_addf(&buf, "%s.idx", item->string);
823 string_list_insert(include, buf.buf);
826 strbuf_release(&buf);
829 static int write_midx_included_packs(struct string_list *include,
830 struct pack_geometry *geometry,
831 struct string_list *names,
832 const char *refs_snapshot,
833 int show_progress, int write_bitmaps)
835 struct child_process cmd = CHILD_PROCESS_INIT;
836 struct string_list_item *item;
837 struct packed_git *preferred = get_preferred_pack(geometry);
838 FILE *in;
839 int ret;
841 if (!include->nr)
842 return 0;
844 cmd.in = -1;
845 cmd.git_cmd = 1;
847 strvec_push(&cmd.args, "multi-pack-index");
848 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
850 if (show_progress)
851 strvec_push(&cmd.args, "--progress");
852 else
853 strvec_push(&cmd.args, "--no-progress");
855 if (write_bitmaps)
856 strvec_push(&cmd.args, "--bitmap");
858 if (preferred)
859 strvec_pushf(&cmd.args, "--preferred-pack=%s",
860 pack_basename(preferred));
861 else if (names->nr) {
862 /* The largest pack was repacked, meaning that either
863 * one or two packs exist depending on whether the
864 * repository has a cruft pack or not.
866 * Select the non-cruft one as preferred to encourage
867 * pack-reuse among packs containing reachable objects
868 * over unreachable ones.
870 * (Note we could write multiple packs here if
871 * `--max-pack-size` was given, but any one of them
872 * will suffice, so pick the first one.)
874 for_each_string_list_item(item, names) {
875 struct generated_pack_data *data = item->util;
876 if (has_pack_ext(data, ".mtimes"))
877 continue;
879 strvec_pushf(&cmd.args, "--preferred-pack=pack-%s.pack",
880 item->string);
881 break;
883 } else {
885 * No packs were kept, and no packs were written. The
886 * only thing remaining are .keep packs (unless
887 * --pack-kept-objects was given).
889 * Set the `--preferred-pack` arbitrarily here.
894 if (refs_snapshot)
895 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
897 ret = start_command(&cmd);
898 if (ret)
899 return ret;
901 in = xfdopen(cmd.in, "w");
902 for_each_string_list_item(item, include)
903 fprintf(in, "%s\n", item->string);
904 fclose(in);
906 return finish_command(&cmd);
909 static void remove_redundant_bitmaps(struct string_list *include,
910 const char *packdir)
912 struct strbuf path = STRBUF_INIT;
913 struct string_list_item *item;
914 size_t packdir_len;
916 strbuf_addstr(&path, packdir);
917 strbuf_addch(&path, '/');
918 packdir_len = path.len;
921 * Remove any pack bitmaps corresponding to packs which are now
922 * included in the MIDX.
924 for_each_string_list_item(item, include) {
925 strbuf_addstr(&path, item->string);
926 strbuf_strip_suffix(&path, ".idx");
927 strbuf_addstr(&path, ".bitmap");
929 if (unlink(path.buf) && errno != ENOENT)
930 warning_errno(_("could not remove stale bitmap: %s"),
931 path.buf);
933 strbuf_setlen(&path, packdir_len);
935 strbuf_release(&path);
938 static int finish_pack_objects_cmd(struct child_process *cmd,
939 struct string_list *names,
940 int local)
942 FILE *out;
943 struct strbuf line = STRBUF_INIT;
945 out = xfdopen(cmd->out, "r");
946 while (strbuf_getline_lf(&line, out) != EOF) {
947 struct string_list_item *item;
949 if (line.len != the_hash_algo->hexsz)
950 die(_("repack: Expecting full hex object ID lines only "
951 "from pack-objects."));
953 * Avoid putting packs written outside of the repository in the
954 * list of names.
956 if (local) {
957 item = string_list_append(names, line.buf);
958 item->util = populate_pack_exts(line.buf);
961 fclose(out);
963 strbuf_release(&line);
965 return finish_command(cmd);
968 static int write_filtered_pack(const struct pack_objects_args *args,
969 const char *destination,
970 const char *pack_prefix,
971 struct existing_packs *existing,
972 struct string_list *names)
974 struct child_process cmd = CHILD_PROCESS_INIT;
975 struct string_list_item *item;
976 FILE *in;
977 int ret;
978 const char *caret;
979 const char *scratch;
980 int local = skip_prefix(destination, packdir, &scratch);
982 prepare_pack_objects(&cmd, args, destination);
984 strvec_push(&cmd.args, "--stdin-packs");
986 if (!pack_kept_objects)
987 strvec_push(&cmd.args, "--honor-pack-keep");
988 for_each_string_list_item(item, &existing->kept_packs)
989 strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
991 cmd.in = -1;
993 ret = start_command(&cmd);
994 if (ret)
995 return ret;
998 * Here 'names' contains only the pack(s) that were just
999 * written, which is exactly the packs we want to keep. Also
1000 * 'existing_kept_packs' already contains the packs in
1001 * 'keep_pack_list'.
1003 in = xfdopen(cmd.in, "w");
1004 for_each_string_list_item(item, names)
1005 fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
1006 for_each_string_list_item(item, &existing->non_kept_packs)
1007 fprintf(in, "%s.pack\n", item->string);
1008 for_each_string_list_item(item, &existing->cruft_packs)
1009 fprintf(in, "%s.pack\n", item->string);
1010 caret = pack_kept_objects ? "" : "^";
1011 for_each_string_list_item(item, &existing->kept_packs)
1012 fprintf(in, "%s%s.pack\n", caret, item->string);
1013 fclose(in);
1015 return finish_pack_objects_cmd(&cmd, names, local);
1018 static int existing_cruft_pack_cmp(const void *va, const void *vb)
1020 struct packed_git *a = *(struct packed_git **)va;
1021 struct packed_git *b = *(struct packed_git **)vb;
1023 if (a->pack_size < b->pack_size)
1024 return -1;
1025 if (a->pack_size > b->pack_size)
1026 return 1;
1027 return 0;
1030 static void collapse_small_cruft_packs(FILE *in, size_t max_size,
1031 struct existing_packs *existing)
1033 struct packed_git **existing_cruft, *p;
1034 struct strbuf buf = STRBUF_INIT;
1035 size_t total_size = 0;
1036 size_t existing_cruft_nr = 0;
1037 size_t i;
1039 ALLOC_ARRAY(existing_cruft, existing->cruft_packs.nr);
1041 for (p = get_all_packs(the_repository); p; p = p->next) {
1042 if (!(p->is_cruft && p->pack_local))
1043 continue;
1045 strbuf_reset(&buf);
1046 strbuf_addstr(&buf, pack_basename(p));
1047 strbuf_strip_suffix(&buf, ".pack");
1049 if (!string_list_has_string(&existing->cruft_packs, buf.buf))
1050 continue;
1052 if (existing_cruft_nr >= existing->cruft_packs.nr)
1053 BUG("too many cruft packs (found %"PRIuMAX", but knew "
1054 "of %"PRIuMAX")",
1055 (uintmax_t)existing_cruft_nr + 1,
1056 (uintmax_t)existing->cruft_packs.nr);
1057 existing_cruft[existing_cruft_nr++] = p;
1060 QSORT(existing_cruft, existing_cruft_nr, existing_cruft_pack_cmp);
1062 for (i = 0; i < existing_cruft_nr; i++) {
1063 size_t proposed;
1065 p = existing_cruft[i];
1066 proposed = st_add(total_size, p->pack_size);
1068 if (proposed <= max_size) {
1069 total_size = proposed;
1070 fprintf(in, "-%s\n", pack_basename(p));
1071 } else {
1072 retain_cruft_pack(existing, p);
1073 fprintf(in, "%s\n", pack_basename(p));
1077 for (i = 0; i < existing->non_kept_packs.nr; i++)
1078 fprintf(in, "-%s.pack\n",
1079 existing->non_kept_packs.items[i].string);
1081 strbuf_release(&buf);
1082 free(existing_cruft);
1085 static int write_cruft_pack(const struct pack_objects_args *args,
1086 const char *destination,
1087 const char *pack_prefix,
1088 const char *cruft_expiration,
1089 struct string_list *names,
1090 struct existing_packs *existing)
1092 struct child_process cmd = CHILD_PROCESS_INIT;
1093 struct string_list_item *item;
1094 FILE *in;
1095 int ret;
1096 const char *scratch;
1097 int local = skip_prefix(destination, packdir, &scratch);
1099 prepare_pack_objects(&cmd, args, destination);
1101 strvec_push(&cmd.args, "--cruft");
1102 if (cruft_expiration)
1103 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
1104 cruft_expiration);
1106 strvec_push(&cmd.args, "--honor-pack-keep");
1107 strvec_push(&cmd.args, "--non-empty");
1109 cmd.in = -1;
1111 ret = start_command(&cmd);
1112 if (ret)
1113 return ret;
1116 * names has a confusing double use: it both provides the list
1117 * of just-written new packs, and accepts the name of the cruft
1118 * pack we are writing.
1120 * By the time it is read here, it contains only the pack(s)
1121 * that were just written, which is exactly the set of packs we
1122 * want to consider kept.
1124 * If `--expire-to` is given, the double-use served by `names`
1125 * ensures that the pack written to `--expire-to` excludes any
1126 * objects contained in the cruft pack.
1128 in = xfdopen(cmd.in, "w");
1129 for_each_string_list_item(item, names)
1130 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
1131 if (args->max_pack_size && !cruft_expiration) {
1132 collapse_small_cruft_packs(in, args->max_pack_size, existing);
1133 } else {
1134 for_each_string_list_item(item, &existing->non_kept_packs)
1135 fprintf(in, "-%s.pack\n", item->string);
1136 for_each_string_list_item(item, &existing->cruft_packs)
1137 fprintf(in, "-%s.pack\n", item->string);
1139 for_each_string_list_item(item, &existing->kept_packs)
1140 fprintf(in, "%s.pack\n", item->string);
1141 fclose(in);
1143 return finish_pack_objects_cmd(&cmd, names, local);
1146 static const char *find_pack_prefix(const char *packdir, const char *packtmp)
1148 const char *pack_prefix;
1149 if (!skip_prefix(packtmp, packdir, &pack_prefix))
1150 die(_("pack prefix %s does not begin with objdir %s"),
1151 packtmp, packdir);
1152 if (*pack_prefix == '/')
1153 pack_prefix++;
1154 return pack_prefix;
1157 int cmd_repack(int argc,
1158 const char **argv,
1159 const char *prefix,
1160 struct repository *repo UNUSED)
1162 struct child_process cmd = CHILD_PROCESS_INIT;
1163 struct string_list_item *item;
1164 struct string_list names = STRING_LIST_INIT_DUP;
1165 struct existing_packs existing = EXISTING_PACKS_INIT;
1166 struct pack_geometry geometry = { 0 };
1167 struct tempfile *refs_snapshot = NULL;
1168 int i, ext, ret;
1169 int show_progress;
1171 /* variables to be filled by option parsing */
1172 int delete_redundant = 0;
1173 const char *unpack_unreachable = NULL;
1174 int keep_unreachable = 0;
1175 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
1176 struct pack_objects_args po_args = { 0 };
1177 struct pack_objects_args cruft_po_args = { 0 };
1178 int write_midx = 0;
1179 const char *cruft_expiration = NULL;
1180 const char *expire_to = NULL;
1181 const char *filter_to = NULL;
1182 const char *opt_window = NULL;
1183 const char *opt_window_memory = NULL;
1184 const char *opt_depth = NULL;
1185 const char *opt_threads = NULL;
1187 struct option builtin_repack_options[] = {
1188 OPT_BIT('a', NULL, &pack_everything,
1189 N_("pack everything in a single pack"), ALL_INTO_ONE),
1190 OPT_BIT('A', NULL, &pack_everything,
1191 N_("same as -a, and turn unreachable objects loose"),
1192 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
1193 OPT_BIT(0, "cruft", &pack_everything,
1194 N_("same as -a, pack unreachable cruft objects separately"),
1195 PACK_CRUFT),
1196 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
1197 N_("with --cruft, expire objects older than this")),
1198 OPT_MAGNITUDE(0, "max-cruft-size", &cruft_po_args.max_pack_size,
1199 N_("with --cruft, limit the size of new cruft packs")),
1200 OPT_BOOL('d', NULL, &delete_redundant,
1201 N_("remove redundant packs, and run git-prune-packed")),
1202 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
1203 N_("pass --no-reuse-delta to git-pack-objects")),
1204 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
1205 N_("pass --no-reuse-object to git-pack-objects")),
1206 OPT_NEGBIT('n', NULL, &run_update_server_info,
1207 N_("do not run git-update-server-info"), 1),
1208 OPT__QUIET(&po_args.quiet, N_("be quiet")),
1209 OPT_BOOL('l', "local", &po_args.local,
1210 N_("pass --local to git-pack-objects")),
1211 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
1212 N_("write bitmap index")),
1213 OPT_BOOL('i', "delta-islands", &use_delta_islands,
1214 N_("pass --delta-islands to git-pack-objects")),
1215 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
1216 N_("with -A, do not loosen objects older than this")),
1217 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
1218 N_("with -a, repack unreachable objects")),
1219 OPT_STRING(0, "window", &opt_window, N_("n"),
1220 N_("size of the window used for delta compression")),
1221 OPT_STRING(0, "window-memory", &opt_window_memory, N_("bytes"),
1222 N_("same as the above, but limit memory size instead of entries count")),
1223 OPT_STRING(0, "depth", &opt_depth, N_("n"),
1224 N_("limits the maximum delta depth")),
1225 OPT_STRING(0, "threads", &opt_threads, N_("n"),
1226 N_("limits the maximum number of threads")),
1227 OPT_MAGNITUDE(0, "max-pack-size", &po_args.max_pack_size,
1228 N_("maximum size of each packfile")),
1229 OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options),
1230 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
1231 N_("repack objects in packs marked with .keep")),
1232 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
1233 N_("do not repack this pack")),
1234 OPT_INTEGER('g', "geometric", &geometry.split_factor,
1235 N_("find a geometric progression with factor <N>")),
1236 OPT_BOOL('m', "write-midx", &write_midx,
1237 N_("write a multi-pack index of the resulting packs")),
1238 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
1239 N_("pack prefix to store a pack containing pruned objects")),
1240 OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
1241 N_("pack prefix to store a pack containing filtered out objects")),
1242 OPT_END()
1245 list_objects_filter_init(&po_args.filter_options);
1247 git_config(repack_config, &cruft_po_args);
1249 argc = parse_options(argc, argv, prefix, builtin_repack_options,
1250 git_repack_usage, 0);
1252 po_args.window = xstrdup_or_null(opt_window);
1253 po_args.window_memory = xstrdup_or_null(opt_window_memory);
1254 po_args.depth = xstrdup_or_null(opt_depth);
1255 po_args.threads = xstrdup_or_null(opt_threads);
1257 if (delete_redundant && repository_format_precious_objects)
1258 die(_("cannot delete packs in a precious-objects repo"));
1260 die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
1261 keep_unreachable, "-k/--keep-unreachable",
1262 pack_everything & PACK_CRUFT, "--cruft");
1264 if (pack_everything & PACK_CRUFT)
1265 pack_everything |= ALL_INTO_ONE;
1267 if (write_bitmaps < 0) {
1268 if (!write_midx &&
1269 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
1270 write_bitmaps = 0;
1272 if (pack_kept_objects < 0)
1273 pack_kept_objects = write_bitmaps > 0 && !write_midx;
1275 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
1276 die(_(incremental_bitmap_conflict_error));
1278 if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1280 * When asked to do a local repack, but we have
1281 * packfiles that are inherited from an alternate, then
1282 * we cannot guarantee that the multi-pack-index would
1283 * have full coverage of all objects. We thus disable
1284 * writing bitmaps in that case.
1286 warning(_("disabling bitmap writing, as some objects are not being packed"));
1287 write_bitmaps = 0;
1290 if (write_midx && write_bitmaps) {
1291 struct strbuf path = STRBUF_INIT;
1293 strbuf_addf(&path, "%s/%s_XXXXXX", repo_get_object_directory(the_repository),
1294 "bitmap-ref-tips");
1296 refs_snapshot = xmks_tempfile(path.buf);
1297 midx_snapshot_refs(refs_snapshot);
1299 strbuf_release(&path);
1302 packdir = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
1303 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1304 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1306 collect_pack_filenames(&existing, &keep_pack_list);
1308 if (geometry.split_factor) {
1309 if (pack_everything)
1310 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1311 init_pack_geometry(&geometry, &existing, &po_args);
1312 split_pack_geometry(&geometry);
1315 prepare_pack_objects(&cmd, &po_args, packtmp);
1317 show_progress = !po_args.quiet && isatty(2);
1319 strvec_push(&cmd.args, "--keep-true-parents");
1320 if (!pack_kept_objects)
1321 strvec_push(&cmd.args, "--honor-pack-keep");
1322 for (i = 0; i < keep_pack_list.nr; i++)
1323 strvec_pushf(&cmd.args, "--keep-pack=%s",
1324 keep_pack_list.items[i].string);
1325 strvec_push(&cmd.args, "--non-empty");
1326 if (!geometry.split_factor) {
1328 * We need to grab all reachable objects, including those that
1329 * are reachable from reflogs and the index.
1331 * When repacking into a geometric progression of packs,
1332 * however, we ask 'git pack-objects --stdin-packs', and it is
1333 * not about packing objects based on reachability but about
1334 * repacking all the objects in specified packs and loose ones
1335 * (indeed, --stdin-packs is incompatible with these options).
1337 strvec_push(&cmd.args, "--all");
1338 strvec_push(&cmd.args, "--reflog");
1339 strvec_push(&cmd.args, "--indexed-objects");
1341 if (repo_has_promisor_remote(the_repository))
1342 strvec_push(&cmd.args, "--exclude-promisor-objects");
1343 if (!write_midx) {
1344 if (write_bitmaps > 0)
1345 strvec_push(&cmd.args, "--write-bitmap-index");
1346 else if (write_bitmaps < 0)
1347 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1349 if (use_delta_islands)
1350 strvec_push(&cmd.args, "--delta-islands");
1352 if (pack_everything & ALL_INTO_ONE) {
1353 repack_promisor_objects(&po_args, &names);
1355 if (has_existing_non_kept_packs(&existing) &&
1356 delete_redundant &&
1357 !(pack_everything & PACK_CRUFT)) {
1358 for_each_string_list_item(item, &names) {
1359 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1360 packtmp_name, item->string);
1362 if (unpack_unreachable) {
1363 strvec_pushf(&cmd.args,
1364 "--unpack-unreachable=%s",
1365 unpack_unreachable);
1366 } else if (pack_everything & LOOSEN_UNREACHABLE) {
1367 strvec_push(&cmd.args,
1368 "--unpack-unreachable");
1369 } else if (keep_unreachable) {
1370 strvec_push(&cmd.args, "--keep-unreachable");
1371 strvec_push(&cmd.args, "--pack-loose-unreachable");
1374 } else if (geometry.split_factor) {
1375 strvec_push(&cmd.args, "--stdin-packs");
1376 strvec_push(&cmd.args, "--unpacked");
1377 } else {
1378 strvec_push(&cmd.args, "--unpacked");
1379 strvec_push(&cmd.args, "--incremental");
1382 if (po_args.filter_options.choice)
1383 strvec_pushf(&cmd.args, "--filter=%s",
1384 expand_list_objects_filter_spec(&po_args.filter_options));
1385 else if (filter_to)
1386 die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
1388 if (geometry.split_factor)
1389 cmd.in = -1;
1390 else
1391 cmd.no_stdin = 1;
1393 ret = start_command(&cmd);
1394 if (ret)
1395 goto cleanup;
1397 if (geometry.split_factor) {
1398 FILE *in = xfdopen(cmd.in, "w");
1400 * The resulting pack should contain all objects in packs that
1401 * are going to be rolled up, but exclude objects in packs which
1402 * are being left alone.
1404 for (i = 0; i < geometry.split; i++)
1405 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1406 for (i = geometry.split; i < geometry.pack_nr; i++)
1407 fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
1408 fclose(in);
1411 ret = finish_pack_objects_cmd(&cmd, &names, 1);
1412 if (ret)
1413 goto cleanup;
1415 if (!names.nr && !po_args.quiet)
1416 printf_ln(_("Nothing new to pack."));
1418 if (pack_everything & PACK_CRUFT) {
1419 const char *pack_prefix = find_pack_prefix(packdir, packtmp);
1421 if (!cruft_po_args.window)
1422 cruft_po_args.window = xstrdup_or_null(po_args.window);
1423 if (!cruft_po_args.window_memory)
1424 cruft_po_args.window_memory = xstrdup_or_null(po_args.window_memory);
1425 if (!cruft_po_args.depth)
1426 cruft_po_args.depth = xstrdup_or_null(po_args.depth);
1427 if (!cruft_po_args.threads)
1428 cruft_po_args.threads = xstrdup_or_null(po_args.threads);
1429 if (!cruft_po_args.max_pack_size)
1430 cruft_po_args.max_pack_size = po_args.max_pack_size;
1432 cruft_po_args.local = po_args.local;
1433 cruft_po_args.quiet = po_args.quiet;
1435 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1436 cruft_expiration, &names,
1437 &existing);
1438 if (ret)
1439 goto cleanup;
1441 if (delete_redundant && expire_to) {
1443 * If `--expire-to` is given with `-d`, it's possible
1444 * that we're about to prune some objects. With cruft
1445 * packs, pruning is implicit: any objects from existing
1446 * packs that weren't picked up by new packs are removed
1447 * when their packs are deleted.
1449 * Generate an additional cruft pack, with one twist:
1450 * `names` now includes the name of the cruft pack
1451 * written in the previous step. So the contents of
1452 * _this_ cruft pack exclude everything contained in the
1453 * existing cruft pack (that is, all of the unreachable
1454 * objects which are no older than
1455 * `--cruft-expiration`).
1457 * To make this work, cruft_expiration must become NULL
1458 * so that this cruft pack doesn't actually prune any
1459 * objects. If it were non-NULL, this call would always
1460 * generate an empty pack (since every object not in the
1461 * cruft pack generated above will have an mtime older
1462 * than the expiration).
1464 ret = write_cruft_pack(&cruft_po_args, expire_to,
1465 pack_prefix,
1466 NULL,
1467 &names,
1468 &existing);
1469 if (ret)
1470 goto cleanup;
1474 if (po_args.filter_options.choice) {
1475 if (!filter_to)
1476 filter_to = packtmp;
1478 ret = write_filtered_pack(&po_args,
1479 filter_to,
1480 find_pack_prefix(packdir, packtmp),
1481 &existing,
1482 &names);
1483 if (ret)
1484 goto cleanup;
1487 string_list_sort(&names);
1489 close_object_store(the_repository->objects);
1492 * Ok we have prepared all new packfiles.
1494 for_each_string_list_item(item, &names) {
1495 struct generated_pack_data *data = item->util;
1497 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1498 char *fname;
1500 fname = mkpathdup("%s/pack-%s%s",
1501 packdir, item->string, exts[ext].name);
1503 if (data->tempfiles[ext]) {
1504 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1505 struct stat statbuffer;
1507 if (!stat(fname_old, &statbuffer)) {
1508 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1509 chmod(fname_old, statbuffer.st_mode);
1512 if (rename_tempfile(&data->tempfiles[ext], fname))
1513 die_errno(_("renaming pack to '%s' failed"), fname);
1514 } else if (!exts[ext].optional)
1515 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1516 exts[ext].name, packtmp, item->string);
1517 else if (unlink(fname) < 0 && errno != ENOENT)
1518 die_errno(_("could not unlink: %s"), fname);
1520 free(fname);
1523 /* End of pack replacement. */
1525 if (delete_redundant && pack_everything & ALL_INTO_ONE)
1526 mark_packs_for_deletion(&existing, &names);
1528 if (write_midx) {
1529 struct string_list include = STRING_LIST_INIT_DUP;
1530 midx_included_packs(&include, &existing, &names, &geometry);
1532 ret = write_midx_included_packs(&include, &geometry, &names,
1533 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1534 show_progress, write_bitmaps > 0);
1536 if (!ret && write_bitmaps)
1537 remove_redundant_bitmaps(&include, packdir);
1539 string_list_clear(&include, 0);
1541 if (ret)
1542 goto cleanup;
1545 reprepare_packed_git(the_repository);
1547 if (delete_redundant) {
1548 int opts = 0;
1549 remove_redundant_existing_packs(&existing);
1551 if (geometry.split_factor)
1552 geometry_remove_redundant_packs(&geometry, &names,
1553 &existing);
1554 if (show_progress)
1555 opts |= PRUNE_PACKED_VERBOSE;
1556 prune_packed_objects(opts);
1558 if (!keep_unreachable &&
1559 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1560 unpack_unreachable) &&
1561 is_repository_shallow(the_repository))
1562 prune_shallow(PRUNE_QUICK);
1565 if (run_update_server_info)
1566 update_server_info(0);
1568 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1569 unsigned flags = 0;
1570 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
1571 flags |= MIDX_WRITE_INCREMENTAL;
1572 write_midx_file(repo_get_object_directory(the_repository),
1573 NULL, NULL, flags);
1576 cleanup:
1577 string_list_clear(&keep_pack_list, 0);
1578 string_list_clear(&names, 1);
1579 existing_packs_release(&existing);
1580 free_pack_geometry(&geometry);
1581 pack_objects_args_release(&po_args);
1582 pack_objects_args_release(&cruft_po_args);
1584 return ret;