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