]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/repack.c
Merge branch 'mp/rebase-label-length-limit' into maint-2.42
[thirdparty/git.git] / builtin / repack.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "parse-options.h"
8 #include "path.h"
9 #include "run-command.h"
10 #include "server-info.h"
11 #include "sigchain.h"
12 #include "strbuf.h"
13 #include "string-list.h"
14 #include "strvec.h"
15 #include "midx.h"
16 #include "packfile.h"
17 #include "prune-packed.h"
18 #include "object-store-ll.h"
19 #include "promisor-remote.h"
20 #include "shallow.h"
21 #include "pack.h"
22 #include "pack-bitmap.h"
23 #include "refs.h"
24
25 #define ALL_INTO_ONE 1
26 #define LOOSEN_UNREACHABLE 2
27 #define PACK_CRUFT 4
28
29 #define DELETE_PACK 1
30 #define CRUFT_PACK 2
31
32 static int pack_everything;
33 static int delta_base_offset = 1;
34 static int pack_kept_objects = -1;
35 static int write_bitmaps = -1;
36 static int use_delta_islands;
37 static int run_update_server_info = 1;
38 static char *packdir, *packtmp_name, *packtmp;
39
40 static const char *const git_repack_usage[] = {
41 N_("git repack [<options>]"),
42 NULL
43 };
44
45 static const char incremental_bitmap_conflict_error[] = N_(
46 "Incremental repacks are incompatible with bitmap indexes. Use\n"
47 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
48 );
49
50 struct pack_objects_args {
51 const char *window;
52 const char *window_memory;
53 const char *depth;
54 const char *threads;
55 const char *max_pack_size;
56 int no_reuse_delta;
57 int no_reuse_object;
58 int quiet;
59 int local;
60 };
61
62 static int repack_config(const char *var, const char *value,
63 const struct config_context *ctx, void *cb)
64 {
65 struct pack_objects_args *cruft_po_args = cb;
66 if (!strcmp(var, "repack.usedeltabaseoffset")) {
67 delta_base_offset = git_config_bool(var, value);
68 return 0;
69 }
70 if (!strcmp(var, "repack.packkeptobjects")) {
71 pack_kept_objects = git_config_bool(var, value);
72 return 0;
73 }
74 if (!strcmp(var, "repack.writebitmaps") ||
75 !strcmp(var, "pack.writebitmaps")) {
76 write_bitmaps = git_config_bool(var, value);
77 return 0;
78 }
79 if (!strcmp(var, "repack.usedeltaislands")) {
80 use_delta_islands = git_config_bool(var, value);
81 return 0;
82 }
83 if (strcmp(var, "repack.updateserverinfo") == 0) {
84 run_update_server_info = git_config_bool(var, value);
85 return 0;
86 }
87 if (!strcmp(var, "repack.cruftwindow"))
88 return git_config_string(&cruft_po_args->window, var, value);
89 if (!strcmp(var, "repack.cruftwindowmemory"))
90 return git_config_string(&cruft_po_args->window_memory, var, value);
91 if (!strcmp(var, "repack.cruftdepth"))
92 return git_config_string(&cruft_po_args->depth, var, value);
93 if (!strcmp(var, "repack.cruftthreads"))
94 return git_config_string(&cruft_po_args->threads, var, value);
95 return git_default_config(var, value, ctx, cb);
96 }
97
98 /*
99 * Adds all packs hex strings (pack-$HASH) to either fname_nonkept_list
100 * or 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.
103 */
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)
107 {
108 struct packed_git *p;
109 struct strbuf buf = STRBUF_INIT;
110
111 for (p = get_all_packs(the_repository); p; p = p->next) {
112 int i;
113 const char *base;
114
115 if (!p->pack_local)
116 continue;
117
118 base = pack_basename(p);
119
120 for (i = 0; i < extra_keep->nr; i++)
121 if (!fspathcmp(base, extra_keep->items[i].string))
122 break;
123
124 strbuf_reset(&buf);
125 strbuf_addstr(&buf, base);
126 strbuf_strip_suffix(&buf, ".pack");
127
128 if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
129 string_list_append(fname_kept_list, buf.buf);
130 else {
131 struct string_list_item *item;
132 item = string_list_append(fname_nonkept_list, buf.buf);
133 if (p->is_cruft)
134 item->util = (void*)(uintptr_t)CRUFT_PACK;
135 }
136 }
137
138 string_list_sort(fname_kept_list);
139 strbuf_release(&buf);
140 }
141
142 static void remove_redundant_pack(const char *dir_name, const char *base_name)
143 {
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);
152 }
153
154 static void prepare_pack_objects(struct child_process *cmd,
155 const struct pack_objects_args *args,
156 const char *out)
157 {
158 strvec_push(&cmd->args, "pack-objects");
159 if (args->window)
160 strvec_pushf(&cmd->args, "--window=%s", args->window);
161 if (args->window_memory)
162 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
163 if (args->depth)
164 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
165 if (args->threads)
166 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
167 if (args->max_pack_size)
168 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
169 if (args->no_reuse_delta)
170 strvec_pushf(&cmd->args, "--no-reuse-delta");
171 if (args->no_reuse_object)
172 strvec_pushf(&cmd->args, "--no-reuse-object");
173 if (args->local)
174 strvec_push(&cmd->args, "--local");
175 if (args->quiet)
176 strvec_push(&cmd->args, "--quiet");
177 if (delta_base_offset)
178 strvec_push(&cmd->args, "--delta-base-offset");
179 strvec_push(&cmd->args, out);
180 cmd->git_cmd = 1;
181 cmd->out = -1;
182 }
183
184 /*
185 * Write oid to the given struct child_process's stdin, starting it first if
186 * necessary.
187 */
188 static int write_oid(const struct object_id *oid,
189 struct packed_git *pack UNUSED,
190 uint32_t pos UNUSED, void *data)
191 {
192 struct child_process *cmd = data;
193
194 if (cmd->in == -1) {
195 if (start_command(cmd))
196 die(_("could not start pack-objects to repack promisor objects"));
197 }
198
199 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
200 xwrite(cmd->in, "\n", 1);
201 return 0;
202 }
203
204 static struct {
205 const char *name;
206 unsigned optional:1;
207 } exts[] = {
208 {".pack"},
209 {".rev", 1},
210 {".mtimes", 1},
211 {".bitmap", 1},
212 {".promisor", 1},
213 {".idx"},
214 };
215
216 struct generated_pack_data {
217 struct tempfile *tempfiles[ARRAY_SIZE(exts)];
218 };
219
220 static struct generated_pack_data *populate_pack_exts(const char *name)
221 {
222 struct stat statbuf;
223 struct strbuf path = STRBUF_INIT;
224 struct generated_pack_data *data = xcalloc(1, sizeof(*data));
225 int i;
226
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);
230
231 if (stat(path.buf, &statbuf))
232 continue;
233
234 data->tempfiles[i] = register_tempfile(path.buf);
235 }
236
237 strbuf_release(&path);
238 return data;
239 }
240
241 static void repack_promisor_objects(const struct pack_objects_args *args,
242 struct string_list *names)
243 {
244 struct child_process cmd = CHILD_PROCESS_INIT;
245 FILE *out;
246 struct strbuf line = STRBUF_INIT;
247
248 prepare_pack_objects(&cmd, args, packtmp);
249 cmd.in = -1;
250
251 /*
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.
257 */
258 for_each_packed_object(write_oid, &cmd,
259 FOR_EACH_OBJECT_PROMISOR_ONLY);
260
261 if (cmd.in == -1) {
262 /* No packed objects; cmd was never started */
263 child_process_clear(&cmd);
264 return;
265 }
266
267 close(cmd.in);
268
269 out = xfdopen(cmd.out, "r");
270 while (strbuf_getline_lf(&line, out) != EOF) {
271 struct string_list_item *item;
272 char *promisor_name;
273
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);
277
278 /*
279 * pack-objects creates the .pack and .idx files, but not the
280 * .promisor file. Create the .promisor file, which is empty.
281 *
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.
288 */
289 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
290 line.buf);
291 write_promisor_file(promisor_name, NULL, 0);
292
293 item->util = populate_pack_exts(item->string);
294
295 free(promisor_name);
296 }
297 fclose(out);
298 if (finish_command(&cmd))
299 die(_("could not finish pack-objects to repack promisor objects"));
300 }
301
302 struct pack_geometry {
303 struct packed_git **pack;
304 uint32_t pack_nr, pack_alloc;
305 uint32_t split;
306 };
307
308 static uint32_t geometry_pack_weight(struct packed_git *p)
309 {
310 if (open_pack_index(p))
311 die(_("cannot open index for %s"), p->pack_name);
312 return p->num_objects;
313 }
314
315 static int geometry_cmp(const void *va, const void *vb)
316 {
317 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
318 bw = geometry_pack_weight(*(struct packed_git **)vb);
319
320 if (aw < bw)
321 return -1;
322 if (aw > bw)
323 return 1;
324 return 0;
325 }
326
327 static void init_pack_geometry(struct pack_geometry **geometry_p,
328 struct string_list *existing_kept_packs,
329 const struct pack_objects_args *args)
330 {
331 struct packed_git *p;
332 struct pack_geometry *geometry;
333 struct strbuf buf = STRBUF_INIT;
334
335 *geometry_p = xcalloc(1, sizeof(struct pack_geometry));
336 geometry = *geometry_p;
337
338 for (p = get_all_packs(the_repository); p; p = p->next) {
339 if (args->local && !p->pack_local)
340 /*
341 * When asked to only repack local packfiles we skip
342 * over any packfiles that are borrowed from alternate
343 * object directories.
344 */
345 continue;
346
347 if (!pack_kept_objects) {
348 /*
349 * Any pack that has its pack_keep bit set will appear
350 * in existing_kept_packs below, but this saves us from
351 * doing a more expensive check.
352 */
353 if (p->pack_keep)
354 continue;
355
356 /*
357 * The pack may be kept via the --keep-pack option;
358 * check 'existing_kept_packs' to determine whether to
359 * ignore it.
360 */
361 strbuf_reset(&buf);
362 strbuf_addstr(&buf, pack_basename(p));
363 strbuf_strip_suffix(&buf, ".pack");
364
365 if (string_list_has_string(existing_kept_packs, buf.buf))
366 continue;
367 }
368 if (p->is_cruft)
369 continue;
370
371 ALLOC_GROW(geometry->pack,
372 geometry->pack_nr + 1,
373 geometry->pack_alloc);
374
375 geometry->pack[geometry->pack_nr] = p;
376 geometry->pack_nr++;
377 }
378
379 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
380 strbuf_release(&buf);
381 }
382
383 static void split_pack_geometry(struct pack_geometry *geometry, int factor)
384 {
385 uint32_t i;
386 uint32_t split;
387 off_t total_size = 0;
388
389 if (!geometry->pack_nr) {
390 geometry->split = geometry->pack_nr;
391 return;
392 }
393
394 /*
395 * First, count the number of packs (in descending order of size) which
396 * already form a geometric progression.
397 */
398 for (i = geometry->pack_nr - 1; i > 0; i--) {
399 struct packed_git *ours = geometry->pack[i];
400 struct packed_git *prev = geometry->pack[i - 1];
401
402 if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
403 die(_("pack %s too large to consider in geometric "
404 "progression"),
405 prev->pack_name);
406
407 if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
408 break;
409 }
410
411 split = i;
412
413 if (split) {
414 /*
415 * Move the split one to the right, since the top element in the
416 * last-compared pair can't be in the progression. Only do this
417 * when we split in the middle of the array (otherwise if we got
418 * to the end, then the split is in the right place).
419 */
420 split++;
421 }
422
423 /*
424 * Then, anything to the left of 'split' must be in a new pack. But,
425 * creating that new pack may cause packs in the heavy half to no longer
426 * form a geometric progression.
427 *
428 * Compute an expected size of the new pack, and then determine how many
429 * packs in the heavy half need to be joined into it (if any) to restore
430 * the geometric progression.
431 */
432 for (i = 0; i < split; i++) {
433 struct packed_git *p = geometry->pack[i];
434
435 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
436 die(_("pack %s too large to roll up"), p->pack_name);
437 total_size += geometry_pack_weight(p);
438 }
439 for (i = split; i < geometry->pack_nr; i++) {
440 struct packed_git *ours = geometry->pack[i];
441
442 if (unsigned_mult_overflows(factor, total_size))
443 die(_("pack %s too large to roll up"), ours->pack_name);
444
445 if (geometry_pack_weight(ours) < factor * total_size) {
446 if (unsigned_add_overflows(total_size,
447 geometry_pack_weight(ours)))
448 die(_("pack %s too large to roll up"),
449 ours->pack_name);
450
451 split++;
452 total_size += geometry_pack_weight(ours);
453 } else
454 break;
455 }
456
457 geometry->split = split;
458 }
459
460 static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
461 {
462 uint32_t i;
463
464 if (!geometry) {
465 /*
466 * No geometry means either an all-into-one repack (in which
467 * case there is only one pack left and it is the largest) or an
468 * incremental one.
469 *
470 * If repacking incrementally, then we could check the size of
471 * all packs to determine which should be preferred, but leave
472 * this for later.
473 */
474 return NULL;
475 }
476 if (geometry->split == geometry->pack_nr)
477 return NULL;
478
479 /*
480 * The preferred pack is the largest pack above the split line. In
481 * other words, it is the largest pack that does not get rolled up in
482 * the geometric repack.
483 */
484 for (i = geometry->pack_nr; i > geometry->split; i--)
485 /*
486 * A pack that is not local would never be included in a
487 * multi-pack index. We thus skip over any non-local packs.
488 */
489 if (geometry->pack[i - 1]->pack_local)
490 return geometry->pack[i - 1];
491
492 return NULL;
493 }
494
495 static void free_pack_geometry(struct pack_geometry *geometry)
496 {
497 if (!geometry)
498 return;
499
500 free(geometry->pack);
501 free(geometry);
502 }
503
504 struct midx_snapshot_ref_data {
505 struct tempfile *f;
506 struct oidset seen;
507 int preferred;
508 };
509
510 static int midx_snapshot_ref_one(const char *refname UNUSED,
511 const struct object_id *oid,
512 int flag UNUSED, void *_data)
513 {
514 struct midx_snapshot_ref_data *data = _data;
515 struct object_id peeled;
516
517 if (!peel_iterated_oid(oid, &peeled))
518 oid = &peeled;
519
520 if (oidset_insert(&data->seen, oid))
521 return 0; /* already seen */
522
523 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
524 return 0;
525
526 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
527 oid_to_hex(oid));
528
529 return 0;
530 }
531
532 static void midx_snapshot_refs(struct tempfile *f)
533 {
534 struct midx_snapshot_ref_data data;
535 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
536
537 data.f = f;
538 data.preferred = 0;
539 oidset_init(&data.seen, 0);
540
541 if (!fdopen_tempfile(f, "w"))
542 die(_("could not open tempfile %s for writing"),
543 get_tempfile_path(f));
544
545 if (preferred) {
546 struct string_list_item *item;
547
548 data.preferred = 1;
549 for_each_string_list_item(item, preferred)
550 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
551 data.preferred = 0;
552 }
553
554 for_each_ref(midx_snapshot_ref_one, &data);
555
556 if (close_tempfile_gently(f)) {
557 int save_errno = errno;
558 delete_tempfile(&f);
559 errno = save_errno;
560 die_errno(_("could not close refs snapshot tempfile"));
561 }
562
563 oidset_clear(&data.seen);
564 }
565
566 static void midx_included_packs(struct string_list *include,
567 struct string_list *existing_nonkept_packs,
568 struct string_list *existing_kept_packs,
569 struct string_list *names,
570 struct pack_geometry *geometry)
571 {
572 struct string_list_item *item;
573
574 for_each_string_list_item(item, existing_kept_packs)
575 string_list_insert(include, xstrfmt("%s.idx", item->string));
576 for_each_string_list_item(item, names)
577 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
578 if (geometry) {
579 struct strbuf buf = STRBUF_INIT;
580 uint32_t i;
581 for (i = geometry->split; i < geometry->pack_nr; i++) {
582 struct packed_git *p = geometry->pack[i];
583
584 /*
585 * The multi-pack index never refers to packfiles part
586 * of an alternate object database, so we skip these.
587 * While git-multi-pack-index(1) would silently ignore
588 * them anyway, this allows us to skip executing the
589 * command completely when we have only non-local
590 * packfiles.
591 */
592 if (!p->pack_local)
593 continue;
594
595 strbuf_addstr(&buf, pack_basename(p));
596 strbuf_strip_suffix(&buf, ".pack");
597 strbuf_addstr(&buf, ".idx");
598
599 string_list_insert(include, strbuf_detach(&buf, NULL));
600 }
601
602 for_each_string_list_item(item, existing_nonkept_packs) {
603 if (!((uintptr_t)item->util & CRUFT_PACK)) {
604 /*
605 * no need to check DELETE_PACK, since we're not
606 * doing an ALL_INTO_ONE repack
607 */
608 continue;
609 }
610 string_list_insert(include, xstrfmt("%s.idx", item->string));
611 }
612 } else {
613 for_each_string_list_item(item, existing_nonkept_packs) {
614 if ((uintptr_t)item->util & DELETE_PACK)
615 continue;
616 string_list_insert(include, xstrfmt("%s.idx", item->string));
617 }
618 }
619 }
620
621 static int write_midx_included_packs(struct string_list *include,
622 struct pack_geometry *geometry,
623 const char *refs_snapshot,
624 int show_progress, int write_bitmaps)
625 {
626 struct child_process cmd = CHILD_PROCESS_INIT;
627 struct string_list_item *item;
628 struct packed_git *preferred = get_preferred_pack(geometry);
629 FILE *in;
630 int ret;
631
632 if (!include->nr)
633 return 0;
634
635 cmd.in = -1;
636 cmd.git_cmd = 1;
637
638 strvec_push(&cmd.args, "multi-pack-index");
639 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
640
641 if (show_progress)
642 strvec_push(&cmd.args, "--progress");
643 else
644 strvec_push(&cmd.args, "--no-progress");
645
646 if (write_bitmaps)
647 strvec_push(&cmd.args, "--bitmap");
648
649 if (preferred)
650 strvec_pushf(&cmd.args, "--preferred-pack=%s",
651 pack_basename(preferred));
652
653 if (refs_snapshot)
654 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
655
656 ret = start_command(&cmd);
657 if (ret)
658 return ret;
659
660 in = xfdopen(cmd.in, "w");
661 for_each_string_list_item(item, include)
662 fprintf(in, "%s\n", item->string);
663 fclose(in);
664
665 return finish_command(&cmd);
666 }
667
668 static void remove_redundant_bitmaps(struct string_list *include,
669 const char *packdir)
670 {
671 struct strbuf path = STRBUF_INIT;
672 struct string_list_item *item;
673 size_t packdir_len;
674
675 strbuf_addstr(&path, packdir);
676 strbuf_addch(&path, '/');
677 packdir_len = path.len;
678
679 /*
680 * Remove any pack bitmaps corresponding to packs which are now
681 * included in the MIDX.
682 */
683 for_each_string_list_item(item, include) {
684 strbuf_addstr(&path, item->string);
685 strbuf_strip_suffix(&path, ".idx");
686 strbuf_addstr(&path, ".bitmap");
687
688 if (unlink(path.buf) && errno != ENOENT)
689 warning_errno(_("could not remove stale bitmap: %s"),
690 path.buf);
691
692 strbuf_setlen(&path, packdir_len);
693 }
694 strbuf_release(&path);
695 }
696
697 static int write_cruft_pack(const struct pack_objects_args *args,
698 const char *destination,
699 const char *pack_prefix,
700 const char *cruft_expiration,
701 struct string_list *names,
702 struct string_list *existing_packs,
703 struct string_list *existing_kept_packs)
704 {
705 struct child_process cmd = CHILD_PROCESS_INIT;
706 struct strbuf line = STRBUF_INIT;
707 struct string_list_item *item;
708 FILE *in, *out;
709 int ret;
710 const char *scratch;
711 int local = skip_prefix(destination, packdir, &scratch);
712
713 prepare_pack_objects(&cmd, args, destination);
714
715 strvec_push(&cmd.args, "--cruft");
716 if (cruft_expiration)
717 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
718 cruft_expiration);
719
720 strvec_push(&cmd.args, "--honor-pack-keep");
721 strvec_push(&cmd.args, "--non-empty");
722 strvec_push(&cmd.args, "--max-pack-size=0");
723
724 cmd.in = -1;
725
726 ret = start_command(&cmd);
727 if (ret)
728 return ret;
729
730 /*
731 * names has a confusing double use: it both provides the list
732 * of just-written new packs, and accepts the name of the cruft
733 * pack we are writing.
734 *
735 * By the time it is read here, it contains only the pack(s)
736 * that were just written, which is exactly the set of packs we
737 * want to consider kept.
738 *
739 * If `--expire-to` is given, the double-use served by `names`
740 * ensures that the pack written to `--expire-to` excludes any
741 * objects contained in the cruft pack.
742 */
743 in = xfdopen(cmd.in, "w");
744 for_each_string_list_item(item, names)
745 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
746 for_each_string_list_item(item, existing_packs)
747 fprintf(in, "-%s.pack\n", item->string);
748 for_each_string_list_item(item, existing_kept_packs)
749 fprintf(in, "%s.pack\n", item->string);
750 fclose(in);
751
752 out = xfdopen(cmd.out, "r");
753 while (strbuf_getline_lf(&line, out) != EOF) {
754 struct string_list_item *item;
755
756 if (line.len != the_hash_algo->hexsz)
757 die(_("repack: Expecting full hex object ID lines only "
758 "from pack-objects."));
759 /*
760 * avoid putting packs written outside of the repository in the
761 * list of names
762 */
763 if (local) {
764 item = string_list_append(names, line.buf);
765 item->util = populate_pack_exts(line.buf);
766 }
767 }
768 fclose(out);
769
770 strbuf_release(&line);
771
772 return finish_command(&cmd);
773 }
774
775 int cmd_repack(int argc, const char **argv, const char *prefix)
776 {
777 struct child_process cmd = CHILD_PROCESS_INIT;
778 struct string_list_item *item;
779 struct string_list names = STRING_LIST_INIT_DUP;
780 struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
781 struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
782 struct pack_geometry *geometry = NULL;
783 struct strbuf line = STRBUF_INIT;
784 struct tempfile *refs_snapshot = NULL;
785 int i, ext, ret;
786 FILE *out;
787 int show_progress;
788
789 /* variables to be filled by option parsing */
790 int delete_redundant = 0;
791 const char *unpack_unreachable = NULL;
792 int keep_unreachable = 0;
793 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
794 struct pack_objects_args po_args = {NULL};
795 struct pack_objects_args cruft_po_args = {NULL};
796 int geometric_factor = 0;
797 int write_midx = 0;
798 const char *cruft_expiration = NULL;
799 const char *expire_to = NULL;
800
801 struct option builtin_repack_options[] = {
802 OPT_BIT('a', NULL, &pack_everything,
803 N_("pack everything in a single pack"), ALL_INTO_ONE),
804 OPT_BIT('A', NULL, &pack_everything,
805 N_("same as -a, and turn unreachable objects loose"),
806 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
807 OPT_BIT(0, "cruft", &pack_everything,
808 N_("same as -a, pack unreachable cruft objects separately"),
809 PACK_CRUFT),
810 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
811 N_("with --cruft, expire objects older than this")),
812 OPT_BOOL('d', NULL, &delete_redundant,
813 N_("remove redundant packs, and run git-prune-packed")),
814 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
815 N_("pass --no-reuse-delta to git-pack-objects")),
816 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
817 N_("pass --no-reuse-object to git-pack-objects")),
818 OPT_NEGBIT('n', NULL, &run_update_server_info,
819 N_("do not run git-update-server-info"), 1),
820 OPT__QUIET(&po_args.quiet, N_("be quiet")),
821 OPT_BOOL('l', "local", &po_args.local,
822 N_("pass --local to git-pack-objects")),
823 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
824 N_("write bitmap index")),
825 OPT_BOOL('i', "delta-islands", &use_delta_islands,
826 N_("pass --delta-islands to git-pack-objects")),
827 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
828 N_("with -A, do not loosen objects older than this")),
829 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
830 N_("with -a, repack unreachable objects")),
831 OPT_STRING(0, "window", &po_args.window, N_("n"),
832 N_("size of the window used for delta compression")),
833 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
834 N_("same as the above, but limit memory size instead of entries count")),
835 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
836 N_("limits the maximum delta depth")),
837 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
838 N_("limits the maximum number of threads")),
839 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
840 N_("maximum size of each packfile")),
841 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
842 N_("repack objects in packs marked with .keep")),
843 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
844 N_("do not repack this pack")),
845 OPT_INTEGER('g', "geometric", &geometric_factor,
846 N_("find a geometric progression with factor <N>")),
847 OPT_BOOL('m', "write-midx", &write_midx,
848 N_("write a multi-pack index of the resulting packs")),
849 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
850 N_("pack prefix to store a pack containing pruned objects")),
851 OPT_END()
852 };
853
854 git_config(repack_config, &cruft_po_args);
855
856 argc = parse_options(argc, argv, prefix, builtin_repack_options,
857 git_repack_usage, 0);
858
859 if (delete_redundant && repository_format_precious_objects)
860 die(_("cannot delete packs in a precious-objects repo"));
861
862 if (keep_unreachable &&
863 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
864 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
865
866 if (pack_everything & PACK_CRUFT) {
867 pack_everything |= ALL_INTO_ONE;
868
869 if (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))
870 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
871 if (keep_unreachable)
872 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
873 }
874
875 if (write_bitmaps < 0) {
876 if (!write_midx &&
877 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
878 write_bitmaps = 0;
879 } else if (write_bitmaps &&
880 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
881 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
882 write_bitmaps = 0;
883 }
884 if (pack_kept_objects < 0)
885 pack_kept_objects = write_bitmaps > 0 && !write_midx;
886
887 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
888 die(_(incremental_bitmap_conflict_error));
889
890 if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
891 /*
892 * When asked to do a local repack, but we have
893 * packfiles that are inherited from an alternate, then
894 * we cannot guarantee that the multi-pack-index would
895 * have full coverage of all objects. We thus disable
896 * writing bitmaps in that case.
897 */
898 warning(_("disabling bitmap writing, as some objects are not being packed"));
899 write_bitmaps = 0;
900 }
901
902 if (write_midx && write_bitmaps) {
903 struct strbuf path = STRBUF_INIT;
904
905 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
906 "bitmap-ref-tips");
907
908 refs_snapshot = xmks_tempfile(path.buf);
909 midx_snapshot_refs(refs_snapshot);
910
911 strbuf_release(&path);
912 }
913
914 packdir = mkpathdup("%s/pack", get_object_directory());
915 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
916 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
917
918 collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
919 &keep_pack_list);
920
921 if (geometric_factor) {
922 if (pack_everything)
923 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
924 init_pack_geometry(&geometry, &existing_kept_packs, &po_args);
925 split_pack_geometry(geometry, geometric_factor);
926 }
927
928 prepare_pack_objects(&cmd, &po_args, packtmp);
929
930 show_progress = !po_args.quiet && isatty(2);
931
932 strvec_push(&cmd.args, "--keep-true-parents");
933 if (!pack_kept_objects)
934 strvec_push(&cmd.args, "--honor-pack-keep");
935 for (i = 0; i < keep_pack_list.nr; i++)
936 strvec_pushf(&cmd.args, "--keep-pack=%s",
937 keep_pack_list.items[i].string);
938 strvec_push(&cmd.args, "--non-empty");
939 if (!geometry) {
940 /*
941 * We need to grab all reachable objects, including those that
942 * are reachable from reflogs and the index.
943 *
944 * When repacking into a geometric progression of packs,
945 * however, we ask 'git pack-objects --stdin-packs', and it is
946 * not about packing objects based on reachability but about
947 * repacking all the objects in specified packs and loose ones
948 * (indeed, --stdin-packs is incompatible with these options).
949 */
950 strvec_push(&cmd.args, "--all");
951 strvec_push(&cmd.args, "--reflog");
952 strvec_push(&cmd.args, "--indexed-objects");
953 }
954 if (repo_has_promisor_remote(the_repository))
955 strvec_push(&cmd.args, "--exclude-promisor-objects");
956 if (!write_midx) {
957 if (write_bitmaps > 0)
958 strvec_push(&cmd.args, "--write-bitmap-index");
959 else if (write_bitmaps < 0)
960 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
961 }
962 if (use_delta_islands)
963 strvec_push(&cmd.args, "--delta-islands");
964
965 if (pack_everything & ALL_INTO_ONE) {
966 repack_promisor_objects(&po_args, &names);
967
968 if (existing_nonkept_packs.nr && delete_redundant &&
969 !(pack_everything & PACK_CRUFT)) {
970 for_each_string_list_item(item, &names) {
971 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
972 packtmp_name, item->string);
973 }
974 if (unpack_unreachable) {
975 strvec_pushf(&cmd.args,
976 "--unpack-unreachable=%s",
977 unpack_unreachable);
978 } else if (pack_everything & LOOSEN_UNREACHABLE) {
979 strvec_push(&cmd.args,
980 "--unpack-unreachable");
981 } else if (keep_unreachable) {
982 strvec_push(&cmd.args, "--keep-unreachable");
983 strvec_push(&cmd.args, "--pack-loose-unreachable");
984 }
985 }
986 } else if (geometry) {
987 strvec_push(&cmd.args, "--stdin-packs");
988 strvec_push(&cmd.args, "--unpacked");
989 } else {
990 strvec_push(&cmd.args, "--unpacked");
991 strvec_push(&cmd.args, "--incremental");
992 }
993
994 if (geometry)
995 cmd.in = -1;
996 else
997 cmd.no_stdin = 1;
998
999 ret = start_command(&cmd);
1000 if (ret)
1001 goto cleanup;
1002
1003 if (geometry) {
1004 FILE *in = xfdopen(cmd.in, "w");
1005 /*
1006 * The resulting pack should contain all objects in packs that
1007 * are going to be rolled up, but exclude objects in packs which
1008 * are being left alone.
1009 */
1010 for (i = 0; i < geometry->split; i++)
1011 fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
1012 for (i = geometry->split; i < geometry->pack_nr; i++)
1013 fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
1014 fclose(in);
1015 }
1016
1017 out = xfdopen(cmd.out, "r");
1018 while (strbuf_getline_lf(&line, out) != EOF) {
1019 struct string_list_item *item;
1020
1021 if (line.len != the_hash_algo->hexsz)
1022 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
1023 item = string_list_append(&names, line.buf);
1024 item->util = populate_pack_exts(item->string);
1025 }
1026 strbuf_release(&line);
1027 fclose(out);
1028 ret = finish_command(&cmd);
1029 if (ret)
1030 goto cleanup;
1031
1032 if (!names.nr && !po_args.quiet)
1033 printf_ln(_("Nothing new to pack."));
1034
1035 if (pack_everything & PACK_CRUFT) {
1036 const char *pack_prefix;
1037 if (!skip_prefix(packtmp, packdir, &pack_prefix))
1038 die(_("pack prefix %s does not begin with objdir %s"),
1039 packtmp, packdir);
1040 if (*pack_prefix == '/')
1041 pack_prefix++;
1042
1043 if (!cruft_po_args.window)
1044 cruft_po_args.window = po_args.window;
1045 if (!cruft_po_args.window_memory)
1046 cruft_po_args.window_memory = po_args.window_memory;
1047 if (!cruft_po_args.depth)
1048 cruft_po_args.depth = po_args.depth;
1049 if (!cruft_po_args.threads)
1050 cruft_po_args.threads = po_args.threads;
1051
1052 cruft_po_args.local = po_args.local;
1053 cruft_po_args.quiet = po_args.quiet;
1054
1055 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1056 cruft_expiration, &names,
1057 &existing_nonkept_packs,
1058 &existing_kept_packs);
1059 if (ret)
1060 goto cleanup;
1061
1062 if (delete_redundant && expire_to) {
1063 /*
1064 * If `--expire-to` is given with `-d`, it's possible
1065 * that we're about to prune some objects. With cruft
1066 * packs, pruning is implicit: any objects from existing
1067 * packs that weren't picked up by new packs are removed
1068 * when their packs are deleted.
1069 *
1070 * Generate an additional cruft pack, with one twist:
1071 * `names` now includes the name of the cruft pack
1072 * written in the previous step. So the contents of
1073 * _this_ cruft pack exclude everything contained in the
1074 * existing cruft pack (that is, all of the unreachable
1075 * objects which are no older than
1076 * `--cruft-expiration`).
1077 *
1078 * To make this work, cruft_expiration must become NULL
1079 * so that this cruft pack doesn't actually prune any
1080 * objects. If it were non-NULL, this call would always
1081 * generate an empty pack (since every object not in the
1082 * cruft pack generated above will have an mtime older
1083 * than the expiration).
1084 */
1085 ret = write_cruft_pack(&cruft_po_args, expire_to,
1086 pack_prefix,
1087 NULL,
1088 &names,
1089 &existing_nonkept_packs,
1090 &existing_kept_packs);
1091 if (ret)
1092 goto cleanup;
1093 }
1094 }
1095
1096 string_list_sort(&names);
1097
1098 close_object_store(the_repository->objects);
1099
1100 /*
1101 * Ok we have prepared all new packfiles.
1102 */
1103 for_each_string_list_item(item, &names) {
1104 struct generated_pack_data *data = item->util;
1105
1106 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1107 char *fname;
1108
1109 fname = mkpathdup("%s/pack-%s%s",
1110 packdir, item->string, exts[ext].name);
1111
1112 if (data->tempfiles[ext]) {
1113 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1114 struct stat statbuffer;
1115
1116 if (!stat(fname_old, &statbuffer)) {
1117 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1118 chmod(fname_old, statbuffer.st_mode);
1119 }
1120
1121 if (rename_tempfile(&data->tempfiles[ext], fname))
1122 die_errno(_("renaming pack to '%s' failed"), fname);
1123 } else if (!exts[ext].optional)
1124 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1125 exts[ext].name, packtmp, item->string);
1126 else if (unlink(fname) < 0 && errno != ENOENT)
1127 die_errno(_("could not unlink: %s"), fname);
1128
1129 free(fname);
1130 }
1131 }
1132 /* End of pack replacement. */
1133
1134 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
1135 const int hexsz = the_hash_algo->hexsz;
1136 for_each_string_list_item(item, &existing_nonkept_packs) {
1137 char *sha1;
1138 size_t len = strlen(item->string);
1139 if (len < hexsz)
1140 continue;
1141 sha1 = item->string + len - hexsz;
1142 /*
1143 * Mark this pack for deletion, which ensures that this
1144 * pack won't be included in a MIDX (if `--write-midx`
1145 * was given) and that we will actually delete this pack
1146 * (if `-d` was given).
1147 */
1148 if (!string_list_has_string(&names, sha1))
1149 item->util = (void*)(uintptr_t)((size_t)item->util | DELETE_PACK);
1150 }
1151 }
1152
1153 if (write_midx) {
1154 struct string_list include = STRING_LIST_INIT_NODUP;
1155 midx_included_packs(&include, &existing_nonkept_packs,
1156 &existing_kept_packs, &names, geometry);
1157
1158 ret = write_midx_included_packs(&include, geometry,
1159 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1160 show_progress, write_bitmaps > 0);
1161
1162 if (!ret && write_bitmaps)
1163 remove_redundant_bitmaps(&include, packdir);
1164
1165 string_list_clear(&include, 0);
1166
1167 if (ret)
1168 goto cleanup;
1169 }
1170
1171 reprepare_packed_git(the_repository);
1172
1173 if (delete_redundant) {
1174 int opts = 0;
1175 for_each_string_list_item(item, &existing_nonkept_packs) {
1176 if (!((uintptr_t)item->util & DELETE_PACK))
1177 continue;
1178 remove_redundant_pack(packdir, item->string);
1179 }
1180
1181 if (geometry) {
1182 struct strbuf buf = STRBUF_INIT;
1183
1184 uint32_t i;
1185 for (i = 0; i < geometry->split; i++) {
1186 struct packed_git *p = geometry->pack[i];
1187 if (string_list_has_string(&names,
1188 hash_to_hex(p->hash)))
1189 continue;
1190
1191 strbuf_reset(&buf);
1192 strbuf_addstr(&buf, pack_basename(p));
1193 strbuf_strip_suffix(&buf, ".pack");
1194
1195 if ((p->pack_keep) ||
1196 (string_list_has_string(&existing_kept_packs,
1197 buf.buf)))
1198 continue;
1199
1200 remove_redundant_pack(packdir, buf.buf);
1201 }
1202 strbuf_release(&buf);
1203 }
1204 if (show_progress)
1205 opts |= PRUNE_PACKED_VERBOSE;
1206 prune_packed_objects(opts);
1207
1208 if (!keep_unreachable &&
1209 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1210 unpack_unreachable) &&
1211 is_repository_shallow(the_repository))
1212 prune_shallow(PRUNE_QUICK);
1213 }
1214
1215 if (run_update_server_info)
1216 update_server_info(0);
1217
1218 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1219 unsigned flags = 0;
1220 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
1221 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
1222 write_midx_file(get_object_directory(), NULL, NULL, flags);
1223 }
1224
1225 cleanup:
1226 string_list_clear(&names, 1);
1227 string_list_clear(&existing_nonkept_packs, 0);
1228 string_list_clear(&existing_kept_packs, 0);
1229 free_pack_geometry(geometry);
1230
1231 return ret;
1232 }