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