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