Sync with 2.38.5
[git.git] / builtin / repack.c
blob65eb1b8bd22b4119908abe700cfd3a6d143289f1
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "parse-options.h"
6 #include "run-command.h"
7 #include "sigchain.h"
8 #include "strbuf.h"
9 #include "string-list.h"
10 #include "strvec.h"
11 #include "midx.h"
12 #include "packfile.h"
13 #include "prune-packed.h"
14 #include "object-store.h"
15 #include "promisor-remote.h"
16 #include "shallow.h"
17 #include "pack.h"
18 #include "pack-bitmap.h"
19 #include "refs.h"
21 #define ALL_INTO_ONE 1
22 #define LOOSEN_UNREACHABLE 2
23 #define PACK_CRUFT 4
25 #define DELETE_PACK 1
26 #define CRUFT_PACK 2
28 static int pack_everything;
29 static int delta_base_offset = 1;
30 static int pack_kept_objects = -1;
31 static int write_bitmaps = -1;
32 static int use_delta_islands;
33 static int run_update_server_info = 1;
34 static char *packdir, *packtmp_name, *packtmp;
36 static const char *const git_repack_usage[] = {
37 N_("git repack [<options>]"),
38 NULL
41 static const char incremental_bitmap_conflict_error[] = N_(
42 "Incremental repacks are incompatible with bitmap indexes. Use\n"
43 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
46 struct pack_objects_args {
47 const char *window;
48 const char *window_memory;
49 const char *depth;
50 const char *threads;
51 const char *max_pack_size;
52 int no_reuse_delta;
53 int no_reuse_object;
54 int quiet;
55 int local;
58 static int repack_config(const char *var, const char *value, void *cb)
60 struct pack_objects_args *cruft_po_args = cb;
61 if (!strcmp(var, "repack.usedeltabaseoffset")) {
62 delta_base_offset = git_config_bool(var, value);
63 return 0;
65 if (!strcmp(var, "repack.packkeptobjects")) {
66 pack_kept_objects = git_config_bool(var, value);
67 return 0;
69 if (!strcmp(var, "repack.writebitmaps") ||
70 !strcmp(var, "pack.writebitmaps")) {
71 write_bitmaps = git_config_bool(var, value);
72 return 0;
74 if (!strcmp(var, "repack.usedeltaislands")) {
75 use_delta_islands = git_config_bool(var, value);
76 return 0;
78 if (strcmp(var, "repack.updateserverinfo") == 0) {
79 run_update_server_info = git_config_bool(var, value);
80 return 0;
82 if (!strcmp(var, "repack.cruftwindow"))
83 return git_config_string(&cruft_po_args->window, var, value);
84 if (!strcmp(var, "repack.cruftwindowmemory"))
85 return git_config_string(&cruft_po_args->window_memory, var, value);
86 if (!strcmp(var, "repack.cruftdepth"))
87 return git_config_string(&cruft_po_args->depth, var, value);
88 if (!strcmp(var, "repack.cruftthreads"))
89 return git_config_string(&cruft_po_args->threads, var, value);
90 return git_default_config(var, value, cb);
94 * Adds all packs hex strings to either fname_nonkept_list or
95 * fname_kept_list based on whether each pack has a corresponding
96 * .keep file or not. Packs without a .keep file are not to be kept
97 * if we are going to pack everything into one file.
99 static void collect_pack_filenames(struct string_list *fname_nonkept_list,
100 struct string_list *fname_kept_list,
101 const struct string_list *extra_keep)
103 DIR *dir;
104 struct dirent *e;
105 char *fname;
107 if (!(dir = opendir(packdir)))
108 return;
110 while ((e = readdir(dir)) != NULL) {
111 size_t len;
112 int i;
114 if (!strip_suffix(e->d_name, ".pack", &len))
115 continue;
117 for (i = 0; i < extra_keep->nr; i++)
118 if (!fspathcmp(e->d_name, extra_keep->items[i].string))
119 break;
121 fname = xmemdupz(e->d_name, len);
123 if ((extra_keep->nr > 0 && i < extra_keep->nr) ||
124 (file_exists(mkpath("%s/%s.keep", packdir, fname)))) {
125 string_list_append_nodup(fname_kept_list, fname);
126 } else {
127 struct string_list_item *item;
128 item = string_list_append_nodup(fname_nonkept_list,
129 fname);
130 if (file_exists(mkpath("%s/%s.mtimes", packdir, fname)))
131 item->util = (void*)(uintptr_t)CRUFT_PACK;
134 closedir(dir);
136 string_list_sort(fname_kept_list);
139 static void remove_redundant_pack(const char *dir_name, const char *base_name)
141 struct strbuf buf = STRBUF_INIT;
142 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
143 strbuf_addf(&buf, "%s.pack", base_name);
144 if (m && midx_contains_pack(m, buf.buf))
145 clear_midx_file(the_repository);
146 strbuf_insertf(&buf, 0, "%s/", dir_name);
147 unlink_pack_path(buf.buf, 1);
148 strbuf_release(&buf);
151 static void prepare_pack_objects(struct child_process *cmd,
152 const struct pack_objects_args *args,
153 const char *out)
155 strvec_push(&cmd->args, "pack-objects");
156 if (args->window)
157 strvec_pushf(&cmd->args, "--window=%s", args->window);
158 if (args->window_memory)
159 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
160 if (args->depth)
161 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
162 if (args->threads)
163 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
164 if (args->max_pack_size)
165 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
166 if (args->no_reuse_delta)
167 strvec_pushf(&cmd->args, "--no-reuse-delta");
168 if (args->no_reuse_object)
169 strvec_pushf(&cmd->args, "--no-reuse-object");
170 if (args->local)
171 strvec_push(&cmd->args, "--local");
172 if (args->quiet)
173 strvec_push(&cmd->args, "--quiet");
174 if (delta_base_offset)
175 strvec_push(&cmd->args, "--delta-base-offset");
176 strvec_push(&cmd->args, out);
177 cmd->git_cmd = 1;
178 cmd->out = -1;
182 * Write oid to the given struct child_process's stdin, starting it first if
183 * necessary.
185 static int write_oid(const struct object_id *oid, struct packed_git *pack,
186 uint32_t pos, void *data)
188 struct child_process *cmd = data;
190 if (cmd->in == -1) {
191 if (start_command(cmd))
192 die(_("could not start pack-objects to repack promisor objects"));
195 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
196 xwrite(cmd->in, "\n", 1);
197 return 0;
200 static struct {
201 const char *name;
202 unsigned optional:1;
203 } exts[] = {
204 {".pack"},
205 {".rev", 1},
206 {".mtimes", 1},
207 {".bitmap", 1},
208 {".promisor", 1},
209 {".idx"},
212 struct generated_pack_data {
213 struct tempfile *tempfiles[ARRAY_SIZE(exts)];
216 static struct generated_pack_data *populate_pack_exts(const char *name)
218 struct stat statbuf;
219 struct strbuf path = STRBUF_INIT;
220 struct generated_pack_data *data = xcalloc(1, sizeof(*data));
221 int i;
223 for (i = 0; i < ARRAY_SIZE(exts); i++) {
224 strbuf_reset(&path);
225 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
227 if (stat(path.buf, &statbuf))
228 continue;
230 data->tempfiles[i] = register_tempfile(path.buf);
233 strbuf_release(&path);
234 return data;
237 static void repack_promisor_objects(const struct pack_objects_args *args,
238 struct string_list *names)
240 struct child_process cmd = CHILD_PROCESS_INIT;
241 FILE *out;
242 struct strbuf line = STRBUF_INIT;
244 prepare_pack_objects(&cmd, args, packtmp);
245 cmd.in = -1;
248 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
249 * hints may result in suboptimal deltas in the resulting pack. See if
250 * the OIDs can be sent with fake paths such that pack-objects can use a
251 * {type -> existing pack order} ordering when computing deltas instead
252 * of a {type -> size} ordering, which may produce better deltas.
254 for_each_packed_object(write_oid, &cmd,
255 FOR_EACH_OBJECT_PROMISOR_ONLY);
257 if (cmd.in == -1) {
258 /* No packed objects; cmd was never started */
259 child_process_clear(&cmd);
260 return;
263 close(cmd.in);
265 out = xfdopen(cmd.out, "r");
266 while (strbuf_getline_lf(&line, out) != EOF) {
267 struct string_list_item *item;
268 char *promisor_name;
270 if (line.len != the_hash_algo->hexsz)
271 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
272 item = string_list_append(names, line.buf);
275 * pack-objects creates the .pack and .idx files, but not the
276 * .promisor file. Create the .promisor file, which is empty.
278 * NEEDSWORK: fetch-pack sometimes generates non-empty
279 * .promisor files containing the ref names and associated
280 * hashes at the point of generation of the corresponding
281 * packfile, but this would not preserve their contents. Maybe
282 * concatenate the contents of all .promisor files instead of
283 * just creating a new empty file.
285 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
286 line.buf);
287 write_promisor_file(promisor_name, NULL, 0);
289 item->util = populate_pack_exts(item->string);
291 free(promisor_name);
293 fclose(out);
294 if (finish_command(&cmd))
295 die(_("could not finish pack-objects to repack promisor objects"));
298 struct pack_geometry {
299 struct packed_git **pack;
300 uint32_t pack_nr, pack_alloc;
301 uint32_t split;
304 static uint32_t geometry_pack_weight(struct packed_git *p)
306 if (open_pack_index(p))
307 die(_("cannot open index for %s"), p->pack_name);
308 return p->num_objects;
311 static int geometry_cmp(const void *va, const void *vb)
313 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
314 bw = geometry_pack_weight(*(struct packed_git **)vb);
316 if (aw < bw)
317 return -1;
318 if (aw > bw)
319 return 1;
320 return 0;
323 static void init_pack_geometry(struct pack_geometry **geometry_p,
324 struct string_list *existing_kept_packs)
326 struct packed_git *p;
327 struct pack_geometry *geometry;
328 struct strbuf buf = STRBUF_INIT;
330 *geometry_p = xcalloc(1, sizeof(struct pack_geometry));
331 geometry = *geometry_p;
333 for (p = get_all_packs(the_repository); p; p = p->next) {
334 if (!pack_kept_objects) {
336 * Any pack that has its pack_keep bit set will appear
337 * in existing_kept_packs below, but this saves us from
338 * doing a more expensive check.
340 if (p->pack_keep)
341 continue;
344 * The pack may be kept via the --keep-pack option;
345 * check 'existing_kept_packs' to determine whether to
346 * ignore it.
348 strbuf_reset(&buf);
349 strbuf_addstr(&buf, pack_basename(p));
350 strbuf_strip_suffix(&buf, ".pack");
352 if (string_list_has_string(existing_kept_packs, buf.buf))
353 continue;
355 if (p->is_cruft)
356 continue;
358 ALLOC_GROW(geometry->pack,
359 geometry->pack_nr + 1,
360 geometry->pack_alloc);
362 geometry->pack[geometry->pack_nr] = p;
363 geometry->pack_nr++;
366 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
367 strbuf_release(&buf);
370 static void split_pack_geometry(struct pack_geometry *geometry, int factor)
372 uint32_t i;
373 uint32_t split;
374 off_t total_size = 0;
376 if (!geometry->pack_nr) {
377 geometry->split = geometry->pack_nr;
378 return;
382 * First, count the number of packs (in descending order of size) which
383 * already form a geometric progression.
385 for (i = geometry->pack_nr - 1; i > 0; i--) {
386 struct packed_git *ours = geometry->pack[i];
387 struct packed_git *prev = geometry->pack[i - 1];
389 if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
390 die(_("pack %s too large to consider in geometric "
391 "progression"),
392 prev->pack_name);
394 if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
395 break;
398 split = i;
400 if (split) {
402 * Move the split one to the right, since the top element in the
403 * last-compared pair can't be in the progression. Only do this
404 * when we split in the middle of the array (otherwise if we got
405 * to the end, then the split is in the right place).
407 split++;
411 * Then, anything to the left of 'split' must be in a new pack. But,
412 * creating that new pack may cause packs in the heavy half to no longer
413 * form a geometric progression.
415 * Compute an expected size of the new pack, and then determine how many
416 * packs in the heavy half need to be joined into it (if any) to restore
417 * the geometric progression.
419 for (i = 0; i < split; i++) {
420 struct packed_git *p = geometry->pack[i];
422 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
423 die(_("pack %s too large to roll up"), p->pack_name);
424 total_size += geometry_pack_weight(p);
426 for (i = split; i < geometry->pack_nr; i++) {
427 struct packed_git *ours = geometry->pack[i];
429 if (unsigned_mult_overflows(factor, total_size))
430 die(_("pack %s too large to roll up"), ours->pack_name);
432 if (geometry_pack_weight(ours) < factor * total_size) {
433 if (unsigned_add_overflows(total_size,
434 geometry_pack_weight(ours)))
435 die(_("pack %s too large to roll up"),
436 ours->pack_name);
438 split++;
439 total_size += geometry_pack_weight(ours);
440 } else
441 break;
444 geometry->split = split;
447 static struct packed_git *get_largest_active_pack(struct pack_geometry *geometry)
449 if (!geometry) {
451 * No geometry means either an all-into-one repack (in which
452 * case there is only one pack left and it is the largest) or an
453 * incremental one.
455 * If repacking incrementally, then we could check the size of
456 * all packs to determine which should be preferred, but leave
457 * this for later.
459 return NULL;
461 if (geometry->split == geometry->pack_nr)
462 return NULL;
463 return geometry->pack[geometry->pack_nr - 1];
466 static void clear_pack_geometry(struct pack_geometry *geometry)
468 if (!geometry)
469 return;
471 free(geometry->pack);
472 geometry->pack_nr = 0;
473 geometry->pack_alloc = 0;
474 geometry->split = 0;
477 struct midx_snapshot_ref_data {
478 struct tempfile *f;
479 struct oidset seen;
480 int preferred;
483 static int midx_snapshot_ref_one(const char *refname UNUSED,
484 const struct object_id *oid,
485 int flag UNUSED, void *_data)
487 struct midx_snapshot_ref_data *data = _data;
488 struct object_id peeled;
490 if (!peel_iterated_oid(oid, &peeled))
491 oid = &peeled;
493 if (oidset_insert(&data->seen, oid))
494 return 0; /* already seen */
496 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
497 return 0;
499 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
500 oid_to_hex(oid));
502 return 0;
505 static void midx_snapshot_refs(struct tempfile *f)
507 struct midx_snapshot_ref_data data;
508 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
510 data.f = f;
511 data.preferred = 0;
512 oidset_init(&data.seen, 0);
514 if (!fdopen_tempfile(f, "w"))
515 die(_("could not open tempfile %s for writing"),
516 get_tempfile_path(f));
518 if (preferred) {
519 struct string_list_item *item;
521 data.preferred = 1;
522 for_each_string_list_item(item, preferred)
523 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
524 data.preferred = 0;
527 for_each_ref(midx_snapshot_ref_one, &data);
529 if (close_tempfile_gently(f)) {
530 int save_errno = errno;
531 delete_tempfile(&f);
532 errno = save_errno;
533 die_errno(_("could not close refs snapshot tempfile"));
536 oidset_clear(&data.seen);
539 static void midx_included_packs(struct string_list *include,
540 struct string_list *existing_nonkept_packs,
541 struct string_list *existing_kept_packs,
542 struct string_list *names,
543 struct pack_geometry *geometry)
545 struct string_list_item *item;
547 for_each_string_list_item(item, existing_kept_packs)
548 string_list_insert(include, xstrfmt("%s.idx", item->string));
549 for_each_string_list_item(item, names)
550 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
551 if (geometry) {
552 struct strbuf buf = STRBUF_INIT;
553 uint32_t i;
554 for (i = geometry->split; i < geometry->pack_nr; i++) {
555 struct packed_git *p = geometry->pack[i];
557 strbuf_addstr(&buf, pack_basename(p));
558 strbuf_strip_suffix(&buf, ".pack");
559 strbuf_addstr(&buf, ".idx");
561 string_list_insert(include, strbuf_detach(&buf, NULL));
564 for_each_string_list_item(item, existing_nonkept_packs) {
565 if (!((uintptr_t)item->util & CRUFT_PACK)) {
567 * no need to check DELETE_PACK, since we're not
568 * doing an ALL_INTO_ONE repack
570 continue;
572 string_list_insert(include, xstrfmt("%s.idx", item->string));
574 } else {
575 for_each_string_list_item(item, existing_nonkept_packs) {
576 if ((uintptr_t)item->util & DELETE_PACK)
577 continue;
578 string_list_insert(include, xstrfmt("%s.idx", item->string));
583 static int write_midx_included_packs(struct string_list *include,
584 struct pack_geometry *geometry,
585 const char *refs_snapshot,
586 int show_progress, int write_bitmaps)
588 struct child_process cmd = CHILD_PROCESS_INIT;
589 struct string_list_item *item;
590 struct packed_git *largest = get_largest_active_pack(geometry);
591 FILE *in;
592 int ret;
594 if (!include->nr)
595 return 0;
597 cmd.in = -1;
598 cmd.git_cmd = 1;
600 strvec_push(&cmd.args, "multi-pack-index");
601 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
603 if (show_progress)
604 strvec_push(&cmd.args, "--progress");
605 else
606 strvec_push(&cmd.args, "--no-progress");
608 if (write_bitmaps)
609 strvec_push(&cmd.args, "--bitmap");
611 if (largest)
612 strvec_pushf(&cmd.args, "--preferred-pack=%s",
613 pack_basename(largest));
615 if (refs_snapshot)
616 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
618 ret = start_command(&cmd);
619 if (ret)
620 return ret;
622 in = xfdopen(cmd.in, "w");
623 for_each_string_list_item(item, include)
624 fprintf(in, "%s\n", item->string);
625 fclose(in);
627 return finish_command(&cmd);
630 static void remove_redundant_bitmaps(struct string_list *include,
631 const char *packdir)
633 struct strbuf path = STRBUF_INIT;
634 struct string_list_item *item;
635 size_t packdir_len;
637 strbuf_addstr(&path, packdir);
638 strbuf_addch(&path, '/');
639 packdir_len = path.len;
642 * Remove any pack bitmaps corresponding to packs which are now
643 * included in the MIDX.
645 for_each_string_list_item(item, include) {
646 strbuf_addstr(&path, item->string);
647 strbuf_strip_suffix(&path, ".idx");
648 strbuf_addstr(&path, ".bitmap");
650 if (unlink(path.buf) && errno != ENOENT)
651 warning_errno(_("could not remove stale bitmap: %s"),
652 path.buf);
654 strbuf_setlen(&path, packdir_len);
656 strbuf_release(&path);
659 static int write_cruft_pack(const struct pack_objects_args *args,
660 const char *destination,
661 const char *pack_prefix,
662 const char *cruft_expiration,
663 struct string_list *names,
664 struct string_list *existing_packs,
665 struct string_list *existing_kept_packs)
667 struct child_process cmd = CHILD_PROCESS_INIT;
668 struct strbuf line = STRBUF_INIT;
669 struct string_list_item *item;
670 FILE *in, *out;
671 int ret;
672 const char *scratch;
673 int local = skip_prefix(destination, packdir, &scratch);
675 prepare_pack_objects(&cmd, args, destination);
677 strvec_push(&cmd.args, "--cruft");
678 if (cruft_expiration)
679 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
680 cruft_expiration);
682 strvec_push(&cmd.args, "--honor-pack-keep");
683 strvec_push(&cmd.args, "--non-empty");
684 strvec_push(&cmd.args, "--max-pack-size=0");
686 cmd.in = -1;
688 ret = start_command(&cmd);
689 if (ret)
690 return ret;
693 * names has a confusing double use: it both provides the list
694 * of just-written new packs, and accepts the name of the cruft
695 * pack we are writing.
697 * By the time it is read here, it contains only the pack(s)
698 * that were just written, which is exactly the set of packs we
699 * want to consider kept.
701 * If `--expire-to` is given, the double-use served by `names`
702 * ensures that the pack written to `--expire-to` excludes any
703 * objects contained in the cruft pack.
705 in = xfdopen(cmd.in, "w");
706 for_each_string_list_item(item, names)
707 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
708 for_each_string_list_item(item, existing_packs)
709 fprintf(in, "-%s.pack\n", item->string);
710 for_each_string_list_item(item, existing_kept_packs)
711 fprintf(in, "%s.pack\n", item->string);
712 fclose(in);
714 out = xfdopen(cmd.out, "r");
715 while (strbuf_getline_lf(&line, out) != EOF) {
716 struct string_list_item *item;
718 if (line.len != the_hash_algo->hexsz)
719 die(_("repack: Expecting full hex object ID lines only "
720 "from pack-objects."));
722 * avoid putting packs written outside of the repository in the
723 * list of names
725 if (local) {
726 item = string_list_append(names, line.buf);
727 item->util = populate_pack_exts(line.buf);
730 fclose(out);
732 strbuf_release(&line);
734 return finish_command(&cmd);
737 int cmd_repack(int argc, const char **argv, const char *prefix)
739 struct child_process cmd = CHILD_PROCESS_INIT;
740 struct string_list_item *item;
741 struct string_list names = STRING_LIST_INIT_DUP;
742 struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
743 struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
744 struct pack_geometry *geometry = NULL;
745 struct strbuf line = STRBUF_INIT;
746 struct tempfile *refs_snapshot = NULL;
747 int i, ext, ret;
748 FILE *out;
749 int show_progress;
751 /* variables to be filled by option parsing */
752 int delete_redundant = 0;
753 const char *unpack_unreachable = NULL;
754 int keep_unreachable = 0;
755 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
756 struct pack_objects_args po_args = {NULL};
757 struct pack_objects_args cruft_po_args = {NULL};
758 int geometric_factor = 0;
759 int write_midx = 0;
760 const char *cruft_expiration = NULL;
761 const char *expire_to = NULL;
763 struct option builtin_repack_options[] = {
764 OPT_BIT('a', NULL, &pack_everything,
765 N_("pack everything in a single pack"), ALL_INTO_ONE),
766 OPT_BIT('A', NULL, &pack_everything,
767 N_("same as -a, and turn unreachable objects loose"),
768 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
769 OPT_BIT(0, "cruft", &pack_everything,
770 N_("same as -a, pack unreachable cruft objects separately"),
771 PACK_CRUFT),
772 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
773 N_("with -C, expire objects older than this")),
774 OPT_BOOL('d', NULL, &delete_redundant,
775 N_("remove redundant packs, and run git-prune-packed")),
776 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
777 N_("pass --no-reuse-delta to git-pack-objects")),
778 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
779 N_("pass --no-reuse-object to git-pack-objects")),
780 OPT_NEGBIT('n', NULL, &run_update_server_info,
781 N_("do not run git-update-server-info"), 1),
782 OPT__QUIET(&po_args.quiet, N_("be quiet")),
783 OPT_BOOL('l', "local", &po_args.local,
784 N_("pass --local to git-pack-objects")),
785 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
786 N_("write bitmap index")),
787 OPT_BOOL('i', "delta-islands", &use_delta_islands,
788 N_("pass --delta-islands to git-pack-objects")),
789 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
790 N_("with -A, do not loosen objects older than this")),
791 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
792 N_("with -a, repack unreachable objects")),
793 OPT_STRING(0, "window", &po_args.window, N_("n"),
794 N_("size of the window used for delta compression")),
795 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
796 N_("same as the above, but limit memory size instead of entries count")),
797 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
798 N_("limits the maximum delta depth")),
799 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
800 N_("limits the maximum number of threads")),
801 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
802 N_("maximum size of each packfile")),
803 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
804 N_("repack objects in packs marked with .keep")),
805 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
806 N_("do not repack this pack")),
807 OPT_INTEGER('g', "geometric", &geometric_factor,
808 N_("find a geometric progression with factor <N>")),
809 OPT_BOOL('m', "write-midx", &write_midx,
810 N_("write a multi-pack index of the resulting packs")),
811 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
812 N_("pack prefix to store a pack containing pruned objects")),
813 OPT_END()
816 git_config(repack_config, &cruft_po_args);
818 argc = parse_options(argc, argv, prefix, builtin_repack_options,
819 git_repack_usage, 0);
821 if (delete_redundant && repository_format_precious_objects)
822 die(_("cannot delete packs in a precious-objects repo"));
824 if (keep_unreachable &&
825 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
826 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
828 if (pack_everything & PACK_CRUFT) {
829 pack_everything |= ALL_INTO_ONE;
831 if (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))
832 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
833 if (keep_unreachable)
834 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
837 if (write_bitmaps < 0) {
838 if (!write_midx &&
839 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
840 write_bitmaps = 0;
841 } else if (write_bitmaps &&
842 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
843 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
844 write_bitmaps = 0;
846 if (pack_kept_objects < 0)
847 pack_kept_objects = write_bitmaps > 0 && !write_midx;
849 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
850 die(_(incremental_bitmap_conflict_error));
852 if (write_midx && write_bitmaps) {
853 struct strbuf path = STRBUF_INIT;
855 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
856 "bitmap-ref-tips");
858 refs_snapshot = xmks_tempfile(path.buf);
859 midx_snapshot_refs(refs_snapshot);
861 strbuf_release(&path);
864 packdir = mkpathdup("%s/pack", get_object_directory());
865 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
866 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
868 collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
869 &keep_pack_list);
871 if (geometric_factor) {
872 if (pack_everything)
873 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
874 init_pack_geometry(&geometry, &existing_kept_packs);
875 split_pack_geometry(geometry, geometric_factor);
878 prepare_pack_objects(&cmd, &po_args, packtmp);
880 show_progress = !po_args.quiet && isatty(2);
882 strvec_push(&cmd.args, "--keep-true-parents");
883 if (!pack_kept_objects)
884 strvec_push(&cmd.args, "--honor-pack-keep");
885 for (i = 0; i < keep_pack_list.nr; i++)
886 strvec_pushf(&cmd.args, "--keep-pack=%s",
887 keep_pack_list.items[i].string);
888 strvec_push(&cmd.args, "--non-empty");
889 if (!geometry) {
891 * We need to grab all reachable objects, including those that
892 * are reachable from reflogs and the index.
894 * When repacking into a geometric progression of packs,
895 * however, we ask 'git pack-objects --stdin-packs', and it is
896 * not about packing objects based on reachability but about
897 * repacking all the objects in specified packs and loose ones
898 * (indeed, --stdin-packs is incompatible with these options).
900 strvec_push(&cmd.args, "--all");
901 strvec_push(&cmd.args, "--reflog");
902 strvec_push(&cmd.args, "--indexed-objects");
904 if (has_promisor_remote())
905 strvec_push(&cmd.args, "--exclude-promisor-objects");
906 if (!write_midx) {
907 if (write_bitmaps > 0)
908 strvec_push(&cmd.args, "--write-bitmap-index");
909 else if (write_bitmaps < 0)
910 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
912 if (use_delta_islands)
913 strvec_push(&cmd.args, "--delta-islands");
915 if (pack_everything & ALL_INTO_ONE) {
916 repack_promisor_objects(&po_args, &names);
918 if (existing_nonkept_packs.nr && delete_redundant &&
919 !(pack_everything & PACK_CRUFT)) {
920 for_each_string_list_item(item, &names) {
921 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
922 packtmp_name, item->string);
924 if (unpack_unreachable) {
925 strvec_pushf(&cmd.args,
926 "--unpack-unreachable=%s",
927 unpack_unreachable);
928 } else if (pack_everything & LOOSEN_UNREACHABLE) {
929 strvec_push(&cmd.args,
930 "--unpack-unreachable");
931 } else if (keep_unreachable) {
932 strvec_push(&cmd.args, "--keep-unreachable");
933 strvec_push(&cmd.args, "--pack-loose-unreachable");
936 } else if (geometry) {
937 strvec_push(&cmd.args, "--stdin-packs");
938 strvec_push(&cmd.args, "--unpacked");
939 } else {
940 strvec_push(&cmd.args, "--unpacked");
941 strvec_push(&cmd.args, "--incremental");
944 if (geometry)
945 cmd.in = -1;
946 else
947 cmd.no_stdin = 1;
949 ret = start_command(&cmd);
950 if (ret)
951 return ret;
953 if (geometry) {
954 FILE *in = xfdopen(cmd.in, "w");
956 * The resulting pack should contain all objects in packs that
957 * are going to be rolled up, but exclude objects in packs which
958 * are being left alone.
960 for (i = 0; i < geometry->split; i++)
961 fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
962 for (i = geometry->split; i < geometry->pack_nr; i++)
963 fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
964 fclose(in);
967 out = xfdopen(cmd.out, "r");
968 while (strbuf_getline_lf(&line, out) != EOF) {
969 struct string_list_item *item;
971 if (line.len != the_hash_algo->hexsz)
972 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
973 item = string_list_append(&names, line.buf);
974 item->util = populate_pack_exts(item->string);
976 fclose(out);
977 ret = finish_command(&cmd);
978 if (ret)
979 return ret;
981 if (!names.nr && !po_args.quiet)
982 printf_ln(_("Nothing new to pack."));
984 if (pack_everything & PACK_CRUFT) {
985 const char *pack_prefix;
986 if (!skip_prefix(packtmp, packdir, &pack_prefix))
987 die(_("pack prefix %s does not begin with objdir %s"),
988 packtmp, packdir);
989 if (*pack_prefix == '/')
990 pack_prefix++;
992 if (!cruft_po_args.window)
993 cruft_po_args.window = po_args.window;
994 if (!cruft_po_args.window_memory)
995 cruft_po_args.window_memory = po_args.window_memory;
996 if (!cruft_po_args.depth)
997 cruft_po_args.depth = po_args.depth;
998 if (!cruft_po_args.threads)
999 cruft_po_args.threads = po_args.threads;
1001 cruft_po_args.local = po_args.local;
1002 cruft_po_args.quiet = po_args.quiet;
1004 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1005 cruft_expiration, &names,
1006 &existing_nonkept_packs,
1007 &existing_kept_packs);
1008 if (ret)
1009 return ret;
1011 if (delete_redundant && expire_to) {
1013 * If `--expire-to` is given with `-d`, it's possible
1014 * that we're about to prune some objects. With cruft
1015 * packs, pruning is implicit: any objects from existing
1016 * packs that weren't picked up by new packs are removed
1017 * when their packs are deleted.
1019 * Generate an additional cruft pack, with one twist:
1020 * `names` now includes the name of the cruft pack
1021 * written in the previous step. So the contents of
1022 * _this_ cruft pack exclude everything contained in the
1023 * existing cruft pack (that is, all of the unreachable
1024 * objects which are no older than
1025 * `--cruft-expiration`).
1027 * To make this work, cruft_expiration must become NULL
1028 * so that this cruft pack doesn't actually prune any
1029 * objects. If it were non-NULL, this call would always
1030 * generate an empty pack (since every object not in the
1031 * cruft pack generated above will have an mtime older
1032 * than the expiration).
1034 ret = write_cruft_pack(&cruft_po_args, expire_to,
1035 pack_prefix,
1036 NULL,
1037 &names,
1038 &existing_nonkept_packs,
1039 &existing_kept_packs);
1040 if (ret)
1041 return ret;
1045 string_list_sort(&names);
1047 close_object_store(the_repository->objects);
1050 * Ok we have prepared all new packfiles.
1052 for_each_string_list_item(item, &names) {
1053 struct generated_pack_data *data = item->util;
1055 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1056 char *fname;
1058 fname = mkpathdup("%s/pack-%s%s",
1059 packdir, item->string, exts[ext].name);
1061 if (data->tempfiles[ext]) {
1062 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1063 struct stat statbuffer;
1065 if (!stat(fname_old, &statbuffer)) {
1066 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1067 chmod(fname_old, statbuffer.st_mode);
1070 if (rename_tempfile(&data->tempfiles[ext], fname))
1071 die_errno(_("renaming pack to '%s' failed"), fname);
1072 } else if (!exts[ext].optional)
1073 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1074 exts[ext].name, packtmp, item->string);
1075 else if (unlink(fname) < 0 && errno != ENOENT)
1076 die_errno(_("could not unlink: %s"), fname);
1078 free(fname);
1081 /* End of pack replacement. */
1083 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
1084 const int hexsz = the_hash_algo->hexsz;
1085 for_each_string_list_item(item, &existing_nonkept_packs) {
1086 char *sha1;
1087 size_t len = strlen(item->string);
1088 if (len < hexsz)
1089 continue;
1090 sha1 = item->string + len - hexsz;
1092 * Mark this pack for deletion, which ensures that this
1093 * pack won't be included in a MIDX (if `--write-midx`
1094 * was given) and that we will actually delete this pack
1095 * (if `-d` was given).
1097 if (!string_list_has_string(&names, sha1))
1098 item->util = (void*)(uintptr_t)((size_t)item->util | DELETE_PACK);
1102 if (write_midx) {
1103 struct string_list include = STRING_LIST_INIT_NODUP;
1104 midx_included_packs(&include, &existing_nonkept_packs,
1105 &existing_kept_packs, &names, geometry);
1107 ret = write_midx_included_packs(&include, geometry,
1108 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1109 show_progress, write_bitmaps > 0);
1111 if (!ret && write_bitmaps)
1112 remove_redundant_bitmaps(&include, packdir);
1114 string_list_clear(&include, 0);
1116 if (ret)
1117 return ret;
1120 reprepare_packed_git(the_repository);
1122 if (delete_redundant) {
1123 int opts = 0;
1124 for_each_string_list_item(item, &existing_nonkept_packs) {
1125 if (!((uintptr_t)item->util & DELETE_PACK))
1126 continue;
1127 remove_redundant_pack(packdir, item->string);
1130 if (geometry) {
1131 struct strbuf buf = STRBUF_INIT;
1133 uint32_t i;
1134 for (i = 0; i < geometry->split; i++) {
1135 struct packed_git *p = geometry->pack[i];
1136 if (string_list_has_string(&names,
1137 hash_to_hex(p->hash)))
1138 continue;
1140 strbuf_reset(&buf);
1141 strbuf_addstr(&buf, pack_basename(p));
1142 strbuf_strip_suffix(&buf, ".pack");
1144 if ((p->pack_keep) ||
1145 (string_list_has_string(&existing_kept_packs,
1146 buf.buf)))
1147 continue;
1149 remove_redundant_pack(packdir, buf.buf);
1151 strbuf_release(&buf);
1153 if (show_progress)
1154 opts |= PRUNE_PACKED_VERBOSE;
1155 prune_packed_objects(opts);
1157 if (!keep_unreachable &&
1158 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1159 unpack_unreachable) &&
1160 is_repository_shallow(the_repository))
1161 prune_shallow(PRUNE_QUICK);
1164 if (run_update_server_info)
1165 update_server_info(0);
1167 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1168 unsigned flags = 0;
1169 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
1170 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
1171 write_midx_file(get_object_directory(), NULL, NULL, flags);
1174 string_list_clear(&names, 1);
1175 string_list_clear(&existing_nonkept_packs, 0);
1176 string_list_clear(&existing_kept_packs, 0);
1177 clear_pack_geometry(geometry);
1178 strbuf_release(&line);
1180 return 0;