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