interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / repack.c

797 lines · 26036 bytes Raw Blame History
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
30static int pack_everything;
31static int write_bitmaps = -1;
32static int use_delta_islands;
33static int run_update_server_info = 1;
34static char *packdir, *packtmp_name, *packtmp;
35static int midx_must_contain_cruft = 1;
36static int drop_filtered;
37static int dry_run;
38static int write_bitmaps_given;
39
40static 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
48static 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
56struct 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
63static 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
123static 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
138static 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
158int 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.
355 */
356 delete_redundant = 1;
357
358 ret = enumerate_promisor_blobs(repo, &po_args.filter_options, &drop_oids);
359
360 if (ret)
361 goto cleanup;
362
363 /*
364 * Refuse to drop blobs that the current index references.
365 * Such a blob would only be lazily re-fetched by the next
366 * command that touches the worktree, so dropping it reclaims
367 * nothing. This guard just avoids that churn. Bare
368 * repositories have no index, so the check is skipped there.
369 */
370 if (!is_bare_repository(repo) && oidset_size(&drop_oids)) {
371 struct index_state *istate = repo->index;
372 unsigned int i;
373
374 if (repo_read_index(repo) < 0)
375 die(_("could not read the index"));
376
377 for (i = 0; i < istate->cache_nr; i++) {
378 const struct cache_entry *ce = istate->cache[i];
379
380 if (oidset_contains(&drop_oids, &ce->oid))
381 die(_("cannot drop '%s' (%s): it is referenced by the current index"),
382 ce->name, oid_to_hex(&ce->oid));
383 }
384 }
385
386 if (dry_run) {
387 struct oidset_iter iter;
388 const struct object_id *oid;
389
390 oidset_iter_init(&drop_oids, &iter);
391 while ((oid = oidset_iter_next(&iter)))
392 printf("%s\n", oid_to_hex(oid));
393
394 /*
395 * Only list the candidates to be dropped; skip
396 * the actual repack
397 */
398 ret = 0;
399 goto cleanup;
400 }
401 }
402
403 if (delete_redundant && repo->repository_format_precious_objects)
404 die(_("cannot delete packs in a precious-objects repo"));
405
406 die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A",
407 keep_unreachable, "-k/--keep-unreachable",
408 pack_everything & PACK_CRUFT, "--cruft");
409
410 if (pack_everything & PACK_CRUFT)
411 pack_everything |= ALL_INTO_ONE;
412
413 if (write_bitmaps < 0) {
414 if (write_midx == REPACK_WRITE_MIDX_NONE &&
415 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository(repo)))
416 write_bitmaps = 0;
417 }
418 if (po_args.pack_kept_objects < 0)
419 po_args.pack_kept_objects = write_bitmaps > 0 &&
420 write_midx == REPACK_WRITE_MIDX_NONE;
421
422 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) &&
423 write_midx == REPACK_WRITE_MIDX_NONE)
424 die(_(incremental_bitmap_conflict_error));
425
426 if (write_bitmaps && po_args.local &&
427 odb_has_alternates(repo->objects)) {
428 /*
429 * When asked to do a local repack, but we have
430 * packfiles that are inherited from an alternate, then
431 * we cannot guarantee that the multi-pack-index would
432 * have full coverage of all objects. We thus disable
433 * writing bitmaps in that case.
434 */
435 warning(_("disabling bitmap writing, as some objects are not being packed"));
436 write_bitmaps = 0;
437 }
438
439 if (config_ctx.midx_split_factor < 2)
440 die(_("invalid value for %s: %d"), "--midx-split-factor",
441 config_ctx.midx_split_factor);
442 if (config_ctx.midx_new_layer_threshold < 1)
443 die(_("invalid value for %s: %d"), "--midx-new-layer-threshold",
444 config_ctx.midx_new_layer_threshold);
445
446 if (write_midx != REPACK_WRITE_MIDX_NONE && write_bitmaps) {
447 struct strbuf path = STRBUF_INIT;
448
449 strbuf_addf(&path, "%s/%s_XXXXXX",
450 repo_get_object_directory(repo),
451 "bitmap-ref-tips");
452
453 refs_snapshot = xmks_tempfile(path.buf);
454 midx_snapshot_refs(repo, refs_snapshot);
455
456 strbuf_release(&path);
457 }
458
459 packdir = mkpathdup("%s/pack", repo_get_object_directory(repo));
460 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
461 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
462
463 existing.repo = repo;
464 existing_packs_collect(&existing, &keep_pack_list);
465
466 if (geometry.split_factor) {
467 if (pack_everything)
468 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
469 if (write_midx == REPACK_WRITE_MIDX_INCREMENTAL) {
470 geometry.midx_layer_threshold = config_ctx.midx_new_layer_threshold;
471 geometry.midx_layer_threshold_set = true;
472 }
473 pack_geometry_init(&geometry, &existing, &po_args);
474 pack_geometry_split(&geometry);
475 }
476
477 prepare_pack_objects(&cmd, &po_args, packtmp);
478
479 show_progress = !po_args.quiet && isatty(2);
480
481 strvec_push(&cmd.args, "--keep-true-parents");
482 for (i = 0; i < keep_pack_list.nr; i++)
483 strvec_pushf(&cmd.args, "--keep-pack=%s",
484 keep_pack_list.items[i].string);
485 strvec_push(&cmd.args, "--non-empty");
486 if (!geometry.split_factor) {
487 /*
488 * We need to grab all reachable objects, including those that
489 * are reachable from reflogs and the index.
490 *
491 * When repacking into a geometric progression of packs,
492 * however, we ask 'git pack-objects --stdin-packs', and it is
493 * not about packing objects based on reachability but about
494 * repacking all the objects in specified packs and loose ones
495 * (indeed, --stdin-packs is incompatible with these options).
496 */
497 strvec_push(&cmd.args, "--all");
498 strvec_push(&cmd.args, "--reflog");
499 strvec_push(&cmd.args, "--indexed-objects");
500 }
501 if (repo_has_promisor_remote(repo))
502 strvec_push(&cmd.args, "--exclude-promisor-objects");
503 if (write_midx == REPACK_WRITE_MIDX_NONE) {
504 if (write_bitmaps > 0)
505 strvec_push(&cmd.args, "--write-bitmap-index");
506 else if (write_bitmaps < 0)
507 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
508 }
509 if (use_delta_islands)
510 strvec_push(&cmd.args, "--delta-islands");
511
512 if (pack_everything & ALL_INTO_ONE) {
513 repack_promisor_objects(repo, &po_args, &names, packtmp,
514 drop_filtered ? &drop_oids : NULL);
515
516 if (existing_packs_has_non_kept(&existing) &&
517 delete_redundant &&
518 !(pack_everything & PACK_CRUFT)) {
519 for_each_string_list_item(item, &names) {
520 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
521 packtmp_name, item->string);
522 }
523 if (unpack_unreachable) {
524 strvec_pushf(&cmd.args,
525 "--unpack-unreachable=%s",
526 unpack_unreachable);
527 } else if (pack_everything & LOOSEN_UNREACHABLE) {
528 strvec_push(&cmd.args,
529 "--unpack-unreachable");
530 } else if (keep_unreachable) {
531 strvec_push(&cmd.args, "--keep-unreachable");
532 }
533 }
534
535 if (keep_unreachable && delete_redundant &&
536 !(pack_everything & PACK_CRUFT))
537 strvec_push(&cmd.args, "--pack-loose-unreachable");
538 } else if (geometry.split_factor) {
539 pack_geometry_repack_promisors(repo, &po_args, &geometry,
540 &names, packtmp);
541
542 if (midx_must_contain_cruft)
543 strvec_push(&cmd.args, "--stdin-packs");
544 else
545 strvec_push(&cmd.args, "--stdin-packs=follow");
546 strvec_push(&cmd.args, "--unpacked");
547 } else {
548 strvec_push(&cmd.args, "--unpacked");
549 strvec_push(&cmd.args, "--incremental");
550 }
551
552 if (po_args.filter_options.choice)
553 strvec_pushf(&cmd.args, "--filter=%s",
554 expand_list_objects_filter_spec(&po_args.filter_options));
555 else if (filter_to)
556 die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
557
558 if (geometry.split_factor)
559 cmd.in = -1;
560 else
561 cmd.no_stdin = 1;
562
563 ret = start_command(&cmd);
564 if (ret)
565 goto cleanup;
566
567 if (geometry.split_factor) {
568 FILE *in = xfdopen(cmd.in, "w");
569 /*
570 * The resulting pack should contain all objects in packs that
571 * are going to be rolled up, but exclude objects in packs which
572 * are being left alone.
573 */
574 for (i = 0; i < geometry.split; i++)
575 fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
576 for (i = geometry.split; i < geometry.pack_nr; i++) {
577 const char *basename = pack_basename(geometry.pack[i]);
578 char marker = '^';
579
580 if (!midx_must_contain_cruft &&
581 !string_list_has_string(&existing.midx_packs,
582 basename)) {
583 /*
584 * Assume non-MIDX'd packs are not
585 * necessarily closed under
586 * reachability.
587 */
588 marker = '!';
589 }
590
591 fprintf(in, "%c%s\n", marker, basename);
592 }
593 fclose(in);
594 }
595
596 {
597 struct write_pack_opts opts = {
598 .packdir = packdir,
599 .destination = packdir,
600 .packtmp = packtmp,
601 };
602 ret = finish_pack_objects_cmd(repo->hash_algo, &opts, &cmd,
603 &names);
604 if (ret)
605 goto cleanup;
606 }
607
608 if (!names.nr) {
609 struct odb_source_files *files = odb_source_files_downcast(existing.source);
610
611 if (!po_args.quiet)
612 printf_ln(_("Nothing new to pack."));
613 /*
614 * If we didn't write any new packs, the non-cruft packs
615 * may refer to once-unreachable objects in the cruft
616 * pack(s).
617 *
618 * If there isn't already a MIDX, the one we write
619 * must include the cruft pack(s), in case the
620 * non-cruft pack(s) refer to once-cruft objects.
621 *
622 * If there is already a MIDX, we can punt here, since
623 * midx_has_unknown_packs() will make the decision for
624 * us.
625 */
626 if (!get_multi_pack_index(files->dirs->packed))
627 midx_must_contain_cruft = 1;
628 }
629
630 if (pack_everything & PACK_CRUFT) {
631 struct write_pack_opts opts = {
632 .po_args = &cruft_po_args,
633 .destination = packtmp,
634 .packtmp = packtmp,
635 .packdir = packdir,
636 };
637
638 if (!cruft_po_args.window)
639 cruft_po_args.window = xstrdup_or_null(po_args.window);
640 if (!cruft_po_args.window_memory)
641 cruft_po_args.window_memory = xstrdup_or_null(po_args.window_memory);
642 if (!cruft_po_args.depth)
643 cruft_po_args.depth = xstrdup_or_null(po_args.depth);
644 if (!cruft_po_args.threads)
645 cruft_po_args.threads = xstrdup_or_null(po_args.threads);
646 if (!cruft_po_args.max_pack_size)
647 cruft_po_args.max_pack_size = po_args.max_pack_size;
648
649 cruft_po_args.local = po_args.local;
650 cruft_po_args.quiet = po_args.quiet;
651 cruft_po_args.delta_base_offset = po_args.delta_base_offset;
652 cruft_po_args.pack_kept_objects = 0;
653
654 ret = write_cruft_pack(&opts, cruft_expiration,
655 combine_cruft_below_size, &names,
656 &existing);
657 if (ret)
658 goto cleanup;
659
660 if (delete_redundant && expire_to) {
661 /*
662 * If `--expire-to` is given with `-d`, it's possible
663 * that we're about to prune some objects. With cruft
664 * packs, pruning is implicit: any objects from existing
665 * packs that weren't picked up by new packs are removed
666 * when their packs are deleted.
667 *
668 * Generate an additional cruft pack, with one twist:
669 * `names` now includes the name of the cruft pack
670 * written in the previous step. So the contents of
671 * _this_ cruft pack exclude everything contained in the
672 * existing cruft pack (that is, all of the unreachable
673 * objects which are no older than
674 * `--cruft-expiration`).
675 *
676 * To make this work, cruft_expiration must become NULL
677 * so that this cruft pack doesn't actually prune any
678 * objects. If it were non-NULL, this call would always
679 * generate an empty pack (since every object not in the
680 * cruft pack generated above will have an mtime older
681 * than the expiration).
682 *
683 * Pretend we don't have a `--combine-cruft-below-size`
684 * argument, since we're not selectively combining
685 * anything based on size to generate the limbo cruft
686 * pack, but rather removing all cruft packs from the
687 * main repository regardless of size.
688 */
689 opts.destination = expire_to;
690 ret = write_cruft_pack(&opts, NULL, 0ul, &names,
691 &existing);
692 if (ret)
693 goto cleanup;
694 }
695 }
696
697 if (po_args.filter_options.choice && !drop_filtered) {
698 struct write_pack_opts opts = {
699 .po_args = &po_args,
700 .destination = filter_to,
701 .packdir = packdir,
702 .packtmp = packtmp,
703 };
704
705 if (!opts.destination)
706 opts.destination = packtmp;
707
708 ret = write_filtered_pack(&opts, &existing, &names);
709 if (ret)
710 goto cleanup;
711 }
712
713 string_list_sort(&names);
714
715 odb_close(repo->objects);
716
717 /*
718 * Ok we have prepared all new packfiles.
719 */
720 for_each_string_list_item(item, &names)
721 generated_pack_install(item->util, item->string, packdir,
722 packtmp);
723 /* End of pack replacement. */
724
725 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
726 if (write_midx == REPACK_WRITE_MIDX_INCREMENTAL)
727 existing_packs_retain_midx_packs(&existing);
728 existing_packs_mark_for_deletion(&existing, &names);
729 }
730
731 if (write_midx != REPACK_WRITE_MIDX_NONE) {
732 struct repack_write_midx_opts opts = {
733 .existing = &existing,
734 .geometry = &geometry,
735 .names = &names,
736 .refs_snapshot = refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
737 .packdir = packdir,
738 .show_progress = show_progress,
739 .write_bitmaps = write_bitmaps > 0,
740 .midx_must_contain_cruft = midx_must_contain_cruft,
741 .midx_split_factor = config_ctx.midx_split_factor,
742 .midx_new_layer_threshold = config_ctx.midx_new_layer_threshold,
743 .mode = write_midx,
744 };
745
746 ret = repack_write_midx(&opts);
747 if (ret)
748 goto cleanup;
749 }
750
751 odb_reprepare(repo->objects);
752
753 if (delete_redundant) {
754 int opts = 0;
755 bool wrote_incremental_midx = write_midx == REPACK_WRITE_MIDX_INCREMENTAL;
756
757 existing_packs_remove_redundant(&existing, packdir,
758 wrote_incremental_midx);
759
760 if (geometry.split_factor)
761 pack_geometry_remove_redundant(&geometry, &names,
762 &existing, packdir,
763 wrote_incremental_midx);
764 if (show_progress)
765 opts |= PRUNE_PACKED_VERBOSE;
766 prune_packed_objects(repo, opts);
767
768 if (!keep_unreachable &&
769 (!(pack_everything & LOOSEN_UNREACHABLE) ||
770 unpack_unreachable) &&
771 is_repository_shallow(repo))
772 prune_shallow(PRUNE_QUICK);
773 }
774
775 if (run_update_server_info)
776 update_server_info(repo, 0);
777
778 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
779 struct odb_source_files *files = odb_source_files_downcast(existing.source);
780 unsigned flags = 0;
781
782 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
783 flags |= MIDX_WRITE_INCREMENTAL;
784 write_midx_file(files->dirs->packed, NULL, NULL, flags);
785 }
786
787cleanup:
788 string_list_clear(&keep_pack_list, 0);
789 string_list_clear(&names, 1);
790 oidset_clear(&drop_oids);
791 existing_packs_release(&existing);
792 pack_geometry_release(&geometry);
793 pack_objects_args_release(&po_args);
794 pack_objects_args_release(&cruft_po_args);
795
796 return ret;
797}