ci: avoid brew for installing perforce
[git/debian.git] / builtin / repack.c
blobd1a563d5b65666b68596ad353a945e8e11c6ac70
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 int run_update_server_info = 1;
26 static char *packdir, *packtmp_name, *packtmp;
28 static const char *const git_repack_usage[] = {
29 N_("git repack [<options>]"),
30 NULL
33 static const char incremental_bitmap_conflict_error[] = N_(
34 "Incremental repacks are incompatible with bitmap indexes. Use\n"
35 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
39 static int repack_config(const char *var, const char *value, void *cb)
41 if (!strcmp(var, "repack.usedeltabaseoffset")) {
42 delta_base_offset = git_config_bool(var, value);
43 return 0;
45 if (!strcmp(var, "repack.packkeptobjects")) {
46 pack_kept_objects = git_config_bool(var, value);
47 return 0;
49 if (!strcmp(var, "repack.writebitmaps") ||
50 !strcmp(var, "pack.writebitmaps")) {
51 write_bitmaps = git_config_bool(var, value);
52 return 0;
54 if (!strcmp(var, "repack.usedeltaislands")) {
55 use_delta_islands = git_config_bool(var, value);
56 return 0;
58 if (strcmp(var, "repack.updateserverinfo") == 0) {
59 run_update_server_info = git_config_bool(var, value);
60 return 0;
62 return git_default_config(var, value, cb);
66 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
68 static void remove_temporary_files(void)
70 struct strbuf buf = STRBUF_INIT;
71 size_t dirlen, prefixlen;
72 DIR *dir;
73 struct dirent *e;
75 dir = opendir(packdir);
76 if (!dir)
77 return;
79 /* Point at the slash at the end of ".../objects/pack/" */
80 dirlen = strlen(packdir) + 1;
81 strbuf_addstr(&buf, packtmp);
82 /* Hold the length of ".tmp-%d-pack-" */
83 prefixlen = buf.len - dirlen;
85 while ((e = readdir(dir))) {
86 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
87 continue;
88 strbuf_setlen(&buf, dirlen);
89 strbuf_addstr(&buf, e->d_name);
90 unlink(buf.buf);
92 closedir(dir);
93 strbuf_release(&buf);
96 static void remove_pack_on_signal(int signo)
98 remove_temporary_files();
99 sigchain_pop(signo);
100 raise(signo);
104 * Adds all packs hex strings to either fname_nonkept_list or
105 * fname_kept_list based on whether each pack has a corresponding
106 * .keep file or not. Packs without a .keep file are not to be kept
107 * if we are going to pack everything into one file.
109 static void collect_pack_filenames(struct string_list *fname_nonkept_list,
110 struct string_list *fname_kept_list,
111 const struct string_list *extra_keep)
113 DIR *dir;
114 struct dirent *e;
115 char *fname;
117 if (!(dir = opendir(packdir)))
118 return;
120 while ((e = readdir(dir)) != NULL) {
121 size_t len;
122 int i;
124 if (!strip_suffix(e->d_name, ".pack", &len))
125 continue;
127 for (i = 0; i < extra_keep->nr; i++)
128 if (!fspathcmp(e->d_name, extra_keep->items[i].string))
129 break;
131 fname = xmemdupz(e->d_name, len);
133 if ((extra_keep->nr > 0 && i < extra_keep->nr) ||
134 (file_exists(mkpath("%s/%s.keep", packdir, fname))))
135 string_list_append_nodup(fname_kept_list, fname);
136 else
137 string_list_append_nodup(fname_nonkept_list, fname);
139 closedir(dir);
142 static void remove_redundant_pack(const char *dir_name, const char *base_name)
144 struct strbuf buf = STRBUF_INIT;
145 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
146 strbuf_addf(&buf, "%s.pack", base_name);
147 if (m && midx_contains_pack(m, buf.buf))
148 clear_midx_file(the_repository);
149 strbuf_insertf(&buf, 0, "%s/", dir_name);
150 unlink_pack_path(buf.buf, 1);
151 strbuf_release(&buf);
154 struct pack_objects_args {
155 const char *window;
156 const char *window_memory;
157 const char *depth;
158 const char *threads;
159 const char *max_pack_size;
160 int no_reuse_delta;
161 int no_reuse_object;
162 int quiet;
163 int local;
166 static void prepare_pack_objects(struct child_process *cmd,
167 const struct pack_objects_args *args)
169 strvec_push(&cmd->args, "pack-objects");
170 if (args->window)
171 strvec_pushf(&cmd->args, "--window=%s", args->window);
172 if (args->window_memory)
173 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
174 if (args->depth)
175 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
176 if (args->threads)
177 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
178 if (args->max_pack_size)
179 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
180 if (args->no_reuse_delta)
181 strvec_pushf(&cmd->args, "--no-reuse-delta");
182 if (args->no_reuse_object)
183 strvec_pushf(&cmd->args, "--no-reuse-object");
184 if (args->local)
185 strvec_push(&cmd->args, "--local");
186 if (args->quiet)
187 strvec_push(&cmd->args, "--quiet");
188 if (delta_base_offset)
189 strvec_push(&cmd->args, "--delta-base-offset");
190 strvec_push(&cmd->args, packtmp);
191 cmd->git_cmd = 1;
192 cmd->out = -1;
196 * Write oid to the given struct child_process's stdin, starting it first if
197 * necessary.
199 static int write_oid(const struct object_id *oid, struct packed_git *pack,
200 uint32_t pos, void *data)
202 struct child_process *cmd = data;
204 if (cmd->in == -1) {
205 if (start_command(cmd))
206 die(_("could not start pack-objects to repack promisor objects"));
209 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
210 xwrite(cmd->in, "\n", 1);
211 return 0;
214 static struct {
215 const char *name;
216 unsigned optional:1;
217 } exts[] = {
218 {".pack"},
219 {".rev", 1},
220 {".bitmap", 1},
221 {".promisor", 1},
222 {".idx"},
225 static unsigned populate_pack_exts(char *name)
227 struct stat statbuf;
228 struct strbuf path = STRBUF_INIT;
229 unsigned ret = 0;
230 int i;
232 for (i = 0; i < ARRAY_SIZE(exts); i++) {
233 strbuf_reset(&path);
234 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
236 if (stat(path.buf, &statbuf))
237 continue;
239 ret |= (1 << i);
242 strbuf_release(&path);
243 return ret;
246 static void repack_promisor_objects(const struct pack_objects_args *args,
247 struct string_list *names)
249 struct child_process cmd = CHILD_PROCESS_INIT;
250 FILE *out;
251 struct strbuf line = STRBUF_INIT;
253 prepare_pack_objects(&cmd, args);
254 cmd.in = -1;
257 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
258 * hints may result in suboptimal deltas in the resulting pack. See if
259 * the OIDs can be sent with fake paths such that pack-objects can use a
260 * {type -> existing pack order} ordering when computing deltas instead
261 * of a {type -> size} ordering, which may produce better deltas.
263 for_each_packed_object(write_oid, &cmd,
264 FOR_EACH_OBJECT_PROMISOR_ONLY);
266 if (cmd.in == -1) {
267 /* No packed objects; cmd was never started */
268 child_process_clear(&cmd);
269 return;
272 close(cmd.in);
274 out = xfdopen(cmd.out, "r");
275 while (strbuf_getline_lf(&line, out) != EOF) {
276 struct string_list_item *item;
277 char *promisor_name;
279 if (line.len != the_hash_algo->hexsz)
280 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
281 item = string_list_append(names, line.buf);
284 * pack-objects creates the .pack and .idx files, but not the
285 * .promisor file. Create the .promisor file, which is empty.
287 * NEEDSWORK: fetch-pack sometimes generates non-empty
288 * .promisor files containing the ref names and associated
289 * hashes at the point of generation of the corresponding
290 * packfile, but this would not preserve their contents. Maybe
291 * concatenate the contents of all .promisor files instead of
292 * just creating a new empty file.
294 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
295 line.buf);
296 write_promisor_file(promisor_name, NULL, 0);
298 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
300 free(promisor_name);
302 fclose(out);
303 if (finish_command(&cmd))
304 die(_("could not finish pack-objects to repack promisor objects"));
307 #define ALL_INTO_ONE 1
308 #define LOOSEN_UNREACHABLE 2
310 struct pack_geometry {
311 struct packed_git **pack;
312 uint32_t pack_nr, pack_alloc;
313 uint32_t split;
316 static uint32_t geometry_pack_weight(struct packed_git *p)
318 if (open_pack_index(p))
319 die(_("cannot open index for %s"), p->pack_name);
320 return p->num_objects;
323 static int geometry_cmp(const void *va, const void *vb)
325 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
326 bw = geometry_pack_weight(*(struct packed_git **)vb);
328 if (aw < bw)
329 return -1;
330 if (aw > bw)
331 return 1;
332 return 0;
335 static void init_pack_geometry(struct pack_geometry **geometry_p)
337 struct packed_git *p;
338 struct pack_geometry *geometry;
340 *geometry_p = xcalloc(1, sizeof(struct pack_geometry));
341 geometry = *geometry_p;
343 for (p = get_all_packs(the_repository); p; p = p->next) {
344 if (!pack_kept_objects && p->pack_keep)
345 continue;
347 ALLOC_GROW(geometry->pack,
348 geometry->pack_nr + 1,
349 geometry->pack_alloc);
351 geometry->pack[geometry->pack_nr] = p;
352 geometry->pack_nr++;
355 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
358 static void split_pack_geometry(struct pack_geometry *geometry, int factor)
360 uint32_t i;
361 uint32_t split;
362 off_t total_size = 0;
364 if (!geometry->pack_nr) {
365 geometry->split = geometry->pack_nr;
366 return;
370 * First, count the number of packs (in descending order of size) which
371 * already form a geometric progression.
373 for (i = geometry->pack_nr - 1; i > 0; i--) {
374 struct packed_git *ours = geometry->pack[i];
375 struct packed_git *prev = geometry->pack[i - 1];
377 if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
378 die(_("pack %s too large to consider in geometric "
379 "progression"),
380 prev->pack_name);
382 if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
383 break;
386 split = i;
388 if (split) {
390 * Move the split one to the right, since the top element in the
391 * last-compared pair can't be in the progression. Only do this
392 * when we split in the middle of the array (otherwise if we got
393 * to the end, then the split is in the right place).
395 split++;
399 * Then, anything to the left of 'split' must be in a new pack. But,
400 * creating that new pack may cause packs in the heavy half to no longer
401 * form a geometric progression.
403 * Compute an expected size of the new pack, and then determine how many
404 * packs in the heavy half need to be joined into it (if any) to restore
405 * the geometric progression.
407 for (i = 0; i < split; i++) {
408 struct packed_git *p = geometry->pack[i];
410 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
411 die(_("pack %s too large to roll up"), p->pack_name);
412 total_size += geometry_pack_weight(p);
414 for (i = split; i < geometry->pack_nr; i++) {
415 struct packed_git *ours = geometry->pack[i];
417 if (unsigned_mult_overflows(factor, total_size))
418 die(_("pack %s too large to roll up"), ours->pack_name);
420 if (geometry_pack_weight(ours) < factor * total_size) {
421 if (unsigned_add_overflows(total_size,
422 geometry_pack_weight(ours)))
423 die(_("pack %s too large to roll up"),
424 ours->pack_name);
426 split++;
427 total_size += geometry_pack_weight(ours);
428 } else
429 break;
432 geometry->split = split;
435 static struct packed_git *get_largest_active_pack(struct pack_geometry *geometry)
437 if (!geometry) {
439 * No geometry means either an all-into-one repack (in which
440 * case there is only one pack left and it is the largest) or an
441 * incremental one.
443 * If repacking incrementally, then we could check the size of
444 * all packs to determine which should be preferred, but leave
445 * this for later.
447 return NULL;
449 if (geometry->split == geometry->pack_nr)
450 return NULL;
451 return geometry->pack[geometry->pack_nr - 1];
454 static void clear_pack_geometry(struct pack_geometry *geometry)
456 if (!geometry)
457 return;
459 free(geometry->pack);
460 geometry->pack_nr = 0;
461 geometry->pack_alloc = 0;
462 geometry->split = 0;
465 struct midx_snapshot_ref_data {
466 struct tempfile *f;
467 struct oidset seen;
468 int preferred;
471 static int midx_snapshot_ref_one(const char *refname,
472 const struct object_id *oid,
473 int flag, void *_data)
475 struct midx_snapshot_ref_data *data = _data;
476 struct object_id peeled;
478 if (!peel_iterated_oid(oid, &peeled))
479 oid = &peeled;
481 if (oidset_insert(&data->seen, oid))
482 return 0; /* already seen */
484 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
485 return 0;
487 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
488 oid_to_hex(oid));
490 return 0;
493 static void midx_snapshot_refs(struct tempfile *f)
495 struct midx_snapshot_ref_data data;
496 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
498 data.f = f;
499 data.preferred = 0;
500 oidset_init(&data.seen, 0);
502 if (!fdopen_tempfile(f, "w"))
503 die(_("could not open tempfile %s for writing"),
504 get_tempfile_path(f));
506 if (preferred) {
507 struct string_list_item *item;
509 data.preferred = 1;
510 for_each_string_list_item(item, preferred)
511 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
512 data.preferred = 0;
515 for_each_ref(midx_snapshot_ref_one, &data);
517 if (close_tempfile_gently(f)) {
518 int save_errno = errno;
519 delete_tempfile(&f);
520 errno = save_errno;
521 die_errno(_("could not close refs snapshot tempfile"));
524 oidset_clear(&data.seen);
527 static void midx_included_packs(struct string_list *include,
528 struct string_list *existing_nonkept_packs,
529 struct string_list *existing_kept_packs,
530 struct string_list *names,
531 struct pack_geometry *geometry)
533 struct string_list_item *item;
535 for_each_string_list_item(item, existing_kept_packs)
536 string_list_insert(include, xstrfmt("%s.idx", item->string));
537 for_each_string_list_item(item, names)
538 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
539 if (geometry) {
540 struct strbuf buf = STRBUF_INIT;
541 uint32_t i;
542 for (i = geometry->split; i < geometry->pack_nr; i++) {
543 struct packed_git *p = geometry->pack[i];
545 strbuf_addstr(&buf, pack_basename(p));
546 strbuf_strip_suffix(&buf, ".pack");
547 strbuf_addstr(&buf, ".idx");
549 string_list_insert(include, strbuf_detach(&buf, NULL));
551 } else {
552 for_each_string_list_item(item, existing_nonkept_packs) {
553 if (item->util)
554 continue;
555 string_list_insert(include, xstrfmt("%s.idx", item->string));
560 static int write_midx_included_packs(struct string_list *include,
561 struct pack_geometry *geometry,
562 const char *refs_snapshot,
563 int show_progress, int write_bitmaps)
565 struct child_process cmd = CHILD_PROCESS_INIT;
566 struct string_list_item *item;
567 struct packed_git *largest = get_largest_active_pack(geometry);
568 FILE *in;
569 int ret;
571 if (!include->nr)
572 return 0;
574 cmd.in = -1;
575 cmd.git_cmd = 1;
577 strvec_push(&cmd.args, "multi-pack-index");
578 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
580 if (show_progress)
581 strvec_push(&cmd.args, "--progress");
582 else
583 strvec_push(&cmd.args, "--no-progress");
585 if (write_bitmaps)
586 strvec_push(&cmd.args, "--bitmap");
588 if (largest)
589 strvec_pushf(&cmd.args, "--preferred-pack=%s",
590 pack_basename(largest));
592 if (refs_snapshot)
593 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
595 ret = start_command(&cmd);
596 if (ret)
597 return ret;
599 in = xfdopen(cmd.in, "w");
600 for_each_string_list_item(item, include)
601 fprintf(in, "%s\n", item->string);
602 fclose(in);
604 return finish_command(&cmd);
607 int cmd_repack(int argc, const char **argv, const char *prefix)
609 struct child_process cmd = CHILD_PROCESS_INIT;
610 struct string_list_item *item;
611 struct string_list names = STRING_LIST_INIT_DUP;
612 struct string_list rollback = STRING_LIST_INIT_NODUP;
613 struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
614 struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
615 struct pack_geometry *geometry = NULL;
616 struct strbuf line = STRBUF_INIT;
617 struct tempfile *refs_snapshot = NULL;
618 int i, ext, ret;
619 FILE *out;
620 int show_progress;
622 /* variables to be filled by option parsing */
623 int pack_everything = 0;
624 int delete_redundant = 0;
625 const char *unpack_unreachable = NULL;
626 int keep_unreachable = 0;
627 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
628 struct pack_objects_args po_args = {NULL};
629 int geometric_factor = 0;
630 int write_midx = 0;
632 struct option builtin_repack_options[] = {
633 OPT_BIT('a', NULL, &pack_everything,
634 N_("pack everything in a single pack"), ALL_INTO_ONE),
635 OPT_BIT('A', NULL, &pack_everything,
636 N_("same as -a, and turn unreachable objects loose"),
637 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
638 OPT_BOOL('d', NULL, &delete_redundant,
639 N_("remove redundant packs, and run git-prune-packed")),
640 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
641 N_("pass --no-reuse-delta to git-pack-objects")),
642 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
643 N_("pass --no-reuse-object to git-pack-objects")),
644 OPT_NEGBIT('n', NULL, &run_update_server_info,
645 N_("do not run git-update-server-info"), 1),
646 OPT__QUIET(&po_args.quiet, N_("be quiet")),
647 OPT_BOOL('l', "local", &po_args.local,
648 N_("pass --local to git-pack-objects")),
649 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
650 N_("write bitmap index")),
651 OPT_BOOL('i', "delta-islands", &use_delta_islands,
652 N_("pass --delta-islands to git-pack-objects")),
653 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
654 N_("with -A, do not loosen objects older than this")),
655 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
656 N_("with -a, repack unreachable objects")),
657 OPT_STRING(0, "window", &po_args.window, N_("n"),
658 N_("size of the window used for delta compression")),
659 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
660 N_("same as the above, but limit memory size instead of entries count")),
661 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
662 N_("limits the maximum delta depth")),
663 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
664 N_("limits the maximum number of threads")),
665 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
666 N_("maximum size of each packfile")),
667 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
668 N_("repack objects in packs marked with .keep")),
669 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
670 N_("do not repack this pack")),
671 OPT_INTEGER('g', "geometric", &geometric_factor,
672 N_("find a geometric progression with factor <N>")),
673 OPT_BOOL('m', "write-midx", &write_midx,
674 N_("write a multi-pack index of the resulting packs")),
675 OPT_END()
678 git_config(repack_config, NULL);
680 argc = parse_options(argc, argv, prefix, builtin_repack_options,
681 git_repack_usage, 0);
683 if (delete_redundant && repository_format_precious_objects)
684 die(_("cannot delete packs in a precious-objects repo"));
686 if (keep_unreachable &&
687 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
688 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
690 if (write_bitmaps < 0) {
691 if (!write_midx &&
692 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
693 write_bitmaps = 0;
694 } else if (write_bitmaps &&
695 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
696 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
697 write_bitmaps = 0;
699 if (pack_kept_objects < 0)
700 pack_kept_objects = write_bitmaps > 0 && !write_midx;
702 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
703 die(_(incremental_bitmap_conflict_error));
705 if (write_midx && write_bitmaps) {
706 struct strbuf path = STRBUF_INIT;
708 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
709 "bitmap-ref-tips");
711 refs_snapshot = xmks_tempfile(path.buf);
712 midx_snapshot_refs(refs_snapshot);
714 strbuf_release(&path);
717 if (geometric_factor) {
718 if (pack_everything)
719 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
720 init_pack_geometry(&geometry);
721 split_pack_geometry(geometry, geometric_factor);
724 packdir = mkpathdup("%s/pack", get_object_directory());
725 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
726 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
728 sigchain_push_common(remove_pack_on_signal);
730 prepare_pack_objects(&cmd, &po_args);
732 show_progress = !po_args.quiet && isatty(2);
734 strvec_push(&cmd.args, "--keep-true-parents");
735 if (!pack_kept_objects)
736 strvec_push(&cmd.args, "--honor-pack-keep");
737 for (i = 0; i < keep_pack_list.nr; i++)
738 strvec_pushf(&cmd.args, "--keep-pack=%s",
739 keep_pack_list.items[i].string);
740 strvec_push(&cmd.args, "--non-empty");
741 if (!geometry) {
743 * We need to grab all reachable objects, including those that
744 * are reachable from reflogs and the index.
746 * When repacking into a geometric progression of packs,
747 * however, we ask 'git pack-objects --stdin-packs', and it is
748 * not about packing objects based on reachability but about
749 * repacking all the objects in specified packs and loose ones
750 * (indeed, --stdin-packs is incompatible with these options).
752 strvec_push(&cmd.args, "--all");
753 strvec_push(&cmd.args, "--reflog");
754 strvec_push(&cmd.args, "--indexed-objects");
756 if (has_promisor_remote())
757 strvec_push(&cmd.args, "--exclude-promisor-objects");
758 if (!write_midx) {
759 if (write_bitmaps > 0)
760 strvec_push(&cmd.args, "--write-bitmap-index");
761 else if (write_bitmaps < 0)
762 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
764 if (use_delta_islands)
765 strvec_push(&cmd.args, "--delta-islands");
767 collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
768 &keep_pack_list);
770 if (pack_everything & ALL_INTO_ONE) {
771 repack_promisor_objects(&po_args, &names);
773 if (existing_nonkept_packs.nr && delete_redundant) {
774 for_each_string_list_item(item, &names) {
775 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
776 packtmp_name, item->string);
778 if (unpack_unreachable) {
779 strvec_pushf(&cmd.args,
780 "--unpack-unreachable=%s",
781 unpack_unreachable);
782 } else if (pack_everything & LOOSEN_UNREACHABLE) {
783 strvec_push(&cmd.args,
784 "--unpack-unreachable");
785 } else if (keep_unreachable) {
786 strvec_push(&cmd.args, "--keep-unreachable");
787 strvec_push(&cmd.args, "--pack-loose-unreachable");
790 } else if (geometry) {
791 strvec_push(&cmd.args, "--stdin-packs");
792 strvec_push(&cmd.args, "--unpacked");
793 } else {
794 strvec_push(&cmd.args, "--unpacked");
795 strvec_push(&cmd.args, "--incremental");
798 if (geometry)
799 cmd.in = -1;
800 else
801 cmd.no_stdin = 1;
803 ret = start_command(&cmd);
804 if (ret)
805 return ret;
807 if (geometry) {
808 FILE *in = xfdopen(cmd.in, "w");
810 * The resulting pack should contain all objects in packs that
811 * are going to be rolled up, but exclude objects in packs which
812 * are being left alone.
814 for (i = 0; i < geometry->split; i++)
815 fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
816 for (i = geometry->split; i < geometry->pack_nr; i++)
817 fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
818 fclose(in);
821 out = xfdopen(cmd.out, "r");
822 while (strbuf_getline_lf(&line, out) != EOF) {
823 if (line.len != the_hash_algo->hexsz)
824 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
825 string_list_append(&names, line.buf);
827 fclose(out);
828 ret = finish_command(&cmd);
829 if (ret)
830 return ret;
832 if (!names.nr && !po_args.quiet)
833 printf_ln(_("Nothing new to pack."));
835 for_each_string_list_item(item, &names) {
836 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
839 close_object_store(the_repository->objects);
842 * Ok we have prepared all new packfiles.
844 for_each_string_list_item(item, &names) {
845 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
846 char *fname, *fname_old;
848 fname = mkpathdup("%s/pack-%s%s",
849 packdir, item->string, exts[ext].name);
850 fname_old = mkpathdup("%s-%s%s",
851 packtmp, item->string, exts[ext].name);
853 if (((uintptr_t)item->util) & ((uintptr_t)1 << ext)) {
854 struct stat statbuffer;
855 if (!stat(fname_old, &statbuffer)) {
856 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
857 chmod(fname_old, statbuffer.st_mode);
860 if (rename(fname_old, fname))
861 die_errno(_("renaming '%s' failed"), fname_old);
862 } else if (!exts[ext].optional)
863 die(_("missing required file: %s"), fname_old);
864 else if (unlink(fname) < 0 && errno != ENOENT)
865 die_errno(_("could not unlink: %s"), fname);
867 free(fname);
868 free(fname_old);
871 /* End of pack replacement. */
873 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
874 const int hexsz = the_hash_algo->hexsz;
875 string_list_sort(&names);
876 for_each_string_list_item(item, &existing_nonkept_packs) {
877 char *sha1;
878 size_t len = strlen(item->string);
879 if (len < hexsz)
880 continue;
881 sha1 = item->string + len - hexsz;
883 * Mark this pack for deletion, which ensures that this
884 * pack won't be included in a MIDX (if `--write-midx`
885 * was given) and that we will actually delete this pack
886 * (if `-d` was given).
888 item->util = (void*)(intptr_t)!string_list_has_string(&names, sha1);
892 if (write_midx) {
893 struct string_list include = STRING_LIST_INIT_NODUP;
894 midx_included_packs(&include, &existing_nonkept_packs,
895 &existing_kept_packs, &names, geometry);
897 ret = write_midx_included_packs(&include, geometry,
898 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
899 show_progress, write_bitmaps > 0);
901 string_list_clear(&include, 0);
903 if (ret)
904 return ret;
907 reprepare_packed_git(the_repository);
909 if (delete_redundant) {
910 int opts = 0;
911 for_each_string_list_item(item, &existing_nonkept_packs) {
912 if (!item->util)
913 continue;
914 remove_redundant_pack(packdir, item->string);
917 if (geometry) {
918 struct strbuf buf = STRBUF_INIT;
920 uint32_t i;
921 for (i = 0; i < geometry->split; i++) {
922 struct packed_git *p = geometry->pack[i];
923 if (string_list_has_string(&names,
924 hash_to_hex(p->hash)))
925 continue;
927 strbuf_reset(&buf);
928 strbuf_addstr(&buf, pack_basename(p));
929 strbuf_strip_suffix(&buf, ".pack");
931 remove_redundant_pack(packdir, buf.buf);
933 strbuf_release(&buf);
935 if (show_progress)
936 opts |= PRUNE_PACKED_VERBOSE;
937 prune_packed_objects(opts);
939 if (!keep_unreachable &&
940 (!(pack_everything & LOOSEN_UNREACHABLE) ||
941 unpack_unreachable) &&
942 is_repository_shallow(the_repository))
943 prune_shallow(PRUNE_QUICK);
946 if (run_update_server_info)
947 update_server_info(0);
948 remove_temporary_files();
950 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
951 unsigned flags = 0;
952 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
953 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
954 write_midx_file(get_object_directory(), NULL, NULL, flags);
957 string_list_clear(&names, 0);
958 string_list_clear(&rollback, 0);
959 string_list_clear(&existing_nonkept_packs, 0);
960 string_list_clear(&existing_kept_packs, 0);
961 clear_pack_geometry(geometry);
962 strbuf_release(&line);
964 return 0;