]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/repack.c
repack: refactor finishing pack-objects command
[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
ff8504e4
CC
809static int finish_pack_objects_cmd(struct child_process *cmd,
810 struct string_list *names,
811 int local)
812{
813 FILE *out;
814 struct strbuf line = STRBUF_INIT;
815
816 out = xfdopen(cmd->out, "r");
817 while (strbuf_getline_lf(&line, out) != EOF) {
818 struct string_list_item *item;
819
820 if (line.len != the_hash_algo->hexsz)
821 die(_("repack: Expecting full hex object ID lines only "
822 "from pack-objects."));
823 /*
824 * Avoid putting packs written outside of the repository in the
825 * list of names.
826 */
827 if (local) {
828 item = string_list_append(names, line.buf);
829 item->util = populate_pack_exts(line.buf);
830 }
831 }
832 fclose(out);
833
834 strbuf_release(&line);
835
836 return finish_command(cmd);
837}
838
f9825d1c 839static int write_cruft_pack(const struct pack_objects_args *args,
c12cda47 840 const char *destination,
f9825d1c 841 const char *pack_prefix,
eddad368 842 const char *cruft_expiration,
f9825d1c 843 struct string_list *names,
e2b43831 844 struct existing_packs *existing)
f9825d1c
TB
845{
846 struct child_process cmd = CHILD_PROCESS_INIT;
f9825d1c 847 struct string_list_item *item;
ff8504e4 848 FILE *in;
f9825d1c 849 int ret;
c12cda47
TB
850 const char *scratch;
851 int local = skip_prefix(destination, packdir, &scratch);
f9825d1c 852
c12cda47 853 prepare_pack_objects(&cmd, args, destination);
f9825d1c
TB
854
855 strvec_push(&cmd.args, "--cruft");
856 if (cruft_expiration)
857 strvec_pushf(&cmd.args, "--cruft-expiration=%s",
858 cruft_expiration);
859
860 strvec_push(&cmd.args, "--honor-pack-keep");
861 strvec_push(&cmd.args, "--non-empty");
f9825d1c
TB
862
863 cmd.in = -1;
864
865 ret = start_command(&cmd);
866 if (ret)
867 return ret;
868
869 /*
870 * names has a confusing double use: it both provides the list
871 * of just-written new packs, and accepts the name of the cruft
872 * pack we are writing.
873 *
874 * By the time it is read here, it contains only the pack(s)
875 * that were just written, which is exactly the set of packs we
876 * want to consider kept.
91badeba
TB
877 *
878 * If `--expire-to` is given, the double-use served by `names`
879 * ensures that the pack written to `--expire-to` excludes any
880 * objects contained in the cruft pack.
f9825d1c
TB
881 */
882 in = xfdopen(cmd.in, "w");
883 for_each_string_list_item(item, names)
884 fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
e2b43831 885 for_each_string_list_item(item, &existing->non_kept_packs)
f9825d1c 886 fprintf(in, "-%s.pack\n", item->string);
eabfaf8e 887 for_each_string_list_item(item, &existing->cruft_packs)
f9825d1c 888 fprintf(in, "-%s.pack\n", item->string);
e2b43831 889 for_each_string_list_item(item, &existing->kept_packs)
f9825d1c
TB
890 fprintf(in, "%s.pack\n", item->string);
891 fclose(in);
892
ff8504e4 893 return finish_pack_objects_cmd(&cmd, names, local);
f9825d1c
TB
894}
895
a1bbc6c0
SB
896int cmd_repack(int argc, const char **argv, const char *prefix)
897{
d3180279 898 struct child_process cmd = CHILD_PROCESS_INIT;
a1bbc6c0 899 struct string_list_item *item;
a1bbc6c0 900 struct string_list names = STRING_LIST_INIT_DUP;
e2b43831 901 struct existing_packs existing = EXISTING_PACKS_INIT;
99d51978 902 struct pack_geometry geometry = { 0 };
324efc90 903 struct tempfile *refs_snapshot = NULL;
2fcb03b5 904 int i, ext, ret;
47ca93d0 905 int show_progress;
a1bbc6c0
SB
906
907 /* variables to be filled by option parsing */
a1bbc6c0 908 int delete_redundant = 0;
aa8bd519 909 const char *unpack_unreachable = NULL;
905f27b8 910 int keep_unreachable = 0;
ed7e5fc3 911 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
2b958e79 912 struct pack_objects_args po_args = {NULL};
4571324b 913 struct pack_objects_args cruft_po_args = {NULL};
1d89d88d 914 int write_midx = 0;
eddad368 915 const char *cruft_expiration = NULL;
91badeba 916 const char *expire_to = NULL;
a1bbc6c0
SB
917
918 struct option builtin_repack_options[] = {
919 OPT_BIT('a', NULL, &pack_everything,
920 N_("pack everything in a single pack"), ALL_INTO_ONE),
921 OPT_BIT('A', NULL, &pack_everything,
922 N_("same as -a, and turn unreachable objects loose"),
923 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
f9825d1c
TB
924 OPT_BIT(0, "cruft", &pack_everything,
925 N_("same as -a, pack unreachable cruft objects separately"),
926 PACK_CRUFT),
927 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
c512f311 928 N_("with --cruft, expire objects older than this")),
a1bbc6c0
SB
929 OPT_BOOL('d', NULL, &delete_redundant,
930 N_("remove redundant packs, and run git-prune-packed")),
2b958e79 931 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
a1bbc6c0 932 N_("pass --no-reuse-delta to git-pack-objects")),
2b958e79 933 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
a1bbc6c0 934 N_("pass --no-reuse-object to git-pack-objects")),
64a6151d
PS
935 OPT_NEGBIT('n', NULL, &run_update_server_info,
936 N_("do not run git-update-server-info"), 1),
2b958e79
JT
937 OPT__QUIET(&po_args.quiet, N_("be quiet")),
938 OPT_BOOL('l', "local", &po_args.local,
a1bbc6c0 939 N_("pass --local to git-pack-objects")),
d078d85b 940 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
5cf2741c 941 N_("write bitmap index")),
16d75fa4
JK
942 OPT_BOOL('i', "delta-islands", &use_delta_islands,
943 N_("pass --delta-islands to git-pack-objects")),
a1bbc6c0
SB
944 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
945 N_("with -A, do not loosen objects older than this")),
905f27b8
JK
946 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
947 N_("with -a, repack unreachable objects")),
2b958e79 948 OPT_STRING(0, "window", &po_args.window, N_("n"),
a1bbc6c0 949 N_("size of the window used for delta compression")),
2b958e79 950 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
a1bbc6c0 951 N_("same as the above, but limit memory size instead of entries count")),
2b958e79 952 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
a1bbc6c0 953 N_("limits the maximum delta depth")),
2b958e79 954 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
40bcf318 955 N_("limits the maximum number of threads")),
2b958e79 956 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
a1bbc6c0 957 N_("maximum size of each packfile")),
ee34a2be
JK
958 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
959 N_("repack objects in packs marked with .keep")),
ed7e5fc3
NTND
960 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
961 N_("do not repack this pack")),
99d51978 962 OPT_INTEGER('g', "geometric", &geometry.split_factor,
0fabafd0 963 N_("find a geometric progression with factor <N>")),
1d89d88d
TB
964 OPT_BOOL('m', "write-midx", &write_midx,
965 N_("write a multi-pack index of the resulting packs")),
91badeba
TB
966 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
967 N_("pack prefix to store a pack containing pruned objects")),
a1bbc6c0
SB
968 OPT_END()
969 };
970
4571324b 971 git_config(repack_config, &cruft_po_args);
a1bbc6c0
SB
972
973 argc = parse_options(argc, argv, prefix, builtin_repack_options,
974 git_repack_usage, 0);
975
067fbd41
JK
976 if (delete_redundant && repository_format_precious_objects)
977 die(_("cannot delete packs in a precious-objects repo"));
978
905f27b8
JK
979 if (keep_unreachable &&
980 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
12909b6b 981 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
905f27b8 982
f9825d1c
TB
983 if (pack_everything & PACK_CRUFT) {
984 pack_everything |= ALL_INTO_ONE;
985
986 if (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))
987 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
988 if (keep_unreachable)
989 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
990 }
991
73284822 992 if (write_bitmaps < 0) {
1d89d88d
TB
993 if (!write_midx &&
994 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
25575015 995 write_bitmaps = 0;
ff1e653c
TB
996 } else if (write_bitmaps &&
997 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
998 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
999 write_bitmaps = 0;
73284822 1000 }
ee34a2be 1001 if (pack_kept_objects < 0)
e4d0c11c 1002 pack_kept_objects = write_bitmaps > 0 && !write_midx;
ee34a2be 1003
1d89d88d 1004 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
1c409a70
DT
1005 die(_(incremental_bitmap_conflict_error));
1006
d85cd187
PS
1007 if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1008 /*
1009 * When asked to do a local repack, but we have
1010 * packfiles that are inherited from an alternate, then
1011 * we cannot guarantee that the multi-pack-index would
1012 * have full coverage of all objects. We thus disable
1013 * writing bitmaps in that case.
1014 */
1015 warning(_("disabling bitmap writing, as some objects are not being packed"));
1016 write_bitmaps = 0;
1017 }
1018
324efc90
TB
1019 if (write_midx && write_bitmaps) {
1020 struct strbuf path = STRBUF_INIT;
1021
1022 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1023 "bitmap-ref-tips");
1024
1025 refs_snapshot = xmks_tempfile(path.buf);
1026 midx_snapshot_refs(refs_snapshot);
1027
1028 strbuf_release(&path);
1029 }
1030
4b5a808b
VD
1031 packdir = mkpathdup("%s/pack", get_object_directory());
1032 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1033 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1034
e2b43831 1035 collect_pack_filenames(&existing, &keep_pack_list);
4b5a808b 1036
99d51978 1037 if (geometry.split_factor) {
0fabafd0 1038 if (pack_everything)
12909b6b 1039 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
e2b43831 1040 init_pack_geometry(&geometry, &existing, &po_args);
99d51978 1041 split_pack_geometry(&geometry);
0fabafd0
TB
1042 }
1043
4e7b65ba 1044 prepare_pack_objects(&cmd, &po_args, packtmp);
2b958e79 1045
47ca93d0
DS
1046 show_progress = !po_args.quiet && isatty(2);
1047
22f9b7f3 1048 strvec_push(&cmd.args, "--keep-true-parents");
ee34a2be 1049 if (!pack_kept_objects)
22f9b7f3 1050 strvec_push(&cmd.args, "--honor-pack-keep");
ed7e5fc3 1051 for (i = 0; i < keep_pack_list.nr; i++)
22f9b7f3 1052 strvec_pushf(&cmd.args, "--keep-pack=%s",
f6d8942b 1053 keep_pack_list.items[i].string);
22f9b7f3 1054 strvec_push(&cmd.args, "--non-empty");
99d51978 1055 if (!geometry.split_factor) {
0fabafd0 1056 /*
ccae01ca
JH
1057 * We need to grab all reachable objects, including those that
1058 * are reachable from reflogs and the index.
0fabafd0 1059 *
ccae01ca
JH
1060 * When repacking into a geometric progression of packs,
1061 * however, we ask 'git pack-objects --stdin-packs', and it is
1062 * not about packing objects based on reachability but about
1063 * repacking all the objects in specified packs and loose ones
1064 * (indeed, --stdin-packs is incompatible with these options).
0fabafd0
TB
1065 */
1066 strvec_push(&cmd.args, "--all");
1067 strvec_push(&cmd.args, "--reflog");
1068 strvec_push(&cmd.args, "--indexed-objects");
1069 }
a5183d76 1070 if (repo_has_promisor_remote(the_repository))
22f9b7f3 1071 strvec_push(&cmd.args, "--exclude-promisor-objects");
1d89d88d
TB
1072 if (!write_midx) {
1073 if (write_bitmaps > 0)
1074 strvec_push(&cmd.args, "--write-bitmap-index");
1075 else if (write_bitmaps < 0)
1076 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1077 }
16d75fa4 1078 if (use_delta_islands)
22f9b7f3 1079 strvec_push(&cmd.args, "--delta-islands");
a1bbc6c0 1080
90f838bc 1081 if (pack_everything & ALL_INTO_ONE) {
5d19e813
JT
1082 repack_promisor_objects(&po_args, &names);
1083
4bbfb003
TB
1084 if (has_existing_non_kept_packs(&existing) &&
1085 delete_redundant &&
f9825d1c 1086 !(pack_everything & PACK_CRUFT)) {
a643157d
RS
1087 for_each_string_list_item(item, &names) {
1088 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1089 packtmp_name, item->string);
1090 }
8d422993 1091 if (unpack_unreachable) {
22f9b7f3 1092 strvec_pushf(&cmd.args,
f6d8942b
JK
1093 "--unpack-unreachable=%s",
1094 unpack_unreachable);
8d422993 1095 } else if (pack_everything & LOOSEN_UNREACHABLE) {
22f9b7f3 1096 strvec_push(&cmd.args,
f6d8942b 1097 "--unpack-unreachable");
905f27b8 1098 } else if (keep_unreachable) {
22f9b7f3
JK
1099 strvec_push(&cmd.args, "--keep-unreachable");
1100 strvec_push(&cmd.args, "--pack-loose-unreachable");
8d422993 1101 }
a1bbc6c0 1102 }
99d51978 1103 } else if (geometry.split_factor) {
0fabafd0
TB
1104 strvec_push(&cmd.args, "--stdin-packs");
1105 strvec_push(&cmd.args, "--unpacked");
a1bbc6c0 1106 } else {
22f9b7f3
JK
1107 strvec_push(&cmd.args, "--unpacked");
1108 strvec_push(&cmd.args, "--incremental");
a1bbc6c0
SB
1109 }
1110
99d51978 1111 if (geometry.split_factor)
0fabafd0
TB
1112 cmd.in = -1;
1113 else
1114 cmd.no_stdin = 1;
a1bbc6c0
SB
1115
1116 ret = start_command(&cmd);
1117 if (ret)
90428ddc 1118 goto cleanup;
a1bbc6c0 1119
99d51978 1120 if (geometry.split_factor) {
0fabafd0
TB
1121 FILE *in = xfdopen(cmd.in, "w");
1122 /*
1123 * The resulting pack should contain all objects in packs that
1124 * are going to be rolled up, but exclude objects in packs which
1125 * are being left alone.
1126 */
99d51978
TB
1127 for (i = 0; i < geometry.split; i++)
1128 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1129 for (i = geometry.split; i < geometry.pack_nr; i++)
1130 fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
0fabafd0
TB
1131 fclose(in);
1132 }
1133
ff8504e4 1134 ret = finish_pack_objects_cmd(&cmd, &names, 1);
a1bbc6c0 1135 if (ret)
90428ddc 1136 goto cleanup;
a1bbc6c0 1137
2b958e79 1138 if (!names.nr && !po_args.quiet)
c83d950e 1139 printf_ln(_("Nothing new to pack."));
a1bbc6c0 1140
f9825d1c
TB
1141 if (pack_everything & PACK_CRUFT) {
1142 const char *pack_prefix;
1143 if (!skip_prefix(packtmp, packdir, &pack_prefix))
1144 die(_("pack prefix %s does not begin with objdir %s"),
1145 packtmp, packdir);
1146 if (*pack_prefix == '/')
1147 pack_prefix++;
1148
4571324b
TB
1149 if (!cruft_po_args.window)
1150 cruft_po_args.window = po_args.window;
1151 if (!cruft_po_args.window_memory)
1152 cruft_po_args.window_memory = po_args.window_memory;
1153 if (!cruft_po_args.depth)
1154 cruft_po_args.depth = po_args.depth;
1155 if (!cruft_po_args.threads)
1156 cruft_po_args.threads = po_args.threads;
61568efa
TB
1157 if (!cruft_po_args.max_pack_size)
1158 cruft_po_args.max_pack_size = po_args.max_pack_size;
4571324b
TB
1159
1160 cruft_po_args.local = po_args.local;
1161 cruft_po_args.quiet = po_args.quiet;
1162
c12cda47 1163 ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
eddad368 1164 cruft_expiration, &names,
e2b43831 1165 &existing);
f9825d1c 1166 if (ret)
90428ddc 1167 goto cleanup;
91badeba
TB
1168
1169 if (delete_redundant && expire_to) {
1170 /*
1171 * If `--expire-to` is given with `-d`, it's possible
1172 * that we're about to prune some objects. With cruft
1173 * packs, pruning is implicit: any objects from existing
1174 * packs that weren't picked up by new packs are removed
1175 * when their packs are deleted.
1176 *
1177 * Generate an additional cruft pack, with one twist:
1178 * `names` now includes the name of the cruft pack
1179 * written in the previous step. So the contents of
1180 * _this_ cruft pack exclude everything contained in the
1181 * existing cruft pack (that is, all of the unreachable
1182 * objects which are no older than
1183 * `--cruft-expiration`).
1184 *
1185 * To make this work, cruft_expiration must become NULL
1186 * so that this cruft pack doesn't actually prune any
1187 * objects. If it were non-NULL, this call would always
1188 * generate an empty pack (since every object not in the
1189 * cruft pack generated above will have an mtime older
1190 * than the expiration).
1191 */
1192 ret = write_cruft_pack(&cruft_po_args, expire_to,
1193 pack_prefix,
1194 NULL,
1195 &names,
e2b43831 1196 &existing);
91badeba 1197 if (ret)
90428ddc 1198 goto cleanup;
91badeba 1199 }
f9825d1c
TB
1200 }
1201
66731ff9
TB
1202 string_list_sort(&names);
1203
2d511cfc 1204 close_object_store(the_repository->objects);
5bdece0d 1205
a1bbc6c0
SB
1206 /*
1207 * Ok we have prepared all new packfiles.
a1bbc6c0 1208 */
a1bbc6c0 1209 for_each_string_list_item(item, &names) {
d3d9c519
JK
1210 struct generated_pack_data *data = item->util;
1211
b328c216 1212 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
9cf10d87 1213 char *fname;
525e18c0 1214
a1bbc6c0 1215 fname = mkpathdup("%s/pack-%s%s",
42a02d85 1216 packdir, item->string, exts[ext].name);
2fcb03b5 1217
9cf10d87
JK
1218 if (data->tempfiles[ext]) {
1219 const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
2fcb03b5 1220 struct stat statbuffer;
9cf10d87 1221
2fcb03b5
TB
1222 if (!stat(fname_old, &statbuffer)) {
1223 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1224 chmod(fname_old, statbuffer.st_mode);
1225 }
1226
9cf10d87
JK
1227 if (rename_tempfile(&data->tempfiles[ext], fname))
1228 die_errno(_("renaming pack to '%s' failed"), fname);
2fcb03b5 1229 } else if (!exts[ext].optional)
a4880b20
JK
1230 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1231 exts[ext].name, packtmp, item->string);
2fcb03b5
TB
1232 else if (unlink(fname) < 0 && errno != ENOENT)
1233 die_errno(_("could not unlink: %s"), fname);
a1bbc6c0 1234
e3cf2303 1235 free(fname);
a1bbc6c0
SB
1236 }
1237 }
a1bbc6c0
SB
1238 /* End of pack replacement. */
1239
054b5e48
TB
1240 if (delete_redundant && pack_everything & ALL_INTO_ONE)
1241 mark_packs_for_deletion(&existing, &names);
1d89d88d
TB
1242
1243 if (write_midx) {
1244 struct string_list include = STRING_LIST_INIT_NODUP;
e2b43831 1245 midx_included_packs(&include, &existing, &names, &geometry);
1d89d88d 1246
99d51978 1247 ret = write_midx_included_packs(&include, &geometry,
324efc90 1248 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1d89d88d
TB
1249 show_progress, write_bitmaps > 0);
1250
55d902cd
TB
1251 if (!ret && write_bitmaps)
1252 remove_redundant_bitmaps(&include, packdir);
1253
1d89d88d
TB
1254 string_list_clear(&include, 0);
1255
1256 if (ret)
90428ddc 1257 goto cleanup;
1d89d88d
TB
1258 }
1259
5d19e813
JT
1260 reprepare_packed_git(the_repository);
1261
a1bbc6c0 1262 if (delete_redundant) {
4489a480 1263 int opts = 0;
f2d3bf17 1264 remove_redundant_existing_packs(&existing);
0fabafd0 1265
639c4a39
TB
1266 if (geometry.split_factor)
1267 geometry_remove_redundant_packs(&geometry, &names,
1268 &existing);
47ca93d0 1269 if (show_progress)
4489a480
RS
1270 opts |= PRUNE_PACKED_VERBOSE;
1271 prune_packed_objects(opts);
5dcfbf56
JS
1272
1273 if (!keep_unreachable &&
1274 (!(pack_everything & LOOSEN_UNREACHABLE) ||
1275 unpack_unreachable) &&
1276 is_repository_shallow(the_repository))
1277 prune_shallow(PRUNE_QUICK);
a1bbc6c0
SB
1278 }
1279
64a6151d 1280 if (run_update_server_info)
4489a480 1281 update_server_info(0);
0465a505 1282
ff1e653c
TB
1283 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1284 unsigned flags = 0;
1285 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
1286 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
08944d1c 1287 write_midx_file(get_object_directory(), NULL, NULL, flags);
ff1e653c 1288 }
0465a505 1289
90428ddc 1290cleanup:
d3d9c519 1291 string_list_clear(&names, 1);
e2b43831 1292 existing_packs_release(&existing);
99d51978 1293 free_pack_geometry(&geometry);
a1bbc6c0 1294
90428ddc 1295 return ret;
a1bbc6c0 1296}