pack-bitmap: improve grammar of "xor chain" error message
[git/debian.git] / builtin / repack.c
blob482b66f57d66b08f359c723fdf65808a34a05766
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;
35 static char *cruft_expiration;
37 static const char *const git_repack_usage[] = {
38 N_("git repack [<options>]"),
39 NULL
42 static const char incremental_bitmap_conflict_error[] = N_(
43 "Incremental repacks are incompatible with bitmap indexes. Use\n"
44 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
47 struct pack_objects_args {
48 const char *window;
49 const char *window_memory;
50 const char *depth;
51 const char *threads;
52 const char *max_pack_size;
53 int no_reuse_delta;
54 int no_reuse_object;
55 int quiet;
56 int local;
59 static int repack_config(const char *var, const char *value, void *cb)
61 struct pack_objects_args *cruft_po_args = cb;
62 if (!strcmp(var, "repack.usedeltabaseoffset")) {
63 delta_base_offset = git_config_bool(var, value);
64 return 0;
66 if (!strcmp(var, "repack.packkeptobjects")) {
67 pack_kept_objects = git_config_bool(var, value);
68 return 0;
70 if (!strcmp(var, "repack.writebitmaps") ||
71 !strcmp(var, "pack.writebitmaps")) {
72 write_bitmaps = git_config_bool(var, value);
73 return 0;
75 if (!strcmp(var, "repack.usedeltaislands")) {
76 use_delta_islands = git_config_bool(var, value);
77 return 0;
79 if (strcmp(var, "repack.updateserverinfo") == 0) {
80 run_update_server_info = git_config_bool(var, value);
81 return 0;
83 if (!strcmp(var, "repack.cruftwindow"))
84 return git_config_string(&cruft_po_args->window, var, value);
85 if (!strcmp(var, "repack.cruftwindowmemory"))
86 return git_config_string(&cruft_po_args->window_memory, var, value);
87 if (!strcmp(var, "repack.cruftdepth"))
88 return git_config_string(&cruft_po_args->depth, var, value);
89 if (!strcmp(var, "repack.cruftthreads"))
90 return git_config_string(&cruft_po_args->threads, var, value);
91 return git_default_config(var, value, cb);
95 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
97 static void remove_temporary_files(void)
99 struct strbuf buf = STRBUF_INIT;
100 size_t dirlen, prefixlen;
101 DIR *dir;
102 struct dirent *e;
104 dir = opendir(packdir);
105 if (!dir)
106 return;
108 /* Point at the slash at the end of ".../objects/pack/" */
109 dirlen = strlen(packdir) + 1;
110 strbuf_addstr(&buf, packtmp);
111 /* Hold the length of ".tmp-%d-pack-" */
112 prefixlen = buf.len - dirlen;
114 while ((e = readdir(dir))) {
115 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
116 continue;
117 strbuf_setlen(&buf, dirlen);
118 strbuf_addstr(&buf, e->d_name);
119 unlink(buf.buf);
121 closedir(dir);
122 strbuf_release(&buf);
125 static void remove_pack_on_signal(int signo)
127 remove_temporary_files();
128 sigchain_pop(signo);
129 raise(signo);
133 * Adds all packs hex strings to either fname_nonkept_list or
134 * fname_kept_list based on whether each pack has a corresponding
135 * .keep file or not. Packs without a .keep file are not to be kept
136 * if we are going to pack everything into one file.
138 static void collect_pack_filenames(struct string_list *fname_nonkept_list,
139 struct string_list *fname_kept_list,
140 const struct string_list *extra_keep)
142 DIR *dir;
143 struct dirent *e;
144 char *fname;
146 if (!(dir = opendir(packdir)))
147 return;
149 while ((e = readdir(dir)) != NULL) {
150 size_t len;
151 int i;
153 if (!strip_suffix(e->d_name, ".pack", &len))
154 continue;
156 for (i = 0; i < extra_keep->nr; i++)
157 if (!fspathcmp(e->d_name, extra_keep->items[i].string))
158 break;
160 fname = xmemdupz(e->d_name, len);
162 if ((extra_keep->nr > 0 && i < extra_keep->nr) ||
163 (file_exists(mkpath("%s/%s.keep", packdir, fname)))) {
164 string_list_append_nodup(fname_kept_list, fname);
165 } else {
166 struct string_list_item *item;
167 item = string_list_append_nodup(fname_nonkept_list,
168 fname);
169 if (file_exists(mkpath("%s/%s.mtimes", packdir, fname)))
170 item->util = (void*)(uintptr_t)CRUFT_PACK;
173 closedir(dir);
175 string_list_sort(fname_kept_list);
178 static void remove_redundant_pack(const char *dir_name, const char *base_name)
180 struct strbuf buf = STRBUF_INIT;
181 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
182 strbuf_addf(&buf, "%s.pack", base_name);
183 if (m && midx_contains_pack(m, buf.buf))
184 clear_midx_file(the_repository);
185 strbuf_insertf(&buf, 0, "%s/", dir_name);
186 unlink_pack_path(buf.buf, 1);
187 strbuf_release(&buf);
190 static void prepare_pack_objects(struct child_process *cmd,
191 const struct pack_objects_args *args)
193 strvec_push(&cmd->args, "pack-objects");
194 if (args->window)
195 strvec_pushf(&cmd->args, "--window=%s", args->window);
196 if (args->window_memory)
197 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
198 if (args->depth)
199 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
200 if (args->threads)
201 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
202 if (args->max_pack_size)
203 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
204 if (args->no_reuse_delta)
205 strvec_pushf(&cmd->args, "--no-reuse-delta");
206 if (args->no_reuse_object)
207 strvec_pushf(&cmd->args, "--no-reuse-object");
208 if (args->local)
209 strvec_push(&cmd->args, "--local");
210 if (args->quiet)
211 strvec_push(&cmd->args, "--quiet");
212 if (delta_base_offset)
213 strvec_push(&cmd->args, "--delta-base-offset");
214 strvec_push(&cmd->args, packtmp);
215 cmd->git_cmd = 1;
216 cmd->out = -1;
220 * Write oid to the given struct child_process's stdin, starting it first if
221 * necessary.
223 static int write_oid(const struct object_id *oid, struct packed_git *pack,
224 uint32_t pos, void *data)
226 struct child_process *cmd = data;
228 if (cmd->in == -1) {
229 if (start_command(cmd))
230 die(_("could not start pack-objects to repack promisor objects"));
233 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
234 xwrite(cmd->in, "\n", 1);
235 return 0;
238 static struct {
239 const char *name;
240 unsigned optional:1;
241 } exts[] = {
242 {".pack"},
243 {".rev", 1},
244 {".mtimes", 1},
245 {".bitmap", 1},
246 {".promisor", 1},
247 {".idx"},
250 static unsigned populate_pack_exts(char *name)
252 struct stat statbuf;
253 struct strbuf path = STRBUF_INIT;
254 unsigned ret = 0;
255 int i;
257 for (i = 0; i < ARRAY_SIZE(exts); i++) {
258 strbuf_reset(&path);
259 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
261 if (stat(path.buf, &statbuf))
262 continue;
264 ret |= (1 << i);
267 strbuf_release(&path);
268 return ret;
271 static void repack_promisor_objects(const struct pack_objects_args *args,
272 struct string_list *names)
274 struct child_process cmd = CHILD_PROCESS_INIT;
275 FILE *out;
276 struct strbuf line = STRBUF_INIT;
278 prepare_pack_objects(&cmd, args);
279 cmd.in = -1;
282 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
283 * hints may result in suboptimal deltas in the resulting pack. See if
284 * the OIDs can be sent with fake paths such that pack-objects can use a
285 * {type -> existing pack order} ordering when computing deltas instead
286 * of a {type -> size} ordering, which may produce better deltas.
288 for_each_packed_object(write_oid, &cmd,
289 FOR_EACH_OBJECT_PROMISOR_ONLY);
291 if (cmd.in == -1) {
292 /* No packed objects; cmd was never started */
293 child_process_clear(&cmd);
294 return;
297 close(cmd.in);
299 out = xfdopen(cmd.out, "r");
300 while (strbuf_getline_lf(&line, out) != EOF) {
301 struct string_list_item *item;
302 char *promisor_name;
304 if (line.len != the_hash_algo->hexsz)
305 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
306 item = string_list_append(names, line.buf);
309 * pack-objects creates the .pack and .idx files, but not the
310 * .promisor file. Create the .promisor file, which is empty.
312 * NEEDSWORK: fetch-pack sometimes generates non-empty
313 * .promisor files containing the ref names and associated
314 * hashes at the point of generation of the corresponding
315 * packfile, but this would not preserve their contents. Maybe
316 * concatenate the contents of all .promisor files instead of
317 * just creating a new empty file.
319 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
320 line.buf);
321 write_promisor_file(promisor_name, NULL, 0);
323 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
325 free(promisor_name);
327 fclose(out);
328 if (finish_command(&cmd))
329 die(_("could not finish pack-objects to repack promisor objects"));
332 struct pack_geometry {
333 struct packed_git **pack;
334 uint32_t pack_nr, pack_alloc;
335 uint32_t split;
338 static uint32_t geometry_pack_weight(struct packed_git *p)
340 if (open_pack_index(p))
341 die(_("cannot open index for %s"), p->pack_name);
342 return p->num_objects;
345 static int geometry_cmp(const void *va, const void *vb)
347 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
348 bw = geometry_pack_weight(*(struct packed_git **)vb);
350 if (aw < bw)
351 return -1;
352 if (aw > bw)
353 return 1;
354 return 0;
357 static void init_pack_geometry(struct pack_geometry **geometry_p,
358 struct string_list *existing_kept_packs)
360 struct packed_git *p;
361 struct pack_geometry *geometry;
362 struct strbuf buf = STRBUF_INIT;
364 *geometry_p = xcalloc(1, sizeof(struct pack_geometry));
365 geometry = *geometry_p;
367 for (p = get_all_packs(the_repository); p; p = p->next) {
368 if (!pack_kept_objects) {
370 * Any pack that has its pack_keep bit set will appear
371 * in existing_kept_packs below, but this saves us from
372 * doing a more expensive check.
374 if (p->pack_keep)
375 continue;
378 * The pack may be kept via the --keep-pack option;
379 * check 'existing_kept_packs' to determine whether to
380 * ignore it.
382 strbuf_reset(&buf);
383 strbuf_addstr(&buf, pack_basename(p));
384 strbuf_strip_suffix(&buf, ".pack");
386 if (string_list_has_string(existing_kept_packs, buf.buf))
387 continue;
389 if (p->is_cruft)
390 continue;
392 ALLOC_GROW(geometry->pack,
393 geometry->pack_nr + 1,
394 geometry->pack_alloc);
396 geometry->pack[geometry->pack_nr] = p;
397 geometry->pack_nr++;
400 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
401 strbuf_release(&buf);
404 static void split_pack_geometry(struct pack_geometry *geometry, int factor)
406 uint32_t i;
407 uint32_t split;
408 off_t total_size = 0;
410 if (!geometry->pack_nr) {
411 geometry->split = geometry->pack_nr;
412 return;
416 * First, count the number of packs (in descending order of size) which
417 * already form a geometric progression.
419 for (i = geometry->pack_nr - 1; i > 0; i--) {
420 struct packed_git *ours = geometry->pack[i];
421 struct packed_git *prev = geometry->pack[i - 1];
423 if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
424 die(_("pack %s too large to consider in geometric "
425 "progression"),
426 prev->pack_name);
428 if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
429 break;
432 split = i;
434 if (split) {
436 * Move the split one to the right, since the top element in the
437 * last-compared pair can't be in the progression. Only do this
438 * when we split in the middle of the array (otherwise if we got
439 * to the end, then the split is in the right place).
441 split++;
445 * Then, anything to the left of 'split' must be in a new pack. But,
446 * creating that new pack may cause packs in the heavy half to no longer
447 * form a geometric progression.
449 * Compute an expected size of the new pack, and then determine how many
450 * packs in the heavy half need to be joined into it (if any) to restore
451 * the geometric progression.
453 for (i = 0; i < split; i++) {
454 struct packed_git *p = geometry->pack[i];
456 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
457 die(_("pack %s too large to roll up"), p->pack_name);
458 total_size += geometry_pack_weight(p);
460 for (i = split; i < geometry->pack_nr; i++) {
461 struct packed_git *ours = geometry->pack[i];
463 if (unsigned_mult_overflows(factor, total_size))
464 die(_("pack %s too large to roll up"), ours->pack_name);
466 if (geometry_pack_weight(ours) < factor * total_size) {
467 if (unsigned_add_overflows(total_size,
468 geometry_pack_weight(ours)))
469 die(_("pack %s too large to roll up"),
470 ours->pack_name);
472 split++;
473 total_size += geometry_pack_weight(ours);
474 } else
475 break;
478 geometry->split = split;
481 static struct packed_git *get_largest_active_pack(struct pack_geometry *geometry)
483 if (!geometry) {
485 * No geometry means either an all-into-one repack (in which
486 * case there is only one pack left and it is the largest) or an
487 * incremental one.
489 * If repacking incrementally, then we could check the size of
490 * all packs to determine which should be preferred, but leave
491 * this for later.
493 return NULL;
495 if (geometry->split == geometry->pack_nr)
496 return NULL;
497 return geometry->pack[geometry->pack_nr - 1];
500 static void clear_pack_geometry(struct pack_geometry *geometry)
502 if (!geometry)
503 return;
505 free(geometry->pack);
506 geometry->pack_nr = 0;
507 geometry->pack_alloc = 0;
508 geometry->split = 0;
511 struct midx_snapshot_ref_data {
512 struct tempfile *f;
513 struct oidset seen;
514 int preferred;
517 static int midx_snapshot_ref_one(const char *refname,
518 const struct object_id *oid,
519 int flag, void *_data)
521 struct midx_snapshot_ref_data *data = _data;
522 struct object_id peeled;
524 if (!peel_iterated_oid(oid, &peeled))
525 oid = &peeled;
527 if (oidset_insert(&data->seen, oid))
528 return 0; /* already seen */
530 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
531 return 0;
533 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
534 oid_to_hex(oid));
536 return 0;
539 static void midx_snapshot_refs(struct tempfile *f)
541 struct midx_snapshot_ref_data data;
542 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
544 data.f = f;
545 data.preferred = 0;
546 oidset_init(&data.seen, 0);
548 if (!fdopen_tempfile(f, "w"))
549 die(_("could not open tempfile %s for writing"),
550 get_tempfile_path(f));
552 if (preferred) {
553 struct string_list_item *item;
555 data.preferred = 1;
556 for_each_string_list_item(item, preferred)
557 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
558 data.preferred = 0;
561 for_each_ref(midx_snapshot_ref_one, &data);
563 if (close_tempfile_gently(f)) {
564 int save_errno = errno;
565 delete_tempfile(&f);
566 errno = save_errno;
567 die_errno(_("could not close refs snapshot tempfile"));
570 oidset_clear(&data.seen);
573 static void midx_included_packs(struct string_list *include,
574 struct string_list *existing_nonkept_packs,
575 struct string_list *existing_kept_packs,
576 struct string_list *names,
577 struct pack_geometry *geometry)
579 struct string_list_item *item;
581 for_each_string_list_item(item, existing_kept_packs)
582 string_list_insert(include, xstrfmt("%s.idx", item->string));
583 for_each_string_list_item(item, names)
584 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
585 if (geometry) {
586 struct strbuf buf = STRBUF_INIT;
587 uint32_t i;
588 for (i = geometry->split; i < geometry->pack_nr; i++) {
589 struct packed_git *p = geometry->pack[i];
591 strbuf_addstr(&buf, pack_basename(p));
592 strbuf_strip_suffix(&buf, ".pack");
593 strbuf_addstr(&buf, ".idx");
595 string_list_insert(include, strbuf_detach(&buf, NULL));
598 for_each_string_list_item(item, existing_nonkept_packs) {
599 if (!((uintptr_t)item->util & CRUFT_PACK)) {
601 * no need to check DELETE_PACK, since we're not
602 * doing an ALL_INTO_ONE repack
604 continue;
606 string_list_insert(include, xstrfmt("%s.idx", item->string));
608 } else {
609 for_each_string_list_item(item, existing_nonkept_packs) {
610 if ((uintptr_t)item->util & DELETE_PACK)
611 continue;
612 string_list_insert(include, xstrfmt("%s.idx", item->string));
617 static int write_midx_included_packs(struct string_list *include,
618 struct pack_geometry *geometry,
619 const char *refs_snapshot,
620 int show_progress, int write_bitmaps)
622 struct child_process cmd = CHILD_PROCESS_INIT;
623 struct string_list_item *item;
624 struct packed_git *largest = get_largest_active_pack(geometry);
625 FILE *in;
626 int ret;
628 if (!include->nr)
629 return 0;
631 cmd.in = -1;
632 cmd.git_cmd = 1;
634 strvec_push(&cmd.args, "multi-pack-index");
635 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
637 if (show_progress)
638 strvec_push(&cmd.args, "--progress");
639 else
640 strvec_push(&cmd.args, "--no-progress");
642 if (write_bitmaps)
643 strvec_push(&cmd.args, "--bitmap");
645 if (largest)
646 strvec_pushf(&cmd.args, "--preferred-pack=%s",
647 pack_basename(largest));
649 if (refs_snapshot)
650 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
652 ret = start_command(&cmd);
653 if (ret)
654 return ret;
656 in = xfdopen(cmd.in, "w");
657 for_each_string_list_item(item, include)
658 fprintf(in, "%s\n", item->string);
659 fclose(in);
661 return finish_command(&cmd);
664 static int write_cruft_pack(const struct pack_objects_args *args,
665 const char *pack_prefix,
666 struct string_list *names,
667 struct string_list *existing_packs,
668 struct string_list *existing_kept_packs)
670 struct child_process cmd = CHILD_PROCESS_INIT;
671 struct strbuf line = STRBUF_INIT;
672 struct string_list_item *item;
673 FILE *in, *out;
674 int ret;
676 prepare_pack_objects(&cmd, args);
678 strvec_push(&cmd.args, "--cruft");
679 if (cruft_expiration)
680 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
681 cruft_expiration);
683 strvec_push(&cmd.args, "--honor-pack-keep");
684 strvec_push(&cmd.args, "--non-empty");
685 strvec_push(&cmd.args, "--max-pack-size=0");
687 cmd.in = -1;
689 ret = start_command(&cmd);
690 if (ret)
691 return ret;
694 * names has a confusing double use: it both provides the list
695 * of just-written new packs, and accepts the name of the cruft
696 * pack we are writing.
698 * By the time it is read here, it contains only the pack(s)
699 * that were just written, which is exactly the set of packs we
700 * want to consider kept.
702 in = xfdopen(cmd.in, "w");
703 for_each_string_list_item(item, names)
704 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
705 for_each_string_list_item(item, existing_packs)
706 fprintf(in, "-%s.pack\n", item->string);
707 for_each_string_list_item(item, existing_kept_packs)
708 fprintf(in, "%s.pack\n", item->string);
709 fclose(in);
711 out = xfdopen(cmd.out, "r");
712 while (strbuf_getline_lf(&line, out) != EOF) {
713 if (line.len != the_hash_algo->hexsz)
714 die(_("repack: Expecting full hex object ID lines only "
715 "from pack-objects."));
716 string_list_append(names, line.buf);
718 fclose(out);
720 strbuf_release(&line);
722 return finish_command(&cmd);
725 int cmd_repack(int argc, const char **argv, const char *prefix)
727 struct child_process cmd = CHILD_PROCESS_INIT;
728 struct string_list_item *item;
729 struct string_list names = STRING_LIST_INIT_DUP;
730 struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
731 struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
732 struct pack_geometry *geometry = NULL;
733 struct strbuf line = STRBUF_INIT;
734 struct tempfile *refs_snapshot = NULL;
735 int i, ext, ret;
736 FILE *out;
737 int show_progress;
739 /* variables to be filled by option parsing */
740 int delete_redundant = 0;
741 const char *unpack_unreachable = NULL;
742 int keep_unreachable = 0;
743 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
744 struct pack_objects_args po_args = {NULL};
745 struct pack_objects_args cruft_po_args = {NULL};
746 int geometric_factor = 0;
747 int write_midx = 0;
749 struct option builtin_repack_options[] = {
750 OPT_BIT('a', NULL, &pack_everything,
751 N_("pack everything in a single pack"), ALL_INTO_ONE),
752 OPT_BIT('A', NULL, &pack_everything,
753 N_("same as -a, and turn unreachable objects loose"),
754 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
755 OPT_BIT(0, "cruft", &pack_everything,
756 N_("same as -a, pack unreachable cruft objects separately"),
757 PACK_CRUFT),
758 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
759 N_("with -C, expire objects older than this")),
760 OPT_BOOL('d', NULL, &delete_redundant,
761 N_("remove redundant packs, and run git-prune-packed")),
762 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
763 N_("pass --no-reuse-delta to git-pack-objects")),
764 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
765 N_("pass --no-reuse-object to git-pack-objects")),
766 OPT_NEGBIT('n', NULL, &run_update_server_info,
767 N_("do not run git-update-server-info"), 1),
768 OPT__QUIET(&po_args.quiet, N_("be quiet")),
769 OPT_BOOL('l', "local", &po_args.local,
770 N_("pass --local to git-pack-objects")),
771 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
772 N_("write bitmap index")),
773 OPT_BOOL('i', "delta-islands", &use_delta_islands,
774 N_("pass --delta-islands to git-pack-objects")),
775 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
776 N_("with -A, do not loosen objects older than this")),
777 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
778 N_("with -a, repack unreachable objects")),
779 OPT_STRING(0, "window", &po_args.window, N_("n"),
780 N_("size of the window used for delta compression")),
781 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
782 N_("same as the above, but limit memory size instead of entries count")),
783 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
784 N_("limits the maximum delta depth")),
785 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
786 N_("limits the maximum number of threads")),
787 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
788 N_("maximum size of each packfile")),
789 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
790 N_("repack objects in packs marked with .keep")),
791 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
792 N_("do not repack this pack")),
793 OPT_INTEGER('g', "geometric", &geometric_factor,
794 N_("find a geometric progression with factor <N>")),
795 OPT_BOOL('m', "write-midx", &write_midx,
796 N_("write a multi-pack index of the resulting packs")),
797 OPT_END()
800 git_config(repack_config, &cruft_po_args);
802 argc = parse_options(argc, argv, prefix, builtin_repack_options,
803 git_repack_usage, 0);
805 if (delete_redundant && repository_format_precious_objects)
806 die(_("cannot delete packs in a precious-objects repo"));
808 if (keep_unreachable &&
809 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
810 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
812 if (pack_everything & PACK_CRUFT) {
813 pack_everything |= ALL_INTO_ONE;
815 if (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))
816 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
817 if (keep_unreachable)
818 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
821 if (write_bitmaps < 0) {
822 if (!write_midx &&
823 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
824 write_bitmaps = 0;
825 } else if (write_bitmaps &&
826 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
827 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
828 write_bitmaps = 0;
830 if (pack_kept_objects < 0)
831 pack_kept_objects = write_bitmaps > 0 && !write_midx;
833 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
834 die(_(incremental_bitmap_conflict_error));
836 if (write_midx && write_bitmaps) {
837 struct strbuf path = STRBUF_INIT;
839 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
840 "bitmap-ref-tips");
842 refs_snapshot = xmks_tempfile(path.buf);
843 midx_snapshot_refs(refs_snapshot);
845 strbuf_release(&path);
848 packdir = mkpathdup("%s/pack", get_object_directory());
849 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
850 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
852 collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
853 &keep_pack_list);
855 if (geometric_factor) {
856 if (pack_everything)
857 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
858 init_pack_geometry(&geometry, &existing_kept_packs);
859 split_pack_geometry(geometry, geometric_factor);
862 sigchain_push_common(remove_pack_on_signal);
864 prepare_pack_objects(&cmd, &po_args);
866 show_progress = !po_args.quiet && isatty(2);
868 strvec_push(&cmd.args, "--keep-true-parents");
869 if (!pack_kept_objects)
870 strvec_push(&cmd.args, "--honor-pack-keep");
871 for (i = 0; i < keep_pack_list.nr; i++)
872 strvec_pushf(&cmd.args, "--keep-pack=%s",
873 keep_pack_list.items[i].string);
874 strvec_push(&cmd.args, "--non-empty");
875 if (!geometry) {
877 * We need to grab all reachable objects, including those that
878 * are reachable from reflogs and the index.
880 * When repacking into a geometric progression of packs,
881 * however, we ask 'git pack-objects --stdin-packs', and it is
882 * not about packing objects based on reachability but about
883 * repacking all the objects in specified packs and loose ones
884 * (indeed, --stdin-packs is incompatible with these options).
886 strvec_push(&cmd.args, "--all");
887 strvec_push(&cmd.args, "--reflog");
888 strvec_push(&cmd.args, "--indexed-objects");
890 if (has_promisor_remote())
891 strvec_push(&cmd.args, "--exclude-promisor-objects");
892 if (!write_midx) {
893 if (write_bitmaps > 0)
894 strvec_push(&cmd.args, "--write-bitmap-index");
895 else if (write_bitmaps < 0)
896 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
898 if (use_delta_islands)
899 strvec_push(&cmd.args, "--delta-islands");
901 if (pack_everything & ALL_INTO_ONE) {
902 repack_promisor_objects(&po_args, &names);
904 if (existing_nonkept_packs.nr && delete_redundant &&
905 !(pack_everything & PACK_CRUFT)) {
906 for_each_string_list_item(item, &names) {
907 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
908 packtmp_name, item->string);
910 if (unpack_unreachable) {
911 strvec_pushf(&cmd.args,
912 "--unpack-unreachable=%s",
913 unpack_unreachable);
914 } else if (pack_everything & LOOSEN_UNREACHABLE) {
915 strvec_push(&cmd.args,
916 "--unpack-unreachable");
917 } else if (keep_unreachable) {
918 strvec_push(&cmd.args, "--keep-unreachable");
919 strvec_push(&cmd.args, "--pack-loose-unreachable");
922 } else if (geometry) {
923 strvec_push(&cmd.args, "--stdin-packs");
924 strvec_push(&cmd.args, "--unpacked");
925 } else {
926 strvec_push(&cmd.args, "--unpacked");
927 strvec_push(&cmd.args, "--incremental");
930 if (geometry)
931 cmd.in = -1;
932 else
933 cmd.no_stdin = 1;
935 ret = start_command(&cmd);
936 if (ret)
937 return ret;
939 if (geometry) {
940 FILE *in = xfdopen(cmd.in, "w");
942 * The resulting pack should contain all objects in packs that
943 * are going to be rolled up, but exclude objects in packs which
944 * are being left alone.
946 for (i = 0; i < geometry->split; i++)
947 fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
948 for (i = geometry->split; i < geometry->pack_nr; i++)
949 fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
950 fclose(in);
953 out = xfdopen(cmd.out, "r");
954 while (strbuf_getline_lf(&line, out) != EOF) {
955 if (line.len != the_hash_algo->hexsz)
956 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
957 string_list_append(&names, line.buf);
959 fclose(out);
960 ret = finish_command(&cmd);
961 if (ret)
962 return ret;
964 if (!names.nr && !po_args.quiet)
965 printf_ln(_("Nothing new to pack."));
967 if (pack_everything & PACK_CRUFT) {
968 const char *pack_prefix;
969 if (!skip_prefix(packtmp, packdir, &pack_prefix))
970 die(_("pack prefix %s does not begin with objdir %s"),
971 packtmp, packdir);
972 if (*pack_prefix == '/')
973 pack_prefix++;
975 if (!cruft_po_args.window)
976 cruft_po_args.window = po_args.window;
977 if (!cruft_po_args.window_memory)
978 cruft_po_args.window_memory = po_args.window_memory;
979 if (!cruft_po_args.depth)
980 cruft_po_args.depth = po_args.depth;
981 if (!cruft_po_args.threads)
982 cruft_po_args.threads = po_args.threads;
984 cruft_po_args.local = po_args.local;
985 cruft_po_args.quiet = po_args.quiet;
987 ret = write_cruft_pack(&cruft_po_args, pack_prefix, &names,
988 &existing_nonkept_packs,
989 &existing_kept_packs);
990 if (ret)
991 return ret;
994 string_list_sort(&names);
996 for_each_string_list_item(item, &names) {
997 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
1000 close_object_store(the_repository->objects);
1003 * Ok we have prepared all new packfiles.
1005 for_each_string_list_item(item, &names) {
1006 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1007 char *fname, *fname_old;
1009 fname = mkpathdup("%s/pack-%s%s",
1010 packdir, item->string, exts[ext].name);
1011 fname_old = mkpathdup("%s-%s%s",
1012 packtmp, item->string, exts[ext].name);
1014 if (((uintptr_t)item->util) & ((uintptr_t)1 << ext)) {
1015 struct stat statbuffer;
1016 if (!stat(fname_old, &statbuffer)) {
1017 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1018 chmod(fname_old, statbuffer.st_mode);
1021 if (rename(fname_old, fname))
1022 die_errno(_("renaming '%s' failed"), fname_old);
1023 } else if (!exts[ext].optional)
1024 die(_("missing required file: %s"), fname_old);
1025 else if (unlink(fname) < 0 && errno != ENOENT)
1026 die_errno(_("could not unlink: %s"), fname);
1028 free(fname);
1029 free(fname_old);
1032 /* End of pack replacement. */
1034 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
1035 const int hexsz = the_hash_algo->hexsz;
1036 for_each_string_list_item(item, &existing_nonkept_packs) {
1037 char *sha1;
1038 size_t len = strlen(item->string);
1039 if (len < hexsz)
1040 continue;
1041 sha1 = item->string + len - hexsz;
1043 * Mark this pack for deletion, which ensures that this
1044 * pack won't be included in a MIDX (if `--write-midx`
1045 * was given) and that we will actually delete this pack
1046 * (if `-d` was given).
1048 if (!string_list_has_string(&names, sha1))
1049 item->util = (void*)(uintptr_t)((size_t)item->util | DELETE_PACK);
1053 if (write_midx) {
1054 struct string_list include = STRING_LIST_INIT_NODUP;
1055 midx_included_packs(&include, &existing_nonkept_packs,
1056 &existing_kept_packs, &names, geometry);
1058 ret = write_midx_included_packs(&include, geometry,
1059 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1060 show_progress, write_bitmaps > 0);
1062 string_list_clear(&include, 0);
1064 if (ret)
1065 return ret;
1068 reprepare_packed_git(the_repository);
1070 if (delete_redundant) {
1071 int opts = 0;
1072 for_each_string_list_item(item, &existing_nonkept_packs) {
1073 if (!((uintptr_t)item->util & DELETE_PACK))
1074 continue;
1075 remove_redundant_pack(packdir, item->string);
1078 if (geometry) {
1079 struct strbuf buf = STRBUF_INIT;
1081 uint32_t i;
1082 for (i = 0; i < geometry->split; i++) {
1083 struct packed_git *p = geometry->pack[i];
1084 if (string_list_has_string(&names,
1085 hash_to_hex(p->hash)))
1086 continue;
1088 strbuf_reset(&buf);
1089 strbuf_addstr(&buf, pack_basename(p));
1090 strbuf_strip_suffix(&buf, ".pack");
1092 remove_redundant_pack(packdir, buf.buf);
1094 strbuf_release(&buf);
1096 if (show_progress)
1097 opts |= PRUNE_PACKED_VERBOSE;
1098 prune_packed_objects(opts);
1100 if (!keep_unreachable &&
1101 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1102 unpack_unreachable) &&
1103 is_repository_shallow(the_repository))
1104 prune_shallow(PRUNE_QUICK);
1107 if (run_update_server_info)
1108 update_server_info(0);
1109 remove_temporary_files();
1111 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1112 unsigned flags = 0;
1113 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
1114 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
1115 write_midx_file(get_object_directory(), NULL, NULL, flags);
1118 string_list_clear(&names, 0);
1119 string_list_clear(&existing_nonkept_packs, 0);
1120 string_list_clear(&existing_kept_packs, 0);
1121 clear_pack_geometry(geometry);
1122 strbuf_release(&line);
1124 return 0;