at commit a0189536

1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "builtin.h"
4 #include "config.h"
5 #include "environment.h"
6 #include "parse-options.h"
7 #include "path.h"
8 #include "run-command.h"
9 #include "server-info.h"
10 #include "string-list.h"
11 #include "midx.h"
12 #include "packfile.h"
13 #include "prune-packed.h"
14 #include "promisor-remote.h"
15 #include "repack.h"
16 #include "shallow.h"
17 #include "list-objects-filter-options.h"
18 #include "oidset.h"
19 #include "hex.h"
20 #include "wt-status.h"
21 #include "read-cache-ll.h"
22
23 #define ALL_INTO_ONE 1
24 #define LOOSEN_UNREACHABLE 2
25 #define PACK_CRUFT 4
26
27 #define DELETE_PACK 1
28 #define RETAIN_PACK 2
29
30 static int pack_everything;
31 static int write_bitmaps = -1;
32 static int use_delta_islands;
33 static int run_update_server_info = 1;
34 static char *packdir, *packtmp_name, *packtmp;
35 static int midx_must_contain_cruft = 1;
36 static int drop_filtered;
37 static int dry_run;
38 static int write_bitmaps_given;
39
40 static const char *const git_repack_usage[] = {
41 N_("git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]\n"
42 "[--window=<n>] [--depth=<n>] [--threads=<n>] [--keep-pack=<pack-name>]\n"
43 "[--write-midx[=<mode>]] [--name-hash-version=<n>] [--path-walk]\n"
44 "[--filter=<filter-spec>] [--drop-filtered [--dry-run]]"),
45 NULL
46 };
47
48 static const char incremental_bitmap_conflict_error[] = N_(
49 "Incremental repacks are incompatible with bitmap indexes. Use\n"
50 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
51 );
52
53 #define DEFAULT_MIDX_SPLIT_FACTOR 2
54 #define DEFAULT_MIDX_NEW_LAYER_THRESHOLD 8
55
56 struct repack_config_ctx {
57 struct pack_objects_args *po_args;
58 struct pack_objects_args *cruft_po_args;
59 int midx_split_factor;
60 int midx_new_layer_threshold;
61 };
62
63 static int repack_config(const char *var, const char *value,
64 const struct config_context *ctx, void *cb)
65 {
66 struct repack_config_ctx *repack_ctx = cb;
67 struct pack_objects_args *po_args = repack_ctx->po_args;
68 struct pack_objects_args *cruft_po_args = repack_ctx->cruft_po_args;
69 if (!strcmp(var, "repack.usedeltabaseoffset")) {
70 po_args->delta_base_offset = git_config_bool(var, value);
71 return 0;
72 }
73 if (!strcmp(var, "repack.packkeptobjects")) {
74 po_args->pack_kept_objects = git_config_bool(var, value);
75 return 0;
76 }
77 if (!strcmp(var, "repack.writebitmaps") ||
78 !strcmp(var, "pack.writebitmaps")) {
79 write_bitmaps = git_config_bool(var, value);
80 return 0;
81 }
82 if (!strcmp(var, "repack.usedeltaislands")) {
83 use_delta_islands = git_config_bool(var, value);
84 return 0;
85 }
86 if (strcmp(var, "repack.updateserverinfo") == 0) {
87 run_update_server_info = git_config_bool(var, value);
88 return 0;
89 }
90 if (!strcmp(var, "repack.cruftwindow")) {
91 free(cruft_po_args->window);
92 return git_config_string(&cruft_po_args->window, var, value);
93 }
94 if (!strcmp(var, "repack.cruftwindowmemory")) {
95 free(cruft_po_args->window_memory);
96 return git_config_string(&cruft_po_args->window_memory, var, value);
97 }
98 if (!strcmp(var, "repack.cruftdepth")) {
99 free(cruft_po_args->depth);
100 return git_config_string(&cruft_po_args->depth, var, value);
101 }
102 if (!strcmp(var, "repack.cruftthreads")) {
103 free(cruft_po_args->threads);
104 return git_config_string(&cruft_po_args->threads, var, value);
105 }
106 if (!strcmp(var, "repack.midxmustcontaincruft")) {
107 midx_must_contain_cruft = git_config_bool(var, value);
108 return 0;
109 }
110 if (!strcmp(var, "repack.midxsplitfactor")) {
111 repack_ctx->midx_split_factor = git_config_int(var, value,
112 ctx->kvi);
113 return 0;
114 }
115 if (!strcmp(var, "repack.midxnewlayerthreshold")) {
116 repack_ctx->midx_new_layer_threshold = git_config_int(var, value,
117 ctx->kvi);
118 return 0;
119 }
120 return git_default_config(var, value, ctx, cb);
121 }
122
123 static int option_parse_write_bitmaps(const struct option *opt, const char *arg,
124 int unset)
125 {
126 int *value = opt->value;
127
128 BUG_ON_OPT_ARG(arg);
129 if (unset)
130 *value = 0;
131 else
132 *value = 1;
133
134 write_bitmaps_given = 1;
135 return 0;
136 }
137
138 static int option_parse_write_midx(const struct option *opt, const char *arg,
139 int unset)
140 {
141 enum repack_write_midx_mode *cfg = opt->value;
142
143 if (unset) {
144 *cfg = REPACK_WRITE_MIDX_NONE;
145 return 0;
146 }
147
148 if (!arg || !*arg)
149 *cfg = REPACK_WRITE_MIDX_DEFAULT;
150 else if (!strcmp(arg, "incremental"))
151 *cfg = REPACK_WRITE_MIDX_INCREMENTAL;
152 else
153 return error(_("unknown value for %s: %s"), opt->long_name, arg);
154
155 return 0;
156 }
157
158 int cmd_repack(int argc,
159 const char **argv,
160 const char *prefix,
161 struct repository *repo)
162 {
163 struct child_process cmd = CHILD_PROCESS_INIT;
164 struct string_list_item *item;
165 struct string_list names = STRING_LIST_INIT_DUP;
166 struct existing_packs existing = EXISTING_PACKS_INIT;
167 struct oidset drop_oids = OIDSET_INIT;
168 struct pack_geometry geometry = { 0 };
169 struct tempfile *refs_snapshot = NULL;
170 int i, ret;
171 int show_progress;
172
173 /* variables to be filled by option parsing */
174 struct repack_config_ctx config_ctx;
175 int delete_redundant = 0;
176 const char *unpack_unreachable = NULL;
177 int keep_unreachable = 0;
178 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
179 struct pack_objects_args po_args = PACK_OBJECTS_ARGS_INIT;
180 struct pack_objects_args cruft_po_args = PACK_OBJECTS_ARGS_INIT;
181 enum repack_write_midx_mode write_midx = REPACK_WRITE_MIDX_NONE;
182 const char *cruft_expiration = NULL;
183 const char *expire_to = NULL;
184 const char *filter_to = NULL;
185 const char *opt_window = NULL;
186 const char *opt_window_memory = NULL;
187 const char *opt_depth = NULL;
188 const char *opt_threads = NULL;
189 unsigned long combine_cruft_below_size = 0ul;
190
191 struct option builtin_repack_options[] = {
192 OPT_BIT('a', NULL, &pack_everything,
193 N_("pack everything in a single pack"), ALL_INTO_ONE),
194 OPT_BIT('A', NULL, &pack_everything,
195 N_("same as -a, and turn unreachable objects loose"),
196 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
197 OPT_BIT(0, "cruft", &pack_everything,
198 N_("same as -a, pack unreachable cruft objects separately"),
199 PACK_CRUFT),
200 OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"),
201 N_("with --cruft, expire objects older than this")),
202 OPT_UNSIGNED(0, "combine-cruft-below-size",
203 &combine_cruft_below_size,
204 N_("with --cruft, only repack cruft packs smaller than this")),
205 OPT_UNSIGNED(0, "max-cruft-size", &cruft_po_args.max_pack_size,
206 N_("with --cruft, limit the size of new cruft packs")),
207 OPT_BOOL('d', NULL, &delete_redundant,
208 N_("remove redundant packs, and run git-prune-packed")),
209 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
210 N_("pass --no-reuse-delta to git-pack-objects")),
211 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
212 N_("pass --no-reuse-object to git-pack-objects")),
213 OPT_INTEGER(0, "name-hash-version", &po_args.name_hash_version,
214 N_("specify the name hash version to use for grouping similar objects by path")),
215 OPT_BOOL(0, "path-walk", &po_args.path_walk,
216 N_("pass --path-walk to git-pack-objects")),
217 OPT_NEGBIT('n', NULL, &run_update_server_info,
218 N_("do not run git-update-server-info"), 1),
219 OPT__QUIET(&po_args.quiet, N_("be quiet")),
220 OPT_BOOL('l', "local", &po_args.local,
221 N_("pass --local to git-pack-objects")),
222 OPT_CALLBACK_F('b', "write-bitmap-index", &write_bitmaps, NULL,
223 N_("write bitmap index"),
224 PARSE_OPT_NOARG, option_parse_write_bitmaps),
225 OPT_BOOL('i', "delta-islands", &use_delta_islands,
226 N_("pass --delta-islands to git-pack-objects")),
227 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
228 N_("with -A, do not loosen objects older than this")),
229 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
230 N_("with -a, repack unreachable objects")),
231 OPT_STRING(0, "window", &opt_window, N_("n"),
232 N_("size of the window used for delta compression")),
233 OPT_STRING(0, "window-memory", &opt_window_memory, N_("bytes"),
234 N_("same as the above, but limit memory size instead of entries count")),
235 OPT_STRING(0, "depth", &opt_depth, N_("n"),
236 N_("limits the maximum delta depth")),
237 OPT_STRING(0, "threads", &opt_threads, N_("n"),
238 N_("limits the maximum number of threads")),
239 OPT_UNSIGNED(0, "max-pack-size", &po_args.max_pack_size,
240 N_("maximum size of each packfile")),
241 OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options),
242 OPT_BOOL(0, "pack-kept-objects", &po_args.pack_kept_objects,
243 N_("repack objects in packs marked with .keep")),
244 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
245 N_("do not repack this pack")),
246 OPT_INTEGER('g', "geometric", &geometry.split_factor,
247 N_("find a geometric progression with factor <N>")),
248 OPT_CALLBACK_F(0, "write-midx", &write_midx,
249 N_("mode"),
250 N_("write a multi-pack index of the resulting packs"),
251 PARSE_OPT_OPTARG, option_parse_write_midx),
252 OPT_SET_INT_F('m', NULL, &write_midx,
253 N_("write a multi-pack index of the resulting packs"),
254 REPACK_WRITE_MIDX_DEFAULT,
255 PARSE_OPT_HIDDEN),
256 OPT_STRING(0, "expire-to", &expire_to, N_("dir"),
257 N_("pack prefix to store a pack containing pruned objects")),
258 OPT_STRING(0, "filter-to", &filter_to, N_("dir"),
259 N_("pack prefix to store a pack containing filtered out objects")),
260 OPT_BOOL(0, "drop-filtered", &drop_filtered,
261 N_("delete filtered out objects (requires --filter)")),
262 OPT_BOOL(0, "dry-run", &dry_run,
263 N_("only show which objects would be dropped")),
264 OPT_END()
265 };
266
267 list_objects_filter_init(&po_args.filter_options);
268
269 memset(&config_ctx, 0, sizeof(config_ctx));
270 config_ctx.po_args = &po_args;
271 config_ctx.cruft_po_args = &cruft_po_args;
272 config_ctx.midx_split_factor = DEFAULT_MIDX_SPLIT_FACTOR;
273 config_ctx.midx_new_layer_threshold = DEFAULT_MIDX_NEW_LAYER_THRESHOLD;
274
275 repo_config(repo, repack_config, &config_ctx);
276
277 argc = parse_options(argc, argv, prefix, builtin_repack_options,
278 git_repack_usage, 0);
279
280 po_args.window = xstrdup_or_null(opt_window);
281 po_args.window_memory = xstrdup_or_null(opt_window_memory);
282 po_args.depth = xstrdup_or_null(opt_depth);
283 po_args.threads = xstrdup_or_null(opt_threads);
284
285 die_for_incompatible_opt2(drop_filtered, "--drop-filtered",
286 !!filter_to, "--filter-to");
287
288 if (dry_run && !drop_filtered)
289 die(_("--dry-run only takes effect with --drop-filtered"));
290
291 if (drop_filtered) {
292 if (!po_args.filter_options.choice)
293 die(_("--drop-filtered requires --filter"));
294
295 if (!(pack_everything & ALL_INTO_ONE))
296 die(_("--drop-filtered requires -a"));
297
298 /*
299 * Only blob:limit=<n> is supported for now. Reject other
300 * filter choices early, before walking the object database.
301 */
302 if (po_args.filter_options.choice != LOFC_BLOB_LIMIT)
303 die(_("--drop-filtered only supports --filter=blob:limit=<n> for now"));
304
305 /*
306 * An explicit -b on the command line is a conflict we have to
307 * report; a bitmap setting from config is silently overridden
308 * for the duration of the command.
309 */
310 if (write_bitmaps_given && write_bitmaps > 0)
311 die(_("options '%s' and '%s' cannot be used together"),
312 "--drop-filtered", "--write-bitmap-index");
313
314 /*
315 * Without a promisor remote there is nowhere to re-fetch the
316 * dropped objects from, so dropping them would be permanent
317 * data loss.
318 */
319 if (!repo_has_promisor_remote(repo))
320 die(_("--drop-filtered requires a promisor remote"));
321
322 /*
323 * Refuse to run while another operation is in progress. A
324 * dropped object would just be lazily re-fetched when the
325 * operation resumes, but triggering a network fetch in the
326 * middle of a half-finished
327 * merge/rebase/cherry-pick/revert/bisect is a poor
328 * experience, so this is a UX convenience rather than a
329 * safety measure. Bare repositories have no such state, so
330 * the check is skipped there.
331 */
332 if (!is_bare_repository(repo)) {
333 struct wt_status_state state = { 0 };
334
335 wt_status_get_state(repo, &state, 0);
336 if (state.merge_in_progress || state.revert_in_progress ||
337 state.rebase_in_progress || state.bisect_in_progress ||
338 state.cherry_pick_in_progress || state.am_in_progress ||
339 state.rebase_interactive_in_progress) {
340 wt_status_state_free_buffers(&state);
341 die(_("--drop-filtered cannot be used while "
342 "another operation (merge, rebase, am, "
343 "cherry-pick, revert, or bisect) is in "
344 "progress"));
345 }
346 wt_status_state_free_buffers(&state);
347 }
348
349 write_bitmaps = 0;
350
351 /*
352 * Dropping objects means rebuilding the promisor packs
353 * without them and then removing the old packs, so the
354 * redundant packs must be deleted. Imply -d on a real run.
355 */
356 if (!dry_run)
357 delete_redundant = 1;
358
359 ret = enumerate_promisor_blobs(repo, &po_args.filter_options, &drop_oids);
360
361 if (ret)
362 goto cleanup;
363
364 /*
365 * Refuse to drop blobs that the current index references.
366 * Such a blob would only be lazily re-fetched by the next
367 * command that touches the worktree, so dropping it reclaims
368 * nothing. This guard just avoids that churn. Bare
369 * repositories have no index, so the check is skipped there.
370 */
371 if (!is_bare_repository(repo) && oidset_size(&drop_oids)) {
372 struct index_state *istate = repo->index;
373 unsigned int i;
374
375 if (repo_read_index(repo) < 0)
376 die(_("could not read the index"));
377
378 for (i = 0; i < istate->cache_nr; i++) {
379 const struct cache_entry *ce = istate->cache[i];
380
381 if (oidset_contains(&drop_oids, &ce->oid))
382 die(_("cannot drop '%s' (%s): it is referenced by the current index"),
383 ce->name, oid_to_hex(&ce->oid));
384 }
385 }
386
387 if (dry_run) {
388 struct oidset_iter iter;
389 const struct object_id *oid;
390
391 oidset_iter_init(&drop_oids, &iter);
392 while ((oid = oidset_iter_next(&iter)))
393 printf("%s\n", oid_to_hex(oid));
394 }
395 }
396
397 if (delete_redundant && repo->repository_format_precious_objects)
398 die(_("cannot delete packs in a precious-objects repo"));
399
400 die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
401 keep_unreachable, "-k/--keep-unreachable",
402 pack_everything & PACK_CRUFT, "--cruft");
403
404 if (pack_everything & PACK_CRUFT)
405 pack_everything |= ALL_INTO_ONE;
406
407 if (write_bitmaps < 0) {
408 if (write_midx == REPACK_WRITE_MIDX_NONE &&
409 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository(repo)))
410 write_bitmaps = 0;
411 }
412 if (po_args.pack_kept_objects < 0)
413 po_args.pack_kept_objects = write_bitmaps > 0 &&
414 write_midx == REPACK_WRITE_MIDX_NONE;
415
416 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) &&
417 write_midx == REPACK_WRITE_MIDX_NONE)
418 die(_(incremental_bitmap_conflict_error));
419
420 if (write_bitmaps && po_args.local &&
421 odb_has_alternates(repo->objects)) {
422 /*
423 * When asked to do a local repack, but we have
424 * packfiles that are inherited from an alternate, then
425 * we cannot guarantee that the multi-pack-index would
426 * have full coverage of all objects. We thus disable
427 * writing bitmaps in that case.
428 */
429 warning(_("disabling bitmap writing, as some objects are not being packed"));
430 write_bitmaps = 0;
431 }
432
433 if (config_ctx.midx_split_factor < 2)
434 die(_("invalid value for %s: %d"), "--midx-split-factor",
435 config_ctx.midx_split_factor);
436 if (config_ctx.midx_new_layer_threshold < 1)
437 die(_("invalid value for %s: %d"), "--midx-new-layer-threshold",
438 config_ctx.midx_new_layer_threshold);
439
440 if (write_midx != REPACK_WRITE_MIDX_NONE && write_bitmaps) {
441 struct strbuf path = STRBUF_INIT;
442
443 strbuf_addf(&path, "%s/%s_XXXXXX",
444 repo_get_object_directory(repo),
445 "bitmap-ref-tips");
446
447 refs_snapshot = xmks_tempfile(path.buf);
448 midx_snapshot_refs(repo, refs_snapshot);
449
450 strbuf_release(&path);
451 }
452
453 packdir = mkpathdup("%s/pack", repo_get_object_directory(repo));
454 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
455 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
456
457 existing.repo = repo;
458 existing_packs_collect(&existing, &keep_pack_list);
459
460 if (geometry.split_factor) {
461 if (pack_everything)
462 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
463 if (write_midx == REPACK_WRITE_MIDX_INCREMENTAL) {
464 geometry.midx_layer_threshold = config_ctx.midx_new_layer_threshold;
465 geometry.midx_layer_threshold_set = true;
466 }
467 pack_geometry_init(&geometry, &existing, &po_args);
468 pack_geometry_split(&geometry);
469 }
470
471 prepare_pack_objects(&cmd, &po_args, packtmp);
472
473 show_progress = !po_args.quiet && isatty(2);
474
475 strvec_push(&cmd.args, "--keep-true-parents");
476 for (i = 0; i < keep_pack_list.nr; i++)
477 strvec_pushf(&cmd.args, "--keep-pack=%s",
478 keep_pack_list.items[i].string);
479 strvec_push(&cmd.args, "--non-empty");
480 if (!geometry.split_factor) {
481 /*
482 * We need to grab all reachable objects, including those that
483 * are reachable from reflogs and the index.
484 *
485 * When repacking into a geometric progression of packs,
486 * however, we ask 'git pack-objects --stdin-packs', and it is
487 * not about packing objects based on reachability but about
488 * repacking all the objects in specified packs and loose ones
489 * (indeed, --stdin-packs is incompatible with these options).
490 */
491 strvec_push(&cmd.args, "--all");
492 strvec_push(&cmd.args, "--reflog");
493 strvec_push(&cmd.args, "--indexed-objects");
494 }
495 if (repo_has_promisor_remote(repo))
496 strvec_push(&cmd.args, "--exclude-promisor-objects");
497 if (write_midx == REPACK_WRITE_MIDX_NONE) {
498 if (write_bitmaps > 0)
499 strvec_push(&cmd.args, "--write-bitmap-index");
500 else if (write_bitmaps < 0)
501 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
502 }
503 if (use_delta_islands)
504 strvec_push(&cmd.args, "--delta-islands");
505
506 if (pack_everything & ALL_INTO_ONE) {
507 repack_promisor_objects(repo, &po_args, &names, packtmp,
508 (drop_filtered && !dry_run) ? &drop_oids : NULL);
509
510 if (existing_packs_has_non_kept(&existing) &&
511 delete_redundant &&
512 !(pack_everything & PACK_CRUFT)) {
513 for_each_string_list_item(item, &names) {
514 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
515 packtmp_name, item->string);
516 }
517 if (unpack_unreachable) {
518 strvec_pushf(&cmd.args,
519 "--unpack-unreachable=%s",
520 unpack_unreachable);
521 } else if (pack_everything & LOOSEN_UNREACHABLE) {
522 strvec_push(&cmd.args,
523 "--unpack-unreachable");
524 } else if (keep_unreachable) {
525 strvec_push(&cmd.args, "--keep-unreachable");
526 }
527 }
528
529 if (keep_unreachable && delete_redundant &&
530 !(pack_everything & PACK_CRUFT))
531 strvec_push(&cmd.args, "--pack-loose-unreachable");
532 } else if (geometry.split_factor) {
533 pack_geometry_repack_promisors(repo, &po_args, &geometry,
534 &names, packtmp);
535
536 if (midx_must_contain_cruft)
537 strvec_push(&cmd.args, "--stdin-packs");
538 else
539 strvec_push(&cmd.args, "--stdin-packs=follow");
540 strvec_push(&cmd.args, "--unpacked");
541 } else {
542 strvec_push(&cmd.args, "--unpacked");
543 strvec_push(&cmd.args, "--incremental");
544 }
545
546 if (po_args.filter_options.choice)
547 strvec_pushf(&cmd.args, "--filter=%s",
548 expand_list_objects_filter_spec(&po_args.filter_options));
549 else if (filter_to)
550 die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
551
552 if (geometry.split_factor)
553 cmd.in = -1;
554 else
555 cmd.no_stdin = 1;
556
557 ret = start_command(&cmd);
558 if (ret)
559 goto cleanup;
560
561 if (geometry.split_factor) {
562 FILE *in = xfdopen(cmd.in, "w");
563 /*
564 * The resulting pack should contain all objects in packs that
565 * are going to be rolled up, but exclude objects in packs which
566 * are being left alone.
567 */
568 for (i = 0; i < geometry.split; i++)
569 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
570 for (i = geometry.split; i < geometry.pack_nr; i++) {
571 const char *basename = pack_basename(geometry.pack[i]);
572 char marker = '^';
573
574 if (!midx_must_contain_cruft &&
575 !string_list_has_string(&existing.midx_packs,
576 basename)) {
577 /*
578 * Assume non-MIDX'd packs are not
579 * necessarily closed under
580 * reachability.
581 */
582 marker = '!';
583 }
584
585 fprintf(in, "%c%s\n", marker, basename);
586 }
587 fclose(in);
588 }
589
590 {
591 struct write_pack_opts opts = {
592 .packdir = packdir,
593 .destination = packdir,
594 .packtmp = packtmp,
595 };
596 ret = finish_pack_objects_cmd(repo->hash_algo, &opts, &cmd,
597 &names);
598 if (ret)
599 goto cleanup;
600 }
601
602 if (!names.nr) {
603 struct odb_source_files *files = odb_source_files_downcast(existing.source);
604
605 if (!po_args.quiet)
606 printf_ln(_("Nothing new to pack."));
607 /*
608 * If we didn't write any new packs, the non-cruft packs
609 * may refer to once-unreachable objects in the cruft
610 * pack(s).
611 *
612 * If there isn't already a MIDX, the one we write
613 * must include the cruft pack(s), in case the
614 * non-cruft pack(s) refer to once-cruft objects.
615 *
616 * If there is already a MIDX, we can punt here, since
617 * midx_has_unknown_packs() will make the decision for
618 * us.
619 */
620 if (!get_multi_pack_index(files->packed))
621 midx_must_contain_cruft = 1;
622 }
623
624 if (pack_everything & PACK_CRUFT) {
625 struct write_pack_opts opts = {
626 .po_args = &cruft_po_args,
627 .destination = packtmp,
628 .packtmp = packtmp,
629 .packdir = packdir,
630 };
631
632 if (!cruft_po_args.window)
633 cruft_po_args.window = xstrdup_or_null(po_args.window);
634 if (!cruft_po_args.window_memory)
635 cruft_po_args.window_memory = xstrdup_or_null(po_args.window_memory);
636 if (!cruft_po_args.depth)
637 cruft_po_args.depth = xstrdup_or_null(po_args.depth);
638 if (!cruft_po_args.threads)
639 cruft_po_args.threads = xstrdup_or_null(po_args.threads);
640 if (!cruft_po_args.max_pack_size)
641 cruft_po_args.max_pack_size = po_args.max_pack_size;
642
643 cruft_po_args.local = po_args.local;
644 cruft_po_args.quiet = po_args.quiet;
645 cruft_po_args.delta_base_offset = po_args.delta_base_offset;
646 cruft_po_args.pack_kept_objects = 0;
647
648 ret = write_cruft_pack(&opts, cruft_expiration,
649 combine_cruft_below_size, &names,
650 &existing);
651 if (ret)
652 goto cleanup;
653
654 if (delete_redundant && expire_to) {
655 /*
656 * If `--expire-to` is given with `-d`, it's possible
657 * that we're about to prune some objects. With cruft
658 * packs, pruning is implicit: any objects from existing
659 * packs that weren't picked up by new packs are removed
660 * when their packs are deleted.
661 *
662 * Generate an additional cruft pack, with one twist:
663 * `names` now includes the name of the cruft pack
664 * written in the previous step. So the contents of
665 * _this_ cruft pack exclude everything contained in the
666 * existing cruft pack (that is, all of the unreachable
667 * objects which are no older than
668 * `--cruft-expiration`).
669 *
670 * To make this work, cruft_expiration must become NULL
671 * so that this cruft pack doesn't actually prune any
672 * objects. If it were non-NULL, this call would always
673 * generate an empty pack (since every object not in the
674 * cruft pack generated above will have an mtime older
675 * than the expiration).
676 *
677 * Pretend we don't have a `--combine-cruft-below-size`
678 * argument, since we're not selectively combining
679 * anything based on size to generate the limbo cruft
680 * pack, but rather removing all cruft packs from the
681 * main repository regardless of size.
682 */
683 opts.destination = expire_to;
684 ret = write_cruft_pack(&opts, NULL, 0ul, &names,
685 &existing);
686 if (ret)
687 goto cleanup;
688 }
689 }
690
691 if (po_args.filter_options.choice && !drop_filtered) {
692 struct write_pack_opts opts = {
693 .po_args = &po_args,
694 .destination = filter_to,
695 .packdir = packdir,
696 .packtmp = packtmp,
697 };
698
699 if (!opts.destination)
700 opts.destination = packtmp;
701
702 ret = write_filtered_pack(&opts, &existing, &names);
703 if (ret)
704 goto cleanup;
705 }
706
707 string_list_sort(&names);
708
709 odb_close(repo->objects);
710
711 /*
712 * Ok we have prepared all new packfiles.
713 */
714 for_each_string_list_item(item, &names)
715 generated_pack_install(item->util, item->string, packdir,
716 packtmp);
717 /* End of pack replacement. */
718
719 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
720 if (write_midx == REPACK_WRITE_MIDX_INCREMENTAL)
721 existing_packs_retain_midx_packs(&existing);
722 existing_packs_mark_for_deletion(&existing, &names);
723 }
724
725 if (write_midx != REPACK_WRITE_MIDX_NONE) {
726 struct repack_write_midx_opts opts = {
727 .existing = &existing,
728 .geometry = &geometry,
729 .names = &names,
730 .refs_snapshot = refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
731 .packdir = packdir,
732 .show_progress = show_progress,
733 .write_bitmaps = write_bitmaps > 0,
734 .midx_must_contain_cruft = midx_must_contain_cruft,
735 .midx_split_factor = config_ctx.midx_split_factor,
736 .midx_new_layer_threshold = config_ctx.midx_new_layer_threshold,
737 .mode = write_midx,
738 };
739
740 ret = repack_write_midx(&opts);
741 if (ret)
742 goto cleanup;
743 }
744
745 odb_reprepare(repo->objects);
746
747 if (delete_redundant) {
748 int opts = 0;
749 bool wrote_incremental_midx = write_midx == REPACK_WRITE_MIDX_INCREMENTAL;
750
751 existing_packs_remove_redundant(&existing, packdir,
752 wrote_incremental_midx);
753
754 if (geometry.split_factor)
755 pack_geometry_remove_redundant(&geometry, &names,
756 &existing, packdir,
757 wrote_incremental_midx);
758 if (show_progress)
759 opts |= PRUNE_PACKED_VERBOSE;
760 prune_packed_objects(opts);
761
762 if (!keep_unreachable &&
763 (!(pack_everything & LOOSEN_UNREACHABLE) ||
764 unpack_unreachable) &&
765 is_repository_shallow(repo))
766 prune_shallow(PRUNE_QUICK);
767 }
768
769 if (run_update_server_info)
770 update_server_info(repo, 0);
771
772 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
773 struct odb_source_files *files = odb_source_files_downcast(existing.source);
774 unsigned flags = 0;
775
776 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
777 flags |= MIDX_WRITE_INCREMENTAL;
778 write_midx_file(files->packed, NULL, NULL, flags);
779 }
780
781 cleanup:
782 string_list_clear(&keep_pack_list, 0);
783 string_list_clear(&names, 1);
784 oidset_clear(&drop_oids);
785 existing_packs_release(&existing);
786 pack_geometry_release(&geometry);
787 pack_objects_args_release(&po_args);
788 pack_objects_args_release(&cruft_po_args);
789
790 return ret;
791 }