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