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