4
#include "environment.h"
7
#include "parse-options.h"
9
#include "run-command.h"
10
#include "server-info.h"
12
#include "string-list.h"
16
#include "prune-packed.h"
17
#include "object-store-ll.h"
18
#include "promisor-remote.h"
21
#include "pack-bitmap.h"
23
#include "list-objects-filter-options.h"
26
#define LOOSEN_UNREACHABLE 2
32
static int pack_everything;
33
static int delta_base_offset = 1;
34
static int pack_kept_objects = -1;
35
static int write_bitmaps = -1;
36
static int use_delta_islands;
37
static int run_update_server_info = 1;
38
static char *packdir, *packtmp_name, *packtmp;
40
static const char *const git_repack_usage[] = {
41
N_("git repack [<options>]"),
45
static const char incremental_bitmap_conflict_error[] = N_(
46
"Incremental repacks are incompatible with bitmap indexes. Use\n"
47
"--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
50
struct pack_objects_args {
55
unsigned long max_pack_size;
60
struct list_objects_filter_options filter_options;
63
static int repack_config(const char *var, const char *value,
64
const struct config_context *ctx, void *cb)
66
struct pack_objects_args *cruft_po_args = cb;
67
if (!strcmp(var, "repack.usedeltabaseoffset")) {
68
delta_base_offset = git_config_bool(var, value);
71
if (!strcmp(var, "repack.packkeptobjects")) {
72
pack_kept_objects = git_config_bool(var, value);
75
if (!strcmp(var, "repack.writebitmaps") ||
76
!strcmp(var, "pack.writebitmaps")) {
77
write_bitmaps = git_config_bool(var, value);
80
if (!strcmp(var, "repack.usedeltaislands")) {
81
use_delta_islands = git_config_bool(var, value);
84
if (strcmp(var, "repack.updateserverinfo") == 0) {
85
run_update_server_info = git_config_bool(var, value);
88
if (!strcmp(var, "repack.cruftwindow"))
89
return git_config_string(&cruft_po_args->window, var, value);
90
if (!strcmp(var, "repack.cruftwindowmemory"))
91
return git_config_string(&cruft_po_args->window_memory, var, value);
92
if (!strcmp(var, "repack.cruftdepth"))
93
return git_config_string(&cruft_po_args->depth, var, value);
94
if (!strcmp(var, "repack.cruftthreads"))
95
return git_config_string(&cruft_po_args->threads, var, value);
96
return git_default_config(var, value, ctx, cb);
99
struct existing_packs {
100
struct string_list kept_packs;
101
struct string_list non_kept_packs;
102
struct string_list cruft_packs;
105
#define EXISTING_PACKS_INIT { \
106
.kept_packs = STRING_LIST_INIT_DUP, \
107
.non_kept_packs = STRING_LIST_INIT_DUP, \
108
.cruft_packs = STRING_LIST_INIT_DUP, \
111
static int has_existing_non_kept_packs(const struct existing_packs *existing)
113
return existing->non_kept_packs.nr || existing->cruft_packs.nr;
116
static void pack_mark_for_deletion(struct string_list_item *item)
118
item->util = (void*)((uintptr_t)item->util | DELETE_PACK);
121
static void pack_unmark_for_deletion(struct string_list_item *item)
123
item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK);
126
static int pack_is_marked_for_deletion(struct string_list_item *item)
128
return (uintptr_t)item->util & DELETE_PACK;
131
static void pack_mark_retained(struct string_list_item *item)
133
item->util = (void*)((uintptr_t)item->util | RETAIN_PACK);
136
static int pack_is_retained(struct string_list_item *item)
138
return (uintptr_t)item->util & RETAIN_PACK;
141
static void mark_packs_for_deletion_1(struct string_list *names,
142
struct string_list *list)
144
struct string_list_item *item;
145
const int hexsz = the_hash_algo->hexsz;
147
for_each_string_list_item(item, list) {
149
size_t len = strlen(item->string);
152
sha1 = item->string + len - hexsz;
154
if (pack_is_retained(item)) {
155
pack_unmark_for_deletion(item);
156
} else if (!string_list_has_string(names, sha1)) {
164
pack_mark_for_deletion(item);
169
static void retain_cruft_pack(struct existing_packs *existing,
170
struct packed_git *cruft)
172
struct strbuf buf = STRBUF_INIT;
173
struct string_list_item *item;
175
strbuf_addstr(&buf, pack_basename(cruft));
176
strbuf_strip_suffix(&buf, ".pack");
178
item = string_list_lookup(&existing->cruft_packs, buf.buf);
180
BUG("could not find cruft pack '%s'", pack_basename(cruft));
182
pack_mark_retained(item);
183
strbuf_release(&buf);
186
static void mark_packs_for_deletion(struct existing_packs *existing,
187
struct string_list *names)
190
mark_packs_for_deletion_1(names, &existing->non_kept_packs);
191
mark_packs_for_deletion_1(names, &existing->cruft_packs);
194
static void remove_redundant_pack(const char *dir_name, const char *base_name)
196
struct strbuf buf = STRBUF_INIT;
197
struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
198
strbuf_addf(&buf, "%s.pack", base_name);
199
if (m && midx_contains_pack(m, buf.buf))
200
clear_midx_file(the_repository);
201
strbuf_insertf(&buf, 0, "%s/", dir_name);
202
unlink_pack_path(buf.buf, 1);
203
strbuf_release(&buf);
206
static void remove_redundant_packs_1(struct string_list *packs)
208
struct string_list_item *item;
209
for_each_string_list_item(item, packs) {
210
if (!pack_is_marked_for_deletion(item))
212
remove_redundant_pack(packdir, item->string);
216
static void remove_redundant_existing_packs(struct existing_packs *existing)
218
remove_redundant_packs_1(&existing->non_kept_packs);
219
remove_redundant_packs_1(&existing->cruft_packs);
222
static void existing_packs_release(struct existing_packs *existing)
224
string_list_clear(&existing->kept_packs, 0);
225
string_list_clear(&existing->non_kept_packs, 0);
226
string_list_clear(&existing->cruft_packs, 0);
235
static void collect_pack_filenames(struct existing_packs *existing,
236
const struct string_list *extra_keep)
238
struct packed_git *p;
239
struct strbuf buf = STRBUF_INIT;
241
for (p = get_all_packs(the_repository); p; p = p->next) {
248
base = pack_basename(p);
250
for (i = 0; i < extra_keep->nr; i++)
251
if (!fspathcmp(base, extra_keep->items[i].string))
255
strbuf_addstr(&buf, base);
256
strbuf_strip_suffix(&buf, ".pack");
258
if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep)
259
string_list_append(&existing->kept_packs, buf.buf);
260
else if (p->is_cruft)
261
string_list_append(&existing->cruft_packs, buf.buf);
263
string_list_append(&existing->non_kept_packs, buf.buf);
266
string_list_sort(&existing->kept_packs);
267
string_list_sort(&existing->non_kept_packs);
268
string_list_sort(&existing->cruft_packs);
269
strbuf_release(&buf);
272
static void prepare_pack_objects(struct child_process *cmd,
273
const struct pack_objects_args *args,
276
strvec_push(&cmd->args, "pack-objects");
278
strvec_pushf(&cmd->args, "--window=%s", args->window);
279
if (args->window_memory)
280
strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
282
strvec_pushf(&cmd->args, "--depth=%s", args->depth);
284
strvec_pushf(&cmd->args, "--threads=%s", args->threads);
285
if (args->max_pack_size)
286
strvec_pushf(&cmd->args, "--max-pack-size=%lu", args->max_pack_size);
287
if (args->no_reuse_delta)
288
strvec_pushf(&cmd->args, "--no-reuse-delta");
289
if (args->no_reuse_object)
290
strvec_pushf(&cmd->args, "--no-reuse-object");
292
strvec_push(&cmd->args, "--local");
294
strvec_push(&cmd->args, "--quiet");
295
if (delta_base_offset)
296
strvec_push(&cmd->args, "--delta-base-offset");
297
strvec_push(&cmd->args, out);
306
static int write_oid(const struct object_id *oid,
307
struct packed_git *pack UNUSED,
308
uint32_t pos UNUSED, void *data)
310
struct child_process *cmd = data;
313
if (start_command(cmd))
314
die(_("could not start pack-objects to repack promisor objects"));
317
if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
318
write_in_full(cmd->in, "\n", 1) < 0)
319
die(_("failed to feed promisor objects to pack-objects"));
335
struct generated_pack_data {
336
struct tempfile *tempfiles[ARRAY_SIZE(exts)];
339
static struct generated_pack_data *populate_pack_exts(const char *name)
342
struct strbuf path = STRBUF_INIT;
343
struct generated_pack_data *data = xcalloc(1, sizeof(*data));
346
for (i = 0; i < ARRAY_SIZE(exts); i++) {
348
strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
350
if (stat(path.buf, &statbuf))
353
data->tempfiles[i] = register_tempfile(path.buf);
356
strbuf_release(&path);
360
static int has_pack_ext(const struct generated_pack_data *data,
364
for (i = 0; i < ARRAY_SIZE(exts); i++) {
365
if (strcmp(exts[i].name, ext))
367
return !!data->tempfiles[i];
369
BUG("unknown pack extension: '%s'", ext);
372
static void repack_promisor_objects(const struct pack_objects_args *args,
373
struct string_list *names)
375
struct child_process cmd = CHILD_PROCESS_INIT;
377
struct strbuf line = STRBUF_INIT;
379
prepare_pack_objects(&cmd, args, packtmp);
389
for_each_packed_object(write_oid, &cmd,
390
FOR_EACH_OBJECT_PROMISOR_ONLY);
394
child_process_clear(&cmd);
400
out = xfdopen(cmd.out, "r");
401
while (strbuf_getline_lf(&line, out) != EOF) {
402
struct string_list_item *item;
405
if (line.len != the_hash_algo->hexsz)
406
die(_("repack: Expecting full hex object ID lines only from pack-objects."));
407
item = string_list_append(names, line.buf);
420
promisor_name = mkpathdup("%s-%s.promisor", packtmp,
422
write_promisor_file(promisor_name, NULL, 0);
424
item->util = populate_pack_exts(item->string);
429
if (finish_command(&cmd))
430
die(_("could not finish pack-objects to repack promisor objects"));
433
struct pack_geometry {
434
struct packed_git **pack;
435
uint32_t pack_nr, pack_alloc;
441
static uint32_t geometry_pack_weight(struct packed_git *p)
443
if (open_pack_index(p))
444
die(_("cannot open index for %s"), p->pack_name);
445
return p->num_objects;
448
static int geometry_cmp(const void *va, const void *vb)
450
uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
451
bw = geometry_pack_weight(*(struct packed_git **)vb);
460
static void init_pack_geometry(struct pack_geometry *geometry,
461
struct existing_packs *existing,
462
const struct pack_objects_args *args)
464
struct packed_git *p;
465
struct strbuf buf = STRBUF_INIT;
467
for (p = get_all_packs(the_repository); p; p = p->next) {
468
if (args->local && !p->pack_local)
476
if (!pack_kept_objects) {
492
strbuf_addstr(&buf, pack_basename(p));
493
strbuf_strip_suffix(&buf, ".pack");
495
if (string_list_has_string(&existing->kept_packs, buf.buf))
501
ALLOC_GROW(geometry->pack,
502
geometry->pack_nr + 1,
503
geometry->pack_alloc);
505
geometry->pack[geometry->pack_nr] = p;
509
QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
510
strbuf_release(&buf);
513
static void split_pack_geometry(struct pack_geometry *geometry)
517
off_t total_size = 0;
519
if (!geometry->pack_nr) {
520
geometry->split = geometry->pack_nr;
528
for (i = geometry->pack_nr - 1; i > 0; i--) {
529
struct packed_git *ours = geometry->pack[i];
530
struct packed_git *prev = geometry->pack[i - 1];
532
if (unsigned_mult_overflows(geometry->split_factor,
533
geometry_pack_weight(prev)))
534
die(_("pack %s too large to consider in geometric "
538
if (geometry_pack_weight(ours) <
539
geometry->split_factor * geometry_pack_weight(prev))
564
for (i = 0; i < split; i++) {
565
struct packed_git *p = geometry->pack[i];
567
if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
568
die(_("pack %s too large to roll up"), p->pack_name);
569
total_size += geometry_pack_weight(p);
571
for (i = split; i < geometry->pack_nr; i++) {
572
struct packed_git *ours = geometry->pack[i];
574
if (unsigned_mult_overflows(geometry->split_factor,
576
die(_("pack %s too large to roll up"), ours->pack_name);
578
if (geometry_pack_weight(ours) <
579
geometry->split_factor * total_size) {
580
if (unsigned_add_overflows(total_size,
581
geometry_pack_weight(ours)))
582
die(_("pack %s too large to roll up"),
586
total_size += geometry_pack_weight(ours);
591
geometry->split = split;
594
static struct packed_git *get_preferred_pack(struct pack_geometry *geometry)
610
if (geometry->split == geometry->pack_nr)
618
for (i = geometry->pack_nr; i > geometry->split; i--)
623
if (geometry->pack[i - 1]->pack_local)
624
return geometry->pack[i - 1];
629
static void geometry_remove_redundant_packs(struct pack_geometry *geometry,
630
struct string_list *names,
631
struct existing_packs *existing)
633
struct strbuf buf = STRBUF_INIT;
636
for (i = 0; i < geometry->split; i++) {
637
struct packed_git *p = geometry->pack[i];
638
if (string_list_has_string(names, hash_to_hex(p->hash)))
642
strbuf_addstr(&buf, pack_basename(p));
643
strbuf_strip_suffix(&buf, ".pack");
645
if ((p->pack_keep) ||
646
(string_list_has_string(&existing->kept_packs, buf.buf)))
649
remove_redundant_pack(packdir, buf.buf);
652
strbuf_release(&buf);
655
static void free_pack_geometry(struct pack_geometry *geometry)
660
free(geometry->pack);
663
struct midx_snapshot_ref_data {
669
static int midx_snapshot_ref_one(const char *refname UNUSED,
670
const char *referent UNUSED,
671
const struct object_id *oid,
672
int flag UNUSED, void *_data)
674
struct midx_snapshot_ref_data *data = _data;
675
struct object_id peeled;
677
if (!peel_iterated_oid(the_repository, oid, &peeled))
680
if (oidset_insert(&data->seen, oid))
683
if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
686
fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
692
static void midx_snapshot_refs(struct tempfile *f)
694
struct midx_snapshot_ref_data data;
695
const struct string_list *preferred = bitmap_preferred_tips(the_repository);
699
oidset_init(&data.seen, 0);
701
if (!fdopen_tempfile(f, "w"))
702
die(_("could not open tempfile %s for writing"),
703
get_tempfile_path(f));
706
struct string_list_item *item;
709
for_each_string_list_item(item, preferred)
710
refs_for_each_ref_in(get_main_ref_store(the_repository),
712
midx_snapshot_ref_one, &data);
716
refs_for_each_ref(get_main_ref_store(the_repository),
717
midx_snapshot_ref_one, &data);
719
if (close_tempfile_gently(f)) {
720
int save_errno = errno;
723
die_errno(_("could not close refs snapshot tempfile"));
726
oidset_clear(&data.seen);
729
static void midx_included_packs(struct string_list *include,
730
struct existing_packs *existing,
731
struct string_list *names,
732
struct pack_geometry *geometry)
734
struct string_list_item *item;
736
for_each_string_list_item(item, &existing->kept_packs)
737
string_list_insert(include, xstrfmt("%s.idx", item->string));
738
for_each_string_list_item(item, names)
739
string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
740
if (geometry->split_factor) {
741
struct strbuf buf = STRBUF_INIT;
743
for (i = geometry->split; i < geometry->pack_nr; i++) {
744
struct packed_git *p = geometry->pack[i];
757
strbuf_addstr(&buf, pack_basename(p));
758
strbuf_strip_suffix(&buf, ".pack");
759
strbuf_addstr(&buf, ".idx");
761
string_list_insert(include, strbuf_detach(&buf, NULL));
764
for_each_string_list_item(item, &existing->non_kept_packs) {
765
if (pack_is_marked_for_deletion(item))
767
string_list_insert(include, xstrfmt("%s.idx", item->string));
771
for_each_string_list_item(item, &existing->cruft_packs) {
785
if (pack_is_marked_for_deletion(item))
787
string_list_insert(include, xstrfmt("%s.idx", item->string));
791
static int write_midx_included_packs(struct string_list *include,
792
struct pack_geometry *geometry,
793
struct string_list *names,
794
const char *refs_snapshot,
795
int show_progress, int write_bitmaps)
797
struct child_process cmd = CHILD_PROCESS_INIT;
798
struct string_list_item *item;
799
struct packed_git *preferred = get_preferred_pack(geometry);
809
strvec_push(&cmd.args, "multi-pack-index");
810
strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
813
strvec_push(&cmd.args, "--progress");
815
strvec_push(&cmd.args, "--no-progress");
818
strvec_push(&cmd.args, "--bitmap");
821
strvec_pushf(&cmd.args, "--preferred-pack=%s",
822
pack_basename(preferred));
823
else if (names->nr) {
836
for_each_string_list_item(item, names) {
837
struct generated_pack_data *data = item->util;
838
if (has_pack_ext(data, ".mtimes"))
841
strvec_pushf(&cmd.args, "--preferred-pack=pack-%s.pack",
857
strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
859
ret = start_command(&cmd);
863
in = xfdopen(cmd.in, "w");
864
for_each_string_list_item(item, include)
865
fprintf(in, "%s\n", item->string);
868
return finish_command(&cmd);
871
static void remove_redundant_bitmaps(struct string_list *include,
874
struct strbuf path = STRBUF_INIT;
875
struct string_list_item *item;
878
strbuf_addstr(&path, packdir);
879
strbuf_addch(&path, '/');
880
packdir_len = path.len;
886
for_each_string_list_item(item, include) {
887
strbuf_addstr(&path, item->string);
888
strbuf_strip_suffix(&path, ".idx");
889
strbuf_addstr(&path, ".bitmap");
891
if (unlink(path.buf) && errno != ENOENT)
892
warning_errno(_("could not remove stale bitmap: %s"),
895
strbuf_setlen(&path, packdir_len);
897
strbuf_release(&path);
900
static int finish_pack_objects_cmd(struct child_process *cmd,
901
struct string_list *names,
905
struct strbuf line = STRBUF_INIT;
907
out = xfdopen(cmd->out, "r");
908
while (strbuf_getline_lf(&line, out) != EOF) {
909
struct string_list_item *item;
911
if (line.len != the_hash_algo->hexsz)
912
die(_("repack: Expecting full hex object ID lines only "
913
"from pack-objects."));
919
item = string_list_append(names, line.buf);
920
item->util = populate_pack_exts(line.buf);
925
strbuf_release(&line);
927
return finish_command(cmd);
930
static int write_filtered_pack(const struct pack_objects_args *args,
931
const char *destination,
932
const char *pack_prefix,
933
struct existing_packs *existing,
934
struct string_list *names)
936
struct child_process cmd = CHILD_PROCESS_INIT;
937
struct string_list_item *item;
942
int local = skip_prefix(destination, packdir, &scratch);
944
prepare_pack_objects(&cmd, args, destination);
946
strvec_push(&cmd.args, "--stdin-packs");
948
if (!pack_kept_objects)
949
strvec_push(&cmd.args, "--honor-pack-keep");
950
for_each_string_list_item(item, &existing->kept_packs)
951
strvec_pushf(&cmd.args, "--keep-pack=%s", item->string);
955
ret = start_command(&cmd);
965
in = xfdopen(cmd.in, "w");
966
for_each_string_list_item(item, names)
967
fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string);
968
for_each_string_list_item(item, &existing->non_kept_packs)
969
fprintf(in, "%s.pack\n", item->string);
970
for_each_string_list_item(item, &existing->cruft_packs)
971
fprintf(in, "%s.pack\n", item->string);
972
caret = pack_kept_objects ? "" : "^";
973
for_each_string_list_item(item, &existing->kept_packs)
974
fprintf(in, "%s%s.pack\n", caret, item->string);
977
return finish_pack_objects_cmd(&cmd, names, local);
980
static int existing_cruft_pack_cmp(const void *va, const void *vb)
982
struct packed_git *a = *(struct packed_git **)va;
983
struct packed_git *b = *(struct packed_git **)vb;
985
if (a->pack_size < b->pack_size)
987
if (a->pack_size > b->pack_size)
992
static void collapse_small_cruft_packs(FILE *in, size_t max_size,
993
struct existing_packs *existing)
995
struct packed_git **existing_cruft, *p;
996
struct strbuf buf = STRBUF_INIT;
997
size_t total_size = 0;
998
size_t existing_cruft_nr = 0;
1001
ALLOC_ARRAY(existing_cruft, existing->cruft_packs.nr);
1003
for (p = get_all_packs(the_repository); p; p = p->next) {
1004
if (!(p->is_cruft && p->pack_local))
1008
strbuf_addstr(&buf, pack_basename(p));
1009
strbuf_strip_suffix(&buf, ".pack");
1011
if (!string_list_has_string(&existing->cruft_packs, buf.buf))
1014
if (existing_cruft_nr >= existing->cruft_packs.nr)
1015
BUG("too many cruft packs (found %"PRIuMAX", but knew "
1017
(uintmax_t)existing_cruft_nr + 1,
1018
(uintmax_t)existing->cruft_packs.nr);
1019
existing_cruft[existing_cruft_nr++] = p;
1022
QSORT(existing_cruft, existing_cruft_nr, existing_cruft_pack_cmp);
1024
for (i = 0; i < existing_cruft_nr; i++) {
1027
p = existing_cruft[i];
1028
proposed = st_add(total_size, p->pack_size);
1030
if (proposed <= max_size) {
1031
total_size = proposed;
1032
fprintf(in, "-%s\n", pack_basename(p));
1034
retain_cruft_pack(existing, p);
1035
fprintf(in, "%s\n", pack_basename(p));
1039
for (i = 0; i < existing->non_kept_packs.nr; i++)
1040
fprintf(in, "-%s.pack\n",
1041
existing->non_kept_packs.items[i].string);
1043
strbuf_release(&buf);
1044
free(existing_cruft);
1047
static int write_cruft_pack(const struct pack_objects_args *args,
1048
const char *destination,
1049
const char *pack_prefix,
1050
const char *cruft_expiration,
1051
struct string_list *names,
1052
struct existing_packs *existing)
1054
struct child_process cmd = CHILD_PROCESS_INIT;
1055
struct string_list_item *item;
1058
const char *scratch;
1059
int local = skip_prefix(destination, packdir, &scratch);
1061
prepare_pack_objects(&cmd, args, destination);
1063
strvec_push(&cmd.args, "--cruft");
1064
if (cruft_expiration)
1065
strvec_pushf(&cmd.args, "--cruft-expiration=%s",
1068
strvec_push(&cmd.args, "--honor-pack-keep");
1069
strvec_push(&cmd.args, "--non-empty");
1073
ret = start_command(&cmd);
1090
in = xfdopen(cmd.in, "w");
1091
for_each_string_list_item(item, names)
1092
fprintf(in, "%s-%s.pack\n", pack_prefix, item->string);
1093
if (args->max_pack_size && !cruft_expiration) {
1094
collapse_small_cruft_packs(in, args->max_pack_size, existing);
1096
for_each_string_list_item(item, &existing->non_kept_packs)
1097
fprintf(in, "-%s.pack\n", item->string);
1098
for_each_string_list_item(item, &existing->cruft_packs)
1099
fprintf(in, "-%s.pack\n", item->string);
1101
for_each_string_list_item(item, &existing->kept_packs)
1102
fprintf(in, "%s.pack\n", item->string);
1105
return finish_pack_objects_cmd(&cmd, names, local);
1108
static const char *find_pack_prefix(const char *packdir, const char *packtmp)
1110
const char *pack_prefix;
1111
if (!skip_prefix(packtmp, packdir, &pack_prefix))
1112
die(_("pack prefix %s does not begin with objdir %s"),
1114
if (*pack_prefix == '/')
1119
int cmd_repack(int argc, const char **argv, const char *prefix)
1121
struct child_process cmd = CHILD_PROCESS_INIT;
1122
struct string_list_item *item;
1123
struct string_list names = STRING_LIST_INIT_DUP;
1124
struct existing_packs existing = EXISTING_PACKS_INIT;
1125
struct pack_geometry geometry = { 0 };
1126
struct tempfile *refs_snapshot = NULL;
1131
int delete_redundant = 0;
1132
const char *unpack_unreachable = NULL;
1133
int keep_unreachable = 0;
1134
struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
1135
struct pack_objects_args po_args = {NULL};
1136
struct pack_objects_args cruft_po_args = {NULL};
1138
const char *cruft_expiration = NULL;
1139
const char *expire_to = NULL;
1140
const char *filter_to = NULL;
1142
struct option builtin_repack_options[] = {
1143
OPT_BIT('a', NULL, &pack_everything,
1144
N_("pack everything in a single pack"), ALL_INTO_ONE),
1145
OPT_BIT('A', NULL, &pack_everything,
1146
N_("same as -a, and turn unreachable objects loose"),
1147
LOOSEN_UNREACHABLE | ALL_INTO_ONE),
1148
OPT_BIT(0, "cruft", &pack_everything,
1149
N_("same as -a, pack unreachable cruft objects separately"),
1151
OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
1152
N_("with --cruft, expire objects older than this")),
1153
OPT_MAGNITUDE(0, "max-cruft-size", &cruft_po_args.max_pack_size,
1154
N_("with --cruft, limit the size of new cruft packs")),
1155
OPT_BOOL('d', NULL, &delete_redundant,
1156
N_("remove redundant packs, and run git-prune-packed")),
1157
OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
1158
N_("pass --no-reuse-delta to git-pack-objects")),
1159
OPT_BOOL('F', NULL, &po_args.no_reuse_object,
1160
N_("pass --no-reuse-object to git-pack-objects")),
1161
OPT_NEGBIT('n', NULL, &run_update_server_info,
1162
N_("do not run git-update-server-info"), 1),
1163
OPT__QUIET(&po_args.quiet, N_("be quiet")),
1164
OPT_BOOL('l', "local", &po_args.local,
1165
N_("pass --local to git-pack-objects")),
1166
OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
1167
N_("write bitmap index")),
1168
OPT_BOOL('i', "delta-islands", &use_delta_islands,
1169
N_("pass --delta-islands to git-pack-objects")),
1170
OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
1171
N_("with -A, do not loosen objects older than this")),
1172
OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
1173
N_("with -a, repack unreachable objects")),
1174
OPT_STRING(0, "window", &po_args.window, N_("n"),
1175
N_("size of the window used for delta compression")),
1176
OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
1177
N_("same as the above, but limit memory size instead of entries count")),
1178
OPT_STRING(0, "depth", &po_args.depth, N_("n"),
1179
N_("limits the maximum delta depth")),
1180
OPT_STRING(0, "threads", &po_args.threads, N_("n"),
1181
N_("limits the maximum number of threads")),
1182
OPT_MAGNITUDE(0, "max-pack-size", &po_args.max_pack_size,
1183
N_("maximum size of each packfile")),
1184
OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options),
1185
OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
1186
N_("repack objects in packs marked with .keep")),
1187
OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
1188
N_("do not repack this pack")),
1189
OPT_INTEGER('g', "geometric", &geometry.split_factor,
1190
N_("find a geometric progression with factor <N>")),
1191
OPT_BOOL('m', "write-midx", &write_midx,
1192
N_("write a multi-pack index of the resulting packs")),
1193
OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
1194
N_("pack prefix to store a pack containing pruned objects")),
1195
OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
1196
N_("pack prefix to store a pack containing filtered out objects")),
1200
list_objects_filter_init(&po_args.filter_options);
1202
git_config(repack_config, &cruft_po_args);
1204
argc = parse_options(argc, argv, prefix, builtin_repack_options,
1205
git_repack_usage, 0);
1207
if (delete_redundant && repository_format_precious_objects)
1208
die(_("cannot delete packs in a precious-objects repo"));
1210
die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
1211
keep_unreachable, "-k/--keep-unreachable",
1212
pack_everything & PACK_CRUFT, "--cruft");
1214
if (pack_everything & PACK_CRUFT)
1215
pack_everything |= ALL_INTO_ONE;
1217
if (write_bitmaps < 0) {
1219
(!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
1222
if (pack_kept_objects < 0)
1223
pack_kept_objects = write_bitmaps > 0 && !write_midx;
1225
if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
1226
die(_(incremental_bitmap_conflict_error));
1228
if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) {
1236
warning(_("disabling bitmap writing, as some objects are not being packed"));
1240
if (write_midx && write_bitmaps) {
1241
struct strbuf path = STRBUF_INIT;
1243
strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
1246
refs_snapshot = xmks_tempfile(path.buf);
1247
midx_snapshot_refs(refs_snapshot);
1249
strbuf_release(&path);
1252
packdir = mkpathdup("%s/pack", get_object_directory());
1253
packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
1254
packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
1256
collect_pack_filenames(&existing, &keep_pack_list);
1258
if (geometry.split_factor) {
1259
if (pack_everything)
1260
die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1261
init_pack_geometry(&geometry, &existing, &po_args);
1262
split_pack_geometry(&geometry);
1265
prepare_pack_objects(&cmd, &po_args, packtmp);
1267
show_progress = !po_args.quiet && isatty(2);
1269
strvec_push(&cmd.args, "--keep-true-parents");
1270
if (!pack_kept_objects)
1271
strvec_push(&cmd.args, "--honor-pack-keep");
1272
for (i = 0; i < keep_pack_list.nr; i++)
1273
strvec_pushf(&cmd.args, "--keep-pack=%s",
1274
keep_pack_list.items[i].string);
1275
strvec_push(&cmd.args, "--non-empty");
1276
if (!geometry.split_factor) {
1287
strvec_push(&cmd.args, "--all");
1288
strvec_push(&cmd.args, "--reflog");
1289
strvec_push(&cmd.args, "--indexed-objects");
1291
if (repo_has_promisor_remote(the_repository))
1292
strvec_push(&cmd.args, "--exclude-promisor-objects");
1294
if (write_bitmaps > 0)
1295
strvec_push(&cmd.args, "--write-bitmap-index");
1296
else if (write_bitmaps < 0)
1297
strvec_push(&cmd.args, "--write-bitmap-index-quiet");
1299
if (use_delta_islands)
1300
strvec_push(&cmd.args, "--delta-islands");
1302
if (pack_everything & ALL_INTO_ONE) {
1303
repack_promisor_objects(&po_args, &names);
1305
if (has_existing_non_kept_packs(&existing) &&
1307
!(pack_everything & PACK_CRUFT)) {
1308
for_each_string_list_item(item, &names) {
1309
strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
1310
packtmp_name, item->string);
1312
if (unpack_unreachable) {
1313
strvec_pushf(&cmd.args,
1314
"--unpack-unreachable=%s",
1315
unpack_unreachable);
1316
} else if (pack_everything & LOOSEN_UNREACHABLE) {
1317
strvec_push(&cmd.args,
1318
"--unpack-unreachable");
1319
} else if (keep_unreachable) {
1320
strvec_push(&cmd.args, "--keep-unreachable");
1321
strvec_push(&cmd.args, "--pack-loose-unreachable");
1324
} else if (geometry.split_factor) {
1325
strvec_push(&cmd.args, "--stdin-packs");
1326
strvec_push(&cmd.args, "--unpacked");
1328
strvec_push(&cmd.args, "--unpacked");
1329
strvec_push(&cmd.args, "--incremental");
1332
if (po_args.filter_options.choice)
1333
strvec_pushf(&cmd.args, "--filter=%s",
1334
expand_list_objects_filter_spec(&po_args.filter_options));
1336
die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
1338
if (geometry.split_factor)
1343
ret = start_command(&cmd);
1347
if (geometry.split_factor) {
1348
FILE *in = xfdopen(cmd.in, "w");
1354
for (i = 0; i < geometry.split; i++)
1355
fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1356
for (i = geometry.split; i < geometry.pack_nr; i++)
1357
fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
1361
ret = finish_pack_objects_cmd(&cmd, &names, 1);
1365
if (!names.nr && !po_args.quiet)
1366
printf_ln(_("Nothing new to pack."));
1368
if (pack_everything & PACK_CRUFT) {
1369
const char *pack_prefix = find_pack_prefix(packdir, packtmp);
1371
if (!cruft_po_args.window)
1372
cruft_po_args.window = po_args.window;
1373
if (!cruft_po_args.window_memory)
1374
cruft_po_args.window_memory = po_args.window_memory;
1375
if (!cruft_po_args.depth)
1376
cruft_po_args.depth = po_args.depth;
1377
if (!cruft_po_args.threads)
1378
cruft_po_args.threads = po_args.threads;
1379
if (!cruft_po_args.max_pack_size)
1380
cruft_po_args.max_pack_size = po_args.max_pack_size;
1382
cruft_po_args.local = po_args.local;
1383
cruft_po_args.quiet = po_args.quiet;
1385
ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix,
1386
cruft_expiration, &names,
1391
if (delete_redundant && expire_to) {
1414
ret = write_cruft_pack(&cruft_po_args, expire_to,
1424
if (po_args.filter_options.choice) {
1426
filter_to = packtmp;
1428
ret = write_filtered_pack(&po_args,
1430
find_pack_prefix(packdir, packtmp),
1437
string_list_sort(&names);
1439
close_object_store(the_repository->objects);
1444
for_each_string_list_item(item, &names) {
1445
struct generated_pack_data *data = item->util;
1447
for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
1450
fname = mkpathdup("%s/pack-%s%s",
1451
packdir, item->string, exts[ext].name);
1453
if (data->tempfiles[ext]) {
1454
const char *fname_old = get_tempfile_path(data->tempfiles[ext]);
1455
struct stat statbuffer;
1457
if (!stat(fname_old, &statbuffer)) {
1458
statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1459
chmod(fname_old, statbuffer.st_mode);
1462
if (rename_tempfile(&data->tempfiles[ext], fname))
1463
die_errno(_("renaming pack to '%s' failed"), fname);
1464
} else if (!exts[ext].optional)
1465
die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1466
exts[ext].name, packtmp, item->string);
1467
else if (unlink(fname) < 0 && errno != ENOENT)
1468
die_errno(_("could not unlink: %s"), fname);
1475
if (delete_redundant && pack_everything & ALL_INTO_ONE)
1476
mark_packs_for_deletion(&existing, &names);
1479
struct string_list include = STRING_LIST_INIT_NODUP;
1480
midx_included_packs(&include, &existing, &names, &geometry);
1482
ret = write_midx_included_packs(&include, &geometry, &names,
1483
refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
1484
show_progress, write_bitmaps > 0);
1486
if (!ret && write_bitmaps)
1487
remove_redundant_bitmaps(&include, packdir);
1489
string_list_clear(&include, 0);
1495
reprepare_packed_git(the_repository);
1497
if (delete_redundant) {
1499
remove_redundant_existing_packs(&existing);
1501
if (geometry.split_factor)
1502
geometry_remove_redundant_packs(&geometry, &names,
1505
opts |= PRUNE_PACKED_VERBOSE;
1506
prune_packed_objects(opts);
1508
if (!keep_unreachable &&
1509
(!(pack_everything & LOOSEN_UNREACHABLE) ||
1510
unpack_unreachable) &&
1511
is_repository_shallow(the_repository))
1512
prune_shallow(PRUNE_QUICK);
1515
if (run_update_server_info)
1516
update_server_info(0);
1518
if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
1520
if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
1521
flags |= MIDX_WRITE_INCREMENTAL;
1522
write_midx_file(get_object_directory(), NULL, NULL, flags);
1526
string_list_clear(&names, 1);
1527
existing_packs_release(&existing);
1528
free_pack_geometry(&geometry);
1529
list_objects_filter_release(&po_args.filter_options);