4 #include "environment.h"
7 #include "parse-options.h"
9 #include "run-command.h"
10 #include "server-info.h"
13 #include "string-list.h"
17 #include "prune-packed.h"
18 #include "object-store-ll.h"
19 #include "promisor-remote.h"
22 #include "pack-bitmap.h"
25 #define ALL_INTO_ONE 1
26 #define LOOSEN_UNREACHABLE 2
31 static int pack_everything
;
32 static int delta_base_offset
= 1;
33 static int pack_kept_objects
= -1;
34 static int write_bitmaps
= -1;
35 static int use_delta_islands
;
36 static int run_update_server_info
= 1;
37 static char *packdir
, *packtmp_name
, *packtmp
;
39 static const char *const git_repack_usage
[] = {
40 N_("git repack [<options>]"),
44 static const char incremental_bitmap_conflict_error
[] = N_(
45 "Incremental repacks are incompatible with bitmap indexes. Use\n"
46 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
49 struct pack_objects_args
{
51 const char *window_memory
;
54 const char *max_pack_size
;
61 static int repack_config(const char *var
, const char *value
,
62 const struct config_context
*ctx
, void *cb
)
64 struct pack_objects_args
*cruft_po_args
= cb
;
65 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
66 delta_base_offset
= git_config_bool(var
, value
);
69 if (!strcmp(var
, "repack.packkeptobjects")) {
70 pack_kept_objects
= git_config_bool(var
, value
);
73 if (!strcmp(var
, "repack.writebitmaps") ||
74 !strcmp(var
, "pack.writebitmaps")) {
75 write_bitmaps
= git_config_bool(var
, value
);
78 if (!strcmp(var
, "repack.usedeltaislands")) {
79 use_delta_islands
= git_config_bool(var
, value
);
82 if (strcmp(var
, "repack.updateserverinfo") == 0) {
83 run_update_server_info
= git_config_bool(var
, value
);
86 if (!strcmp(var
, "repack.cruftwindow"))
87 return git_config_string(&cruft_po_args
->window
, var
, value
);
88 if (!strcmp(var
, "repack.cruftwindowmemory"))
89 return git_config_string(&cruft_po_args
->window_memory
, var
, value
);
90 if (!strcmp(var
, "repack.cruftdepth"))
91 return git_config_string(&cruft_po_args
->depth
, var
, value
);
92 if (!strcmp(var
, "repack.cruftthreads"))
93 return git_config_string(&cruft_po_args
->threads
, var
, value
);
94 return git_default_config(var
, value
, ctx
, cb
);
97 struct existing_packs
{
98 struct string_list kept_packs
;
99 struct string_list non_kept_packs
;
100 struct string_list cruft_packs
;
103 #define EXISTING_PACKS_INIT { \
104 .kept_packs = STRING_LIST_INIT_DUP, \
105 .non_kept_packs = STRING_LIST_INIT_DUP, \
106 .cruft_packs = STRING_LIST_INIT_DUP, \
109 static int has_existing_non_kept_packs(const struct existing_packs
*existing
)
111 return existing
->non_kept_packs
.nr
|| existing
->cruft_packs
.nr
;
114 static void pack_mark_for_deletion(struct string_list_item
*item
)
116 item
->util
= (void*)((uintptr_t)item
->util
| DELETE_PACK
);
119 static int pack_is_marked_for_deletion(struct string_list_item
*item
)
121 return (uintptr_t)item
->util
& DELETE_PACK
;
124 static void mark_packs_for_deletion_1(struct string_list
*names
,
125 struct string_list
*list
)
127 struct string_list_item
*item
;
128 const int hexsz
= the_hash_algo
->hexsz
;
130 for_each_string_list_item(item
, list
) {
132 size_t len
= strlen(item
->string
);
135 sha1
= item
->string
+ len
- hexsz
;
137 * Mark this pack for deletion, which ensures that this
138 * pack won't be included in a MIDX (if `--write-midx`
139 * was given) and that we will actually delete this pack
140 * (if `-d` was given).
142 if (!string_list_has_string(names
, sha1
))
143 pack_mark_for_deletion(item
);
147 static void mark_packs_for_deletion(struct existing_packs
*existing
,
148 struct string_list
*names
)
151 mark_packs_for_deletion_1(names
, &existing
->non_kept_packs
);
152 mark_packs_for_deletion_1(names
, &existing
->cruft_packs
);
155 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
157 struct strbuf buf
= STRBUF_INIT
;
158 struct multi_pack_index
*m
= get_local_multi_pack_index(the_repository
);
159 strbuf_addf(&buf
, "%s.pack", base_name
);
160 if (m
&& midx_contains_pack(m
, buf
.buf
))
161 clear_midx_file(the_repository
);
162 strbuf_insertf(&buf
, 0, "%s/", dir_name
);
163 unlink_pack_path(buf
.buf
, 1);
164 strbuf_release(&buf
);
167 static void remove_redundant_packs_1(struct string_list
*packs
)
169 struct string_list_item
*item
;
170 for_each_string_list_item(item
, packs
) {
171 if (!pack_is_marked_for_deletion(item
))
173 remove_redundant_pack(packdir
, item
->string
);
177 static void remove_redundant_existing_packs(struct existing_packs
*existing
)
179 remove_redundant_packs_1(&existing
->non_kept_packs
);
180 remove_redundant_packs_1(&existing
->cruft_packs
);
183 static void existing_packs_release(struct existing_packs
*existing
)
185 string_list_clear(&existing
->kept_packs
, 0);
186 string_list_clear(&existing
->non_kept_packs
, 0);
187 string_list_clear(&existing
->cruft_packs
, 0);
191 * Adds all packs hex strings (pack-$HASH) to either packs->non_kept
192 * or packs->kept based on whether each pack has a corresponding
193 * .keep file or not. Packs without a .keep file are not to be kept
194 * if we are going to pack everything into one file.
196 static void collect_pack_filenames(struct existing_packs
*existing
,
197 const struct string_list
*extra_keep
)
199 struct packed_git
*p
;
200 struct strbuf buf
= STRBUF_INIT
;
202 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
209 base
= pack_basename(p
);
211 for (i
= 0; i
< extra_keep
->nr
; i
++)
212 if (!fspathcmp(base
, extra_keep
->items
[i
].string
))
216 strbuf_addstr(&buf
, base
);
217 strbuf_strip_suffix(&buf
, ".pack");
219 if ((extra_keep
->nr
> 0 && i
< extra_keep
->nr
) || p
->pack_keep
)
220 string_list_append(&existing
->kept_packs
, buf
.buf
);
221 else if (p
->is_cruft
)
222 string_list_append(&existing
->cruft_packs
, buf
.buf
);
224 string_list_append(&existing
->non_kept_packs
, buf
.buf
);
227 string_list_sort(&existing
->kept_packs
);
228 strbuf_release(&buf
);
231 static void prepare_pack_objects(struct child_process
*cmd
,
232 const struct pack_objects_args
*args
,
235 strvec_push(&cmd
->args
, "pack-objects");
237 strvec_pushf(&cmd
->args
, "--window=%s", args
->window
);
238 if (args
->window_memory
)
239 strvec_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
241 strvec_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
243 strvec_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
244 if (args
->max_pack_size
)
245 strvec_pushf(&cmd
->args
, "--max-pack-size=%s", args
->max_pack_size
);
246 if (args
->no_reuse_delta
)
247 strvec_pushf(&cmd
->args
, "--no-reuse-delta");
248 if (args
->no_reuse_object
)
249 strvec_pushf(&cmd
->args
, "--no-reuse-object");
251 strvec_push(&cmd
->args
, "--local");
253 strvec_push(&cmd
->args
, "--quiet");
254 if (delta_base_offset
)
255 strvec_push(&cmd
->args
, "--delta-base-offset");
256 strvec_push(&cmd
->args
, out
);
262 * Write oid to the given struct child_process's stdin, starting it first if
265 static int write_oid(const struct object_id
*oid
,
266 struct packed_git
*pack UNUSED
,
267 uint32_t pos UNUSED
, void *data
)
269 struct child_process
*cmd
= data
;
272 if (start_command(cmd
))
273 die(_("could not start pack-objects to repack promisor objects"));
276 xwrite(cmd
->in
, oid_to_hex(oid
), the_hash_algo
->hexsz
);
277 xwrite(cmd
->in
, "\n", 1);
293 struct generated_pack_data
{
294 struct tempfile
*tempfiles
[ARRAY_SIZE(exts
)];
297 static struct generated_pack_data
*populate_pack_exts(const char *name
)
300 struct strbuf path
= STRBUF_INIT
;
301 struct generated_pack_data
*data
= xcalloc(1, sizeof(*data
));
304 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
306 strbuf_addf(&path
, "%s-%s%s", packtmp
, name
, exts
[i
].name
);
308 if (stat(path
.buf
, &statbuf
))
311 data
->tempfiles
[i
] = register_tempfile(path
.buf
);
314 strbuf_release(&path
);
318 static void repack_promisor_objects(const struct pack_objects_args
*args
,
319 struct string_list
*names
)
321 struct child_process cmd
= CHILD_PROCESS_INIT
;
323 struct strbuf line
= STRBUF_INIT
;
325 prepare_pack_objects(&cmd
, args
, packtmp
);
329 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
330 * hints may result in suboptimal deltas in the resulting pack. See if
331 * the OIDs can be sent with fake paths such that pack-objects can use a
332 * {type -> existing pack order} ordering when computing deltas instead
333 * of a {type -> size} ordering, which may produce better deltas.
335 for_each_packed_object(write_oid
, &cmd
,
336 FOR_EACH_OBJECT_PROMISOR_ONLY
);
339 /* No packed objects; cmd was never started */
340 child_process_clear(&cmd
);
346 out
= xfdopen(cmd
.out
, "r");
347 while (strbuf_getline_lf(&line
, out
) != EOF
) {
348 struct string_list_item
*item
;
351 if (line
.len
!= the_hash_algo
->hexsz
)
352 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
353 item
= string_list_append(names
, line
.buf
);
356 * pack-objects creates the .pack and .idx files, but not the
357 * .promisor file. Create the .promisor file, which is empty.
359 * NEEDSWORK: fetch-pack sometimes generates non-empty
360 * .promisor files containing the ref names and associated
361 * hashes at the point of generation of the corresponding
362 * packfile, but this would not preserve their contents. Maybe
363 * concatenate the contents of all .promisor files instead of
364 * just creating a new empty file.
366 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
368 write_promisor_file(promisor_name
, NULL
, 0);
370 item
->util
= populate_pack_exts(item
->string
);
375 if (finish_command(&cmd
))
376 die(_("could not finish pack-objects to repack promisor objects"));
379 struct pack_geometry
{
380 struct packed_git
**pack
;
381 uint32_t pack_nr
, pack_alloc
;
387 static uint32_t geometry_pack_weight(struct packed_git
*p
)
389 if (open_pack_index(p
))
390 die(_("cannot open index for %s"), p
->pack_name
);
391 return p
->num_objects
;
394 static int geometry_cmp(const void *va
, const void *vb
)
396 uint32_t aw
= geometry_pack_weight(*(struct packed_git
**)va
),
397 bw
= geometry_pack_weight(*(struct packed_git
**)vb
);
406 static void init_pack_geometry(struct pack_geometry
*geometry
,
407 struct existing_packs
*existing
,
408 const struct pack_objects_args
*args
)
410 struct packed_git
*p
;
411 struct strbuf buf
= STRBUF_INIT
;
413 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
414 if (args
->local
&& !p
->pack_local
)
416 * When asked to only repack local packfiles we skip
417 * over any packfiles that are borrowed from alternate
418 * object directories.
422 if (!pack_kept_objects
) {
424 * Any pack that has its pack_keep bit set will
425 * appear in existing->kept_packs below, but
426 * this saves us from doing a more expensive
433 * The pack may be kept via the --keep-pack
434 * option; check 'existing->kept_packs' to
435 * determine whether to ignore it.
438 strbuf_addstr(&buf
, pack_basename(p
));
439 strbuf_strip_suffix(&buf
, ".pack");
441 if (string_list_has_string(&existing
->kept_packs
, buf
.buf
))
447 ALLOC_GROW(geometry
->pack
,
448 geometry
->pack_nr
+ 1,
449 geometry
->pack_alloc
);
451 geometry
->pack
[geometry
->pack_nr
] = p
;
455 QSORT(geometry
->pack
, geometry
->pack_nr
, geometry_cmp
);
456 strbuf_release(&buf
);
459 static void split_pack_geometry(struct pack_geometry
*geometry
)
463 off_t total_size
= 0;
465 if (!geometry
->pack_nr
) {
466 geometry
->split
= geometry
->pack_nr
;
471 * First, count the number of packs (in descending order of size) which
472 * already form a geometric progression.
474 for (i
= geometry
->pack_nr
- 1; i
> 0; i
--) {
475 struct packed_git
*ours
= geometry
->pack
[i
];
476 struct packed_git
*prev
= geometry
->pack
[i
- 1];
478 if (unsigned_mult_overflows(geometry
->split_factor
,
479 geometry_pack_weight(prev
)))
480 die(_("pack %s too large to consider in geometric "
484 if (geometry_pack_weight(ours
) <
485 geometry
->split_factor
* geometry_pack_weight(prev
))
493 * Move the split one to the right, since the top element in the
494 * last-compared pair can't be in the progression. Only do this
495 * when we split in the middle of the array (otherwise if we got
496 * to the end, then the split is in the right place).
502 * Then, anything to the left of 'split' must be in a new pack. But,
503 * creating that new pack may cause packs in the heavy half to no longer
504 * form a geometric progression.
506 * Compute an expected size of the new pack, and then determine how many
507 * packs in the heavy half need to be joined into it (if any) to restore
508 * the geometric progression.
510 for (i
= 0; i
< split
; i
++) {
511 struct packed_git
*p
= geometry
->pack
[i
];
513 if (unsigned_add_overflows(total_size
, geometry_pack_weight(p
)))
514 die(_("pack %s too large to roll up"), p
->pack_name
);
515 total_size
+= geometry_pack_weight(p
);
517 for (i
= split
; i
< geometry
->pack_nr
; i
++) {
518 struct packed_git
*ours
= geometry
->pack
[i
];
520 if (unsigned_mult_overflows(geometry
->split_factor
,
522 die(_("pack %s too large to roll up"), ours
->pack_name
);
524 if (geometry_pack_weight(ours
) <
525 geometry
->split_factor
* total_size
) {
526 if (unsigned_add_overflows(total_size
,
527 geometry_pack_weight(ours
)))
528 die(_("pack %s too large to roll up"),
532 total_size
+= geometry_pack_weight(ours
);
537 geometry
->split
= split
;
540 static struct packed_git
*get_preferred_pack(struct pack_geometry
*geometry
)
546 * No geometry means either an all-into-one repack (in which
547 * case there is only one pack left and it is the largest) or an
550 * If repacking incrementally, then we could check the size of
551 * all packs to determine which should be preferred, but leave
556 if (geometry
->split
== geometry
->pack_nr
)
560 * The preferred pack is the largest pack above the split line. In
561 * other words, it is the largest pack that does not get rolled up in
562 * the geometric repack.
564 for (i
= geometry
->pack_nr
; i
> geometry
->split
; i
--)
566 * A pack that is not local would never be included in a
567 * multi-pack index. We thus skip over any non-local packs.
569 if (geometry
->pack
[i
- 1]->pack_local
)
570 return geometry
->pack
[i
- 1];
575 static void geometry_remove_redundant_packs(struct pack_geometry
*geometry
,
576 struct string_list
*names
,
577 struct existing_packs
*existing
)
579 struct strbuf buf
= STRBUF_INIT
;
582 for (i
= 0; i
< geometry
->split
; i
++) {
583 struct packed_git
*p
= geometry
->pack
[i
];
584 if (string_list_has_string(names
, hash_to_hex(p
->hash
)))
588 strbuf_addstr(&buf
, pack_basename(p
));
589 strbuf_strip_suffix(&buf
, ".pack");
591 if ((p
->pack_keep
) ||
592 (string_list_has_string(&existing
->kept_packs
, buf
.buf
)))
595 remove_redundant_pack(packdir
, buf
.buf
);
598 strbuf_release(&buf
);
601 static void free_pack_geometry(struct pack_geometry
*geometry
)
606 free(geometry
->pack
);
609 struct midx_snapshot_ref_data
{
615 static int midx_snapshot_ref_one(const char *refname UNUSED
,
616 const struct object_id
*oid
,
617 int flag UNUSED
, void *_data
)
619 struct midx_snapshot_ref_data
*data
= _data
;
620 struct object_id peeled
;
622 if (!peel_iterated_oid(oid
, &peeled
))
625 if (oidset_insert(&data
->seen
, oid
))
626 return 0; /* already seen */
628 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
631 fprintf(data
->f
->fp
, "%s%s\n", data
->preferred
? "+" : "",
637 static void midx_snapshot_refs(struct tempfile
*f
)
639 struct midx_snapshot_ref_data data
;
640 const struct string_list
*preferred
= bitmap_preferred_tips(the_repository
);
644 oidset_init(&data
.seen
, 0);
646 if (!fdopen_tempfile(f
, "w"))
647 die(_("could not open tempfile %s for writing"),
648 get_tempfile_path(f
));
651 struct string_list_item
*item
;
654 for_each_string_list_item(item
, preferred
)
655 for_each_ref_in(item
->string
, midx_snapshot_ref_one
, &data
);
659 for_each_ref(midx_snapshot_ref_one
, &data
);
661 if (close_tempfile_gently(f
)) {
662 int save_errno
= errno
;
665 die_errno(_("could not close refs snapshot tempfile"));
668 oidset_clear(&data
.seen
);
671 static void midx_included_packs(struct string_list
*include
,
672 struct existing_packs
*existing
,
673 struct string_list
*names
,
674 struct pack_geometry
*geometry
)
676 struct string_list_item
*item
;
678 for_each_string_list_item(item
, &existing
->kept_packs
)
679 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
680 for_each_string_list_item(item
, names
)
681 string_list_insert(include
, xstrfmt("pack-%s.idx", item
->string
));
682 if (geometry
->split_factor
) {
683 struct strbuf buf
= STRBUF_INIT
;
685 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++) {
686 struct packed_git
*p
= geometry
->pack
[i
];
689 * The multi-pack index never refers to packfiles part
690 * of an alternate object database, so we skip these.
691 * While git-multi-pack-index(1) would silently ignore
692 * them anyway, this allows us to skip executing the
693 * command completely when we have only non-local
699 strbuf_addstr(&buf
, pack_basename(p
));
700 strbuf_strip_suffix(&buf
, ".pack");
701 strbuf_addstr(&buf
, ".idx");
703 string_list_insert(include
, strbuf_detach(&buf
, NULL
));
706 for_each_string_list_item(item
, &existing
->non_kept_packs
) {
707 if (pack_is_marked_for_deletion(item
))
709 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
713 for_each_string_list_item(item
, &existing
->cruft_packs
) {
715 * When doing a --geometric repack, there is no need to check
716 * for deleted packs, since we're by definition not doing an
717 * ALL_INTO_ONE repack (hence no packs will be deleted).
718 * Otherwise we must check for and exclude any packs which are
719 * enqueued for deletion.
721 * So we could omit the conditional below in the --geometric
722 * case, but doing so is unnecessary since no packs are marked
723 * as pending deletion (since we only call
724 * `mark_packs_for_deletion()` when doing an all-into-one
727 if (pack_is_marked_for_deletion(item
))
729 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
733 static int write_midx_included_packs(struct string_list
*include
,
734 struct pack_geometry
*geometry
,
735 const char *refs_snapshot
,
736 int show_progress
, int write_bitmaps
)
738 struct child_process cmd
= CHILD_PROCESS_INIT
;
739 struct string_list_item
*item
;
740 struct packed_git
*preferred
= get_preferred_pack(geometry
);
750 strvec_push(&cmd
.args
, "multi-pack-index");
751 strvec_pushl(&cmd
.args
, "write", "--stdin-packs", NULL
);
754 strvec_push(&cmd
.args
, "--progress");
756 strvec_push(&cmd
.args
, "--no-progress");
759 strvec_push(&cmd
.args
, "--bitmap");
762 strvec_pushf(&cmd
.args
, "--preferred-pack=%s",
763 pack_basename(preferred
));
766 strvec_pushf(&cmd
.args
, "--refs-snapshot=%s", refs_snapshot
);
768 ret
= start_command(&cmd
);
772 in
= xfdopen(cmd
.in
, "w");
773 for_each_string_list_item(item
, include
)
774 fprintf(in
, "%s\n", item
->string
);
777 return finish_command(&cmd
);
780 static void remove_redundant_bitmaps(struct string_list
*include
,
783 struct strbuf path
= STRBUF_INIT
;
784 struct string_list_item
*item
;
787 strbuf_addstr(&path
, packdir
);
788 strbuf_addch(&path
, '/');
789 packdir_len
= path
.len
;
792 * Remove any pack bitmaps corresponding to packs which are now
793 * included in the MIDX.
795 for_each_string_list_item(item
, include
) {
796 strbuf_addstr(&path
, item
->string
);
797 strbuf_strip_suffix(&path
, ".idx");
798 strbuf_addstr(&path
, ".bitmap");
800 if (unlink(path
.buf
) && errno
!= ENOENT
)
801 warning_errno(_("could not remove stale bitmap: %s"),
804 strbuf_setlen(&path
, packdir_len
);
806 strbuf_release(&path
);
809 static int write_cruft_pack(const struct pack_objects_args
*args
,
810 const char *destination
,
811 const char *pack_prefix
,
812 const char *cruft_expiration
,
813 struct string_list
*names
,
814 struct existing_packs
*existing
)
816 struct child_process cmd
= CHILD_PROCESS_INIT
;
817 struct strbuf line
= STRBUF_INIT
;
818 struct string_list_item
*item
;
822 int local
= skip_prefix(destination
, packdir
, &scratch
);
824 prepare_pack_objects(&cmd
, args
, destination
);
826 strvec_push(&cmd
.args
, "--cruft");
827 if (cruft_expiration
)
828 strvec_pushf(&cmd
.args
, "--cruft-expiration=%s",
831 strvec_push(&cmd
.args
, "--honor-pack-keep");
832 strvec_push(&cmd
.args
, "--non-empty");
833 strvec_push(&cmd
.args
, "--max-pack-size=0");
837 ret
= start_command(&cmd
);
842 * names has a confusing double use: it both provides the list
843 * of just-written new packs, and accepts the name of the cruft
844 * pack we are writing.
846 * By the time it is read here, it contains only the pack(s)
847 * that were just written, which is exactly the set of packs we
848 * want to consider kept.
850 * If `--expire-to` is given, the double-use served by `names`
851 * ensures that the pack written to `--expire-to` excludes any
852 * objects contained in the cruft pack.
854 in
= xfdopen(cmd
.in
, "w");
855 for_each_string_list_item(item
, names
)
856 fprintf(in
, "%s-%s.pack\n", pack_prefix
, item
->string
);
857 for_each_string_list_item(item
, &existing
->non_kept_packs
)
858 fprintf(in
, "-%s.pack\n", item
->string
);
859 for_each_string_list_item(item
, &existing
->cruft_packs
)
860 fprintf(in
, "-%s.pack\n", item
->string
);
861 for_each_string_list_item(item
, &existing
->kept_packs
)
862 fprintf(in
, "%s.pack\n", item
->string
);
865 out
= xfdopen(cmd
.out
, "r");
866 while (strbuf_getline_lf(&line
, out
) != EOF
) {
867 struct string_list_item
*item
;
869 if (line
.len
!= the_hash_algo
->hexsz
)
870 die(_("repack: Expecting full hex object ID lines only "
871 "from pack-objects."));
873 * avoid putting packs written outside of the repository in the
877 item
= string_list_append(names
, line
.buf
);
878 item
->util
= populate_pack_exts(line
.buf
);
883 strbuf_release(&line
);
885 return finish_command(&cmd
);
888 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
890 struct child_process cmd
= CHILD_PROCESS_INIT
;
891 struct string_list_item
*item
;
892 struct string_list names
= STRING_LIST_INIT_DUP
;
893 struct existing_packs existing
= EXISTING_PACKS_INIT
;
894 struct pack_geometry geometry
= { 0 };
895 struct strbuf line
= STRBUF_INIT
;
896 struct tempfile
*refs_snapshot
= NULL
;
901 /* variables to be filled by option parsing */
902 int delete_redundant
= 0;
903 const char *unpack_unreachable
= NULL
;
904 int keep_unreachable
= 0;
905 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
906 struct pack_objects_args po_args
= {NULL
};
907 struct pack_objects_args cruft_po_args
= {NULL
};
909 const char *cruft_expiration
= NULL
;
910 const char *expire_to
= NULL
;
912 struct option builtin_repack_options
[] = {
913 OPT_BIT('a', NULL
, &pack_everything
,
914 N_("pack everything in a single pack"), ALL_INTO_ONE
),
915 OPT_BIT('A', NULL
, &pack_everything
,
916 N_("same as -a, and turn unreachable objects loose"),
917 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
918 OPT_BIT(0, "cruft", &pack_everything
,
919 N_("same as -a, pack unreachable cruft objects separately"),
921 OPT_STRING(0, "cruft-expiration", &cruft_expiration
, N_("approxidate"),
922 N_("with --cruft, expire objects older than this")),
923 OPT_BOOL('d', NULL
, &delete_redundant
,
924 N_("remove redundant packs, and run git-prune-packed")),
925 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
926 N_("pass --no-reuse-delta to git-pack-objects")),
927 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
928 N_("pass --no-reuse-object to git-pack-objects")),
929 OPT_NEGBIT('n', NULL
, &run_update_server_info
,
930 N_("do not run git-update-server-info"), 1),
931 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
932 OPT_BOOL('l', "local", &po_args
.local
,
933 N_("pass --local to git-pack-objects")),
934 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
935 N_("write bitmap index")),
936 OPT_BOOL('i', "delta-islands", &use_delta_islands
,
937 N_("pass --delta-islands to git-pack-objects")),
938 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
939 N_("with -A, do not loosen objects older than this")),
940 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
941 N_("with -a, repack unreachable objects")),
942 OPT_STRING(0, "window", &po_args
.window
, N_("n"),
943 N_("size of the window used for delta compression")),
944 OPT_STRING(0, "window-memory", &po_args
.window_memory
, N_("bytes"),
945 N_("same as the above, but limit memory size instead of entries count")),
946 OPT_STRING(0, "depth", &po_args
.depth
, N_("n"),
947 N_("limits the maximum delta depth")),
948 OPT_STRING(0, "threads", &po_args
.threads
, N_("n"),
949 N_("limits the maximum number of threads")),
950 OPT_STRING(0, "max-pack-size", &po_args
.max_pack_size
, N_("bytes"),
951 N_("maximum size of each packfile")),
952 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
953 N_("repack objects in packs marked with .keep")),
954 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
955 N_("do not repack this pack")),
956 OPT_INTEGER('g', "geometric", &geometry
.split_factor
,
957 N_("find a geometric progression with factor <N>")),
958 OPT_BOOL('m', "write-midx", &write_midx
,
959 N_("write a multi-pack index of the resulting packs")),
960 OPT_STRING(0, "expire-to", &expire_to
, N_("dir"),
961 N_("pack prefix to store a pack containing pruned objects")),
965 git_config(repack_config
, &cruft_po_args
);
967 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
968 git_repack_usage
, 0);
970 if (delete_redundant
&& repository_format_precious_objects
)
971 die(_("cannot delete packs in a precious-objects repo"));
973 if (keep_unreachable
&&
974 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
975 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
977 if (pack_everything
& PACK_CRUFT
) {
978 pack_everything
|= ALL_INTO_ONE
;
980 if (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
))
981 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
982 if (keep_unreachable
)
983 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
986 if (write_bitmaps
< 0) {
988 (!(pack_everything
& ALL_INTO_ONE
) || !is_bare_repository()))
990 } else if (write_bitmaps
&&
991 git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0) &&
992 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0)) {
995 if (pack_kept_objects
< 0)
996 pack_kept_objects
= write_bitmaps
> 0 && !write_midx
;
998 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
) && !write_midx
)
999 die(_(incremental_bitmap_conflict_error
));
1001 if (write_bitmaps
&& po_args
.local
&& has_alt_odb(the_repository
)) {
1003 * When asked to do a local repack, but we have
1004 * packfiles that are inherited from an alternate, then
1005 * we cannot guarantee that the multi-pack-index would
1006 * have full coverage of all objects. We thus disable
1007 * writing bitmaps in that case.
1009 warning(_("disabling bitmap writing, as some objects are not being packed"));
1013 if (write_midx
&& write_bitmaps
) {
1014 struct strbuf path
= STRBUF_INIT
;
1016 strbuf_addf(&path
, "%s/%s_XXXXXX", get_object_directory(),
1019 refs_snapshot
= xmks_tempfile(path
.buf
);
1020 midx_snapshot_refs(refs_snapshot
);
1022 strbuf_release(&path
);
1025 packdir
= mkpathdup("%s/pack", get_object_directory());
1026 packtmp_name
= xstrfmt(".tmp-%d-pack", (int)getpid());
1027 packtmp
= mkpathdup("%s/%s", packdir
, packtmp_name
);
1029 collect_pack_filenames(&existing
, &keep_pack_list
);
1031 if (geometry
.split_factor
) {
1032 if (pack_everything
)
1033 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
1034 init_pack_geometry(&geometry
, &existing
, &po_args
);
1035 split_pack_geometry(&geometry
);
1038 prepare_pack_objects(&cmd
, &po_args
, packtmp
);
1040 show_progress
= !po_args
.quiet
&& isatty(2);
1042 strvec_push(&cmd
.args
, "--keep-true-parents");
1043 if (!pack_kept_objects
)
1044 strvec_push(&cmd
.args
, "--honor-pack-keep");
1045 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
1046 strvec_pushf(&cmd
.args
, "--keep-pack=%s",
1047 keep_pack_list
.items
[i
].string
);
1048 strvec_push(&cmd
.args
, "--non-empty");
1049 if (!geometry
.split_factor
) {
1051 * We need to grab all reachable objects, including those that
1052 * are reachable from reflogs and the index.
1054 * When repacking into a geometric progression of packs,
1055 * however, we ask 'git pack-objects --stdin-packs', and it is
1056 * not about packing objects based on reachability but about
1057 * repacking all the objects in specified packs and loose ones
1058 * (indeed, --stdin-packs is incompatible with these options).
1060 strvec_push(&cmd
.args
, "--all");
1061 strvec_push(&cmd
.args
, "--reflog");
1062 strvec_push(&cmd
.args
, "--indexed-objects");
1064 if (repo_has_promisor_remote(the_repository
))
1065 strvec_push(&cmd
.args
, "--exclude-promisor-objects");
1067 if (write_bitmaps
> 0)
1068 strvec_push(&cmd
.args
, "--write-bitmap-index");
1069 else if (write_bitmaps
< 0)
1070 strvec_push(&cmd
.args
, "--write-bitmap-index-quiet");
1072 if (use_delta_islands
)
1073 strvec_push(&cmd
.args
, "--delta-islands");
1075 if (pack_everything
& ALL_INTO_ONE
) {
1076 repack_promisor_objects(&po_args
, &names
);
1078 if (has_existing_non_kept_packs(&existing
) &&
1080 !(pack_everything
& PACK_CRUFT
)) {
1081 for_each_string_list_item(item
, &names
) {
1082 strvec_pushf(&cmd
.args
, "--keep-pack=%s-%s.pack",
1083 packtmp_name
, item
->string
);
1085 if (unpack_unreachable
) {
1086 strvec_pushf(&cmd
.args
,
1087 "--unpack-unreachable=%s",
1088 unpack_unreachable
);
1089 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
1090 strvec_push(&cmd
.args
,
1091 "--unpack-unreachable");
1092 } else if (keep_unreachable
) {
1093 strvec_push(&cmd
.args
, "--keep-unreachable");
1094 strvec_push(&cmd
.args
, "--pack-loose-unreachable");
1097 } else if (geometry
.split_factor
) {
1098 strvec_push(&cmd
.args
, "--stdin-packs");
1099 strvec_push(&cmd
.args
, "--unpacked");
1101 strvec_push(&cmd
.args
, "--unpacked");
1102 strvec_push(&cmd
.args
, "--incremental");
1105 if (geometry
.split_factor
)
1110 ret
= start_command(&cmd
);
1114 if (geometry
.split_factor
) {
1115 FILE *in
= xfdopen(cmd
.in
, "w");
1117 * The resulting pack should contain all objects in packs that
1118 * are going to be rolled up, but exclude objects in packs which
1119 * are being left alone.
1121 for (i
= 0; i
< geometry
.split
; i
++)
1122 fprintf(in
, "%s\n", pack_basename(geometry
.pack
[i
]));
1123 for (i
= geometry
.split
; i
< geometry
.pack_nr
; i
++)
1124 fprintf(in
, "^%s\n", pack_basename(geometry
.pack
[i
]));
1128 out
= xfdopen(cmd
.out
, "r");
1129 while (strbuf_getline_lf(&line
, out
) != EOF
) {
1130 struct string_list_item
*item
;
1132 if (line
.len
!= the_hash_algo
->hexsz
)
1133 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
1134 item
= string_list_append(&names
, line
.buf
);
1135 item
->util
= populate_pack_exts(item
->string
);
1137 strbuf_release(&line
);
1139 ret
= finish_command(&cmd
);
1143 if (!names
.nr
&& !po_args
.quiet
)
1144 printf_ln(_("Nothing new to pack."));
1146 if (pack_everything
& PACK_CRUFT
) {
1147 const char *pack_prefix
;
1148 if (!skip_prefix(packtmp
, packdir
, &pack_prefix
))
1149 die(_("pack prefix %s does not begin with objdir %s"),
1151 if (*pack_prefix
== '/')
1154 if (!cruft_po_args
.window
)
1155 cruft_po_args
.window
= po_args
.window
;
1156 if (!cruft_po_args
.window_memory
)
1157 cruft_po_args
.window_memory
= po_args
.window_memory
;
1158 if (!cruft_po_args
.depth
)
1159 cruft_po_args
.depth
= po_args
.depth
;
1160 if (!cruft_po_args
.threads
)
1161 cruft_po_args
.threads
= po_args
.threads
;
1163 cruft_po_args
.local
= po_args
.local
;
1164 cruft_po_args
.quiet
= po_args
.quiet
;
1166 ret
= write_cruft_pack(&cruft_po_args
, packtmp
, pack_prefix
,
1167 cruft_expiration
, &names
,
1172 if (delete_redundant
&& expire_to
) {
1174 * If `--expire-to` is given with `-d`, it's possible
1175 * that we're about to prune some objects. With cruft
1176 * packs, pruning is implicit: any objects from existing
1177 * packs that weren't picked up by new packs are removed
1178 * when their packs are deleted.
1180 * Generate an additional cruft pack, with one twist:
1181 * `names` now includes the name of the cruft pack
1182 * written in the previous step. So the contents of
1183 * _this_ cruft pack exclude everything contained in the
1184 * existing cruft pack (that is, all of the unreachable
1185 * objects which are no older than
1186 * `--cruft-expiration`).
1188 * To make this work, cruft_expiration must become NULL
1189 * so that this cruft pack doesn't actually prune any
1190 * objects. If it were non-NULL, this call would always
1191 * generate an empty pack (since every object not in the
1192 * cruft pack generated above will have an mtime older
1193 * than the expiration).
1195 ret
= write_cruft_pack(&cruft_po_args
, expire_to
,
1205 string_list_sort(&names
);
1207 close_object_store(the_repository
->objects
);
1210 * Ok we have prepared all new packfiles.
1212 for_each_string_list_item(item
, &names
) {
1213 struct generated_pack_data
*data
= item
->util
;
1215 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
1218 fname
= mkpathdup("%s/pack-%s%s",
1219 packdir
, item
->string
, exts
[ext
].name
);
1221 if (data
->tempfiles
[ext
]) {
1222 const char *fname_old
= get_tempfile_path(data
->tempfiles
[ext
]);
1223 struct stat statbuffer
;
1225 if (!stat(fname_old
, &statbuffer
)) {
1226 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
1227 chmod(fname_old
, statbuffer
.st_mode
);
1230 if (rename_tempfile(&data
->tempfiles
[ext
], fname
))
1231 die_errno(_("renaming pack to '%s' failed"), fname
);
1232 } else if (!exts
[ext
].optional
)
1233 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1234 exts
[ext
].name
, packtmp
, item
->string
);
1235 else if (unlink(fname
) < 0 && errno
!= ENOENT
)
1236 die_errno(_("could not unlink: %s"), fname
);
1241 /* End of pack replacement. */
1243 if (delete_redundant
&& pack_everything
& ALL_INTO_ONE
)
1244 mark_packs_for_deletion(&existing
, &names
);
1247 struct string_list include
= STRING_LIST_INIT_NODUP
;
1248 midx_included_packs(&include
, &existing
, &names
, &geometry
);
1250 ret
= write_midx_included_packs(&include
, &geometry
,
1251 refs_snapshot
? get_tempfile_path(refs_snapshot
) : NULL
,
1252 show_progress
, write_bitmaps
> 0);
1254 if (!ret
&& write_bitmaps
)
1255 remove_redundant_bitmaps(&include
, packdir
);
1257 string_list_clear(&include
, 0);
1263 reprepare_packed_git(the_repository
);
1265 if (delete_redundant
) {
1267 remove_redundant_existing_packs(&existing
);
1269 if (geometry
.split_factor
)
1270 geometry_remove_redundant_packs(&geometry
, &names
,
1273 opts
|= PRUNE_PACKED_VERBOSE
;
1274 prune_packed_objects(opts
);
1276 if (!keep_unreachable
&&
1277 (!(pack_everything
& LOOSEN_UNREACHABLE
) ||
1278 unpack_unreachable
) &&
1279 is_repository_shallow(the_repository
))
1280 prune_shallow(PRUNE_QUICK
);
1283 if (run_update_server_info
)
1284 update_server_info(0);
1286 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0)) {
1288 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0))
1289 flags
|= MIDX_WRITE_BITMAP
| MIDX_WRITE_REV_INDEX
;
1290 write_midx_file(get_object_directory(), NULL
, NULL
, flags
);
1294 string_list_clear(&names
, 1);
1295 existing_packs_release(&existing
);
1296 free_pack_geometry(&geometry
);