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