documentation: fix apostrophe usage
[git.git] / builtin / repack.c
blob529e13120d5a9ad1b14f01241220cbade275b855
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 "sigchain.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"
25 #define ALL_INTO_ONE 1
26 #define LOOSEN_UNREACHABLE 2
27 #define PACK_CRUFT 4
29 #define DELETE_PACK 1
31 static int pack_everything;
32 static int delta_base_offset = 1;
33 static int pack_kept_objects = -1;
34 static int write_bitmaps = -1;
35 static int use_delta_islands;
36 static int run_update_server_info = 1;
37 static char *packdir, *packtmp_name, *packtmp;
39 static const char *const git_repack_usage[] = {
40 N_("git repack [<options>]"),
41 NULL
44 static const char incremental_bitmap_conflict_error[] = N_(
45 "Incremental repacks are incompatible with bitmap indexes. Use\n"
46 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
49 struct pack_objects_args {
50 const char *window;
51 const char *window_memory;
52 const char *depth;
53 const char *threads;
54 const char *max_pack_size;
55 int no_reuse_delta;
56 int no_reuse_object;
57 int quiet;
58 int local;
61 static int repack_config(const char *var, const char *value,
62 const struct config_context *ctx, void *cb)
64 struct pack_objects_args *cruft_po_args = cb;
65 if (!strcmp(var, "repack.usedeltabaseoffset")) {
66 delta_base_offset = git_config_bool(var, value);
67 return 0;
69 if (!strcmp(var, "repack.packkeptobjects")) {
70 pack_kept_objects = git_config_bool(var, value);
71 return 0;
73 if (!strcmp(var, "repack.writebitmaps") ||
74 !strcmp(var, "pack.writebitmaps")) {
75 write_bitmaps = git_config_bool(var, value);
76 return 0;
78 if (!strcmp(var, "repack.usedeltaislands")) {
79 use_delta_islands = git_config_bool(var, value);
80 return 0;
82 if (strcmp(var, "repack.updateserverinfo") == 0) {
83 run_update_server_info = git_config_bool(var, value);
84 return 0;
86 if (!strcmp(var, "repack.cruftwindow"))
87 return git_config_string(&cruft_po_args->window, var, value);
88 if (!strcmp(var, "repack.cruftwindowmemory"))
89 return git_config_string(&cruft_po_args->window_memory, var, value);
90 if (!strcmp(var, "repack.cruftdepth"))
91 return git_config_string(&cruft_po_args->depth, var, value);
92 if (!strcmp(var, "repack.cruftthreads"))
93 return git_config_string(&cruft_po_args->threads, var, value);
94 return git_default_config(var, value, ctx, cb);
97 struct existing_packs {
98 struct string_list kept_packs;
99 struct string_list non_kept_packs;
100 struct string_list cruft_packs;
103 #define EXISTING_PACKS_INIT { \
104 .kept_packs = STRING_LIST_INIT_DUP, \
105 .non_kept_packs = STRING_LIST_INIT_DUP, \
106 .cruft_packs = STRING_LIST_INIT_DUP, \
109 static int has_existing_non_kept_packs(const struct existing_packs *existing)
111 return existing->non_kept_packs.nr || existing->cruft_packs.nr;
114 static void pack_mark_for_deletion(struct string_list_item *item)
116 item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
119 static int pack_is_marked_for_deletion(struct string_list_item *item)
121 return (uintptr_t)item->util & DELETE_PACK;
124 static void mark_packs_for_deletion_1(struct string_list *names,
125 struct string_list *list)
127 struct string_list_item *item;
128 const int hexsz = the_hash_algo->hexsz;
130 for_each_string_list_item(item, list) {
131 char *sha1;
132 size_t len = strlen(item->string);
133 if (len < hexsz)
134 continue;
135 sha1 = item->string + len - hexsz;
137 * Mark this pack for deletion, which ensures that this
138 * pack won't be included in a MIDX (if `--write-midx`
139 * was given) and that we will actually delete this pack
140 * (if `-d` was given).
142 if (!string_list_has_string(names, sha1))
143 pack_mark_for_deletion(item);
147 static void mark_packs_for_deletion(struct existing_packs *existing,
148 struct string_list *names)
151 mark_packs_for_deletion_1(names, &existing->non_kept_packs);
152 mark_packs_for_deletion_1(names, &existing->cruft_packs);
155 static void remove_redundant_pack(const char *dir_name, const char *base_name)
157 struct strbuf buf = STRBUF_INIT;
158 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
159 strbuf_addf(&buf, "%s.pack", base_name);
160 if (m && midx_contains_pack(m, buf.buf))
161 clear_midx_file(the_repository);
162 strbuf_insertf(&buf, 0, "%s/", dir_name);
163 unlink_pack_path(buf.buf, 1);
164 strbuf_release(&buf);
167 static void remove_redundant_packs_1(struct string_list *packs)
169 struct string_list_item *item;
170 for_each_string_list_item(item, packs) {
171 if (!pack_is_marked_for_deletion(item))
172 continue;
173 remove_redundant_pack(packdir, item->string);
177 static void remove_redundant_existing_packs(struct existing_packs *existing)
179 remove_redundant_packs_1(&existing->non_kept_packs);
180 remove_redundant_packs_1(&existing->cruft_packs);
183 static void existing_packs_release(struct existing_packs *existing)
185 string_list_clear(&existing->kept_packs, 0);
186 string_list_clear(&existing->non_kept_packs, 0);
187 string_list_clear(&existing->cruft_packs, 0);
191 * Adds all packs hex strings (pack-$HASH) to either packs->non_kept
192 * or packs->kept based on whether each pack has a corresponding
193 * .keep file or not. Packs without a .keep file are not to be kept
194 * if we are going to pack everything into one file.
196 static void collect_pack_filenames(struct existing_packs *existing,
197 const struct string_list *extra_keep)
199 struct packed_git *p;
200 struct strbuf buf = STRBUF_INIT;
202 for (p = get_all_packs(the_repository); p; p = p->next) {
203 int i;
204 const char *base;
206 if (!p->pack_local)
207 continue;
209 base = pack_basename(p);
211 for (i = 0; i < extra_keep->nr; i++)
212 if (!fspathcmp(base, extra_keep->items[i].string))
213 break;
215 strbuf_reset(&buf);
216 strbuf_addstr(&buf, base);
217 strbuf_strip_suffix(&buf, ".pack");
219 if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
220 string_list_append(&existing->kept_packs, buf.buf);
221 else if (p->is_cruft)
222 string_list_append(&existing->cruft_packs, buf.buf);
223 else
224 string_list_append(&existing->non_kept_packs, buf.buf);
227 string_list_sort(&existing->kept_packs);
228 strbuf_release(&buf);
231 static void prepare_pack_objects(struct child_process *cmd,
232 const struct pack_objects_args *args,
233 const char *out)
235 strvec_push(&cmd->args, "pack-objects");
236 if (args->window)
237 strvec_pushf(&cmd->args, "--window=%s", args->window);
238 if (args->window_memory)
239 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
240 if (args->depth)
241 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
242 if (args->threads)
243 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
244 if (args->max_pack_size)
245 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
246 if (args->no_reuse_delta)
247 strvec_pushf(&cmd->args, "--no-reuse-delta");
248 if (args->no_reuse_object)
249 strvec_pushf(&cmd->args, "--no-reuse-object");
250 if (args->local)
251 strvec_push(&cmd->args, "--local");
252 if (args->quiet)
253 strvec_push(&cmd->args, "--quiet");
254 if (delta_base_offset)
255 strvec_push(&cmd->args, "--delta-base-offset");
256 strvec_push(&cmd->args, out);
257 cmd->git_cmd = 1;
258 cmd->out = -1;
262 * Write oid to the given struct child_process's stdin, starting it first if
263 * necessary.
265 static int write_oid(const struct object_id *oid,
266 struct packed_git *pack UNUSED,
267 uint32_t pos UNUSED, void *data)
269 struct child_process *cmd = data;
271 if (cmd->in == -1) {
272 if (start_command(cmd))
273 die(_("could not start pack-objects to repack promisor objects"));
276 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
277 xwrite(cmd->in, "\n", 1);
278 return 0;
281 static struct {
282 const char *name;
283 unsigned optional:1;
284 } exts[] = {
285 {".pack"},
286 {".rev", 1},
287 {".mtimes", 1},
288 {".bitmap", 1},
289 {".promisor", 1},
290 {".idx"},
293 struct generated_pack_data {
294 struct tempfile *tempfiles[ARRAY_SIZE(exts)];
297 static struct generated_pack_data *populate_pack_exts(const char *name)
299 struct stat statbuf;
300 struct strbuf path = STRBUF_INIT;
301 struct generated_pack_data *data = xcalloc(1, sizeof(*data));
302 int i;
304 for (i = 0; i < ARRAY_SIZE(exts); i++) {
305 strbuf_reset(&path);
306 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
308 if (stat(path.buf, &statbuf))
309 continue;
311 data->tempfiles[i] = register_tempfile(path.buf);
314 strbuf_release(&path);
315 return data;
318 static void repack_promisor_objects(const struct pack_objects_args *args,
319 struct string_list *names)
321 struct child_process cmd = CHILD_PROCESS_INIT;
322 FILE *out;
323 struct strbuf line = STRBUF_INIT;
325 prepare_pack_objects(&cmd, args, packtmp);
326 cmd.in = -1;
329 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
330 * hints may result in suboptimal deltas in the resulting pack. See if
331 * the OIDs can be sent with fake paths such that pack-objects can use a
332 * {type -> existing pack order} ordering when computing deltas instead
333 * of a {type -> size} ordering, which may produce better deltas.
335 for_each_packed_object(write_oid, &cmd,
336 FOR_EACH_OBJECT_PROMISOR_ONLY);
338 if (cmd.in == -1) {
339 /* No packed objects; cmd was never started */
340 child_process_clear(&cmd);
341 return;
344 close(cmd.in);
346 out = xfdopen(cmd.out, "r");
347 while (strbuf_getline_lf(&line, out) != EOF) {
348 struct string_list_item *item;
349 char *promisor_name;
351 if (line.len != the_hash_algo->hexsz)
352 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
353 item = string_list_append(names, line.buf);
356 * pack-objects creates the .pack and .idx files, but not the
357 * .promisor file. Create the .promisor file, which is empty.
359 * NEEDSWORK: fetch-pack sometimes generates non-empty
360 * .promisor files containing the ref names and associated
361 * hashes at the point of generation of the corresponding
362 * packfile, but this would not preserve their contents. Maybe
363 * concatenate the contents of all .promisor files instead of
364 * just creating a new empty file.
366 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
367 line.buf);
368 write_promisor_file(promisor_name, NULL, 0);
370 item->util = populate_pack_exts(item->string);
372 free(promisor_name);
374 fclose(out);
375 if (finish_command(&cmd))
376 die(_("could not finish pack-objects to repack promisor objects"));
379 struct pack_geometry {
380 struct packed_git **pack;
381 uint32_t pack_nr, pack_alloc;
382 uint32_t split;
384 int split_factor;
387 static uint32_t geometry_pack_weight(struct packed_git *p)
389 if (open_pack_index(p))
390 die(_("cannot open index for %s"), p->pack_name);
391 return p->num_objects;
394 static int geometry_cmp(const void *va, const void *vb)
396 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
397 bw = geometry_pack_weight(*(struct packed_git **)vb);
399 if (aw < bw)
400 return -1;
401 if (aw > bw)
402 return 1;
403 return 0;
406 static void init_pack_geometry(struct pack_geometry *geometry,
407 struct existing_packs *existing,
408 const struct pack_objects_args *args)
410 struct packed_git *p;
411 struct strbuf buf = STRBUF_INIT;
413 for (p = get_all_packs(the_repository); p; p = p->next) {
414 if (args->local && !p->pack_local)
416 * When asked to only repack local packfiles we skip
417 * over any packfiles that are borrowed from alternate
418 * object directories.
420 continue;
422 if (!pack_kept_objects) {
424 * Any pack that has its pack_keep bit set will
425 * appear in existing->kept_packs below, but
426 * this saves us from doing a more expensive
427 * check.
429 if (p->pack_keep)
430 continue;
433 * The pack may be kept via the --keep-pack
434 * option; check 'existing->kept_packs' to
435 * determine whether to ignore it.
437 strbuf_reset(&buf);
438 strbuf_addstr(&buf, pack_basename(p));
439 strbuf_strip_suffix(&buf, ".pack");
441 if (string_list_has_string(&existing->kept_packs, buf.buf))
442 continue;
444 if (p->is_cruft)
445 continue;
447 ALLOC_GROW(geometry->pack,
448 geometry->pack_nr + 1,
449 geometry->pack_alloc);
451 geometry->pack[geometry->pack_nr] = p;
452 geometry->pack_nr++;
455 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
456 strbuf_release(&buf);
459 static void split_pack_geometry(struct pack_geometry *geometry)
461 uint32_t i;
462 uint32_t split;
463 off_t total_size = 0;
465 if (!geometry->pack_nr) {
466 geometry->split = geometry->pack_nr;
467 return;
471 * First, count the number of packs (in descending order of size) which
472 * already form a geometric progression.
474 for (i = geometry->pack_nr - 1; i > 0; i--) {
475 struct packed_git *ours = geometry->pack[i];
476 struct packed_git *prev = geometry->pack[i - 1];
478 if (unsigned_mult_overflows(geometry->split_factor,
479 geometry_pack_weight(prev)))
480 die(_("pack %s too large to consider in geometric "
481 "progression"),
482 prev->pack_name);
484 if (geometry_pack_weight(ours) <
485 geometry->split_factor * geometry_pack_weight(prev))
486 break;
489 split = i;
491 if (split) {
493 * Move the split one to the right, since the top element in the
494 * last-compared pair can't be in the progression. Only do this
495 * when we split in the middle of the array (otherwise if we got
496 * to the end, then the split is in the right place).
498 split++;
502 * Then, anything to the left of 'split' must be in a new pack. But,
503 * creating that new pack may cause packs in the heavy half to no longer
504 * form a geometric progression.
506 * Compute an expected size of the new pack, and then determine how many
507 * packs in the heavy half need to be joined into it (if any) to restore
508 * the geometric progression.
510 for (i = 0; i < split; i++) {
511 struct packed_git *p = geometry->pack[i];
513 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
514 die(_("pack %s too large to roll up"), p->pack_name);
515 total_size += geometry_pack_weight(p);
517 for (i = split; i < geometry->pack_nr; i++) {
518 struct packed_git *ours = geometry->pack[i];
520 if (unsigned_mult_overflows(geometry->split_factor,
521 total_size))
522 die(_("pack %s too large to roll up"), ours->pack_name);
524 if (geometry_pack_weight(ours) <
525 geometry->split_factor * total_size) {
526 if (unsigned_add_overflows(total_size,
527 geometry_pack_weight(ours)))
528 die(_("pack %s too large to roll up"),
529 ours->pack_name);
531 split++;
532 total_size += geometry_pack_weight(ours);
533 } else
534 break;
537 geometry->split = split;
540 static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
542 uint32_t i;
544 if (!geometry) {
546 * No geometry means either an all-into-one repack (in which
547 * case there is only one pack left and it is the largest) or an
548 * incremental one.
550 * If repacking incrementally, then we could check the size of
551 * all packs to determine which should be preferred, but leave
552 * this for later.
554 return NULL;
556 if (geometry->split == geometry->pack_nr)
557 return NULL;
560 * The preferred pack is the largest pack above the split line. In
561 * other words, it is the largest pack that does not get rolled up in
562 * the geometric repack.
564 for (i = geometry->pack_nr; i > geometry->split; i--)
566 * A pack that is not local would never be included in a
567 * multi-pack index. We thus skip over any non-local packs.
569 if (geometry->pack[i - 1]->pack_local)
570 return geometry->pack[i - 1];
572 return NULL;
575 static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
576 struct string_list *names,
577 struct existing_packs *existing)
579 struct strbuf buf = STRBUF_INIT;
580 uint32_t i;
582 for (i = 0; i < geometry->split; i++) {
583 struct packed_git *p = geometry->pack[i];
584 if (string_list_has_string(names, hash_to_hex(p->hash)))
585 continue;
587 strbuf_reset(&buf);
588 strbuf_addstr(&buf, pack_basename(p));
589 strbuf_strip_suffix(&buf, ".pack");
591 if ((p->pack_keep) ||
592 (string_list_has_string(&existing->kept_packs, buf.buf)))
593 continue;
595 remove_redundant_pack(packdir, buf.buf);
598 strbuf_release(&buf);
601 static void free_pack_geometry(struct pack_geometry *geometry)
603 if (!geometry)
604 return;
606 free(geometry->pack);
609 struct midx_snapshot_ref_data {
610 struct tempfile *f;
611 struct oidset seen;
612 int preferred;
615 static int midx_snapshot_ref_one(const char *refname UNUSED,
616 const struct object_id *oid,
617 int flag UNUSED, void *_data)
619 struct midx_snapshot_ref_data *data = _data;
620 struct object_id peeled;
622 if (!peel_iterated_oid(oid, &peeled))
623 oid = &peeled;
625 if (oidset_insert(&data->seen, oid))
626 return 0; /* already seen */
628 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
629 return 0;
631 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
632 oid_to_hex(oid));
634 return 0;
637 static void midx_snapshot_refs(struct tempfile *f)
639 struct midx_snapshot_ref_data data;
640 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
642 data.f = f;
643 data.preferred = 0;
644 oidset_init(&data.seen, 0);
646 if (!fdopen_tempfile(f, "w"))
647 die(_("could not open tempfile %s for writing"),
648 get_tempfile_path(f));
650 if (preferred) {
651 struct string_list_item *item;
653 data.preferred = 1;
654 for_each_string_list_item(item, preferred)
655 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
656 data.preferred = 0;
659 for_each_ref(midx_snapshot_ref_one, &data);
661 if (close_tempfile_gently(f)) {
662 int save_errno = errno;
663 delete_tempfile(&f);
664 errno = save_errno;
665 die_errno(_("could not close refs snapshot tempfile"));
668 oidset_clear(&data.seen);
671 static void midx_included_packs(struct string_list *include,
672 struct existing_packs *existing,
673 struct string_list *names,
674 struct pack_geometry *geometry)
676 struct string_list_item *item;
678 for_each_string_list_item(item, &existing->kept_packs)
679 string_list_insert(include, xstrfmt("%s.idx", item->string));
680 for_each_string_list_item(item, names)
681 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
682 if (geometry->split_factor) {
683 struct strbuf buf = STRBUF_INIT;
684 uint32_t i;
685 for (i = geometry->split; i < geometry->pack_nr; i++) {
686 struct packed_git *p = geometry->pack[i];
689 * The multi-pack index never refers to packfiles part
690 * of an alternate object database, so we skip these.
691 * While git-multi-pack-index(1) would silently ignore
692 * them anyway, this allows us to skip executing the
693 * command completely when we have only non-local
694 * packfiles.
696 if (!p->pack_local)
697 continue;
699 strbuf_addstr(&buf, pack_basename(p));
700 strbuf_strip_suffix(&buf, ".pack");
701 strbuf_addstr(&buf, ".idx");
703 string_list_insert(include, strbuf_detach(&buf, NULL));
705 } else {
706 for_each_string_list_item(item, &existing->non_kept_packs) {
707 if (pack_is_marked_for_deletion(item))
708 continue;
709 string_list_insert(include, xstrfmt("%s.idx", item->string));
713 for_each_string_list_item(item, &existing->cruft_packs) {
715 * When doing a --geometric repack, there is no need to check
716 * for deleted packs, since we're by definition not doing an
717 * ALL_INTO_ONE repack (hence no packs will be deleted).
718 * Otherwise we must check for and exclude any packs which are
719 * enqueued for deletion.
721 * So we could omit the conditional below in the --geometric
722 * case, but doing so is unnecessary since no packs are marked
723 * as pending deletion (since we only call
724 * `mark_packs_for_deletion()` when doing an all-into-one
725 * repack).
727 if (pack_is_marked_for_deletion(item))
728 continue;
729 string_list_insert(include, xstrfmt("%s.idx", item->string));
733 static int write_midx_included_packs(struct string_list *include,
734 struct pack_geometry *geometry,
735 const char *refs_snapshot,
736 int show_progress, int write_bitmaps)
738 struct child_process cmd = CHILD_PROCESS_INIT;
739 struct string_list_item *item;
740 struct packed_git *preferred = get_preferred_pack(geometry);
741 FILE *in;
742 int ret;
744 if (!include->nr)
745 return 0;
747 cmd.in = -1;
748 cmd.git_cmd = 1;
750 strvec_push(&cmd.args, "multi-pack-index");
751 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
753 if (show_progress)
754 strvec_push(&cmd.args, "--progress");
755 else
756 strvec_push(&cmd.args, "--no-progress");
758 if (write_bitmaps)
759 strvec_push(&cmd.args, "--bitmap");
761 if (preferred)
762 strvec_pushf(&cmd.args, "--preferred-pack=%s",
763 pack_basename(preferred));
765 if (refs_snapshot)
766 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
768 ret = start_command(&cmd);
769 if (ret)
770 return ret;
772 in = xfdopen(cmd.in, "w");
773 for_each_string_list_item(item, include)
774 fprintf(in, "%s\n", item->string);
775 fclose(in);
777 return finish_command(&cmd);
780 static void remove_redundant_bitmaps(struct string_list *include,
781 const char *packdir)
783 struct strbuf path = STRBUF_INIT;
784 struct string_list_item *item;
785 size_t packdir_len;
787 strbuf_addstr(&path, packdir);
788 strbuf_addch(&path, '/');
789 packdir_len = path.len;
792 * Remove any pack bitmaps corresponding to packs which are now
793 * included in the MIDX.
795 for_each_string_list_item(item, include) {
796 strbuf_addstr(&path, item->string);
797 strbuf_strip_suffix(&path, ".idx");
798 strbuf_addstr(&path, ".bitmap");
800 if (unlink(path.buf) && errno != ENOENT)
801 warning_errno(_("could not remove stale bitmap: %s"),
802 path.buf);
804 strbuf_setlen(&path, packdir_len);
806 strbuf_release(&path);
809 static int write_cruft_pack(const struct pack_objects_args *args,
810 const char *destination,
811 const char *pack_prefix,
812 const char *cruft_expiration,
813 struct string_list *names,
814 struct existing_packs *existing)
816 struct child_process cmd = CHILD_PROCESS_INIT;
817 struct strbuf line = STRBUF_INIT;
818 struct string_list_item *item;
819 FILE *in, *out;
820 int ret;
821 const char *scratch;
822 int local = skip_prefix(destination, packdir, &scratch);
824 prepare_pack_objects(&cmd, args, destination);
826 strvec_push(&cmd.args, "--cruft");
827 if (cruft_expiration)
828 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
829 cruft_expiration);
831 strvec_push(&cmd.args, "--honor-pack-keep");
832 strvec_push(&cmd.args, "--non-empty");
834 cmd.in = -1;
836 ret = start_command(&cmd);
837 if (ret)
838 return ret;
841 * names has a confusing double use: it both provides the list
842 * of just-written new packs, and accepts the name of the cruft
843 * pack we are writing.
845 * By the time it is read here, it contains only the pack(s)
846 * that were just written, which is exactly the set of packs we
847 * want to consider kept.
849 * If `--expire-to` is given, the double-use served by `names`
850 * ensures that the pack written to `--expire-to` excludes any
851 * objects contained in the cruft pack.
853 in = xfdopen(cmd.in, "w");
854 for_each_string_list_item(item, names)
855 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
856 for_each_string_list_item(item, &existing->non_kept_packs)
857 fprintf(in, "-%s.pack\n", item->string);
858 for_each_string_list_item(item, &existing->cruft_packs)
859 fprintf(in, "-%s.pack\n", item->string);
860 for_each_string_list_item(item, &existing->kept_packs)
861 fprintf(in, "%s.pack\n", item->string);
862 fclose(in);
864 out = xfdopen(cmd.out, "r");
865 while (strbuf_getline_lf(&line, out) != EOF) {
866 struct string_list_item *item;
868 if (line.len != the_hash_algo->hexsz)
869 die(_("repack: Expecting full hex object ID lines only "
870 "from pack-objects."));
872 * avoid putting packs written outside of the repository in the
873 * list of names
875 if (local) {
876 item = string_list_append(names, line.buf);
877 item->util = populate_pack_exts(line.buf);
880 fclose(out);
882 strbuf_release(&line);
884 return finish_command(&cmd);
887 int cmd_repack(int argc, const char **argv, const char *prefix)
889 struct child_process cmd = CHILD_PROCESS_INIT;
890 struct string_list_item *item;
891 struct string_list names = STRING_LIST_INIT_DUP;
892 struct existing_packs existing = EXISTING_PACKS_INIT;
893 struct pack_geometry geometry = { 0 };
894 struct strbuf line = STRBUF_INIT;
895 struct tempfile *refs_snapshot = NULL;
896 int i, ext, ret;
897 FILE *out;
898 int show_progress;
900 /* variables to be filled by option parsing */
901 int delete_redundant = 0;
902 const char *unpack_unreachable = NULL;
903 int keep_unreachable = 0;
904 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
905 struct pack_objects_args po_args = {NULL};
906 struct pack_objects_args cruft_po_args = {NULL};
907 int write_midx = 0;
908 const char *cruft_expiration = NULL;
909 const char *expire_to = NULL;
911 struct option builtin_repack_options[] = {
912 OPT_BIT('a', NULL, &pack_everything,
913 N_("pack everything in a single pack"), ALL_INTO_ONE),
914 OPT_BIT('A', NULL, &pack_everything,
915 N_("same as -a, and turn unreachable objects loose"),
916 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
917 OPT_BIT(0, "cruft", &pack_everything,
918 N_("same as -a, pack unreachable cruft objects separately"),
919 PACK_CRUFT),
920 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
921 N_("with --cruft, expire objects older than this")),
922 OPT_BOOL('d', NULL, &delete_redundant,
923 N_("remove redundant packs, and run git-prune-packed")),
924 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
925 N_("pass --no-reuse-delta to git-pack-objects")),
926 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
927 N_("pass --no-reuse-object to git-pack-objects")),
928 OPT_NEGBIT('n', NULL, &run_update_server_info,
929 N_("do not run git-update-server-info"), 1),
930 OPT__QUIET(&po_args.quiet, N_("be quiet")),
931 OPT_BOOL('l', "local", &po_args.local,
932 N_("pass --local to git-pack-objects")),
933 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
934 N_("write bitmap index")),
935 OPT_BOOL('i', "delta-islands", &use_delta_islands,
936 N_("pass --delta-islands to git-pack-objects")),
937 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
938 N_("with -A, do not loosen objects older than this")),
939 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
940 N_("with -a, repack unreachable objects")),
941 OPT_STRING(0, "window", &po_args.window, N_("n"),
942 N_("size of the window used for delta compression")),
943 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
944 N_("same as the above, but limit memory size instead of entries count")),
945 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
946 N_("limits the maximum delta depth")),
947 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
948 N_("limits the maximum number of threads")),
949 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
950 N_("maximum size of each packfile")),
951 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
952 N_("repack objects in packs marked with .keep")),
953 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
954 N_("do not repack this pack")),
955 OPT_INTEGER('g', "geometric", &geometry.split_factor,
956 N_("find a geometric progression with factor <N>")),
957 OPT_BOOL('m', "write-midx", &write_midx,
958 N_("write a multi-pack index of the resulting packs")),
959 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
960 N_("pack prefix to store a pack containing pruned objects")),
961 OPT_END()
964 git_config(repack_config, &cruft_po_args);
966 argc = parse_options(argc, argv, prefix, builtin_repack_options,
967 git_repack_usage, 0);
969 if (delete_redundant && repository_format_precious_objects)
970 die(_("cannot delete packs in a precious-objects repo"));
972 if (keep_unreachable &&
973 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
974 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
976 if (pack_everything & PACK_CRUFT) {
977 pack_everything |= ALL_INTO_ONE;
979 if (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))
980 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
981 if (keep_unreachable)
982 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
985 if (write_bitmaps < 0) {
986 if (!write_midx &&
987 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
988 write_bitmaps = 0;
989 } else if (write_bitmaps &&
990 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
991 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
992 write_bitmaps = 0;
994 if (pack_kept_objects < 0)
995 pack_kept_objects = write_bitmaps > 0 && !write_midx;
997 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
998 die(_(incremental_bitmap_conflict_error));
1000 if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1002 * When asked to do a local repack, but we have
1003 * packfiles that are inherited from an alternate, then
1004 * we cannot guarantee that the multi-pack-index would
1005 * have full coverage of all objects. We thus disable
1006 * writing bitmaps in that case.
1008 warning(_("disabling bitmap writing, as some objects are not being packed"));
1009 write_bitmaps = 0;
1012 if (write_midx && write_bitmaps) {
1013 struct strbuf path = STRBUF_INIT;
1015 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1016 "bitmap-ref-tips");
1018 refs_snapshot = xmks_tempfile(path.buf);
1019 midx_snapshot_refs(refs_snapshot);
1021 strbuf_release(&path);
1024 packdir = mkpathdup("%s/pack", get_object_directory());
1025 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1026 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1028 collect_pack_filenames(&existing, &keep_pack_list);
1030 if (geometry.split_factor) {
1031 if (pack_everything)
1032 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1033 init_pack_geometry(&geometry, &existing, &po_args);
1034 split_pack_geometry(&geometry);
1037 prepare_pack_objects(&cmd, &po_args, packtmp);
1039 show_progress = !po_args.quiet && isatty(2);
1041 strvec_push(&cmd.args, "--keep-true-parents");
1042 if (!pack_kept_objects)
1043 strvec_push(&cmd.args, "--honor-pack-keep");
1044 for (i = 0; i < keep_pack_list.nr; i++)
1045 strvec_pushf(&cmd.args, "--keep-pack=%s",
1046 keep_pack_list.items[i].string);
1047 strvec_push(&cmd.args, "--non-empty");
1048 if (!geometry.split_factor) {
1050 * We need to grab all reachable objects, including those that
1051 * are reachable from reflogs and the index.
1053 * When repacking into a geometric progression of packs,
1054 * however, we ask 'git pack-objects --stdin-packs', and it is
1055 * not about packing objects based on reachability but about
1056 * repacking all the objects in specified packs and loose ones
1057 * (indeed, --stdin-packs is incompatible with these options).
1059 strvec_push(&cmd.args, "--all");
1060 strvec_push(&cmd.args, "--reflog");
1061 strvec_push(&cmd.args, "--indexed-objects");
1063 if (repo_has_promisor_remote(the_repository))
1064 strvec_push(&cmd.args, "--exclude-promisor-objects");
1065 if (!write_midx) {
1066 if (write_bitmaps > 0)
1067 strvec_push(&cmd.args, "--write-bitmap-index");
1068 else if (write_bitmaps < 0)
1069 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1071 if (use_delta_islands)
1072 strvec_push(&cmd.args, "--delta-islands");
1074 if (pack_everything & ALL_INTO_ONE) {
1075 repack_promisor_objects(&po_args, &names);
1077 if (has_existing_non_kept_packs(&existing) &&
1078 delete_redundant &&
1079 !(pack_everything & PACK_CRUFT)) {
1080 for_each_string_list_item(item, &names) {
1081 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1082 packtmp_name, item->string);
1084 if (unpack_unreachable) {
1085 strvec_pushf(&cmd.args,
1086 "--unpack-unreachable=%s",
1087 unpack_unreachable);
1088 } else if (pack_everything & LOOSEN_UNREACHABLE) {
1089 strvec_push(&cmd.args,
1090 "--unpack-unreachable");
1091 } else if (keep_unreachable) {
1092 strvec_push(&cmd.args, "--keep-unreachable");
1093 strvec_push(&cmd.args, "--pack-loose-unreachable");
1096 } else if (geometry.split_factor) {
1097 strvec_push(&cmd.args, "--stdin-packs");
1098 strvec_push(&cmd.args, "--unpacked");
1099 } else {
1100 strvec_push(&cmd.args, "--unpacked");
1101 strvec_push(&cmd.args, "--incremental");
1104 if (geometry.split_factor)
1105 cmd.in = -1;
1106 else
1107 cmd.no_stdin = 1;
1109 ret = start_command(&cmd);
1110 if (ret)
1111 goto cleanup;
1113 if (geometry.split_factor) {
1114 FILE *in = xfdopen(cmd.in, "w");
1116 * The resulting pack should contain all objects in packs that
1117 * are going to be rolled up, but exclude objects in packs which
1118 * are being left alone.
1120 for (i = 0; i < geometry.split; i++)
1121 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1122 for (i = geometry.split; i < geometry.pack_nr; i++)
1123 fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
1124 fclose(in);
1127 out = xfdopen(cmd.out, "r");
1128 while (strbuf_getline_lf(&line, out) != EOF) {
1129 struct string_list_item *item;
1131 if (line.len != the_hash_algo->hexsz)
1132 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
1133 item = string_list_append(&names, line.buf);
1134 item->util = populate_pack_exts(item->string);
1136 strbuf_release(&line);
1137 fclose(out);
1138 ret = finish_command(&cmd);
1139 if (ret)
1140 goto cleanup;
1142 if (!names.nr && !po_args.quiet)
1143 printf_ln(_("Nothing new to pack."));
1145 if (pack_everything & PACK_CRUFT) {
1146 const char *pack_prefix;
1147 if (!skip_prefix(packtmp, packdir, &pack_prefix))
1148 die(_("pack prefix %s does not begin with objdir %s"),
1149 packtmp, packdir);
1150 if (*pack_prefix == '/')
1151 pack_prefix++;
1153 if (!cruft_po_args.window)
1154 cruft_po_args.window = po_args.window;
1155 if (!cruft_po_args.window_memory)
1156 cruft_po_args.window_memory = po_args.window_memory;
1157 if (!cruft_po_args.depth)
1158 cruft_po_args.depth = po_args.depth;
1159 if (!cruft_po_args.threads)
1160 cruft_po_args.threads = po_args.threads;
1161 if (!cruft_po_args.max_pack_size)
1162 cruft_po_args.max_pack_size = po_args.max_pack_size;
1164 cruft_po_args.local = po_args.local;
1165 cruft_po_args.quiet = po_args.quiet;
1167 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1168 cruft_expiration, &names,
1169 &existing);
1170 if (ret)
1171 goto cleanup;
1173 if (delete_redundant && expire_to) {
1175 * If `--expire-to` is given with `-d`, it's possible
1176 * that we're about to prune some objects. With cruft
1177 * packs, pruning is implicit: any objects from existing
1178 * packs that weren't picked up by new packs are removed
1179 * when their packs are deleted.
1181 * Generate an additional cruft pack, with one twist:
1182 * `names` now includes the name of the cruft pack
1183 * written in the previous step. So the contents of
1184 * _this_ cruft pack exclude everything contained in the
1185 * existing cruft pack (that is, all of the unreachable
1186 * objects which are no older than
1187 * `--cruft-expiration`).
1189 * To make this work, cruft_expiration must become NULL
1190 * so that this cruft pack doesn't actually prune any
1191 * objects. If it were non-NULL, this call would always
1192 * generate an empty pack (since every object not in the
1193 * cruft pack generated above will have an mtime older
1194 * than the expiration).
1196 ret = write_cruft_pack(&cruft_po_args, expire_to,
1197 pack_prefix,
1198 NULL,
1199 &names,
1200 &existing);
1201 if (ret)
1202 goto cleanup;
1206 string_list_sort(&names);
1208 close_object_store(the_repository->objects);
1211 * Ok we have prepared all new packfiles.
1213 for_each_string_list_item(item, &names) {
1214 struct generated_pack_data *data = item->util;
1216 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1217 char *fname;
1219 fname = mkpathdup("%s/pack-%s%s",
1220 packdir, item->string, exts[ext].name);
1222 if (data->tempfiles[ext]) {
1223 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1224 struct stat statbuffer;
1226 if (!stat(fname_old, &statbuffer)) {
1227 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1228 chmod(fname_old, statbuffer.st_mode);
1231 if (rename_tempfile(&data->tempfiles[ext], fname))
1232 die_errno(_("renaming pack to '%s' failed"), fname);
1233 } else if (!exts[ext].optional)
1234 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1235 exts[ext].name, packtmp, item->string);
1236 else if (unlink(fname) < 0 && errno != ENOENT)
1237 die_errno(_("could not unlink: %s"), fname);
1239 free(fname);
1242 /* End of pack replacement. */
1244 if (delete_redundant && pack_everything & ALL_INTO_ONE)
1245 mark_packs_for_deletion(&existing, &names);
1247 if (write_midx) {
1248 struct string_list include = STRING_LIST_INIT_NODUP;
1249 midx_included_packs(&include, &existing, &names, &geometry);
1251 ret = write_midx_included_packs(&include, &geometry,
1252 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1253 show_progress, write_bitmaps > 0);
1255 if (!ret && write_bitmaps)
1256 remove_redundant_bitmaps(&include, packdir);
1258 string_list_clear(&include, 0);
1260 if (ret)
1261 goto cleanup;
1264 reprepare_packed_git(the_repository);
1266 if (delete_redundant) {
1267 int opts = 0;
1268 remove_redundant_existing_packs(&existing);
1270 if (geometry.split_factor)
1271 geometry_remove_redundant_packs(&geometry, &names,
1272 &existing);
1273 if (show_progress)
1274 opts |= PRUNE_PACKED_VERBOSE;
1275 prune_packed_objects(opts);
1277 if (!keep_unreachable &&
1278 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1279 unpack_unreachable) &&
1280 is_repository_shallow(the_repository))
1281 prune_shallow(PRUNE_QUICK);
1284 if (run_update_server_info)
1285 update_server_info(0);
1287 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1288 unsigned flags = 0;
1289 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
1290 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
1291 write_midx_file(get_object_directory(), NULL, NULL, flags);
1294 cleanup:
1295 string_list_clear(&names, 1);
1296 existing_packs_release(&existing);
1297 free_pack_geometry(&geometry);
1299 return ret;