1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
7 #include "environment.h"
10 #include "parse-options.h"
12 #include "run-command.h"
13 #include "server-info.h"
15 #include "string-list.h"
19 #include "prune-packed.h"
21 #include "promisor-remote.h"
24 #include "pack-bitmap.h"
26 #include "list-objects-filter-options.h"
28 #define ALL_INTO_ONE 1
29 #define LOOSEN_UNREACHABLE 2
35 static int pack_everything
;
36 static int delta_base_offset
= 1;
37 static int pack_kept_objects
= -1;
38 static int write_bitmaps
= -1;
39 static int use_delta_islands
;
40 static int run_update_server_info
= 1;
41 static char *packdir
, *packtmp_name
, *packtmp
;
42 static int midx_must_contain_cruft
= 1;
44 static const char *const git_repack_usage
[] = {
45 N_("git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]\n"
46 "[--window=<n>] [--depth=<n>] [--threads=<n>] [--keep-pack=<pack-name>]\n"
47 "[--write-midx] [--name-hash-version=<n>] [--path-walk]"),
51 static const char incremental_bitmap_conflict_error
[] = N_(
52 "Incremental repacks are incompatible with bitmap indexes. Use\n"
53 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
56 struct pack_objects_args
{
61 unsigned long max_pack_size
;
66 int name_hash_version
;
68 struct list_objects_filter_options filter_options
;
71 static int repack_config(const char *var
, const char *value
,
72 const struct config_context
*ctx
, void *cb
)
74 struct pack_objects_args
*cruft_po_args
= cb
;
75 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
76 delta_base_offset
= git_config_bool(var
, value
);
79 if (!strcmp(var
, "repack.packkeptobjects")) {
80 pack_kept_objects
= git_config_bool(var
, value
);
83 if (!strcmp(var
, "repack.writebitmaps") ||
84 !strcmp(var
, "pack.writebitmaps")) {
85 write_bitmaps
= git_config_bool(var
, value
);
88 if (!strcmp(var
, "repack.usedeltaislands")) {
89 use_delta_islands
= git_config_bool(var
, value
);
92 if (strcmp(var
, "repack.updateserverinfo") == 0) {
93 run_update_server_info
= git_config_bool(var
, value
);
96 if (!strcmp(var
, "repack.cruftwindow")) {
97 free(cruft_po_args
->window
);
98 return git_config_string(&cruft_po_args
->window
, var
, value
);
100 if (!strcmp(var
, "repack.cruftwindowmemory")) {
101 free(cruft_po_args
->window_memory
);
102 return git_config_string(&cruft_po_args
->window_memory
, var
, value
);
104 if (!strcmp(var
, "repack.cruftdepth")) {
105 free(cruft_po_args
->depth
);
106 return git_config_string(&cruft_po_args
->depth
, var
, value
);
108 if (!strcmp(var
, "repack.cruftthreads")) {
109 free(cruft_po_args
->threads
);
110 return git_config_string(&cruft_po_args
->threads
, var
, value
);
112 if (!strcmp(var
, "repack.midxmustcontaincruft")) {
113 midx_must_contain_cruft
= git_config_bool(var
, value
);
116 return git_default_config(var
, value
, ctx
, cb
);
119 static void pack_objects_args_release(struct pack_objects_args
*args
)
122 free(args
->window_memory
);
125 list_objects_filter_release(&args
->filter_options
);
128 struct existing_packs
{
129 struct string_list kept_packs
;
130 struct string_list non_kept_packs
;
131 struct string_list cruft_packs
;
134 #define EXISTING_PACKS_INIT { \
135 .kept_packs = STRING_LIST_INIT_DUP, \
136 .non_kept_packs = STRING_LIST_INIT_DUP, \
137 .cruft_packs = STRING_LIST_INIT_DUP, \
140 static int has_existing_non_kept_packs(const struct existing_packs
*existing
)
142 return existing
->non_kept_packs
.nr
|| existing
->cruft_packs
.nr
;
145 static void pack_mark_for_deletion(struct string_list_item
*item
)
147 item
->util
= (void*)((uintptr_t)item
->util
| DELETE_PACK
);
150 static void pack_unmark_for_deletion(struct string_list_item
*item
)
152 item
->util
= (void*)((uintptr_t)item
->util
& ~DELETE_PACK
);
155 static int pack_is_marked_for_deletion(struct string_list_item
*item
)
157 return (uintptr_t)item
->util
& DELETE_PACK
;
160 static void pack_mark_retained(struct string_list_item
*item
)
162 item
->util
= (void*)((uintptr_t)item
->util
| RETAIN_PACK
);
165 static int pack_is_retained(struct string_list_item
*item
)
167 return (uintptr_t)item
->util
& RETAIN_PACK
;
170 static void mark_packs_for_deletion_1(struct string_list
*names
,
171 struct string_list
*list
)
173 struct string_list_item
*item
;
174 const int hexsz
= the_hash_algo
->hexsz
;
176 for_each_string_list_item(item
, list
) {
178 size_t len
= strlen(item
->string
);
181 sha1
= item
->string
+ len
- hexsz
;
183 if (pack_is_retained(item
)) {
184 pack_unmark_for_deletion(item
);
185 } else if (!string_list_has_string(names
, sha1
)) {
187 * Mark this pack for deletion, which ensures
188 * that this pack won't be included in a MIDX
189 * (if `--write-midx` was given) and that we
190 * will actually delete this pack (if `-d` was
193 pack_mark_for_deletion(item
);
198 static void retain_cruft_pack(struct existing_packs
*existing
,
199 struct packed_git
*cruft
)
201 struct strbuf buf
= STRBUF_INIT
;
202 struct string_list_item
*item
;
204 strbuf_addstr(&buf
, pack_basename(cruft
));
205 strbuf_strip_suffix(&buf
, ".pack");
207 item
= string_list_lookup(&existing
->cruft_packs
, buf
.buf
);
209 BUG("could not find cruft pack '%s'", pack_basename(cruft
));
211 pack_mark_retained(item
);
212 strbuf_release(&buf
);
215 static void mark_packs_for_deletion(struct existing_packs
*existing
,
216 struct string_list
*names
)
219 mark_packs_for_deletion_1(names
, &existing
->non_kept_packs
);
220 mark_packs_for_deletion_1(names
, &existing
->cruft_packs
);
223 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
225 struct strbuf buf
= STRBUF_INIT
;
226 struct odb_source
*source
= the_repository
->objects
->sources
;
227 struct multi_pack_index
*m
= get_multi_pack_index(source
);
228 strbuf_addf(&buf
, "%s.pack", base_name
);
229 if (m
&& source
->local
&& midx_contains_pack(m
, buf
.buf
))
230 clear_midx_file(the_repository
);
231 strbuf_insertf(&buf
, 0, "%s/", dir_name
);
232 unlink_pack_path(buf
.buf
, 1);
233 strbuf_release(&buf
);
236 static void remove_redundant_packs_1(struct string_list
*packs
)
238 struct string_list_item
*item
;
239 for_each_string_list_item(item
, packs
) {
240 if (!pack_is_marked_for_deletion(item
))
242 remove_redundant_pack(packdir
, item
->string
);
246 static void remove_redundant_existing_packs(struct existing_packs
*existing
)
248 remove_redundant_packs_1(&existing
->non_kept_packs
);
249 remove_redundant_packs_1(&existing
->cruft_packs
);
252 static void existing_packs_release(struct existing_packs
*existing
)
254 string_list_clear(&existing
->kept_packs
, 0);
255 string_list_clear(&existing
->non_kept_packs
, 0);
256 string_list_clear(&existing
->cruft_packs
, 0);
260 * Adds all packs hex strings (pack-$HASH) to either packs->non_kept
261 * or packs->kept based on whether each pack has a corresponding
262 * .keep file or not. Packs without a .keep file are not to be kept
263 * if we are going to pack everything into one file.
265 static void collect_pack_filenames(struct existing_packs
*existing
,
266 const struct string_list
*extra_keep
)
268 struct packed_git
*p
;
269 struct strbuf buf
= STRBUF_INIT
;
271 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
278 base
= pack_basename(p
);
280 for (i
= 0; i
< extra_keep
->nr
; i
++)
281 if (!fspathcmp(base
, extra_keep
->items
[i
].string
))
285 strbuf_addstr(&buf
, base
);
286 strbuf_strip_suffix(&buf
, ".pack");
288 if ((extra_keep
->nr
> 0 && i
< extra_keep
->nr
) || p
->pack_keep
)
289 string_list_append(&existing
->kept_packs
, buf
.buf
);
290 else if (p
->is_cruft
)
291 string_list_append(&existing
->cruft_packs
, buf
.buf
);
293 string_list_append(&existing
->non_kept_packs
, buf
.buf
);
296 string_list_sort(&existing
->kept_packs
);
297 string_list_sort(&existing
->non_kept_packs
);
298 string_list_sort(&existing
->cruft_packs
);
299 strbuf_release(&buf
);
302 static void prepare_pack_objects(struct child_process
*cmd
,
303 const struct pack_objects_args
*args
,
306 strvec_push(&cmd
->args
, "pack-objects");
308 strvec_pushf(&cmd
->args
, "--window=%s", args
->window
);
309 if (args
->window_memory
)
310 strvec_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
312 strvec_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
314 strvec_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
315 if (args
->max_pack_size
)
316 strvec_pushf(&cmd
->args
, "--max-pack-size=%lu", args
->max_pack_size
);
317 if (args
->no_reuse_delta
)
318 strvec_pushf(&cmd
->args
, "--no-reuse-delta");
319 if (args
->no_reuse_object
)
320 strvec_pushf(&cmd
->args
, "--no-reuse-object");
321 if (args
->name_hash_version
)
322 strvec_pushf(&cmd
->args
, "--name-hash-version=%d", args
->name_hash_version
);
324 strvec_pushf(&cmd
->args
, "--path-walk");
326 strvec_push(&cmd
->args
, "--local");
328 strvec_push(&cmd
->args
, "--quiet");
329 if (delta_base_offset
)
330 strvec_push(&cmd
->args
, "--delta-base-offset");
331 strvec_push(&cmd
->args
, out
);
337 * Write oid to the given struct child_process's stdin, starting it first if
340 static int write_oid(const struct object_id
*oid
,
341 struct packed_git
*pack UNUSED
,
342 uint32_t pos UNUSED
, void *data
)
344 struct child_process
*cmd
= data
;
347 if (start_command(cmd
))
348 die(_("could not start pack-objects to repack promisor objects"));
351 if (write_in_full(cmd
->in
, oid_to_hex(oid
), the_hash_algo
->hexsz
) < 0 ||
352 write_in_full(cmd
->in
, "\n", 1) < 0)
353 die(_("failed to feed promisor objects to pack-objects"));
369 struct generated_pack_data
{
370 struct tempfile
*tempfiles
[ARRAY_SIZE(exts
)];
373 static struct generated_pack_data
*populate_pack_exts(const char *name
)
376 struct strbuf path
= STRBUF_INIT
;
377 struct generated_pack_data
*data
= xcalloc(1, sizeof(*data
));
380 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
382 strbuf_addf(&path
, "%s-%s%s", packtmp
, name
, exts
[i
].name
);
384 if (stat(path
.buf
, &statbuf
))
387 data
->tempfiles
[i
] = register_tempfile(path
.buf
);
390 strbuf_release(&path
);
394 static int has_pack_ext(const struct generated_pack_data
*data
,
398 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
399 if (strcmp(exts
[i
].name
, ext
))
401 return !!data
->tempfiles
[i
];
403 BUG("unknown pack extension: '%s'", ext
);
406 static void repack_promisor_objects(const struct pack_objects_args
*args
,
407 struct string_list
*names
)
409 struct child_process cmd
= CHILD_PROCESS_INIT
;
411 struct strbuf line
= STRBUF_INIT
;
413 prepare_pack_objects(&cmd
, args
, packtmp
);
417 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
418 * hints may result in suboptimal deltas in the resulting pack. See if
419 * the OIDs can be sent with fake paths such that pack-objects can use a
420 * {type -> existing pack order} ordering when computing deltas instead
421 * of a {type -> size} ordering, which may produce better deltas.
423 for_each_packed_object(the_repository
, write_oid
, &cmd
,
424 FOR_EACH_OBJECT_PROMISOR_ONLY
);
427 /* No packed objects; cmd was never started */
428 child_process_clear(&cmd
);
434 out
= xfdopen(cmd
.out
, "r");
435 while (strbuf_getline_lf(&line
, out
) != EOF
) {
436 struct string_list_item
*item
;
439 if (line
.len
!= the_hash_algo
->hexsz
)
440 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
441 item
= string_list_append(names
, line
.buf
);
444 * pack-objects creates the .pack and .idx files, but not the
445 * .promisor file. Create the .promisor file, which is empty.
447 * NEEDSWORK: fetch-pack sometimes generates non-empty
448 * .promisor files containing the ref names and associated
449 * hashes at the point of generation of the corresponding
450 * packfile, but this would not preserve their contents. Maybe
451 * concatenate the contents of all .promisor files instead of
452 * just creating a new empty file.
454 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
456 write_promisor_file(promisor_name
, NULL
, 0);
458 item
->util
= populate_pack_exts(item
->string
);
464 if (finish_command(&cmd
))
465 die(_("could not finish pack-objects to repack promisor objects"));
466 strbuf_release(&line
);
469 struct pack_geometry
{
470 struct packed_git
**pack
;
471 uint32_t pack_nr
, pack_alloc
;
477 static uint32_t geometry_pack_weight(struct packed_git
*p
)
479 if (open_pack_index(p
))
480 die(_("cannot open index for %s"), p
->pack_name
);
481 return p
->num_objects
;
484 static int geometry_cmp(const void *va
, const void *vb
)
486 uint32_t aw
= geometry_pack_weight(*(struct packed_git
**)va
),
487 bw
= geometry_pack_weight(*(struct packed_git
**)vb
);
496 static void init_pack_geometry(struct pack_geometry
*geometry
,
497 struct existing_packs
*existing
,
498 const struct pack_objects_args
*args
)
500 struct packed_git
*p
;
501 struct strbuf buf
= STRBUF_INIT
;
503 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
504 if (args
->local
&& !p
->pack_local
)
506 * When asked to only repack local packfiles we skip
507 * over any packfiles that are borrowed from alternate
508 * object directories.
512 if (!pack_kept_objects
) {
514 * Any pack that has its pack_keep bit set will
515 * appear in existing->kept_packs below, but
516 * this saves us from doing a more expensive
523 * The pack may be kept via the --keep-pack
524 * option; check 'existing->kept_packs' to
525 * determine whether to ignore it.
528 strbuf_addstr(&buf
, pack_basename(p
));
529 strbuf_strip_suffix(&buf
, ".pack");
531 if (string_list_has_string(&existing
->kept_packs
, buf
.buf
))
537 ALLOC_GROW(geometry
->pack
,
538 geometry
->pack_nr
+ 1,
539 geometry
->pack_alloc
);
541 geometry
->pack
[geometry
->pack_nr
] = p
;
545 QSORT(geometry
->pack
, geometry
->pack_nr
, geometry_cmp
);
546 strbuf_release(&buf
);
549 static void split_pack_geometry(struct pack_geometry
*geometry
)
553 off_t total_size
= 0;
555 if (!geometry
->pack_nr
) {
556 geometry
->split
= geometry
->pack_nr
;
561 * First, count the number of packs (in descending order of size) which
562 * already form a geometric progression.
564 for (i
= geometry
->pack_nr
- 1; i
> 0; i
--) {
565 struct packed_git
*ours
= geometry
->pack
[i
];
566 struct packed_git
*prev
= geometry
->pack
[i
- 1];
568 if (unsigned_mult_overflows(geometry
->split_factor
,
569 geometry_pack_weight(prev
)))
570 die(_("pack %s too large to consider in geometric "
574 if (geometry_pack_weight(ours
) <
575 geometry
->split_factor
* geometry_pack_weight(prev
))
583 * Move the split one to the right, since the top element in the
584 * last-compared pair can't be in the progression. Only do this
585 * when we split in the middle of the array (otherwise if we got
586 * to the end, then the split is in the right place).
592 * Then, anything to the left of 'split' must be in a new pack. But,
593 * creating that new pack may cause packs in the heavy half to no longer
594 * form a geometric progression.
596 * Compute an expected size of the new pack, and then determine how many
597 * packs in the heavy half need to be joined into it (if any) to restore
598 * the geometric progression.
600 for (i
= 0; i
< split
; i
++) {
601 struct packed_git
*p
= geometry
->pack
[i
];
603 if (unsigned_add_overflows(total_size
, geometry_pack_weight(p
)))
604 die(_("pack %s too large to roll up"), p
->pack_name
);
605 total_size
+= geometry_pack_weight(p
);
607 for (i
= split
; i
< geometry
->pack_nr
; i
++) {
608 struct packed_git
*ours
= geometry
->pack
[i
];
610 if (unsigned_mult_overflows(geometry
->split_factor
,
612 die(_("pack %s too large to roll up"), ours
->pack_name
);
614 if (geometry_pack_weight(ours
) <
615 geometry
->split_factor
* total_size
) {
616 if (unsigned_add_overflows(total_size
,
617 geometry_pack_weight(ours
)))
618 die(_("pack %s too large to roll up"),
622 total_size
+= geometry_pack_weight(ours
);
627 geometry
->split
= split
;
630 static struct packed_git
*get_preferred_pack(struct pack_geometry
*geometry
)
636 * No geometry means either an all-into-one repack (in which
637 * case there is only one pack left and it is the largest) or an
640 * If repacking incrementally, then we could check the size of
641 * all packs to determine which should be preferred, but leave
646 if (geometry
->split
== geometry
->pack_nr
)
650 * The preferred pack is the largest pack above the split line. In
651 * other words, it is the largest pack that does not get rolled up in
652 * the geometric repack.
654 for (i
= geometry
->pack_nr
; i
> geometry
->split
; i
--)
656 * A pack that is not local would never be included in a
657 * multi-pack index. We thus skip over any non-local packs.
659 if (geometry
->pack
[i
- 1]->pack_local
)
660 return geometry
->pack
[i
- 1];
665 static void geometry_remove_redundant_packs(struct pack_geometry
*geometry
,
666 struct string_list
*names
,
667 struct existing_packs
*existing
)
669 struct strbuf buf
= STRBUF_INIT
;
672 for (i
= 0; i
< geometry
->split
; i
++) {
673 struct packed_git
*p
= geometry
->pack
[i
];
674 if (string_list_has_string(names
, hash_to_hex(p
->hash
)))
678 strbuf_addstr(&buf
, pack_basename(p
));
679 strbuf_strip_suffix(&buf
, ".pack");
681 if ((p
->pack_keep
) ||
682 (string_list_has_string(&existing
->kept_packs
, buf
.buf
)))
685 remove_redundant_pack(packdir
, buf
.buf
);
688 strbuf_release(&buf
);
691 static void free_pack_geometry(struct pack_geometry
*geometry
)
696 free(geometry
->pack
);
699 static int midx_has_unknown_packs(char **midx_pack_names
,
700 size_t midx_pack_names_nr
,
701 struct string_list
*include
,
702 struct pack_geometry
*geometry
,
703 struct existing_packs
*existing
)
707 string_list_sort(include
);
709 for (i
= 0; i
< midx_pack_names_nr
; i
++) {
710 const char *pack_name
= midx_pack_names
[i
];
713 * Determine whether or not each MIDX'd pack from the existing
714 * MIDX (if any) is represented in the new MIDX. For each pack
715 * in the MIDX, it must either be:
717 * - In the "include" list of packs to be included in the new
718 * MIDX. Note this function is called before the include
719 * list is populated with any cruft pack(s).
721 * - Below the geometric split line (if using pack geometry),
722 * indicating that the pack won't be included in the new
723 * MIDX, but its contents were rolled up as part of the
726 * - In the existing non-kept packs list (if not using pack
727 * geometry), and marked as non-deleted.
729 if (string_list_has_string(include
, pack_name
)) {
731 } else if (geometry
) {
732 struct strbuf buf
= STRBUF_INIT
;
735 for (j
= 0; j
< geometry
->split
; j
++) {
737 strbuf_addstr(&buf
, pack_basename(geometry
->pack
[j
]));
738 strbuf_strip_suffix(&buf
, ".pack");
739 strbuf_addstr(&buf
, ".idx");
741 if (!strcmp(pack_name
, buf
.buf
)) {
742 strbuf_release(&buf
);
747 strbuf_release(&buf
);
749 if (j
< geometry
->split
)
752 struct string_list_item
*item
;
754 item
= string_list_lookup(&existing
->non_kept_packs
,
756 if (item
&& !pack_is_marked_for_deletion(item
))
761 * If we got to this point, the MIDX includes some pack that we
770 struct midx_snapshot_ref_data
{
776 static int midx_snapshot_ref_one(const char *refname UNUSED
,
777 const char *referent UNUSED
,
778 const struct object_id
*oid
,
779 int flag UNUSED
, void *_data
)
781 struct midx_snapshot_ref_data
*data
= _data
;
782 struct object_id peeled
;
784 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
787 if (oidset_insert(&data
->seen
, oid
))
788 return 0; /* already seen */
790 if (odb_read_object_info(the_repository
->objects
, oid
, NULL
) != OBJ_COMMIT
)
793 fprintf(data
->f
->fp
, "%s%s\n", data
->preferred
? "+" : "",
799 static void midx_snapshot_refs(struct tempfile
*f
)
801 struct midx_snapshot_ref_data data
;
802 const struct string_list
*preferred
= bitmap_preferred_tips(the_repository
);
806 oidset_init(&data
.seen
, 0);
808 if (!fdopen_tempfile(f
, "w"))
809 die(_("could not open tempfile %s for writing"),
810 get_tempfile_path(f
));
813 struct string_list_item
*item
;
816 for_each_string_list_item(item
, preferred
)
817 refs_for_each_ref_in(get_main_ref_store(the_repository
),
819 midx_snapshot_ref_one
, &data
);
823 refs_for_each_ref(get_main_ref_store(the_repository
),
824 midx_snapshot_ref_one
, &data
);
826 if (close_tempfile_gently(f
)) {
827 int save_errno
= errno
;
830 die_errno(_("could not close refs snapshot tempfile"));
833 oidset_clear(&data
.seen
);
836 static void midx_included_packs(struct string_list
*include
,
837 struct existing_packs
*existing
,
838 char **midx_pack_names
,
839 size_t midx_pack_names_nr
,
840 struct string_list
*names
,
841 struct pack_geometry
*geometry
)
843 struct string_list_item
*item
;
844 struct strbuf buf
= STRBUF_INIT
;
846 for_each_string_list_item(item
, &existing
->kept_packs
) {
848 strbuf_addf(&buf
, "%s.idx", item
->string
);
849 string_list_insert(include
, buf
.buf
);
852 for_each_string_list_item(item
, names
) {
854 strbuf_addf(&buf
, "pack-%s.idx", item
->string
);
855 string_list_insert(include
, buf
.buf
);
858 if (geometry
->split_factor
) {
861 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++) {
862 struct packed_git
*p
= geometry
->pack
[i
];
865 * The multi-pack index never refers to packfiles part
866 * of an alternate object database, so we skip these.
867 * While git-multi-pack-index(1) would silently ignore
868 * them anyway, this allows us to skip executing the
869 * command completely when we have only non-local
876 strbuf_addstr(&buf
, pack_basename(p
));
877 strbuf_strip_suffix(&buf
, ".pack");
878 strbuf_addstr(&buf
, ".idx");
880 string_list_insert(include
, buf
.buf
);
883 for_each_string_list_item(item
, &existing
->non_kept_packs
) {
884 if (pack_is_marked_for_deletion(item
))
888 strbuf_addf(&buf
, "%s.idx", item
->string
);
889 string_list_insert(include
, buf
.buf
);
893 if (midx_must_contain_cruft
||
894 midx_has_unknown_packs(midx_pack_names
, midx_pack_names_nr
,
895 include
, geometry
, existing
)) {
897 * If there are one or more unknown pack(s) present (see
898 * midx_has_unknown_packs() for what makes a pack
899 * "unknown") in the MIDX before the repack, keep them
900 * as they may be required to form a reachability
901 * closure if the MIDX is bitmapped.
903 * For example, a cruft pack can be required to form a
904 * reachability closure if the MIDX is bitmapped and one
905 * or more of the bitmap's selected commits reaches a
906 * once-cruft object that was later made reachable.
908 for_each_string_list_item(item
, &existing
->cruft_packs
) {
910 * When doing a --geometric repack, there is no
911 * need to check for deleted packs, since we're
912 * by definition not doing an ALL_INTO_ONE
913 * repack (hence no packs will be deleted).
914 * Otherwise we must check for and exclude any
915 * packs which are enqueued for deletion.
917 * So we could omit the conditional below in the
918 * --geometric case, but doing so is unnecessary
919 * since no packs are marked as pending
920 * deletion (since we only call
921 * `mark_packs_for_deletion()` when doing an
922 * all-into-one repack).
924 if (pack_is_marked_for_deletion(item
))
928 strbuf_addf(&buf
, "%s.idx", item
->string
);
929 string_list_insert(include
, buf
.buf
);
933 * Modern versions of Git (with the appropriate
934 * configuration setting) will write new copies of
935 * once-cruft objects when doing a --geometric repack.
937 * If the MIDX has no cruft pack, new packs written
938 * during a --geometric repack will not rely on the
939 * cruft pack to form a reachability closure, so we can
940 * avoid including them in the MIDX in that case.
945 strbuf_release(&buf
);
948 static int write_midx_included_packs(struct string_list
*include
,
949 struct pack_geometry
*geometry
,
950 struct string_list
*names
,
951 const char *refs_snapshot
,
952 int show_progress
, int write_bitmaps
)
954 struct child_process cmd
= CHILD_PROCESS_INIT
;
955 struct string_list_item
*item
;
956 struct packed_git
*preferred
= get_preferred_pack(geometry
);
966 strvec_push(&cmd
.args
, "multi-pack-index");
967 strvec_pushl(&cmd
.args
, "write", "--stdin-packs", NULL
);
970 strvec_push(&cmd
.args
, "--progress");
972 strvec_push(&cmd
.args
, "--no-progress");
975 strvec_push(&cmd
.args
, "--bitmap");
978 strvec_pushf(&cmd
.args
, "--preferred-pack=%s",
979 pack_basename(preferred
));
980 else if (names
->nr
) {
981 /* The largest pack was repacked, meaning that either
982 * one or two packs exist depending on whether the
983 * repository has a cruft pack or not.
985 * Select the non-cruft one as preferred to encourage
986 * pack-reuse among packs containing reachable objects
987 * over unreachable ones.
989 * (Note we could write multiple packs here if
990 * `--max-pack-size` was given, but any one of them
991 * will suffice, so pick the first one.)
993 for_each_string_list_item(item
, names
) {
994 struct generated_pack_data
*data
= item
->util
;
995 if (has_pack_ext(data
, ".mtimes"))
998 strvec_pushf(&cmd
.args
, "--preferred-pack=pack-%s.pack",
1004 * No packs were kept, and no packs were written. The
1005 * only thing remaining are .keep packs (unless
1006 * --pack-kept-objects was given).
1008 * Set the `--preferred-pack` arbitrarily here.
1014 strvec_pushf(&cmd
.args
, "--refs-snapshot=%s", refs_snapshot
);
1016 ret
= start_command(&cmd
);
1020 in
= xfdopen(cmd
.in
, "w");
1021 for_each_string_list_item(item
, include
)
1022 fprintf(in
, "%s\n", item
->string
);
1025 return finish_command(&cmd
);
1028 static void remove_redundant_bitmaps(struct string_list
*include
,
1029 const char *packdir
)
1031 struct strbuf path
= STRBUF_INIT
;
1032 struct string_list_item
*item
;
1035 strbuf_addstr(&path
, packdir
);
1036 strbuf_addch(&path
, '/');
1037 packdir_len
= path
.len
;
1040 * Remove any pack bitmaps corresponding to packs which are now
1041 * included in the MIDX.
1043 for_each_string_list_item(item
, include
) {
1044 strbuf_addstr(&path
, item
->string
);
1045 strbuf_strip_suffix(&path
, ".idx");
1046 strbuf_addstr(&path
, ".bitmap");
1048 if (unlink(path
.buf
) && errno
!= ENOENT
)
1049 warning_errno(_("could not remove stale bitmap: %s"),
1052 strbuf_setlen(&path
, packdir_len
);
1054 strbuf_release(&path
);
1057 static int finish_pack_objects_cmd(struct child_process
*cmd
,
1058 struct string_list
*names
,
1062 struct strbuf line
= STRBUF_INIT
;
1064 out
= xfdopen(cmd
->out
, "r");
1065 while (strbuf_getline_lf(&line
, out
) != EOF
) {
1066 struct string_list_item
*item
;
1068 if (line
.len
!= the_hash_algo
->hexsz
)
1069 die(_("repack: Expecting full hex object ID lines only "
1070 "from pack-objects."));
1072 * Avoid putting packs written outside of the repository in the
1076 item
= string_list_append(names
, line
.buf
);
1077 item
->util
= populate_pack_exts(line
.buf
);
1082 strbuf_release(&line
);
1084 return finish_command(cmd
);
1087 static int write_filtered_pack(const struct pack_objects_args
*args
,
1088 const char *destination
,
1089 const char *pack_prefix
,
1090 struct existing_packs
*existing
,
1091 struct string_list
*names
)
1093 struct child_process cmd
= CHILD_PROCESS_INIT
;
1094 struct string_list_item
*item
;
1098 const char *scratch
;
1099 int local
= skip_prefix(destination
, packdir
, &scratch
);
1101 prepare_pack_objects(&cmd
, args
, destination
);
1103 strvec_push(&cmd
.args
, "--stdin-packs");
1105 if (!pack_kept_objects
)
1106 strvec_push(&cmd
.args
, "--honor-pack-keep");
1107 for_each_string_list_item(item
, &existing
->kept_packs
)
1108 strvec_pushf(&cmd
.args
, "--keep-pack=%s", item
->string
);
1112 ret
= start_command(&cmd
);
1117 * Here 'names' contains only the pack(s) that were just
1118 * written, which is exactly the packs we want to keep. Also
1119 * 'existing_kept_packs' already contains the packs in
1122 in
= xfdopen(cmd
.in
, "w");
1123 for_each_string_list_item(item
, names
)
1124 fprintf(in
, "^%s-%s.pack\n", pack_prefix
, item
->string
);
1125 for_each_string_list_item(item
, &existing
->non_kept_packs
)
1126 fprintf(in
, "%s.pack\n", item
->string
);
1127 for_each_string_list_item(item
, &existing
->cruft_packs
)
1128 fprintf(in
, "%s.pack\n", item
->string
);
1129 caret
= pack_kept_objects
? "" : "^";
1130 for_each_string_list_item(item
, &existing
->kept_packs
)
1131 fprintf(in
, "%s%s.pack\n", caret
, item
->string
);
1134 return finish_pack_objects_cmd(&cmd
, names
, local
);
1137 static void combine_small_cruft_packs(FILE *in
, size_t combine_cruft_below_size
,
1138 struct existing_packs
*existing
)
1140 struct packed_git
*p
;
1141 struct strbuf buf
= STRBUF_INIT
;
1144 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
1145 if (!(p
->is_cruft
&& p
->pack_local
))
1149 strbuf_addstr(&buf
, pack_basename(p
));
1150 strbuf_strip_suffix(&buf
, ".pack");
1152 if (!string_list_has_string(&existing
->cruft_packs
, buf
.buf
))
1155 if (p
->pack_size
< combine_cruft_below_size
) {
1156 fprintf(in
, "-%s\n", pack_basename(p
));
1158 retain_cruft_pack(existing
, p
);
1159 fprintf(in
, "%s\n", pack_basename(p
));
1163 for (i
= 0; i
< existing
->non_kept_packs
.nr
; i
++)
1164 fprintf(in
, "-%s.pack\n",
1165 existing
->non_kept_packs
.items
[i
].string
);
1167 strbuf_release(&buf
);
1170 static int write_cruft_pack(const struct pack_objects_args
*args
,
1171 const char *destination
,
1172 const char *pack_prefix
,
1173 const char *cruft_expiration
,
1174 unsigned long combine_cruft_below_size
,
1175 struct string_list
*names
,
1176 struct existing_packs
*existing
)
1178 struct child_process cmd
= CHILD_PROCESS_INIT
;
1179 struct string_list_item
*item
;
1182 const char *scratch
;
1183 int local
= skip_prefix(destination
, packdir
, &scratch
);
1185 prepare_pack_objects(&cmd
, args
, destination
);
1187 strvec_push(&cmd
.args
, "--cruft");
1188 if (cruft_expiration
)
1189 strvec_pushf(&cmd
.args
, "--cruft-expiration=%s",
1192 strvec_push(&cmd
.args
, "--honor-pack-keep");
1193 strvec_push(&cmd
.args
, "--non-empty");
1197 ret
= start_command(&cmd
);
1202 * names has a confusing double use: it both provides the list
1203 * of just-written new packs, and accepts the name of the cruft
1204 * pack we are writing.
1206 * By the time it is read here, it contains only the pack(s)
1207 * that were just written, which is exactly the set of packs we
1208 * want to consider kept.
1210 * If `--expire-to` is given, the double-use served by `names`
1211 * ensures that the pack written to `--expire-to` excludes any
1212 * objects contained in the cruft pack.
1214 in
= xfdopen(cmd
.in
, "w");
1215 for_each_string_list_item(item
, names
)
1216 fprintf(in
, "%s-%s.pack\n", pack_prefix
, item
->string
);
1217 if (combine_cruft_below_size
&& !cruft_expiration
) {
1218 combine_small_cruft_packs(in
, combine_cruft_below_size
,
1221 for_each_string_list_item(item
, &existing
->non_kept_packs
)
1222 fprintf(in
, "-%s.pack\n", item
->string
);
1223 for_each_string_list_item(item
, &existing
->cruft_packs
)
1224 fprintf(in
, "-%s.pack\n", item
->string
);
1226 for_each_string_list_item(item
, &existing
->kept_packs
)
1227 fprintf(in
, "%s.pack\n", item
->string
);
1230 return finish_pack_objects_cmd(&cmd
, names
, local
);
1233 static const char *find_pack_prefix(const char *packdir
, const char *packtmp
)
1235 const char *pack_prefix
;
1236 if (!skip_prefix(packtmp
, packdir
, &pack_prefix
))
1237 die(_("pack prefix %s does not begin with objdir %s"),
1239 if (*pack_prefix
== '/')
1244 int cmd_repack(int argc
,
1247 struct repository
*repo UNUSED
)
1249 struct child_process cmd
= CHILD_PROCESS_INIT
;
1250 struct string_list_item
*item
;
1251 struct string_list names
= STRING_LIST_INIT_DUP
;
1252 struct existing_packs existing
= EXISTING_PACKS_INIT
;
1253 struct pack_geometry geometry
= { 0 };
1254 struct tempfile
*refs_snapshot
= NULL
;
1257 char **midx_pack_names
= NULL
;
1258 size_t midx_pack_names_nr
= 0;
1260 /* variables to be filled by option parsing */
1261 int delete_redundant
= 0;
1262 const char *unpack_unreachable
= NULL
;
1263 int keep_unreachable
= 0;
1264 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
1265 struct pack_objects_args po_args
= { 0 };
1266 struct pack_objects_args cruft_po_args
= { 0 };
1268 const char *cruft_expiration
= NULL
;
1269 const char *expire_to
= NULL
;
1270 const char *filter_to
= NULL
;
1271 const char *opt_window
= NULL
;
1272 const char *opt_window_memory
= NULL
;
1273 const char *opt_depth
= NULL
;
1274 const char *opt_threads
= NULL
;
1275 unsigned long combine_cruft_below_size
= 0ul;
1277 struct option builtin_repack_options
[] = {
1278 OPT_BIT('a', NULL
, &pack_everything
,
1279 N_("pack everything in a single pack"), ALL_INTO_ONE
),
1280 OPT_BIT('A', NULL
, &pack_everything
,
1281 N_("same as -a, and turn unreachable objects loose"),
1282 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
1283 OPT_BIT(0, "cruft", &pack_everything
,
1284 N_("same as -a, pack unreachable cruft objects separately"),
1286 OPT_STRING(0, "cruft-expiration", &cruft_expiration
, N_("approxidate"),
1287 N_("with --cruft, expire objects older than this")),
1288 OPT_UNSIGNED(0, "combine-cruft-below-size",
1289 &combine_cruft_below_size
,
1290 N_("with --cruft, only repack cruft packs smaller than this")),
1291 OPT_UNSIGNED(0, "max-cruft-size", &cruft_po_args
.max_pack_size
,
1292 N_("with --cruft, limit the size of new cruft packs")),
1293 OPT_BOOL('d', NULL
, &delete_redundant
,
1294 N_("remove redundant packs, and run git-prune-packed")),
1295 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
1296 N_("pass --no-reuse-delta to git-pack-objects")),
1297 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
1298 N_("pass --no-reuse-object to git-pack-objects")),
1299 OPT_INTEGER(0, "name-hash-version", &po_args
.name_hash_version
,
1300 N_("specify the name hash version to use for grouping similar objects by path")),
1301 OPT_BOOL(0, "path-walk", &po_args
.path_walk
,
1302 N_("pass --path-walk to git-pack-objects")),
1303 OPT_NEGBIT('n', NULL
, &run_update_server_info
,
1304 N_("do not run git-update-server-info"), 1),
1305 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
1306 OPT_BOOL('l', "local", &po_args
.local
,
1307 N_("pass --local to git-pack-objects")),
1308 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
1309 N_("write bitmap index")),
1310 OPT_BOOL('i', "delta-islands", &use_delta_islands
,
1311 N_("pass --delta-islands to git-pack-objects")),
1312 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
1313 N_("with -A, do not loosen objects older than this")),
1314 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
1315 N_("with -a, repack unreachable objects")),
1316 OPT_STRING(0, "window", &opt_window
, N_("n"),
1317 N_("size of the window used for delta compression")),
1318 OPT_STRING(0, "window-memory", &opt_window_memory
, N_("bytes"),
1319 N_("same as the above, but limit memory size instead of entries count")),
1320 OPT_STRING(0, "depth", &opt_depth
, N_("n"),
1321 N_("limits the maximum delta depth")),
1322 OPT_STRING(0, "threads", &opt_threads
, N_("n"),
1323 N_("limits the maximum number of threads")),
1324 OPT_UNSIGNED(0, "max-pack-size", &po_args
.max_pack_size
,
1325 N_("maximum size of each packfile")),
1326 OPT_PARSE_LIST_OBJECTS_FILTER(&po_args
.filter_options
),
1327 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
1328 N_("repack objects in packs marked with .keep")),
1329 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
1330 N_("do not repack this pack")),
1331 OPT_INTEGER('g', "geometric", &geometry
.split_factor
,
1332 N_("find a geometric progression with factor <N>")),
1333 OPT_BOOL('m', "write-midx", &write_midx
,
1334 N_("write a multi-pack index of the resulting packs")),
1335 OPT_STRING(0, "expire-to", &expire_to
, N_("dir"),
1336 N_("pack prefix to store a pack containing pruned objects")),
1337 OPT_STRING(0, "filter-to", &filter_to
, N_("dir"),
1338 N_("pack prefix to store a pack containing filtered out objects")),
1342 list_objects_filter_init(&po_args
.filter_options
);
1344 repo_config(the_repository
, repack_config
, &cruft_po_args
);
1346 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
1347 git_repack_usage
, 0);
1349 po_args
.window
= xstrdup_or_null(opt_window
);
1350 po_args
.window_memory
= xstrdup_or_null(opt_window_memory
);
1351 po_args
.depth
= xstrdup_or_null(opt_depth
);
1352 po_args
.threads
= xstrdup_or_null(opt_threads
);
1354 if (delete_redundant
&& the_repository
->repository_format_precious_objects
)
1355 die(_("cannot delete packs in a precious-objects repo"));
1357 die_for_incompatible_opt3(unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
), "-A",
1358 keep_unreachable
, "-k/--keep-unreachable",
1359 pack_everything
& PACK_CRUFT
, "--cruft");
1361 if (pack_everything
& PACK_CRUFT
)
1362 pack_everything
|= ALL_INTO_ONE
;
1364 if (write_bitmaps
< 0) {
1366 (!(pack_everything
& ALL_INTO_ONE
) || !is_bare_repository()))
1369 if (pack_kept_objects
< 0)
1370 pack_kept_objects
= write_bitmaps
> 0 && !write_midx
;
1372 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
) && !write_midx
)
1373 die(_(incremental_bitmap_conflict_error
));
1375 if (write_bitmaps
&& po_args
.local
&&
1376 odb_has_alternates(the_repository
->objects
)) {
1378 * When asked to do a local repack, but we have
1379 * packfiles that are inherited from an alternate, then
1380 * we cannot guarantee that the multi-pack-index would
1381 * have full coverage of all objects. We thus disable
1382 * writing bitmaps in that case.
1384 warning(_("disabling bitmap writing, as some objects are not being packed"));
1388 if (write_midx
&& write_bitmaps
) {
1389 struct strbuf path
= STRBUF_INIT
;
1391 strbuf_addf(&path
, "%s/%s_XXXXXX", repo_get_object_directory(the_repository
),
1394 refs_snapshot
= xmks_tempfile(path
.buf
);
1395 midx_snapshot_refs(refs_snapshot
);
1397 strbuf_release(&path
);
1400 packdir
= mkpathdup("%s/pack", repo_get_object_directory(the_repository
));
1401 packtmp_name
= xstrfmt(".tmp-%d-pack", (int)getpid());
1402 packtmp
= mkpathdup("%s/%s", packdir
, packtmp_name
);
1404 collect_pack_filenames(&existing
, &keep_pack_list
);
1406 if (geometry
.split_factor
) {
1407 if (pack_everything
)
1408 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1409 init_pack_geometry(&geometry
, &existing
, &po_args
);
1410 split_pack_geometry(&geometry
);
1413 prepare_pack_objects(&cmd
, &po_args
, packtmp
);
1415 show_progress
= !po_args
.quiet
&& isatty(2);
1417 strvec_push(&cmd
.args
, "--keep-true-parents");
1418 if (!pack_kept_objects
)
1419 strvec_push(&cmd
.args
, "--honor-pack-keep");
1420 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
1421 strvec_pushf(&cmd
.args
, "--keep-pack=%s",
1422 keep_pack_list
.items
[i
].string
);
1423 strvec_push(&cmd
.args
, "--non-empty");
1424 if (!geometry
.split_factor
) {
1426 * We need to grab all reachable objects, including those that
1427 * are reachable from reflogs and the index.
1429 * When repacking into a geometric progression of packs,
1430 * however, we ask 'git pack-objects --stdin-packs', and it is
1431 * not about packing objects based on reachability but about
1432 * repacking all the objects in specified packs and loose ones
1433 * (indeed, --stdin-packs is incompatible with these options).
1435 strvec_push(&cmd
.args
, "--all");
1436 strvec_push(&cmd
.args
, "--reflog");
1437 strvec_push(&cmd
.args
, "--indexed-objects");
1439 if (repo_has_promisor_remote(the_repository
))
1440 strvec_push(&cmd
.args
, "--exclude-promisor-objects");
1442 if (write_bitmaps
> 0)
1443 strvec_push(&cmd
.args
, "--write-bitmap-index");
1444 else if (write_bitmaps
< 0)
1445 strvec_push(&cmd
.args
, "--write-bitmap-index-quiet");
1447 if (use_delta_islands
)
1448 strvec_push(&cmd
.args
, "--delta-islands");
1450 if (pack_everything
& ALL_INTO_ONE
) {
1451 repack_promisor_objects(&po_args
, &names
);
1453 if (has_existing_non_kept_packs(&existing
) &&
1455 !(pack_everything
& PACK_CRUFT
)) {
1456 for_each_string_list_item(item
, &names
) {
1457 strvec_pushf(&cmd
.args
, "--keep-pack=%s-%s.pack",
1458 packtmp_name
, item
->string
);
1460 if (unpack_unreachable
) {
1461 strvec_pushf(&cmd
.args
,
1462 "--unpack-unreachable=%s",
1463 unpack_unreachable
);
1464 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
1465 strvec_push(&cmd
.args
,
1466 "--unpack-unreachable");
1467 } else if (keep_unreachable
) {
1468 strvec_push(&cmd
.args
, "--keep-unreachable");
1472 if (keep_unreachable
&& delete_redundant
&&
1473 !(pack_everything
& PACK_CRUFT
))
1474 strvec_push(&cmd
.args
, "--pack-loose-unreachable");
1475 } else if (geometry
.split_factor
) {
1476 if (midx_must_contain_cruft
)
1477 strvec_push(&cmd
.args
, "--stdin-packs");
1479 strvec_push(&cmd
.args
, "--stdin-packs=follow");
1480 strvec_push(&cmd
.args
, "--unpacked");
1482 strvec_push(&cmd
.args
, "--unpacked");
1483 strvec_push(&cmd
.args
, "--incremental");
1486 if (po_args
.filter_options
.choice
)
1487 strvec_pushf(&cmd
.args
, "--filter=%s",
1488 expand_list_objects_filter_spec(&po_args
.filter_options
));
1490 die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter");
1492 if (geometry
.split_factor
)
1497 ret
= start_command(&cmd
);
1501 if (geometry
.split_factor
) {
1502 FILE *in
= xfdopen(cmd
.in
, "w");
1504 * The resulting pack should contain all objects in packs that
1505 * are going to be rolled up, but exclude objects in packs which
1506 * are being left alone.
1508 for (i
= 0; i
< geometry
.split
; i
++)
1509 fprintf(in
, "%s\n", pack_basename(geometry
.pack
[i
]));
1510 for (i
= geometry
.split
; i
< geometry
.pack_nr
; i
++)
1511 fprintf(in
, "^%s\n", pack_basename(geometry
.pack
[i
]));
1515 ret
= finish_pack_objects_cmd(&cmd
, &names
, 1);
1521 printf_ln(_("Nothing new to pack."));
1523 * If we didn't write any new packs, the non-cruft packs
1524 * may refer to once-unreachable objects in the cruft
1527 * If there isn't already a MIDX, the one we write
1528 * must include the cruft pack(s), in case the
1529 * non-cruft pack(s) refer to once-cruft objects.
1531 * If there is already a MIDX, we can punt here, since
1532 * midx_has_unknown_packs() will make the decision for
1535 if (!get_multi_pack_index(the_repository
->objects
->sources
))
1536 midx_must_contain_cruft
= 1;
1539 if (pack_everything
& PACK_CRUFT
) {
1540 const char *pack_prefix
= find_pack_prefix(packdir
, packtmp
);
1542 if (!cruft_po_args
.window
)
1543 cruft_po_args
.window
= xstrdup_or_null(po_args
.window
);
1544 if (!cruft_po_args
.window_memory
)
1545 cruft_po_args
.window_memory
= xstrdup_or_null(po_args
.window_memory
);
1546 if (!cruft_po_args
.depth
)
1547 cruft_po_args
.depth
= xstrdup_or_null(po_args
.depth
);
1548 if (!cruft_po_args
.threads
)
1549 cruft_po_args
.threads
= xstrdup_or_null(po_args
.threads
);
1550 if (!cruft_po_args
.max_pack_size
)
1551 cruft_po_args
.max_pack_size
= po_args
.max_pack_size
;
1553 cruft_po_args
.local
= po_args
.local
;
1554 cruft_po_args
.quiet
= po_args
.quiet
;
1556 ret
= write_cruft_pack(&cruft_po_args
, packtmp
, pack_prefix
,
1558 combine_cruft_below_size
, &names
,
1563 if (delete_redundant
&& expire_to
) {
1565 * If `--expire-to` is given with `-d`, it's possible
1566 * that we're about to prune some objects. With cruft
1567 * packs, pruning is implicit: any objects from existing
1568 * packs that weren't picked up by new packs are removed
1569 * when their packs are deleted.
1571 * Generate an additional cruft pack, with one twist:
1572 * `names` now includes the name of the cruft pack
1573 * written in the previous step. So the contents of
1574 * _this_ cruft pack exclude everything contained in the
1575 * existing cruft pack (that is, all of the unreachable
1576 * objects which are no older than
1577 * `--cruft-expiration`).
1579 * To make this work, cruft_expiration must become NULL
1580 * so that this cruft pack doesn't actually prune any
1581 * objects. If it were non-NULL, this call would always
1582 * generate an empty pack (since every object not in the
1583 * cruft pack generated above will have an mtime older
1584 * than the expiration).
1586 * Pretend we don't have a `--combine-cruft-below-size`
1587 * argument, since we're not selectively combining
1588 * anything based on size to generate the limbo cruft
1589 * pack, but rather removing all cruft packs from the
1590 * main repository regardless of size.
1592 ret
= write_cruft_pack(&cruft_po_args
, expire_to
,
1603 if (po_args
.filter_options
.choice
) {
1605 filter_to
= packtmp
;
1607 ret
= write_filtered_pack(&po_args
,
1609 find_pack_prefix(packdir
, packtmp
),
1616 string_list_sort(&names
);
1618 if (get_multi_pack_index(the_repository
->objects
->sources
)) {
1619 struct multi_pack_index
*m
=
1620 get_multi_pack_index(the_repository
->objects
->sources
);
1622 ALLOC_ARRAY(midx_pack_names
,
1623 m
->num_packs
+ m
->num_packs_in_base
);
1625 for (; m
; m
= m
->base_midx
)
1626 for (uint32_t i
= 0; i
< m
->num_packs
; i
++)
1627 midx_pack_names
[midx_pack_names_nr
++] =
1628 xstrdup(m
->pack_names
[i
]);
1631 close_object_store(the_repository
->objects
);
1634 * Ok we have prepared all new packfiles.
1636 for_each_string_list_item(item
, &names
) {
1637 struct generated_pack_data
*data
= item
->util
;
1639 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
1642 fname
= mkpathdup("%s/pack-%s%s",
1643 packdir
, item
->string
, exts
[ext
].name
);
1645 if (data
->tempfiles
[ext
]) {
1646 const char *fname_old
= get_tempfile_path(data
->tempfiles
[ext
]);
1647 struct stat statbuffer
;
1649 if (!stat(fname_old
, &statbuffer
)) {
1650 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
1651 chmod(fname_old
, statbuffer
.st_mode
);
1654 if (rename_tempfile(&data
->tempfiles
[ext
], fname
))
1655 die_errno(_("renaming pack to '%s' failed"), fname
);
1656 } else if (!exts
[ext
].optional
)
1657 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1658 exts
[ext
].name
, packtmp
, item
->string
);
1659 else if (unlink(fname
) < 0 && errno
!= ENOENT
)
1660 die_errno(_("could not unlink: %s"), fname
);
1665 /* End of pack replacement. */
1667 if (delete_redundant
&& pack_everything
& ALL_INTO_ONE
)
1668 mark_packs_for_deletion(&existing
, &names
);
1671 struct string_list include
= STRING_LIST_INIT_DUP
;
1672 midx_included_packs(&include
, &existing
, midx_pack_names
,
1673 midx_pack_names_nr
, &names
, &geometry
);
1675 ret
= write_midx_included_packs(&include
, &geometry
, &names
,
1676 refs_snapshot
? get_tempfile_path(refs_snapshot
) : NULL
,
1677 show_progress
, write_bitmaps
> 0);
1679 if (!ret
&& write_bitmaps
)
1680 remove_redundant_bitmaps(&include
, packdir
);
1682 string_list_clear(&include
, 0);
1688 reprepare_packed_git(the_repository
);
1690 if (delete_redundant
) {
1692 remove_redundant_existing_packs(&existing
);
1694 if (geometry
.split_factor
)
1695 geometry_remove_redundant_packs(&geometry
, &names
,
1698 opts
|= PRUNE_PACKED_VERBOSE
;
1699 prune_packed_objects(opts
);
1701 if (!keep_unreachable
&&
1702 (!(pack_everything
& LOOSEN_UNREACHABLE
) ||
1703 unpack_unreachable
) &&
1704 is_repository_shallow(the_repository
))
1705 prune_shallow(PRUNE_QUICK
);
1708 if (run_update_server_info
)
1709 update_server_info(the_repository
, 0);
1711 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0)) {
1713 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL
, 0))
1714 flags
|= MIDX_WRITE_INCREMENTAL
;
1715 write_midx_file(the_repository
->objects
->sources
,
1720 string_list_clear(&keep_pack_list
, 0);
1721 string_list_clear(&names
, 1);
1722 existing_packs_release(&existing
);
1723 free_pack_geometry(&geometry
);
1724 for (size_t i
= 0; i
< midx_pack_names_nr
; i
++)
1725 free(midx_pack_names
[i
]);
1726 free(midx_pack_names
);
1727 pack_objects_args_release(&po_args
);
1728 pack_objects_args_release(&cruft_po_args
);