cat-file: don't whitespace-pad "(...)" in SYNOPSIS and usage output
[alt-git.git] / builtin / repack.c
blob9b74e0d46868d23472bf4181d17ce2030293b86c
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 static int delta_base_offset = 1;
22 static int pack_kept_objects = -1;
23 static int write_bitmaps = -1;
24 static int use_delta_islands;
25 static char *packdir, *packtmp_name, *packtmp;
27 static const char *const git_repack_usage[] = {
28 N_("git repack [<options>]"),
29 NULL
32 static const char incremental_bitmap_conflict_error[] = N_(
33 "Incremental repacks are incompatible with bitmap indexes. Use\n"
34 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
38 static int repack_config(const char *var, const char *value, void *cb)
40 if (!strcmp(var, "repack.usedeltabaseoffset")) {
41 delta_base_offset = git_config_bool(var, value);
42 return 0;
44 if (!strcmp(var, "repack.packkeptobjects")) {
45 pack_kept_objects = git_config_bool(var, value);
46 return 0;
48 if (!strcmp(var, "repack.writebitmaps") ||
49 !strcmp(var, "pack.writebitmaps")) {
50 write_bitmaps = git_config_bool(var, value);
51 return 0;
53 if (!strcmp(var, "repack.usedeltaislands")) {
54 use_delta_islands = git_config_bool(var, value);
55 return 0;
57 return git_default_config(var, value, cb);
61 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
63 static void remove_temporary_files(void)
65 struct strbuf buf = STRBUF_INIT;
66 size_t dirlen, prefixlen;
67 DIR *dir;
68 struct dirent *e;
70 dir = opendir(packdir);
71 if (!dir)
72 return;
74 /* Point at the slash at the end of ".../objects/pack/" */
75 dirlen = strlen(packdir) + 1;
76 strbuf_addstr(&buf, packtmp);
77 /* Hold the length of ".tmp-%d-pack-" */
78 prefixlen = buf.len - dirlen;
80 while ((e = readdir(dir))) {
81 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
82 continue;
83 strbuf_setlen(&buf, dirlen);
84 strbuf_addstr(&buf, e->d_name);
85 unlink(buf.buf);
87 closedir(dir);
88 strbuf_release(&buf);
91 static void remove_pack_on_signal(int signo)
93 remove_temporary_files();
94 sigchain_pop(signo);
95 raise(signo);
99 * Adds all packs hex strings to either fname_nonkept_list or
100 * fname_kept_list based on whether each pack has a corresponding
101 * .keep file or not. Packs without a .keep file are not to be kept
102 * if we are going to pack everything into one file.
104 static void collect_pack_filenames(struct string_list *fname_nonkept_list,
105 struct string_list *fname_kept_list,
106 const struct string_list *extra_keep)
108 DIR *dir;
109 struct dirent *e;
110 char *fname;
112 if (!(dir = opendir(packdir)))
113 return;
115 while ((e = readdir(dir)) != NULL) {
116 size_t len;
117 int i;
119 if (!strip_suffix(e->d_name, ".pack", &len))
120 continue;
122 for (i = 0; i < extra_keep->nr; i++)
123 if (!fspathcmp(e->d_name, extra_keep->items[i].string))
124 break;
126 fname = xmemdupz(e->d_name, len);
128 if ((extra_keep->nr > 0 && i < extra_keep->nr) ||
129 (file_exists(mkpath("%s/%s.keep", packdir, fname))))
130 string_list_append_nodup(fname_kept_list, fname);
131 else
132 string_list_append_nodup(fname_nonkept_list, fname);
134 closedir(dir);
137 static void remove_redundant_pack(const char *dir_name, const char *base_name)
139 struct strbuf buf = STRBUF_INIT;
140 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
141 strbuf_addf(&buf, "%s.pack", base_name);
142 if (m && midx_contains_pack(m, buf.buf))
143 clear_midx_file(the_repository);
144 strbuf_insertf(&buf, 0, "%s/", dir_name);
145 unlink_pack_path(buf.buf, 1);
146 strbuf_release(&buf);
149 struct pack_objects_args {
150 const char *window;
151 const char *window_memory;
152 const char *depth;
153 const char *threads;
154 const char *max_pack_size;
155 int no_reuse_delta;
156 int no_reuse_object;
157 int quiet;
158 int local;
161 static void prepare_pack_objects(struct child_process *cmd,
162 const struct pack_objects_args *args)
164 strvec_push(&cmd->args, "pack-objects");
165 if (args->window)
166 strvec_pushf(&cmd->args, "--window=%s", args->window);
167 if (args->window_memory)
168 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
169 if (args->depth)
170 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
171 if (args->threads)
172 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
173 if (args->max_pack_size)
174 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
175 if (args->no_reuse_delta)
176 strvec_pushf(&cmd->args, "--no-reuse-delta");
177 if (args->no_reuse_object)
178 strvec_pushf(&cmd->args, "--no-reuse-object");
179 if (args->local)
180 strvec_push(&cmd->args, "--local");
181 if (args->quiet)
182 strvec_push(&cmd->args, "--quiet");
183 if (delta_base_offset)
184 strvec_push(&cmd->args, "--delta-base-offset");
185 strvec_push(&cmd->args, packtmp);
186 cmd->git_cmd = 1;
187 cmd->out = -1;
191 * Write oid to the given struct child_process's stdin, starting it first if
192 * necessary.
194 static int write_oid(const struct object_id *oid, struct packed_git *pack,
195 uint32_t pos, void *data)
197 struct child_process *cmd = data;
199 if (cmd->in == -1) {
200 if (start_command(cmd))
201 die(_("could not start pack-objects to repack promisor objects"));
204 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
205 xwrite(cmd->in, "\n", 1);
206 return 0;
209 static struct {
210 const char *name;
211 unsigned optional:1;
212 } exts[] = {
213 {".pack"},
214 {".rev", 1},
215 {".bitmap", 1},
216 {".promisor", 1},
217 {".idx"},
220 static unsigned populate_pack_exts(char *name)
222 struct stat statbuf;
223 struct strbuf path = STRBUF_INIT;
224 unsigned ret = 0;
225 int i;
227 for (i = 0; i < ARRAY_SIZE(exts); i++) {
228 strbuf_reset(&path);
229 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
231 if (stat(path.buf, &statbuf))
232 continue;
234 ret |= (1 << i);
237 strbuf_release(&path);
238 return ret;
241 static void repack_promisor_objects(const struct pack_objects_args *args,
242 struct string_list *names)
244 struct child_process cmd = CHILD_PROCESS_INIT;
245 FILE *out;
246 struct strbuf line = STRBUF_INIT;
248 prepare_pack_objects(&cmd, args);
249 cmd.in = -1;
252 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
253 * hints may result in suboptimal deltas in the resulting pack. See if
254 * the OIDs can be sent with fake paths such that pack-objects can use a
255 * {type -> existing pack order} ordering when computing deltas instead
256 * of a {type -> size} ordering, which may produce better deltas.
258 for_each_packed_object(write_oid, &cmd,
259 FOR_EACH_OBJECT_PROMISOR_ONLY);
261 if (cmd.in == -1) {
262 /* No packed objects; cmd was never started */
263 child_process_clear(&cmd);
264 return;
267 close(cmd.in);
269 out = xfdopen(cmd.out, "r");
270 while (strbuf_getline_lf(&line, out) != EOF) {
271 struct string_list_item *item;
272 char *promisor_name;
274 if (line.len != the_hash_algo->hexsz)
275 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
276 item = string_list_append(names, line.buf);
279 * pack-objects creates the .pack and .idx files, but not the
280 * .promisor file. Create the .promisor file, which is empty.
282 * NEEDSWORK: fetch-pack sometimes generates non-empty
283 * .promisor files containing the ref names and associated
284 * hashes at the point of generation of the corresponding
285 * packfile, but this would not preserve their contents. Maybe
286 * concatenate the contents of all .promisor files instead of
287 * just creating a new empty file.
289 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
290 line.buf);
291 write_promisor_file(promisor_name, NULL, 0);
293 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
295 free(promisor_name);
297 fclose(out);
298 if (finish_command(&cmd))
299 die(_("could not finish pack-objects to repack promisor objects"));
302 #define ALL_INTO_ONE 1
303 #define LOOSEN_UNREACHABLE 2
305 struct pack_geometry {
306 struct packed_git **pack;
307 uint32_t pack_nr, pack_alloc;
308 uint32_t split;
311 static uint32_t geometry_pack_weight(struct packed_git *p)
313 if (open_pack_index(p))
314 die(_("cannot open index for %s"), p->pack_name);
315 return p->num_objects;
318 static int geometry_cmp(const void *va, const void *vb)
320 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
321 bw = geometry_pack_weight(*(struct packed_git **)vb);
323 if (aw < bw)
324 return -1;
325 if (aw > bw)
326 return 1;
327 return 0;
330 static void init_pack_geometry(struct pack_geometry **geometry_p)
332 struct packed_git *p;
333 struct pack_geometry *geometry;
335 *geometry_p = xcalloc(1, sizeof(struct pack_geometry));
336 geometry = *geometry_p;
338 for (p = get_all_packs(the_repository); p; p = p->next) {
339 if (!pack_kept_objects && p->pack_keep)
340 continue;
342 ALLOC_GROW(geometry->pack,
343 geometry->pack_nr + 1,
344 geometry->pack_alloc);
346 geometry->pack[geometry->pack_nr] = p;
347 geometry->pack_nr++;
350 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
353 static void split_pack_geometry(struct pack_geometry *geometry, int factor)
355 uint32_t i;
356 uint32_t split;
357 off_t total_size = 0;
359 if (!geometry->pack_nr) {
360 geometry->split = geometry->pack_nr;
361 return;
365 * First, count the number of packs (in descending order of size) which
366 * already form a geometric progression.
368 for (i = geometry->pack_nr - 1; i > 0; i--) {
369 struct packed_git *ours = geometry->pack[i];
370 struct packed_git *prev = geometry->pack[i - 1];
372 if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
373 die(_("pack %s too large to consider in geometric "
374 "progression"),
375 prev->pack_name);
377 if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
378 break;
381 split = i;
383 if (split) {
385 * Move the split one to the right, since the top element in the
386 * last-compared pair can't be in the progression. Only do this
387 * when we split in the middle of the array (otherwise if we got
388 * to the end, then the split is in the right place).
390 split++;
394 * Then, anything to the left of 'split' must be in a new pack. But,
395 * creating that new pack may cause packs in the heavy half to no longer
396 * form a geometric progression.
398 * Compute an expected size of the new pack, and then determine how many
399 * packs in the heavy half need to be joined into it (if any) to restore
400 * the geometric progression.
402 for (i = 0; i < split; i++) {
403 struct packed_git *p = geometry->pack[i];
405 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
406 die(_("pack %s too large to roll up"), p->pack_name);
407 total_size += geometry_pack_weight(p);
409 for (i = split; i < geometry->pack_nr; i++) {
410 struct packed_git *ours = geometry->pack[i];
412 if (unsigned_mult_overflows(factor, total_size))
413 die(_("pack %s too large to roll up"), ours->pack_name);
415 if (geometry_pack_weight(ours) < factor * total_size) {
416 if (unsigned_add_overflows(total_size,
417 geometry_pack_weight(ours)))
418 die(_("pack %s too large to roll up"),
419 ours->pack_name);
421 split++;
422 total_size += geometry_pack_weight(ours);
423 } else
424 break;
427 geometry->split = split;
430 static struct packed_git *get_largest_active_pack(struct pack_geometry *geometry)
432 if (!geometry) {
434 * No geometry means either an all-into-one repack (in which
435 * case there is only one pack left and it is the largest) or an
436 * incremental one.
438 * If repacking incrementally, then we could check the size of
439 * all packs to determine which should be preferred, but leave
440 * this for later.
442 return NULL;
444 if (geometry->split == geometry->pack_nr)
445 return NULL;
446 return geometry->pack[geometry->pack_nr - 1];
449 static void clear_pack_geometry(struct pack_geometry *geometry)
451 if (!geometry)
452 return;
454 free(geometry->pack);
455 geometry->pack_nr = 0;
456 geometry->pack_alloc = 0;
457 geometry->split = 0;
460 struct midx_snapshot_ref_data {
461 struct tempfile *f;
462 struct oidset seen;
463 int preferred;
466 static int midx_snapshot_ref_one(const char *refname,
467 const struct object_id *oid,
468 int flag, void *_data)
470 struct midx_snapshot_ref_data *data = _data;
471 struct object_id peeled;
473 if (!peel_iterated_oid(oid, &peeled))
474 oid = &peeled;
476 if (oidset_insert(&data->seen, oid))
477 return 0; /* already seen */
479 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
480 return 0;
482 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
483 oid_to_hex(oid));
485 return 0;
488 static void midx_snapshot_refs(struct tempfile *f)
490 struct midx_snapshot_ref_data data;
491 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
493 data.f = f;
494 data.preferred = 0;
495 oidset_init(&data.seen, 0);
497 if (!fdopen_tempfile(f, "w"))
498 die(_("could not open tempfile %s for writing"),
499 get_tempfile_path(f));
501 if (preferred) {
502 struct string_list_item *item;
504 data.preferred = 1;
505 for_each_string_list_item(item, preferred)
506 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
507 data.preferred = 0;
510 for_each_ref(midx_snapshot_ref_one, &data);
512 if (close_tempfile_gently(f)) {
513 int save_errno = errno;
514 delete_tempfile(&f);
515 errno = save_errno;
516 die_errno(_("could not close refs snapshot tempfile"));
519 oidset_clear(&data.seen);
522 static void midx_included_packs(struct string_list *include,
523 struct string_list *existing_nonkept_packs,
524 struct string_list *existing_kept_packs,
525 struct string_list *names,
526 struct pack_geometry *geometry)
528 struct string_list_item *item;
530 for_each_string_list_item(item, existing_kept_packs)
531 string_list_insert(include, xstrfmt("%s.idx", item->string));
532 for_each_string_list_item(item, names)
533 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
534 if (geometry) {
535 struct strbuf buf = STRBUF_INIT;
536 uint32_t i;
537 for (i = geometry->split; i < geometry->pack_nr; i++) {
538 struct packed_git *p = geometry->pack[i];
540 strbuf_addstr(&buf, pack_basename(p));
541 strbuf_strip_suffix(&buf, ".pack");
542 strbuf_addstr(&buf, ".idx");
544 string_list_insert(include, strbuf_detach(&buf, NULL));
546 } else {
547 for_each_string_list_item(item, existing_nonkept_packs) {
548 if (item->util)
549 continue;
550 string_list_insert(include, xstrfmt("%s.idx", item->string));
555 static int write_midx_included_packs(struct string_list *include,
556 struct pack_geometry *geometry,
557 const char *refs_snapshot,
558 int show_progress, int write_bitmaps)
560 struct child_process cmd = CHILD_PROCESS_INIT;
561 struct string_list_item *item;
562 struct packed_git *largest = get_largest_active_pack(geometry);
563 FILE *in;
564 int ret;
566 if (!include->nr)
567 return 0;
569 cmd.in = -1;
570 cmd.git_cmd = 1;
572 strvec_push(&cmd.args, "multi-pack-index");
573 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
575 if (show_progress)
576 strvec_push(&cmd.args, "--progress");
577 else
578 strvec_push(&cmd.args, "--no-progress");
580 if (write_bitmaps)
581 strvec_push(&cmd.args, "--bitmap");
583 if (largest)
584 strvec_pushf(&cmd.args, "--preferred-pack=%s",
585 pack_basename(largest));
587 if (refs_snapshot)
588 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
590 ret = start_command(&cmd);
591 if (ret)
592 return ret;
594 in = xfdopen(cmd.in, "w");
595 for_each_string_list_item(item, include)
596 fprintf(in, "%s\n", item->string);
597 fclose(in);
599 return finish_command(&cmd);
602 int cmd_repack(int argc, const char **argv, const char *prefix)
604 struct child_process cmd = CHILD_PROCESS_INIT;
605 struct string_list_item *item;
606 struct string_list names = STRING_LIST_INIT_DUP;
607 struct string_list rollback = STRING_LIST_INIT_NODUP;
608 struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
609 struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
610 struct pack_geometry *geometry = NULL;
611 struct strbuf line = STRBUF_INIT;
612 struct tempfile *refs_snapshot = NULL;
613 int i, ext, ret;
614 FILE *out;
615 int show_progress = isatty(2);
617 /* variables to be filled by option parsing */
618 int pack_everything = 0;
619 int delete_redundant = 0;
620 const char *unpack_unreachable = NULL;
621 int keep_unreachable = 0;
622 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
623 int no_update_server_info = 0;
624 struct pack_objects_args po_args = {NULL};
625 int geometric_factor = 0;
626 int write_midx = 0;
628 struct option builtin_repack_options[] = {
629 OPT_BIT('a', NULL, &pack_everything,
630 N_("pack everything in a single pack"), ALL_INTO_ONE),
631 OPT_BIT('A', NULL, &pack_everything,
632 N_("same as -a, and turn unreachable objects loose"),
633 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
634 OPT_BOOL('d', NULL, &delete_redundant,
635 N_("remove redundant packs, and run git-prune-packed")),
636 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
637 N_("pass --no-reuse-delta to git-pack-objects")),
638 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
639 N_("pass --no-reuse-object to git-pack-objects")),
640 OPT_BOOL('n', NULL, &no_update_server_info,
641 N_("do not run git-update-server-info")),
642 OPT__QUIET(&po_args.quiet, N_("be quiet")),
643 OPT_BOOL('l', "local", &po_args.local,
644 N_("pass --local to git-pack-objects")),
645 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
646 N_("write bitmap index")),
647 OPT_BOOL('i', "delta-islands", &use_delta_islands,
648 N_("pass --delta-islands to git-pack-objects")),
649 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
650 N_("with -A, do not loosen objects older than this")),
651 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
652 N_("with -a, repack unreachable objects")),
653 OPT_STRING(0, "window", &po_args.window, N_("n"),
654 N_("size of the window used for delta compression")),
655 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
656 N_("same as the above, but limit memory size instead of entries count")),
657 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
658 N_("limits the maximum delta depth")),
659 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
660 N_("limits the maximum number of threads")),
661 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
662 N_("maximum size of each packfile")),
663 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
664 N_("repack objects in packs marked with .keep")),
665 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
666 N_("do not repack this pack")),
667 OPT_INTEGER('g', "geometric", &geometric_factor,
668 N_("find a geometric progression with factor <N>")),
669 OPT_BOOL('m', "write-midx", &write_midx,
670 N_("write a multi-pack index of the resulting packs")),
671 OPT_END()
674 git_config(repack_config, NULL);
676 argc = parse_options(argc, argv, prefix, builtin_repack_options,
677 git_repack_usage, 0);
679 if (delete_redundant && repository_format_precious_objects)
680 die(_("cannot delete packs in a precious-objects repo"));
682 if (keep_unreachable &&
683 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
684 die(_("--keep-unreachable and -A are incompatible"));
686 if (write_bitmaps < 0) {
687 if (!write_midx &&
688 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
689 write_bitmaps = 0;
690 } else if (write_bitmaps &&
691 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
692 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
693 write_bitmaps = 0;
695 if (pack_kept_objects < 0)
696 pack_kept_objects = write_bitmaps > 0;
698 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
699 die(_(incremental_bitmap_conflict_error));
701 if (write_midx && write_bitmaps) {
702 struct strbuf path = STRBUF_INIT;
704 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
705 "bitmap-ref-tips");
707 refs_snapshot = xmks_tempfile(path.buf);
708 midx_snapshot_refs(refs_snapshot);
710 strbuf_release(&path);
713 if (geometric_factor) {
714 if (pack_everything)
715 die(_("--geometric is incompatible with -A, -a"));
716 init_pack_geometry(&geometry);
717 split_pack_geometry(geometry, geometric_factor);
720 packdir = mkpathdup("%s/pack", get_object_directory());
721 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
722 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
724 sigchain_push_common(remove_pack_on_signal);
726 prepare_pack_objects(&cmd, &po_args);
728 strvec_push(&cmd.args, "--keep-true-parents");
729 if (!pack_kept_objects)
730 strvec_push(&cmd.args, "--honor-pack-keep");
731 for (i = 0; i < keep_pack_list.nr; i++)
732 strvec_pushf(&cmd.args, "--keep-pack=%s",
733 keep_pack_list.items[i].string);
734 strvec_push(&cmd.args, "--non-empty");
735 if (!geometry) {
737 * We need to grab all reachable objects, including those that
738 * are reachable from reflogs and the index.
740 * When repacking into a geometric progression of packs,
741 * however, we ask 'git pack-objects --stdin-packs', and it is
742 * not about packing objects based on reachability but about
743 * repacking all the objects in specified packs and loose ones
744 * (indeed, --stdin-packs is incompatible with these options).
746 strvec_push(&cmd.args, "--all");
747 strvec_push(&cmd.args, "--reflog");
748 strvec_push(&cmd.args, "--indexed-objects");
750 if (has_promisor_remote())
751 strvec_push(&cmd.args, "--exclude-promisor-objects");
752 if (!write_midx) {
753 if (write_bitmaps > 0)
754 strvec_push(&cmd.args, "--write-bitmap-index");
755 else if (write_bitmaps < 0)
756 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
758 if (use_delta_islands)
759 strvec_push(&cmd.args, "--delta-islands");
761 collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
762 &keep_pack_list);
764 if (pack_everything & ALL_INTO_ONE) {
765 repack_promisor_objects(&po_args, &names);
767 if (existing_nonkept_packs.nr && delete_redundant) {
768 for_each_string_list_item(item, &names) {
769 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
770 packtmp_name, item->string);
772 if (unpack_unreachable) {
773 strvec_pushf(&cmd.args,
774 "--unpack-unreachable=%s",
775 unpack_unreachable);
776 } else if (pack_everything & LOOSEN_UNREACHABLE) {
777 strvec_push(&cmd.args,
778 "--unpack-unreachable");
779 } else if (keep_unreachable) {
780 strvec_push(&cmd.args, "--keep-unreachable");
781 strvec_push(&cmd.args, "--pack-loose-unreachable");
784 } else if (geometry) {
785 strvec_push(&cmd.args, "--stdin-packs");
786 strvec_push(&cmd.args, "--unpacked");
787 } else {
788 strvec_push(&cmd.args, "--unpacked");
789 strvec_push(&cmd.args, "--incremental");
792 if (geometry)
793 cmd.in = -1;
794 else
795 cmd.no_stdin = 1;
797 ret = start_command(&cmd);
798 if (ret)
799 return ret;
801 if (geometry) {
802 FILE *in = xfdopen(cmd.in, "w");
804 * The resulting pack should contain all objects in packs that
805 * are going to be rolled up, but exclude objects in packs which
806 * are being left alone.
808 for (i = 0; i < geometry->split; i++)
809 fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
810 for (i = geometry->split; i < geometry->pack_nr; i++)
811 fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
812 fclose(in);
815 out = xfdopen(cmd.out, "r");
816 while (strbuf_getline_lf(&line, out) != EOF) {
817 if (line.len != the_hash_algo->hexsz)
818 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
819 string_list_append(&names, line.buf);
821 fclose(out);
822 ret = finish_command(&cmd);
823 if (ret)
824 return ret;
826 if (!names.nr && !po_args.quiet)
827 printf_ln(_("Nothing new to pack."));
829 for_each_string_list_item(item, &names) {
830 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
833 close_object_store(the_repository->objects);
836 * Ok we have prepared all new packfiles.
838 for_each_string_list_item(item, &names) {
839 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
840 char *fname, *fname_old;
842 fname = mkpathdup("%s/pack-%s%s",
843 packdir, item->string, exts[ext].name);
844 fname_old = mkpathdup("%s-%s%s",
845 packtmp, item->string, exts[ext].name);
847 if (((uintptr_t)item->util) & (1 << ext)) {
848 struct stat statbuffer;
849 if (!stat(fname_old, &statbuffer)) {
850 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
851 chmod(fname_old, statbuffer.st_mode);
854 if (rename(fname_old, fname))
855 die_errno(_("renaming '%s' failed"), fname_old);
856 } else if (!exts[ext].optional)
857 die(_("missing required file: %s"), fname_old);
858 else if (unlink(fname) < 0 && errno != ENOENT)
859 die_errno(_("could not unlink: %s"), fname);
861 free(fname);
862 free(fname_old);
865 /* End of pack replacement. */
867 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
868 const int hexsz = the_hash_algo->hexsz;
869 string_list_sort(&names);
870 for_each_string_list_item(item, &existing_nonkept_packs) {
871 char *sha1;
872 size_t len = strlen(item->string);
873 if (len < hexsz)
874 continue;
875 sha1 = item->string + len - hexsz;
877 * Mark this pack for deletion, which ensures that this
878 * pack won't be included in a MIDX (if `--write-midx`
879 * was given) and that we will actually delete this pack
880 * (if `-d` was given).
882 item->util = (void*)(intptr_t)!string_list_has_string(&names, sha1);
886 if (write_midx) {
887 struct string_list include = STRING_LIST_INIT_NODUP;
888 midx_included_packs(&include, &existing_nonkept_packs,
889 &existing_kept_packs, &names, geometry);
891 ret = write_midx_included_packs(&include, geometry,
892 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
893 show_progress, write_bitmaps > 0);
895 string_list_clear(&include, 0);
897 if (ret)
898 return ret;
901 reprepare_packed_git(the_repository);
903 if (delete_redundant) {
904 int opts = 0;
905 for_each_string_list_item(item, &existing_nonkept_packs) {
906 if (!item->util)
907 continue;
908 remove_redundant_pack(packdir, item->string);
911 if (geometry) {
912 struct strbuf buf = STRBUF_INIT;
914 uint32_t i;
915 for (i = 0; i < geometry->split; i++) {
916 struct packed_git *p = geometry->pack[i];
917 if (string_list_has_string(&names,
918 hash_to_hex(p->hash)))
919 continue;
921 strbuf_reset(&buf);
922 strbuf_addstr(&buf, pack_basename(p));
923 strbuf_strip_suffix(&buf, ".pack");
925 remove_redundant_pack(packdir, buf.buf);
927 strbuf_release(&buf);
929 if (!po_args.quiet && show_progress)
930 opts |= PRUNE_PACKED_VERBOSE;
931 prune_packed_objects(opts);
933 if (!keep_unreachable &&
934 (!(pack_everything & LOOSEN_UNREACHABLE) ||
935 unpack_unreachable) &&
936 is_repository_shallow(the_repository))
937 prune_shallow(PRUNE_QUICK);
940 if (!no_update_server_info)
941 update_server_info(0);
942 remove_temporary_files();
944 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
945 unsigned flags = 0;
946 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
947 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
948 write_midx_file(get_object_directory(), NULL, NULL, flags);
951 string_list_clear(&names, 0);
952 string_list_clear(&rollback, 0);
953 string_list_clear(&existing_nonkept_packs, 0);
954 string_list_clear(&existing_kept_packs, 0);
955 clear_pack_geometry(geometry);
956 strbuf_release(&line);
958 return 0;