]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/repack.c
c490a51e9192da493dffcbd9b3051bb1fcd90b7b
[thirdparty/git.git] / builtin / repack.c
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
4 #include "builtin.h"
5 #include "config.h"
6 #include "dir.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "parse-options.h"
11 #include "path.h"
12 #include "run-command.h"
13 #include "server-info.h"
14 #include "strbuf.h"
15 #include "string-list.h"
16 #include "strvec.h"
17 #include "midx.h"
18 #include "packfile.h"
19 #include "prune-packed.h"
20 #include "odb.h"
21 #include "promisor-remote.h"
22 #include "shallow.h"
23 #include "pack.h"
24 #include "pack-bitmap.h"
25 #include "refs.h"
26 #include "list-objects-filter-options.h"
27
28 #define ALL_INTO_ONE 1
29 #define LOOSEN_UNREACHABLE 2
30 #define PACK_CRUFT 4
31
32 #define DELETE_PACK 1
33 #define RETAIN_PACK 2
34
35 static int pack_everything;
36 static int delta_base_offset = 1;
37 static int pack_kept_objects = -1;
38 static int write_bitmaps = -1;
39 static int use_delta_islands;
40 static int run_update_server_info = 1;
41 static char *packdir, *packtmp_name, *packtmp;
42 static int midx_must_contain_cruft = 1;
43
44 static const char *const git_repack_usage[] = {
45 N_("git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]\n"
46 "[--window=<n>] [--depth=<n>] [--threads=<n>] [--keep-pack=<pack-name>]\n"
47 "[--write-midx] [--name-hash-version=<n>] [--path-walk]"),
48 NULL
49 };
50
51 static const char incremental_bitmap_conflict_error[] = N_(
52 "Incremental repacks are incompatible with bitmap indexes. Use\n"
53 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
54 );
55
56 struct pack_objects_args {
57 char *window;
58 char *window_memory;
59 char *depth;
60 char *threads;
61 unsigned long max_pack_size;
62 int no_reuse_delta;
63 int no_reuse_object;
64 int quiet;
65 int local;
66 int name_hash_version;
67 int path_walk;
68 struct list_objects_filter_options filter_options;
69 };
70
71 static int repack_config(const char *var, const char *value,
72 const struct config_context *ctx, void *cb)
73 {
74 struct pack_objects_args *cruft_po_args = cb;
75 if (!strcmp(var, "repack.usedeltabaseoffset")) {
76 delta_base_offset = git_config_bool(var, value);
77 return 0;
78 }
79 if (!strcmp(var, "repack.packkeptobjects")) {
80 pack_kept_objects = git_config_bool(var, value);
81 return 0;
82 }
83 if (!strcmp(var, "repack.writebitmaps") ||
84 !strcmp(var, "pack.writebitmaps")) {
85 write_bitmaps = git_config_bool(var, value);
86 return 0;
87 }
88 if (!strcmp(var, "repack.usedeltaislands")) {
89 use_delta_islands = git_config_bool(var, value);
90 return 0;
91 }
92 if (strcmp(var, "repack.updateserverinfo") == 0) {
93 run_update_server_info = git_config_bool(var, value);
94 return 0;
95 }
96 if (!strcmp(var, "repack.cruftwindow")) {
97 free(cruft_po_args->window);
98 return git_config_string(&cruft_po_args->window, var, value);
99 }
100 if (!strcmp(var, "repack.cruftwindowmemory")) {
101 free(cruft_po_args->window_memory);
102 return git_config_string(&cruft_po_args->window_memory, var, value);
103 }
104 if (!strcmp(var, "repack.cruftdepth")) {
105 free(cruft_po_args->depth);
106 return git_config_string(&cruft_po_args->depth, var, value);
107 }
108 if (!strcmp(var, "repack.cruftthreads")) {
109 free(cruft_po_args->threads);
110 return git_config_string(&cruft_po_args->threads, var, value);
111 }
112 if (!strcmp(var, "repack.midxmustcontaincruft")) {
113 midx_must_contain_cruft = git_config_bool(var, value);
114 return 0;
115 }
116 return git_default_config(var, value, ctx, cb);
117 }
118
119 static void pack_objects_args_release(struct pack_objects_args *args)
120 {
121 free(args->window);
122 free(args->window_memory);
123 free(args->depth);
124 free(args->threads);
125 list_objects_filter_release(&args->filter_options);
126 }
127
128 struct existing_packs {
129 struct string_list kept_packs;
130 struct string_list non_kept_packs;
131 struct string_list cruft_packs;
132 };
133
134 #define EXISTING_PACKS_INIT { \
135 .kept_packs = STRING_LIST_INIT_DUP, \
136 .non_kept_packs = STRING_LIST_INIT_DUP, \
137 .cruft_packs = STRING_LIST_INIT_DUP, \
138 }
139
140 static int has_existing_non_kept_packs(const struct existing_packs *existing)
141 {
142 return existing->non_kept_packs.nr || existing->cruft_packs.nr;
143 }
144
145 static void pack_mark_for_deletion(struct string_list_item *item)
146 {
147 item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
148 }
149
150 static void pack_unmark_for_deletion(struct string_list_item *item)
151 {
152 item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
153 }
154
155 static int pack_is_marked_for_deletion(struct string_list_item *item)
156 {
157 return (uintptr_t)item->util & DELETE_PACK;
158 }
159
160 static void pack_mark_retained(struct string_list_item *item)
161 {
162 item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
163 }
164
165 static int pack_is_retained(struct string_list_item *item)
166 {
167 return (uintptr_t)item->util & RETAIN_PACK;
168 }
169
170 static void mark_packs_for_deletion_1(struct string_list *names,
171 struct string_list *list)
172 {
173 struct string_list_item *item;
174 const int hexsz = the_hash_algo->hexsz;
175
176 for_each_string_list_item(item, list) {
177 char *sha1;
178 size_t len = strlen(item->string);
179 if (len < hexsz)
180 continue;
181 sha1 = item->string + len - hexsz;
182
183 if (pack_is_retained(item)) {
184 pack_unmark_for_deletion(item);
185 } else if (!string_list_has_string(names, sha1)) {
186 /*
187 * Mark this pack for deletion, which ensures
188 * that this pack won't be included in a MIDX
189 * (if `--write-midx` was given) and that we
190 * will actually delete this pack (if `-d` was
191 * given).
192 */
193 pack_mark_for_deletion(item);
194 }
195 }
196 }
197
198 static void retain_cruft_pack(struct existing_packs *existing,
199 struct packed_git *cruft)
200 {
201 struct strbuf buf = STRBUF_INIT;
202 struct string_list_item *item;
203
204 strbuf_addstr(&buf, pack_basename(cruft));
205 strbuf_strip_suffix(&buf, ".pack");
206
207 item = string_list_lookup(&existing->cruft_packs, buf.buf);
208 if (!item)
209 BUG("could not find cruft pack '%s'", pack_basename(cruft));
210
211 pack_mark_retained(item);
212 strbuf_release(&buf);
213 }
214
215 static void mark_packs_for_deletion(struct existing_packs *existing,
216 struct string_list *names)
217
218 {
219 mark_packs_for_deletion_1(names, &existing->non_kept_packs);
220 mark_packs_for_deletion_1(names, &existing->cruft_packs);
221 }
222
223 static void remove_redundant_pack(const char *dir_name, const char *base_name)
224 {
225 struct strbuf buf = STRBUF_INIT;
226 struct odb_source *source = the_repository->objects->sources;
227 struct multi_pack_index *m = get_multi_pack_index(source);
228 strbuf_addf(&buf, "%s.pack", base_name);
229 if (m && source->local && midx_contains_pack(m, buf.buf))
230 clear_midx_file(the_repository);
231 strbuf_insertf(&buf, 0, "%s/", dir_name);
232 unlink_pack_path(buf.buf, 1);
233 strbuf_release(&buf);
234 }
235
236 static void remove_redundant_packs_1(struct string_list *packs)
237 {
238 struct string_list_item *item;
239 for_each_string_list_item(item, packs) {
240 if (!pack_is_marked_for_deletion(item))
241 continue;
242 remove_redundant_pack(packdir, item->string);
243 }
244 }
245
246 static void remove_redundant_existing_packs(struct existing_packs *existing)
247 {
248 remove_redundant_packs_1(&existing->non_kept_packs);
249 remove_redundant_packs_1(&existing->cruft_packs);
250 }
251
252 static void existing_packs_release(struct existing_packs *existing)
253 {
254 string_list_clear(&existing->kept_packs, 0);
255 string_list_clear(&existing->non_kept_packs, 0);
256 string_list_clear(&existing->cruft_packs, 0);
257 }
258
259 /*
260 * Adds all packs hex strings (pack-$HASH) to either packs->non_kept
261 * or packs->kept based on whether each pack has a corresponding
262 * .keep file or not. Packs without a .keep file are not to be kept
263 * if we are going to pack everything into one file.
264 */
265 static void collect_pack_filenames(struct existing_packs *existing,
266 const struct string_list *extra_keep)
267 {
268 struct packed_git *p;
269 struct strbuf buf = STRBUF_INIT;
270
271 for (p = get_all_packs(the_repository); p; p = p->next) {
272 int i;
273 const char *base;
274
275 if (!p->pack_local)
276 continue;
277
278 base = pack_basename(p);
279
280 for (i = 0; i < extra_keep->nr; i++)
281 if (!fspathcmp(base, extra_keep->items[i].string))
282 break;
283
284 strbuf_reset(&buf);
285 strbuf_addstr(&buf, base);
286 strbuf_strip_suffix(&buf, ".pack");
287
288 if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
289 string_list_append(&existing->kept_packs, buf.buf);
290 else if (p->is_cruft)
291 string_list_append(&existing->cruft_packs, buf.buf);
292 else
293 string_list_append(&existing->non_kept_packs, buf.buf);
294 }
295
296 string_list_sort(&existing->kept_packs);
297 string_list_sort(&existing->non_kept_packs);
298 string_list_sort(&existing->cruft_packs);
299 strbuf_release(&buf);
300 }
301
302 static void prepare_pack_objects(struct child_process *cmd,
303 const struct pack_objects_args *args,
304 const char *out)
305 {
306 strvec_push(&cmd->args, "pack-objects");
307 if (args->window)
308 strvec_pushf(&cmd->args, "--window=%s", args->window);
309 if (args->window_memory)
310 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
311 if (args->depth)
312 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
313 if (args->threads)
314 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
315 if (args->max_pack_size)
316 strvec_pushf(&cmd->args, "--max-pack-size=%lu", args->max_pack_size);
317 if (args->no_reuse_delta)
318 strvec_pushf(&cmd->args, "--no-reuse-delta");
319 if (args->no_reuse_object)
320 strvec_pushf(&cmd->args, "--no-reuse-object");
321 if (args->name_hash_version)
322 strvec_pushf(&cmd->args, "--name-hash-version=%d", args->name_hash_version);
323 if (args->path_walk)
324 strvec_pushf(&cmd->args, "--path-walk");
325 if (args->local)
326 strvec_push(&cmd->args, "--local");
327 if (args->quiet)
328 strvec_push(&cmd->args, "--quiet");
329 if (delta_base_offset)
330 strvec_push(&cmd->args, "--delta-base-offset");
331 strvec_push(&cmd->args, out);
332 cmd->git_cmd = 1;
333 cmd->out = -1;
334 }
335
336 /*
337 * Write oid to the given struct child_process's stdin, starting it first if
338 * necessary.
339 */
340 static int write_oid(const struct object_id *oid,
341 struct packed_git *pack UNUSED,
342 uint32_t pos UNUSED, void *data)
343 {
344 struct child_process *cmd = data;
345
346 if (cmd->in == -1) {
347 if (start_command(cmd))
348 die(_("could not start pack-objects to repack promisor objects"));
349 }
350
351 if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
352 write_in_full(cmd->in, "\n", 1) < 0)
353 die(_("failed to feed promisor objects to pack-objects"));
354 return 0;
355 }
356
357 static struct {
358 const char *name;
359 unsigned optional:1;
360 } exts[] = {
361 {".pack"},
362 {".rev", 1},
363 {".mtimes", 1},
364 {".bitmap", 1},
365 {".promisor", 1},
366 {".idx"},
367 };
368
369 struct generated_pack_data {
370 struct tempfile *tempfiles[ARRAY_SIZE(exts)];
371 };
372
373 static struct generated_pack_data *populate_pack_exts(const char *name)
374 {
375 struct stat statbuf;
376 struct strbuf path = STRBUF_INIT;
377 struct generated_pack_data *data = xcalloc(1, sizeof(*data));
378 int i;
379
380 for (i = 0; i < ARRAY_SIZE(exts); i++) {
381 strbuf_reset(&path);
382 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
383
384 if (stat(path.buf, &statbuf))
385 continue;
386
387 data->tempfiles[i] = register_tempfile(path.buf);
388 }
389
390 strbuf_release(&path);
391 return data;
392 }
393
394 static int has_pack_ext(const struct generated_pack_data *data,
395 const char *ext)
396 {
397 int i;
398 for (i = 0; i < ARRAY_SIZE(exts); i++) {
399 if (strcmp(exts[i].name, ext))
400 continue;
401 return !!data->tempfiles[i];
402 }
403 BUG("unknown pack extension: '%s'", ext);
404 }
405
406 static void repack_promisor_objects(const struct pack_objects_args *args,
407 struct string_list *names)
408 {
409 struct child_process cmd = CHILD_PROCESS_INIT;
410 FILE *out;
411 struct strbuf line = STRBUF_INIT;
412
413 prepare_pack_objects(&cmd, args, packtmp);
414 cmd.in = -1;
415
416 /*
417 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
418 * hints may result in suboptimal deltas in the resulting pack. See if
419 * the OIDs can be sent with fake paths such that pack-objects can use a
420 * {type -> existing pack order} ordering when computing deltas instead
421 * of a {type -> size} ordering, which may produce better deltas.
422 */
423 for_each_packed_object(the_repository, write_oid, &cmd,
424 FOR_EACH_OBJECT_PROMISOR_ONLY);
425
426 if (cmd.in == -1) {
427 /* No packed objects; cmd was never started */
428 child_process_clear(&cmd);
429 return;
430 }
431
432 close(cmd.in);
433
434 out = xfdopen(cmd.out, "r");
435 while (strbuf_getline_lf(&line, out) != EOF) {
436 struct string_list_item *item;
437 char *promisor_name;
438
439 if (line.len != the_hash_algo->hexsz)
440 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
441 item = string_list_append(names, line.buf);
442
443 /*
444 * pack-objects creates the .pack and .idx files, but not the
445 * .promisor file. Create the .promisor file, which is empty.
446 *
447 * NEEDSWORK: fetch-pack sometimes generates non-empty
448 * .promisor files containing the ref names and associated
449 * hashes at the point of generation of the corresponding
450 * packfile, but this would not preserve their contents. Maybe
451 * concatenate the contents of all .promisor files instead of
452 * just creating a new empty file.
453 */
454 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
455 line.buf);
456 write_promisor_file(promisor_name, NULL, 0);
457
458 item->util = populate_pack_exts(item->string);
459
460 free(promisor_name);
461 }
462
463 fclose(out);
464 if (finish_command(&cmd))
465 die(_("could not finish pack-objects to repack promisor objects"));
466 strbuf_release(&line);
467 }
468
469 struct pack_geometry {
470 struct packed_git **pack;
471 uint32_t pack_nr, pack_alloc;
472 uint32_t split;
473
474 int split_factor;
475 };
476
477 static uint32_t geometry_pack_weight(struct packed_git *p)
478 {
479 if (open_pack_index(p))
480 die(_("cannot open index for %s"), p->pack_name);
481 return p->num_objects;
482 }
483
484 static int geometry_cmp(const void *va, const void *vb)
485 {
486 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
487 bw = geometry_pack_weight(*(struct packed_git **)vb);
488
489 if (aw < bw)
490 return -1;
491 if (aw > bw)
492 return 1;
493 return 0;
494 }
495
496 static void init_pack_geometry(struct pack_geometry *geometry,
497 struct existing_packs *existing,
498 const struct pack_objects_args *args)
499 {
500 struct packed_git *p;
501 struct strbuf buf = STRBUF_INIT;
502
503 for (p = get_all_packs(the_repository); p; p = p->next) {
504 if (args->local && !p->pack_local)
505 /*
506 * When asked to only repack local packfiles we skip
507 * over any packfiles that are borrowed from alternate
508 * object directories.
509 */
510 continue;
511
512 if (!pack_kept_objects) {
513 /*
514 * Any pack that has its pack_keep bit set will
515 * appear in existing->kept_packs below, but
516 * this saves us from doing a more expensive
517 * check.
518 */
519 if (p->pack_keep)
520 continue;
521
522 /*
523 * The pack may be kept via the --keep-pack
524 * option; check 'existing->kept_packs' to
525 * determine whether to ignore it.
526 */
527 strbuf_reset(&buf);
528 strbuf_addstr(&buf, pack_basename(p));
529 strbuf_strip_suffix(&buf, ".pack");
530
531 if (string_list_has_string(&existing->kept_packs, buf.buf))
532 continue;
533 }
534 if (p->is_cruft)
535 continue;
536
537 ALLOC_GROW(geometry->pack,
538 geometry->pack_nr + 1,
539 geometry->pack_alloc);
540
541 geometry->pack[geometry->pack_nr] = p;
542 geometry->pack_nr++;
543 }
544
545 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
546 strbuf_release(&buf);
547 }
548
549 static void split_pack_geometry(struct pack_geometry *geometry)
550 {
551 uint32_t i;
552 uint32_t split;
553 off_t total_size = 0;
554
555 if (!geometry->pack_nr) {
556 geometry->split = geometry->pack_nr;
557 return;
558 }
559
560 /*
561 * First, count the number of packs (in descending order of size) which
562 * already form a geometric progression.
563 */
564 for (i = geometry->pack_nr - 1; i > 0; i--) {
565 struct packed_git *ours = geometry->pack[i];
566 struct packed_git *prev = geometry->pack[i - 1];
567
568 if (unsigned_mult_overflows(geometry->split_factor,
569 geometry_pack_weight(prev)))
570 die(_("pack %s too large to consider in geometric "
571 "progression"),
572 prev->pack_name);
573
574 if (geometry_pack_weight(ours) <
575 geometry->split_factor * geometry_pack_weight(prev))
576 break;
577 }
578
579 split = i;
580
581 if (split) {
582 /*
583 * Move the split one to the right, since the top element in the
584 * last-compared pair can't be in the progression. Only do this
585 * when we split in the middle of the array (otherwise if we got
586 * to the end, then the split is in the right place).
587 */
588 split++;
589 }
590
591 /*
592 * Then, anything to the left of 'split' must be in a new pack. But,
593 * creating that new pack may cause packs in the heavy half to no longer
594 * form a geometric progression.
595 *
596 * Compute an expected size of the new pack, and then determine how many
597 * packs in the heavy half need to be joined into it (if any) to restore
598 * the geometric progression.
599 */
600 for (i = 0; i < split; i++) {
601 struct packed_git *p = geometry->pack[i];
602
603 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
604 die(_("pack %s too large to roll up"), p->pack_name);
605 total_size += geometry_pack_weight(p);
606 }
607 for (i = split; i < geometry->pack_nr; i++) {
608 struct packed_git *ours = geometry->pack[i];
609
610 if (unsigned_mult_overflows(geometry->split_factor,
611 total_size))
612 die(_("pack %s too large to roll up"), ours->pack_name);
613
614 if (geometry_pack_weight(ours) <
615 geometry->split_factor * total_size) {
616 if (unsigned_add_overflows(total_size,
617 geometry_pack_weight(ours)))
618 die(_("pack %s too large to roll up"),
619 ours->pack_name);
620
621 split++;
622 total_size += geometry_pack_weight(ours);
623 } else
624 break;
625 }
626
627 geometry->split = split;
628 }
629
630 static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
631 {
632 uint32_t i;
633
634 if (!geometry) {
635 /*
636 * No geometry means either an all-into-one repack (in which
637 * case there is only one pack left and it is the largest) or an
638 * incremental one.
639 *
640 * If repacking incrementally, then we could check the size of
641 * all packs to determine which should be preferred, but leave
642 * this for later.
643 */
644 return NULL;
645 }
646 if (geometry->split == geometry->pack_nr)
647 return NULL;
648
649 /*
650 * The preferred pack is the largest pack above the split line. In
651 * other words, it is the largest pack that does not get rolled up in
652 * the geometric repack.
653 */
654 for (i = geometry->pack_nr; i > geometry->split; i--)
655 /*
656 * A pack that is not local would never be included in a
657 * multi-pack index. We thus skip over any non-local packs.
658 */
659 if (geometry->pack[i - 1]->pack_local)
660 return geometry->pack[i - 1];
661
662 return NULL;
663 }
664
665 static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
666 struct string_list *names,
667 struct existing_packs *existing)
668 {
669 struct strbuf buf = STRBUF_INIT;
670 uint32_t i;
671
672 for (i = 0; i < geometry->split; i++) {
673 struct packed_git *p = geometry->pack[i];
674 if (string_list_has_string(names, hash_to_hex(p->hash)))
675 continue;
676
677 strbuf_reset(&buf);
678 strbuf_addstr(&buf, pack_basename(p));
679 strbuf_strip_suffix(&buf, ".pack");
680
681 if ((p->pack_keep) ||
682 (string_list_has_string(&existing->kept_packs, buf.buf)))
683 continue;
684
685 remove_redundant_pack(packdir, buf.buf);
686 }
687
688 strbuf_release(&buf);
689 }
690
691 static void free_pack_geometry(struct pack_geometry *geometry)
692 {
693 if (!geometry)
694 return;
695
696 free(geometry->pack);
697 }
698
699 static int midx_has_unknown_packs(char **midx_pack_names,
700 size_t midx_pack_names_nr,
701 struct string_list *include,
702 struct pack_geometry *geometry,
703 struct existing_packs *existing)
704 {
705 size_t i;
706
707 string_list_sort(include);
708
709 for (i = 0; i < midx_pack_names_nr; i++) {
710 const char *pack_name = midx_pack_names[i];
711
712 /*
713 * Determine whether or not each MIDX'd pack from the existing
714 * MIDX (if any) is represented in the new MIDX. For each pack
715 * in the MIDX, it must either be:
716 *
717 * - In the "include" list of packs to be included in the new
718 * MIDX. Note this function is called before the include
719 * list is populated with any cruft pack(s).
720 *
721 * - Below the geometric split line (if using pack geometry),
722 * indicating that the pack won't be included in the new
723 * MIDX, but its contents were rolled up as part of the
724 * geometric repack.
725 *
726 * - In the existing non-kept packs list (if not using pack
727 * geometry), and marked as non-deleted.
728 */
729 if (string_list_has_string(include, pack_name)) {
730 continue;
731 } else if (geometry) {
732 struct strbuf buf = STRBUF_INIT;
733 uint32_t j;
734
735 for (j = 0; j < geometry->split; j++) {
736 strbuf_reset(&buf);
737 strbuf_addstr(&buf, pack_basename(geometry->pack[j]));
738 strbuf_strip_suffix(&buf, ".pack");
739 strbuf_addstr(&buf, ".idx");
740
741 if (!strcmp(pack_name, buf.buf)) {
742 strbuf_release(&buf);
743 break;
744 }
745 }
746
747 strbuf_release(&buf);
748
749 if (j < geometry->split)
750 continue;
751 } else {
752 struct string_list_item *item;
753
754 item = string_list_lookup(&existing->non_kept_packs,
755 pack_name);
756 if (item && !pack_is_marked_for_deletion(item))
757 continue;
758 }
759
760 /*
761 * If we got to this point, the MIDX includes some pack that we
762 * don't know about.
763 */
764 return 1;
765 }
766
767 return 0;
768 }
769
770 struct midx_snapshot_ref_data {
771 struct tempfile *f;
772 struct oidset seen;
773 int preferred;
774 };
775
776 static int midx_snapshot_ref_one(const char *refname UNUSED,
777 const char *referent UNUSED,
778 const struct object_id *oid,
779 int flag UNUSED, void *_data)
780 {
781 struct midx_snapshot_ref_data *data = _data;
782 struct object_id peeled;
783
784 if (!peel_iterated_oid(the_repository, oid, &peeled))
785 oid = &peeled;
786
787 if (oidset_insert(&data->seen, oid))
788 return 0; /* already seen */
789
790 if (odb_read_object_info(the_repository->objects, oid, NULL) != OBJ_COMMIT)
791 return 0;
792
793 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
794 oid_to_hex(oid));
795
796 return 0;
797 }
798
799 static void midx_snapshot_refs(struct tempfile *f)
800 {
801 struct midx_snapshot_ref_data data;
802 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
803
804 data.f = f;
805 data.preferred = 0;
806 oidset_init(&data.seen, 0);
807
808 if (!fdopen_tempfile(f, "w"))
809 die(_("could not open tempfile %s for writing"),
810 get_tempfile_path(f));
811
812 if (preferred) {
813 struct string_list_item *item;
814
815 data.preferred = 1;
816 for_each_string_list_item(item, preferred)
817 refs_for_each_ref_in(get_main_ref_store(the_repository),
818 item->string,
819 midx_snapshot_ref_one, &data);
820 data.preferred = 0;
821 }
822
823 refs_for_each_ref(get_main_ref_store(the_repository),
824 midx_snapshot_ref_one, &data);
825
826 if (close_tempfile_gently(f)) {
827 int save_errno = errno;
828 delete_tempfile(&f);
829 errno = save_errno;
830 die_errno(_("could not close refs snapshot tempfile"));
831 }
832
833 oidset_clear(&data.seen);
834 }
835
836 static void midx_included_packs(struct string_list *include,
837 struct existing_packs *existing,
838 char **midx_pack_names,
839 size_t midx_pack_names_nr,
840 struct string_list *names,
841 struct pack_geometry *geometry)
842 {
843 struct string_list_item *item;
844 struct strbuf buf = STRBUF_INIT;
845
846 for_each_string_list_item(item, &existing->kept_packs) {
847 strbuf_reset(&buf);
848 strbuf_addf(&buf, "%s.idx", item->string);
849 string_list_insert(include, buf.buf);
850 }
851
852 for_each_string_list_item(item, names) {
853 strbuf_reset(&buf);
854 strbuf_addf(&buf, "pack-%s.idx", item->string);
855 string_list_insert(include, buf.buf);
856 }
857
858 if (geometry->split_factor) {
859 uint32_t i;
860
861 for (i = geometry->split; i < geometry->pack_nr; i++) {
862 struct packed_git *p = geometry->pack[i];
863
864 /*
865 * The multi-pack index never refers to packfiles part
866 * of an alternate object database, so we skip these.
867 * While git-multi-pack-index(1) would silently ignore
868 * them anyway, this allows us to skip executing the
869 * command completely when we have only non-local
870 * packfiles.
871 */
872 if (!p->pack_local)
873 continue;
874
875 strbuf_reset(&buf);
876 strbuf_addstr(&buf, pack_basename(p));
877 strbuf_strip_suffix(&buf, ".pack");
878 strbuf_addstr(&buf, ".idx");
879
880 string_list_insert(include, buf.buf);
881 }
882 } else {
883 for_each_string_list_item(item, &existing->non_kept_packs) {
884 if (pack_is_marked_for_deletion(item))
885 continue;
886
887 strbuf_reset(&buf);
888 strbuf_addf(&buf, "%s.idx", item->string);
889 string_list_insert(include, buf.buf);
890 }
891 }
892
893 if (midx_must_contain_cruft ||
894 midx_has_unknown_packs(midx_pack_names, midx_pack_names_nr,
895 include, geometry, existing)) {
896 /*
897 * If there are one or more unknown pack(s) present (see
898 * midx_has_unknown_packs() for what makes a pack
899 * "unknown") in the MIDX before the repack, keep them
900 * as they may be required to form a reachability
901 * closure if the MIDX is bitmapped.
902 *
903 * For example, a cruft pack can be required to form a
904 * reachability closure if the MIDX is bitmapped and one
905 * or more of the bitmap's selected commits reaches a
906 * once-cruft object that was later made reachable.
907 */
908 for_each_string_list_item(item, &existing->cruft_packs) {
909 /*
910 * When doing a --geometric repack, there is no
911 * need to check for deleted packs, since we're
912 * by definition not doing an ALL_INTO_ONE
913 * repack (hence no packs will be deleted).
914 * Otherwise we must check for and exclude any
915 * packs which are enqueued for deletion.
916 *
917 * So we could omit the conditional below in the
918 * --geometric case, but doing so is unnecessary
919 * since no packs are marked as pending
920 * deletion (since we only call
921 * `mark_packs_for_deletion()` when doing an
922 * all-into-one repack).
923 */
924 if (pack_is_marked_for_deletion(item))
925 continue;
926
927 strbuf_reset(&buf);
928 strbuf_addf(&buf, "%s.idx", item->string);
929 string_list_insert(include, buf.buf);
930 }
931 } else {
932 /*
933 * Modern versions of Git (with the appropriate
934 * configuration setting) will write new copies of
935 * once-cruft objects when doing a --geometric repack.
936 *
937 * If the MIDX has no cruft pack, new packs written
938 * during a --geometric repack will not rely on the
939 * cruft pack to form a reachability closure, so we can
940 * avoid including them in the MIDX in that case.
941 */
942 ;
943 }
944
945 strbuf_release(&buf);
946 }
947
948 static int write_midx_included_packs(struct string_list *include,
949 struct pack_geometry *geometry,
950 struct string_list *names,
951 const char *refs_snapshot,
952 int show_progress, int write_bitmaps)
953 {
954 struct child_process cmd = CHILD_PROCESS_INIT;
955 struct string_list_item *item;
956 struct packed_git *preferred = get_preferred_pack(geometry);
957 FILE *in;
958 int ret;
959
960 if (!include->nr)
961 return 0;
962
963 cmd.in = -1;
964 cmd.git_cmd = 1;
965
966 strvec_push(&cmd.args, "multi-pack-index");
967 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
968
969 if (show_progress)
970 strvec_push(&cmd.args, "--progress");
971 else
972 strvec_push(&cmd.args, "--no-progress");
973
974 if (write_bitmaps)
975 strvec_push(&cmd.args, "--bitmap");
976
977 if (preferred)
978 strvec_pushf(&cmd.args, "--preferred-pack=%s",
979 pack_basename(preferred));
980 else if (names->nr) {
981 /* The largest pack was repacked, meaning that either
982 * one or two packs exist depending on whether the
983 * repository has a cruft pack or not.
984 *
985 * Select the non-cruft one as preferred to encourage
986 * pack-reuse among packs containing reachable objects
987 * over unreachable ones.
988 *
989 * (Note we could write multiple packs here if
990 * `--max-pack-size` was given, but any one of them
991 * will suffice, so pick the first one.)
992 */
993 for_each_string_list_item(item, names) {
994 struct generated_pack_data *data = item->util;
995 if (has_pack_ext(data, ".mtimes"))
996 continue;
997
998 strvec_pushf(&cmd.args, "--preferred-pack=pack-%s.pack",
999 item->string);
1000 break;
1001 }
1002 } else {
1003 /*
1004 * No packs were kept, and no packs were written. The
1005 * only thing remaining are .keep packs (unless
1006 * --pack-kept-objects was given).
1007 *
1008 * Set the `--preferred-pack` arbitrarily here.
1009 */
1010 ;
1011 }
1012
1013 if (refs_snapshot)
1014 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
1015
1016 ret = start_command(&cmd);
1017 if (ret)
1018 return ret;
1019
1020 in = xfdopen(cmd.in, "w");
1021 for_each_string_list_item(item, include)
1022 fprintf(in, "%s\n", item->string);
1023 fclose(in);
1024
1025 return finish_command(&cmd);
1026 }
1027
1028 static void remove_redundant_bitmaps(struct string_list *include,
1029 const char *packdir)
1030 {
1031 struct strbuf path = STRBUF_INIT;
1032 struct string_list_item *item;
1033 size_t packdir_len;
1034
1035 strbuf_addstr(&path, packdir);
1036 strbuf_addch(&path, '/');
1037 packdir_len = path.len;
1038
1039 /*
1040 * Remove any pack bitmaps corresponding to packs which are now
1041 * included in the MIDX.
1042 */
1043 for_each_string_list_item(item, include) {
1044 strbuf_addstr(&path, item->string);
1045 strbuf_strip_suffix(&path, ".idx");
1046 strbuf_addstr(&path, ".bitmap");
1047
1048 if (unlink(path.buf) && errno != ENOENT)
1049 warning_errno(_("could not remove stale bitmap: %s"),
1050 path.buf);
1051
1052 strbuf_setlen(&path, packdir_len);
1053 }
1054 strbuf_release(&path);
1055 }
1056
1057 static int finish_pack_objects_cmd(struct child_process *cmd,
1058 struct string_list *names,
1059 int local)
1060 {
1061 FILE *out;
1062 struct strbuf line = STRBUF_INIT;
1063
1064 out = xfdopen(cmd->out, "r");
1065 while (strbuf_getline_lf(&line, out) != EOF) {
1066 struct string_list_item *item;
1067
1068 if (line.len != the_hash_algo->hexsz)
1069 die(_("repack: Expecting full hex object ID lines only "
1070 "from pack-objects."));
1071 /*
1072 * Avoid putting packs written outside of the repository in the
1073 * list of names.
1074 */
1075 if (local) {
1076 item = string_list_append(names, line.buf);
1077 item->util = populate_pack_exts(line.buf);
1078 }
1079 }
1080 fclose(out);
1081
1082 strbuf_release(&line);
1083
1084 return finish_command(cmd);
1085 }
1086
1087 static int write_filtered_pack(const struct pack_objects_args *args,
1088 const char *destination,
1089 const char *pack_prefix,
1090 struct existing_packs *existing,
1091 struct string_list *names)
1092 {
1093 struct child_process cmd = CHILD_PROCESS_INIT;
1094 struct string_list_item *item;
1095 FILE *in;
1096 int ret;
1097 const char *caret;
1098 const char *scratch;
1099 int local = skip_prefix(destination, packdir, &scratch);
1100
1101 prepare_pack_objects(&cmd, args, destination);
1102
1103 strvec_push(&cmd.args, "--stdin-packs");
1104
1105 if (!pack_kept_objects)
1106 strvec_push(&cmd.args, "--honor-pack-keep");
1107 for_each_string_list_item(item, &existing->kept_packs)
1108 strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
1109
1110 cmd.in = -1;
1111
1112 ret = start_command(&cmd);
1113 if (ret)
1114 return ret;
1115
1116 /*
1117 * Here 'names' contains only the pack(s) that were just
1118 * written, which is exactly the packs we want to keep. Also
1119 * 'existing_kept_packs' already contains the packs in
1120 * 'keep_pack_list'.
1121 */
1122 in = xfdopen(cmd.in, "w");
1123 for_each_string_list_item(item, names)
1124 fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
1125 for_each_string_list_item(item, &existing->non_kept_packs)
1126 fprintf(in, "%s.pack\n", item->string);
1127 for_each_string_list_item(item, &existing->cruft_packs)
1128 fprintf(in, "%s.pack\n", item->string);
1129 caret = pack_kept_objects ? "" : "^";
1130 for_each_string_list_item(item, &existing->kept_packs)
1131 fprintf(in, "%s%s.pack\n", caret, item->string);
1132 fclose(in);
1133
1134 return finish_pack_objects_cmd(&cmd, names, local);
1135 }
1136
1137 static void combine_small_cruft_packs(FILE *in, size_t combine_cruft_below_size,
1138 struct existing_packs *existing)
1139 {
1140 struct packed_git *p;
1141 struct strbuf buf = STRBUF_INIT;
1142 size_t i;
1143
1144 for (p = get_all_packs(the_repository); p; p = p->next) {
1145 if (!(p->is_cruft && p->pack_local))
1146 continue;
1147
1148 strbuf_reset(&buf);
1149 strbuf_addstr(&buf, pack_basename(p));
1150 strbuf_strip_suffix(&buf, ".pack");
1151
1152 if (!string_list_has_string(&existing->cruft_packs, buf.buf))
1153 continue;
1154
1155 if (p->pack_size < combine_cruft_below_size) {
1156 fprintf(in, "-%s\n", pack_basename(p));
1157 } else {
1158 retain_cruft_pack(existing, p);
1159 fprintf(in, "%s\n", pack_basename(p));
1160 }
1161 }
1162
1163 for (i = 0; i < existing->non_kept_packs.nr; i++)
1164 fprintf(in, "-%s.pack\n",
1165 existing->non_kept_packs.items[i].string);
1166
1167 strbuf_release(&buf);
1168 }
1169
1170 static int write_cruft_pack(const struct pack_objects_args *args,
1171 const char *destination,
1172 const char *pack_prefix,
1173 const char *cruft_expiration,
1174 unsigned long combine_cruft_below_size,
1175 struct string_list *names,
1176 struct existing_packs *existing)
1177 {
1178 struct child_process cmd = CHILD_PROCESS_INIT;
1179 struct string_list_item *item;
1180 FILE *in;
1181 int ret;
1182 const char *scratch;
1183 int local = skip_prefix(destination, packdir, &scratch);
1184
1185 prepare_pack_objects(&cmd, args, destination);
1186
1187 strvec_push(&cmd.args, "--cruft");
1188 if (cruft_expiration)
1189 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
1190 cruft_expiration);
1191
1192 strvec_push(&cmd.args, "--honor-pack-keep");
1193 strvec_push(&cmd.args, "--non-empty");
1194
1195 cmd.in = -1;
1196
1197 ret = start_command(&cmd);
1198 if (ret)
1199 return ret;
1200
1201 /*
1202 * names has a confusing double use: it both provides the list
1203 * of just-written new packs, and accepts the name of the cruft
1204 * pack we are writing.
1205 *
1206 * By the time it is read here, it contains only the pack(s)
1207 * that were just written, which is exactly the set of packs we
1208 * want to consider kept.
1209 *
1210 * If `--expire-to` is given, the double-use served by `names`
1211 * ensures that the pack written to `--expire-to` excludes any
1212 * objects contained in the cruft pack.
1213 */
1214 in = xfdopen(cmd.in, "w");
1215 for_each_string_list_item(item, names)
1216 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
1217 if (combine_cruft_below_size && !cruft_expiration) {
1218 combine_small_cruft_packs(in, combine_cruft_below_size,
1219 existing);
1220 } else {
1221 for_each_string_list_item(item, &existing->non_kept_packs)
1222 fprintf(in, "-%s.pack\n", item->string);
1223 for_each_string_list_item(item, &existing->cruft_packs)
1224 fprintf(in, "-%s.pack\n", item->string);
1225 }
1226 for_each_string_list_item(item, &existing->kept_packs)
1227 fprintf(in, "%s.pack\n", item->string);
1228 fclose(in);
1229
1230 return finish_pack_objects_cmd(&cmd, names, local);
1231 }
1232
1233 static const char *find_pack_prefix(const char *packdir, const char *packtmp)
1234 {
1235 const char *pack_prefix;
1236 if (!skip_prefix(packtmp, packdir, &pack_prefix))
1237 die(_("pack prefix %s does not begin with objdir %s"),
1238 packtmp, packdir);
1239 if (*pack_prefix == '/')
1240 pack_prefix++;
1241 return pack_prefix;
1242 }
1243
1244 int cmd_repack(int argc,
1245 const char **argv,
1246 const char *prefix,
1247 struct repository *repo UNUSED)
1248 {
1249 struct child_process cmd = CHILD_PROCESS_INIT;
1250 struct string_list_item *item;
1251 struct string_list names = STRING_LIST_INIT_DUP;
1252 struct existing_packs existing = EXISTING_PACKS_INIT;
1253 struct pack_geometry geometry = { 0 };
1254 struct tempfile *refs_snapshot = NULL;
1255 int i, ext, ret;
1256 int show_progress;
1257 char **midx_pack_names = NULL;
1258 size_t midx_pack_names_nr = 0;
1259
1260 /* variables to be filled by option parsing */
1261 int delete_redundant = 0;
1262 const char *unpack_unreachable = NULL;
1263 int keep_unreachable = 0;
1264 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
1265 struct pack_objects_args po_args = { 0 };
1266 struct pack_objects_args cruft_po_args = { 0 };
1267 int write_midx = 0;
1268 const char *cruft_expiration = NULL;
1269 const char *expire_to = NULL;
1270 const char *filter_to = NULL;
1271 const char *opt_window = NULL;
1272 const char *opt_window_memory = NULL;
1273 const char *opt_depth = NULL;
1274 const char *opt_threads = NULL;
1275 unsigned long combine_cruft_below_size = 0ul;
1276
1277 struct option builtin_repack_options[] = {
1278 OPT_BIT('a', NULL, &pack_everything,
1279 N_("pack everything in a single pack"), ALL_INTO_ONE),
1280 OPT_BIT('A', NULL, &pack_everything,
1281 N_("same as -a, and turn unreachable objects loose"),
1282 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
1283 OPT_BIT(0, "cruft", &pack_everything,
1284 N_("same as -a, pack unreachable cruft objects separately"),
1285 PACK_CRUFT),
1286 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
1287 N_("with --cruft, expire objects older than this")),
1288 OPT_UNSIGNED(0, "combine-cruft-below-size",
1289 &combine_cruft_below_size,
1290 N_("with --cruft, only repack cruft packs smaller than this")),
1291 OPT_UNSIGNED(0, "max-cruft-size", &cruft_po_args.max_pack_size,
1292 N_("with --cruft, limit the size of new cruft packs")),
1293 OPT_BOOL('d', NULL, &delete_redundant,
1294 N_("remove redundant packs, and run git-prune-packed")),
1295 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
1296 N_("pass --no-reuse-delta to git-pack-objects")),
1297 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
1298 N_("pass --no-reuse-object to git-pack-objects")),
1299 OPT_INTEGER(0, "name-hash-version", &po_args.name_hash_version,
1300 N_("specify the name hash version to use for grouping similar objects by path")),
1301 OPT_BOOL(0, "path-walk", &po_args.path_walk,
1302 N_("pass --path-walk to git-pack-objects")),
1303 OPT_NEGBIT('n', NULL, &run_update_server_info,
1304 N_("do not run git-update-server-info"), 1),
1305 OPT__QUIET(&po_args.quiet, N_("be quiet")),
1306 OPT_BOOL('l', "local", &po_args.local,
1307 N_("pass --local to git-pack-objects")),
1308 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
1309 N_("write bitmap index")),
1310 OPT_BOOL('i', "delta-islands", &use_delta_islands,
1311 N_("pass --delta-islands to git-pack-objects")),
1312 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
1313 N_("with -A, do not loosen objects older than this")),
1314 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
1315 N_("with -a, repack unreachable objects")),
1316 OPT_STRING(0, "window", &opt_window, N_("n"),
1317 N_("size of the window used for delta compression")),
1318 OPT_STRING(0, "window-memory", &opt_window_memory, N_("bytes"),
1319 N_("same as the above, but limit memory size instead of entries count")),
1320 OPT_STRING(0, "depth", &opt_depth, N_("n"),
1321 N_("limits the maximum delta depth")),
1322 OPT_STRING(0, "threads", &opt_threads, N_("n"),
1323 N_("limits the maximum number of threads")),
1324 OPT_UNSIGNED(0, "max-pack-size", &po_args.max_pack_size,
1325 N_("maximum size of each packfile")),
1326 OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options),
1327 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
1328 N_("repack objects in packs marked with .keep")),
1329 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
1330 N_("do not repack this pack")),
1331 OPT_INTEGER('g', "geometric", &geometry.split_factor,
1332 N_("find a geometric progression with factor <N>")),
1333 OPT_BOOL('m', "write-midx", &write_midx,
1334 N_("write a multi-pack index of the resulting packs")),
1335 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
1336 N_("pack prefix to store a pack containing pruned objects")),
1337 OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
1338 N_("pack prefix to store a pack containing filtered out objects")),
1339 OPT_END()
1340 };
1341
1342 list_objects_filter_init(&po_args.filter_options);
1343
1344 repo_config(the_repository, repack_config, &cruft_po_args);
1345
1346 argc = parse_options(argc, argv, prefix, builtin_repack_options,
1347 git_repack_usage, 0);
1348
1349 po_args.window = xstrdup_or_null(opt_window);
1350 po_args.window_memory = xstrdup_or_null(opt_window_memory);
1351 po_args.depth = xstrdup_or_null(opt_depth);
1352 po_args.threads = xstrdup_or_null(opt_threads);
1353
1354 if (delete_redundant && the_repository->repository_format_precious_objects)
1355 die(_("cannot delete packs in a precious-objects repo"));
1356
1357 die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
1358 keep_unreachable, "-k/--keep-unreachable",
1359 pack_everything & PACK_CRUFT, "--cruft");
1360
1361 if (pack_everything & PACK_CRUFT)
1362 pack_everything |= ALL_INTO_ONE;
1363
1364 if (write_bitmaps < 0) {
1365 if (!write_midx &&
1366 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
1367 write_bitmaps = 0;
1368 }
1369 if (pack_kept_objects < 0)
1370 pack_kept_objects = write_bitmaps > 0 && !write_midx;
1371
1372 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
1373 die(_(incremental_bitmap_conflict_error));
1374
1375 if (write_bitmaps && po_args.local &&
1376 odb_has_alternates(the_repository->objects)) {
1377 /*
1378 * When asked to do a local repack, but we have
1379 * packfiles that are inherited from an alternate, then
1380 * we cannot guarantee that the multi-pack-index would
1381 * have full coverage of all objects. We thus disable
1382 * writing bitmaps in that case.
1383 */
1384 warning(_("disabling bitmap writing, as some objects are not being packed"));
1385 write_bitmaps = 0;
1386 }
1387
1388 if (write_midx && write_bitmaps) {
1389 struct strbuf path = STRBUF_INIT;
1390
1391 strbuf_addf(&path, "%s/%s_XXXXXX", repo_get_object_directory(the_repository),
1392 "bitmap-ref-tips");
1393
1394 refs_snapshot = xmks_tempfile(path.buf);
1395 midx_snapshot_refs(refs_snapshot);
1396
1397 strbuf_release(&path);
1398 }
1399
1400 packdir = mkpathdup("%s/pack", repo_get_object_directory(the_repository));
1401 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1402 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1403
1404 collect_pack_filenames(&existing, &keep_pack_list);
1405
1406 if (geometry.split_factor) {
1407 if (pack_everything)
1408 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1409 init_pack_geometry(&geometry, &existing, &po_args);
1410 split_pack_geometry(&geometry);
1411 }
1412
1413 prepare_pack_objects(&cmd, &po_args, packtmp);
1414
1415 show_progress = !po_args.quiet && isatty(2);
1416
1417 strvec_push(&cmd.args, "--keep-true-parents");
1418 if (!pack_kept_objects)
1419 strvec_push(&cmd.args, "--honor-pack-keep");
1420 for (i = 0; i < keep_pack_list.nr; i++)
1421 strvec_pushf(&cmd.args, "--keep-pack=%s",
1422 keep_pack_list.items[i].string);
1423 strvec_push(&cmd.args, "--non-empty");
1424 if (!geometry.split_factor) {
1425 /*
1426 * We need to grab all reachable objects, including those that
1427 * are reachable from reflogs and the index.
1428 *
1429 * When repacking into a geometric progression of packs,
1430 * however, we ask 'git pack-objects --stdin-packs', and it is
1431 * not about packing objects based on reachability but about
1432 * repacking all the objects in specified packs and loose ones
1433 * (indeed, --stdin-packs is incompatible with these options).
1434 */
1435 strvec_push(&cmd.args, "--all");
1436 strvec_push(&cmd.args, "--reflog");
1437 strvec_push(&cmd.args, "--indexed-objects");
1438 }
1439 if (repo_has_promisor_remote(the_repository))
1440 strvec_push(&cmd.args, "--exclude-promisor-objects");
1441 if (!write_midx) {
1442 if (write_bitmaps > 0)
1443 strvec_push(&cmd.args, "--write-bitmap-index");
1444 else if (write_bitmaps < 0)
1445 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1446 }
1447 if (use_delta_islands)
1448 strvec_push(&cmd.args, "--delta-islands");
1449
1450 if (pack_everything & ALL_INTO_ONE) {
1451 repack_promisor_objects(&po_args, &names);
1452
1453 if (has_existing_non_kept_packs(&existing) &&
1454 delete_redundant &&
1455 !(pack_everything & PACK_CRUFT)) {
1456 for_each_string_list_item(item, &names) {
1457 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1458 packtmp_name, item->string);
1459 }
1460 if (unpack_unreachable) {
1461 strvec_pushf(&cmd.args,
1462 "--unpack-unreachable=%s",
1463 unpack_unreachable);
1464 } else if (pack_everything & LOOSEN_UNREACHABLE) {
1465 strvec_push(&cmd.args,
1466 "--unpack-unreachable");
1467 } else if (keep_unreachable) {
1468 strvec_push(&cmd.args, "--keep-unreachable");
1469 }
1470 }
1471
1472 if (keep_unreachable && delete_redundant &&
1473 !(pack_everything & PACK_CRUFT))
1474 strvec_push(&cmd.args, "--pack-loose-unreachable");
1475 } else if (geometry.split_factor) {
1476 if (midx_must_contain_cruft)
1477 strvec_push(&cmd.args, "--stdin-packs");
1478 else
1479 strvec_push(&cmd.args, "--stdin-packs=follow");
1480 strvec_push(&cmd.args, "--unpacked");
1481 } else {
1482 strvec_push(&cmd.args, "--unpacked");
1483 strvec_push(&cmd.args, "--incremental");
1484 }
1485
1486 if (po_args.filter_options.choice)
1487 strvec_pushf(&cmd.args, "--filter=%s",
1488 expand_list_objects_filter_spec(&po_args.filter_options));
1489 else if (filter_to)
1490 die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
1491
1492 if (geometry.split_factor)
1493 cmd.in = -1;
1494 else
1495 cmd.no_stdin = 1;
1496
1497 ret = start_command(&cmd);
1498 if (ret)
1499 goto cleanup;
1500
1501 if (geometry.split_factor) {
1502 FILE *in = xfdopen(cmd.in, "w");
1503 /*
1504 * The resulting pack should contain all objects in packs that
1505 * are going to be rolled up, but exclude objects in packs which
1506 * are being left alone.
1507 */
1508 for (i = 0; i < geometry.split; i++)
1509 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1510 for (i = geometry.split; i < geometry.pack_nr; i++)
1511 fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
1512 fclose(in);
1513 }
1514
1515 ret = finish_pack_objects_cmd(&cmd, &names, 1);
1516 if (ret)
1517 goto cleanup;
1518
1519 if (!names.nr) {
1520 if (!po_args.quiet)
1521 printf_ln(_("Nothing new to pack."));
1522 /*
1523 * If we didn't write any new packs, the non-cruft packs
1524 * may refer to once-unreachable objects in the cruft
1525 * pack(s).
1526 *
1527 * If there isn't already a MIDX, the one we write
1528 * must include the cruft pack(s), in case the
1529 * non-cruft pack(s) refer to once-cruft objects.
1530 *
1531 * If there is already a MIDX, we can punt here, since
1532 * midx_has_unknown_packs() will make the decision for
1533 * us.
1534 */
1535 if (!get_multi_pack_index(the_repository->objects->sources))
1536 midx_must_contain_cruft = 1;
1537 }
1538
1539 if (pack_everything & PACK_CRUFT) {
1540 const char *pack_prefix = find_pack_prefix(packdir, packtmp);
1541
1542 if (!cruft_po_args.window)
1543 cruft_po_args.window = xstrdup_or_null(po_args.window);
1544 if (!cruft_po_args.window_memory)
1545 cruft_po_args.window_memory = xstrdup_or_null(po_args.window_memory);
1546 if (!cruft_po_args.depth)
1547 cruft_po_args.depth = xstrdup_or_null(po_args.depth);
1548 if (!cruft_po_args.threads)
1549 cruft_po_args.threads = xstrdup_or_null(po_args.threads);
1550 if (!cruft_po_args.max_pack_size)
1551 cruft_po_args.max_pack_size = po_args.max_pack_size;
1552
1553 cruft_po_args.local = po_args.local;
1554 cruft_po_args.quiet = po_args.quiet;
1555
1556 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1557 cruft_expiration,
1558 combine_cruft_below_size, &names,
1559 &existing);
1560 if (ret)
1561 goto cleanup;
1562
1563 if (delete_redundant && expire_to) {
1564 /*
1565 * If `--expire-to` is given with `-d`, it's possible
1566 * that we're about to prune some objects. With cruft
1567 * packs, pruning is implicit: any objects from existing
1568 * packs that weren't picked up by new packs are removed
1569 * when their packs are deleted.
1570 *
1571 * Generate an additional cruft pack, with one twist:
1572 * `names` now includes the name of the cruft pack
1573 * written in the previous step. So the contents of
1574 * _this_ cruft pack exclude everything contained in the
1575 * existing cruft pack (that is, all of the unreachable
1576 * objects which are no older than
1577 * `--cruft-expiration`).
1578 *
1579 * To make this work, cruft_expiration must become NULL
1580 * so that this cruft pack doesn't actually prune any
1581 * objects. If it were non-NULL, this call would always
1582 * generate an empty pack (since every object not in the
1583 * cruft pack generated above will have an mtime older
1584 * than the expiration).
1585 *
1586 * Pretend we don't have a `--combine-cruft-below-size`
1587 * argument, since we're not selectively combining
1588 * anything based on size to generate the limbo cruft
1589 * pack, but rather removing all cruft packs from the
1590 * main repository regardless of size.
1591 */
1592 ret = write_cruft_pack(&cruft_po_args, expire_to,
1593 pack_prefix,
1594 NULL,
1595 0ul,
1596 &names,
1597 &existing);
1598 if (ret)
1599 goto cleanup;
1600 }
1601 }
1602
1603 if (po_args.filter_options.choice) {
1604 if (!filter_to)
1605 filter_to = packtmp;
1606
1607 ret = write_filtered_pack(&po_args,
1608 filter_to,
1609 find_pack_prefix(packdir, packtmp),
1610 &existing,
1611 &names);
1612 if (ret)
1613 goto cleanup;
1614 }
1615
1616 string_list_sort(&names);
1617
1618 if (get_multi_pack_index(the_repository->objects->sources)) {
1619 struct multi_pack_index *m =
1620 get_multi_pack_index(the_repository->objects->sources);
1621
1622 ALLOC_ARRAY(midx_pack_names,
1623 m->num_packs + m->num_packs_in_base);
1624
1625 for (; m; m = m->base_midx)
1626 for (uint32_t i = 0; i < m->num_packs; i++)
1627 midx_pack_names[midx_pack_names_nr++] =
1628 xstrdup(m->pack_names[i]);
1629 }
1630
1631 close_object_store(the_repository->objects);
1632
1633 /*
1634 * Ok we have prepared all new packfiles.
1635 */
1636 for_each_string_list_item(item, &names) {
1637 struct generated_pack_data *data = item->util;
1638
1639 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1640 char *fname;
1641
1642 fname = mkpathdup("%s/pack-%s%s",
1643 packdir, item->string, exts[ext].name);
1644
1645 if (data->tempfiles[ext]) {
1646 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1647 struct stat statbuffer;
1648
1649 if (!stat(fname_old, &statbuffer)) {
1650 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1651 chmod(fname_old, statbuffer.st_mode);
1652 }
1653
1654 if (rename_tempfile(&data->tempfiles[ext], fname))
1655 die_errno(_("renaming pack to '%s' failed"), fname);
1656 } else if (!exts[ext].optional)
1657 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1658 exts[ext].name, packtmp, item->string);
1659 else if (unlink(fname) < 0 && errno != ENOENT)
1660 die_errno(_("could not unlink: %s"), fname);
1661
1662 free(fname);
1663 }
1664 }
1665 /* End of pack replacement. */
1666
1667 if (delete_redundant && pack_everything & ALL_INTO_ONE)
1668 mark_packs_for_deletion(&existing, &names);
1669
1670 if (write_midx) {
1671 struct string_list include = STRING_LIST_INIT_DUP;
1672 midx_included_packs(&include, &existing, midx_pack_names,
1673 midx_pack_names_nr, &names, &geometry);
1674
1675 ret = write_midx_included_packs(&include, &geometry, &names,
1676 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1677 show_progress, write_bitmaps > 0);
1678
1679 if (!ret && write_bitmaps)
1680 remove_redundant_bitmaps(&include, packdir);
1681
1682 string_list_clear(&include, 0);
1683
1684 if (ret)
1685 goto cleanup;
1686 }
1687
1688 reprepare_packed_git(the_repository);
1689
1690 if (delete_redundant) {
1691 int opts = 0;
1692 remove_redundant_existing_packs(&existing);
1693
1694 if (geometry.split_factor)
1695 geometry_remove_redundant_packs(&geometry, &names,
1696 &existing);
1697 if (show_progress)
1698 opts |= PRUNE_PACKED_VERBOSE;
1699 prune_packed_objects(opts);
1700
1701 if (!keep_unreachable &&
1702 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1703 unpack_unreachable) &&
1704 is_repository_shallow(the_repository))
1705 prune_shallow(PRUNE_QUICK);
1706 }
1707
1708 if (run_update_server_info)
1709 update_server_info(the_repository, 0);
1710
1711 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1712 unsigned flags = 0;
1713 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
1714 flags |= MIDX_WRITE_INCREMENTAL;
1715 write_midx_file(the_repository->objects->sources,
1716 NULL, NULL, flags);
1717 }
1718
1719 cleanup:
1720 string_list_clear(&keep_pack_list, 0);
1721 string_list_clear(&names, 1);
1722 existing_packs_release(&existing);
1723 free_pack_geometry(&geometry);
1724 for (size_t i = 0; i < midx_pack_names_nr; i++)
1725 free(midx_pack_names[i]);
1726 free(midx_pack_names);
1727 pack_objects_args_release(&po_args);
1728 pack_objects_args_release(&cruft_po_args);
1729
1730 return ret;
1731 }