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