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