5 #include "parse-options.h"
6 #include "run-command.h"
9 #include "string-list.h"
13 #include "prune-packed.h"
14 #include "object-store.h"
15 #include "promisor-remote.h"
18 #include "pack-bitmap.h"
21 #define ALL_INTO_ONE 1
22 #define LOOSEN_UNREACHABLE 2
28 static int pack_everything
;
29 static int delta_base_offset
= 1;
30 static int pack_kept_objects
= -1;
31 static int write_bitmaps
= -1;
32 static int use_delta_islands
;
33 static int run_update_server_info
= 1;
34 static char *packdir
, *packtmp_name
, *packtmp
;
36 static const char *const git_repack_usage
[] = {
37 N_("git repack [<options>]"),
41 static const char incremental_bitmap_conflict_error
[] = N_(
42 "Incremental repacks are incompatible with bitmap indexes. Use\n"
43 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
46 struct pack_objects_args
{
48 const char *window_memory
;
51 const char *max_pack_size
;
58 static int repack_config(const char *var
, const char *value
, void *cb
)
60 struct pack_objects_args
*cruft_po_args
= cb
;
61 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
62 delta_base_offset
= git_config_bool(var
, value
);
65 if (!strcmp(var
, "repack.packkeptobjects")) {
66 pack_kept_objects
= git_config_bool(var
, value
);
69 if (!strcmp(var
, "repack.writebitmaps") ||
70 !strcmp(var
, "pack.writebitmaps")) {
71 write_bitmaps
= git_config_bool(var
, value
);
74 if (!strcmp(var
, "repack.usedeltaislands")) {
75 use_delta_islands
= git_config_bool(var
, value
);
78 if (strcmp(var
, "repack.updateserverinfo") == 0) {
79 run_update_server_info
= git_config_bool(var
, value
);
82 if (!strcmp(var
, "repack.cruftwindow"))
83 return git_config_string(&cruft_po_args
->window
, var
, value
);
84 if (!strcmp(var
, "repack.cruftwindowmemory"))
85 return git_config_string(&cruft_po_args
->window_memory
, var
, value
);
86 if (!strcmp(var
, "repack.cruftdepth"))
87 return git_config_string(&cruft_po_args
->depth
, var
, value
);
88 if (!strcmp(var
, "repack.cruftthreads"))
89 return git_config_string(&cruft_po_args
->threads
, var
, value
);
90 return git_default_config(var
, value
, cb
);
94 * Adds all packs hex strings to either fname_nonkept_list or
95 * fname_kept_list based on whether each pack has a corresponding
96 * .keep file or not. Packs without a .keep file are not to be kept
97 * if we are going to pack everything into one file.
99 static void collect_pack_filenames(struct string_list
*fname_nonkept_list
,
100 struct string_list
*fname_kept_list
,
101 const struct string_list
*extra_keep
)
107 if (!(dir
= opendir(packdir
)))
110 while ((e
= readdir(dir
)) != NULL
) {
114 if (!strip_suffix(e
->d_name
, ".pack", &len
))
117 for (i
= 0; i
< extra_keep
->nr
; i
++)
118 if (!fspathcmp(e
->d_name
, extra_keep
->items
[i
].string
))
121 fname
= xmemdupz(e
->d_name
, len
);
123 if ((extra_keep
->nr
> 0 && i
< extra_keep
->nr
) ||
124 (file_exists(mkpath("%s/%s.keep", packdir
, fname
)))) {
125 string_list_append_nodup(fname_kept_list
, fname
);
127 struct string_list_item
*item
;
128 item
= string_list_append_nodup(fname_nonkept_list
,
130 if (file_exists(mkpath("%s/%s.mtimes", packdir
, fname
)))
131 item
->util
= (void*)(uintptr_t)CRUFT_PACK
;
136 string_list_sort(fname_kept_list
);
139 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
141 struct strbuf buf
= STRBUF_INIT
;
142 struct multi_pack_index
*m
= get_local_multi_pack_index(the_repository
);
143 strbuf_addf(&buf
, "%s.pack", base_name
);
144 if (m
&& midx_contains_pack(m
, buf
.buf
))
145 clear_midx_file(the_repository
);
146 strbuf_insertf(&buf
, 0, "%s/", dir_name
);
147 unlink_pack_path(buf
.buf
, 1);
148 strbuf_release(&buf
);
151 static void prepare_pack_objects(struct child_process
*cmd
,
152 const struct pack_objects_args
*args
,
155 strvec_push(&cmd
->args
, "pack-objects");
157 strvec_pushf(&cmd
->args
, "--window=%s", args
->window
);
158 if (args
->window_memory
)
159 strvec_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
161 strvec_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
163 strvec_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
164 if (args
->max_pack_size
)
165 strvec_pushf(&cmd
->args
, "--max-pack-size=%s", args
->max_pack_size
);
166 if (args
->no_reuse_delta
)
167 strvec_pushf(&cmd
->args
, "--no-reuse-delta");
168 if (args
->no_reuse_object
)
169 strvec_pushf(&cmd
->args
, "--no-reuse-object");
171 strvec_push(&cmd
->args
, "--local");
173 strvec_push(&cmd
->args
, "--quiet");
174 if (delta_base_offset
)
175 strvec_push(&cmd
->args
, "--delta-base-offset");
176 strvec_push(&cmd
->args
, out
);
182 * Write oid to the given struct child_process's stdin, starting it first if
185 static int write_oid(const struct object_id
*oid
, struct packed_git
*pack
,
186 uint32_t pos
, void *data
)
188 struct child_process
*cmd
= data
;
191 if (start_command(cmd
))
192 die(_("could not start pack-objects to repack promisor objects"));
195 xwrite(cmd
->in
, oid_to_hex(oid
), the_hash_algo
->hexsz
);
196 xwrite(cmd
->in
, "\n", 1);
212 struct generated_pack_data
{
213 struct tempfile
*tempfiles
[ARRAY_SIZE(exts
)];
216 static struct generated_pack_data
*populate_pack_exts(const char *name
)
219 struct strbuf path
= STRBUF_INIT
;
220 struct generated_pack_data
*data
= xcalloc(1, sizeof(*data
));
223 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
225 strbuf_addf(&path
, "%s-%s%s", packtmp
, name
, exts
[i
].name
);
227 if (stat(path
.buf
, &statbuf
))
230 data
->tempfiles
[i
] = register_tempfile(path
.buf
);
233 strbuf_release(&path
);
237 static void repack_promisor_objects(const struct pack_objects_args
*args
,
238 struct string_list
*names
)
240 struct child_process cmd
= CHILD_PROCESS_INIT
;
242 struct strbuf line
= STRBUF_INIT
;
244 prepare_pack_objects(&cmd
, args
, packtmp
);
248 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
249 * hints may result in suboptimal deltas in the resulting pack. See if
250 * the OIDs can be sent with fake paths such that pack-objects can use a
251 * {type -> existing pack order} ordering when computing deltas instead
252 * of a {type -> size} ordering, which may produce better deltas.
254 for_each_packed_object(write_oid
, &cmd
,
255 FOR_EACH_OBJECT_PROMISOR_ONLY
);
258 /* No packed objects; cmd was never started */
259 child_process_clear(&cmd
);
265 out
= xfdopen(cmd
.out
, "r");
266 while (strbuf_getline_lf(&line
, out
) != EOF
) {
267 struct string_list_item
*item
;
270 if (line
.len
!= the_hash_algo
->hexsz
)
271 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
272 item
= string_list_append(names
, line
.buf
);
275 * pack-objects creates the .pack and .idx files, but not the
276 * .promisor file. Create the .promisor file, which is empty.
278 * NEEDSWORK: fetch-pack sometimes generates non-empty
279 * .promisor files containing the ref names and associated
280 * hashes at the point of generation of the corresponding
281 * packfile, but this would not preserve their contents. Maybe
282 * concatenate the contents of all .promisor files instead of
283 * just creating a new empty file.
285 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
287 write_promisor_file(promisor_name
, NULL
, 0);
289 item
->util
= populate_pack_exts(item
->string
);
294 if (finish_command(&cmd
))
295 die(_("could not finish pack-objects to repack promisor objects"));
298 struct pack_geometry
{
299 struct packed_git
**pack
;
300 uint32_t pack_nr
, pack_alloc
;
304 static uint32_t geometry_pack_weight(struct packed_git
*p
)
306 if (open_pack_index(p
))
307 die(_("cannot open index for %s"), p
->pack_name
);
308 return p
->num_objects
;
311 static int geometry_cmp(const void *va
, const void *vb
)
313 uint32_t aw
= geometry_pack_weight(*(struct packed_git
**)va
),
314 bw
= geometry_pack_weight(*(struct packed_git
**)vb
);
323 static void init_pack_geometry(struct pack_geometry
**geometry_p
,
324 struct string_list
*existing_kept_packs
)
326 struct packed_git
*p
;
327 struct pack_geometry
*geometry
;
328 struct strbuf buf
= STRBUF_INIT
;
330 *geometry_p
= xcalloc(1, sizeof(struct pack_geometry
));
331 geometry
= *geometry_p
;
333 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
334 if (!pack_kept_objects
) {
336 * Any pack that has its pack_keep bit set will appear
337 * in existing_kept_packs below, but this saves us from
338 * doing a more expensive check.
344 * The pack may be kept via the --keep-pack option;
345 * check 'existing_kept_packs' to determine whether to
349 strbuf_addstr(&buf
, pack_basename(p
));
350 strbuf_strip_suffix(&buf
, ".pack");
352 if (string_list_has_string(existing_kept_packs
, buf
.buf
))
358 ALLOC_GROW(geometry
->pack
,
359 geometry
->pack_nr
+ 1,
360 geometry
->pack_alloc
);
362 geometry
->pack
[geometry
->pack_nr
] = p
;
366 QSORT(geometry
->pack
, geometry
->pack_nr
, geometry_cmp
);
367 strbuf_release(&buf
);
370 static void split_pack_geometry(struct pack_geometry
*geometry
, int factor
)
374 off_t total_size
= 0;
376 if (!geometry
->pack_nr
) {
377 geometry
->split
= geometry
->pack_nr
;
382 * First, count the number of packs (in descending order of size) which
383 * already form a geometric progression.
385 for (i
= geometry
->pack_nr
- 1; i
> 0; i
--) {
386 struct packed_git
*ours
= geometry
->pack
[i
];
387 struct packed_git
*prev
= geometry
->pack
[i
- 1];
389 if (unsigned_mult_overflows(factor
, geometry_pack_weight(prev
)))
390 die(_("pack %s too large to consider in geometric "
394 if (geometry_pack_weight(ours
) < factor
* geometry_pack_weight(prev
))
402 * Move the split one to the right, since the top element in the
403 * last-compared pair can't be in the progression. Only do this
404 * when we split in the middle of the array (otherwise if we got
405 * to the end, then the split is in the right place).
411 * Then, anything to the left of 'split' must be in a new pack. But,
412 * creating that new pack may cause packs in the heavy half to no longer
413 * form a geometric progression.
415 * Compute an expected size of the new pack, and then determine how many
416 * packs in the heavy half need to be joined into it (if any) to restore
417 * the geometric progression.
419 for (i
= 0; i
< split
; i
++) {
420 struct packed_git
*p
= geometry
->pack
[i
];
422 if (unsigned_add_overflows(total_size
, geometry_pack_weight(p
)))
423 die(_("pack %s too large to roll up"), p
->pack_name
);
424 total_size
+= geometry_pack_weight(p
);
426 for (i
= split
; i
< geometry
->pack_nr
; i
++) {
427 struct packed_git
*ours
= geometry
->pack
[i
];
429 if (unsigned_mult_overflows(factor
, total_size
))
430 die(_("pack %s too large to roll up"), ours
->pack_name
);
432 if (geometry_pack_weight(ours
) < factor
* total_size
) {
433 if (unsigned_add_overflows(total_size
,
434 geometry_pack_weight(ours
)))
435 die(_("pack %s too large to roll up"),
439 total_size
+= geometry_pack_weight(ours
);
444 geometry
->split
= split
;
447 static struct packed_git
*get_preferred_pack(struct pack_geometry
*geometry
)
453 * No geometry means either an all-into-one repack (in which
454 * case there is only one pack left and it is the largest) or an
457 * If repacking incrementally, then we could check the size of
458 * all packs to determine which should be preferred, but leave
463 if (geometry
->split
== geometry
->pack_nr
)
467 * The preferred pack is the largest pack above the split line. In
468 * other words, it is the largest pack that does not get rolled up in
469 * the geometric repack.
471 for (i
= geometry
->pack_nr
; i
> geometry
->split
; i
--)
473 * A pack that is not local would never be included in a
474 * multi-pack index. We thus skip over any non-local packs.
476 if (geometry
->pack
[i
- 1]->pack_local
)
477 return geometry
->pack
[i
- 1];
482 static void clear_pack_geometry(struct pack_geometry
*geometry
)
487 free(geometry
->pack
);
488 geometry
->pack_nr
= 0;
489 geometry
->pack_alloc
= 0;
493 struct midx_snapshot_ref_data
{
499 static int midx_snapshot_ref_one(const char *refname UNUSED
,
500 const struct object_id
*oid
,
501 int flag UNUSED
, void *_data
)
503 struct midx_snapshot_ref_data
*data
= _data
;
504 struct object_id peeled
;
506 if (!peel_iterated_oid(oid
, &peeled
))
509 if (oidset_insert(&data
->seen
, oid
))
510 return 0; /* already seen */
512 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
515 fprintf(data
->f
->fp
, "%s%s\n", data
->preferred
? "+" : "",
521 static void midx_snapshot_refs(struct tempfile
*f
)
523 struct midx_snapshot_ref_data data
;
524 const struct string_list
*preferred
= bitmap_preferred_tips(the_repository
);
528 oidset_init(&data
.seen
, 0);
530 if (!fdopen_tempfile(f
, "w"))
531 die(_("could not open tempfile %s for writing"),
532 get_tempfile_path(f
));
535 struct string_list_item
*item
;
538 for_each_string_list_item(item
, preferred
)
539 for_each_ref_in(item
->string
, midx_snapshot_ref_one
, &data
);
543 for_each_ref(midx_snapshot_ref_one
, &data
);
545 if (close_tempfile_gently(f
)) {
546 int save_errno
= errno
;
549 die_errno(_("could not close refs snapshot tempfile"));
552 oidset_clear(&data
.seen
);
555 static void midx_included_packs(struct string_list
*include
,
556 struct string_list
*existing_nonkept_packs
,
557 struct string_list
*existing_kept_packs
,
558 struct string_list
*names
,
559 struct pack_geometry
*geometry
)
561 struct string_list_item
*item
;
563 for_each_string_list_item(item
, existing_kept_packs
)
564 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
565 for_each_string_list_item(item
, names
)
566 string_list_insert(include
, xstrfmt("pack-%s.idx", item
->string
));
568 struct strbuf buf
= STRBUF_INIT
;
570 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++) {
571 struct packed_git
*p
= geometry
->pack
[i
];
574 * The multi-pack index never refers to packfiles part
575 * of an alternate object database, so we skip these.
576 * While git-multi-pack-index(1) would silently ignore
577 * them anyway, this allows us to skip executing the
578 * command completely when we have only non-local
584 strbuf_addstr(&buf
, pack_basename(p
));
585 strbuf_strip_suffix(&buf
, ".pack");
586 strbuf_addstr(&buf
, ".idx");
588 string_list_insert(include
, strbuf_detach(&buf
, NULL
));
591 for_each_string_list_item(item
, existing_nonkept_packs
) {
592 if (!((uintptr_t)item
->util
& CRUFT_PACK
)) {
594 * no need to check DELETE_PACK, since we're not
595 * doing an ALL_INTO_ONE repack
599 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
602 for_each_string_list_item(item
, existing_nonkept_packs
) {
603 if ((uintptr_t)item
->util
& DELETE_PACK
)
605 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
610 static int write_midx_included_packs(struct string_list
*include
,
611 struct pack_geometry
*geometry
,
612 const char *refs_snapshot
,
613 int show_progress
, int write_bitmaps
)
615 struct child_process cmd
= CHILD_PROCESS_INIT
;
616 struct string_list_item
*item
;
617 struct packed_git
*preferred
= get_preferred_pack(geometry
);
627 strvec_push(&cmd
.args
, "multi-pack-index");
628 strvec_pushl(&cmd
.args
, "write", "--stdin-packs", NULL
);
631 strvec_push(&cmd
.args
, "--progress");
633 strvec_push(&cmd
.args
, "--no-progress");
636 strvec_push(&cmd
.args
, "--bitmap");
639 strvec_pushf(&cmd
.args
, "--preferred-pack=%s",
640 pack_basename(preferred
));
643 strvec_pushf(&cmd
.args
, "--refs-snapshot=%s", refs_snapshot
);
645 ret
= start_command(&cmd
);
649 in
= xfdopen(cmd
.in
, "w");
650 for_each_string_list_item(item
, include
)
651 fprintf(in
, "%s\n", item
->string
);
654 return finish_command(&cmd
);
657 static void remove_redundant_bitmaps(struct string_list
*include
,
660 struct strbuf path
= STRBUF_INIT
;
661 struct string_list_item
*item
;
664 strbuf_addstr(&path
, packdir
);
665 strbuf_addch(&path
, '/');
666 packdir_len
= path
.len
;
669 * Remove any pack bitmaps corresponding to packs which are now
670 * included in the MIDX.
672 for_each_string_list_item(item
, include
) {
673 strbuf_addstr(&path
, item
->string
);
674 strbuf_strip_suffix(&path
, ".idx");
675 strbuf_addstr(&path
, ".bitmap");
677 if (unlink(path
.buf
) && errno
!= ENOENT
)
678 warning_errno(_("could not remove stale bitmap: %s"),
681 strbuf_setlen(&path
, packdir_len
);
683 strbuf_release(&path
);
686 static int write_cruft_pack(const struct pack_objects_args
*args
,
687 const char *destination
,
688 const char *pack_prefix
,
689 const char *cruft_expiration
,
690 struct string_list
*names
,
691 struct string_list
*existing_packs
,
692 struct string_list
*existing_kept_packs
)
694 struct child_process cmd
= CHILD_PROCESS_INIT
;
695 struct strbuf line
= STRBUF_INIT
;
696 struct string_list_item
*item
;
700 int local
= skip_prefix(destination
, packdir
, &scratch
);
702 prepare_pack_objects(&cmd
, args
, destination
);
704 strvec_push(&cmd
.args
, "--cruft");
705 if (cruft_expiration
)
706 strvec_pushf(&cmd
.args
, "--cruft-expiration=%s",
709 strvec_push(&cmd
.args
, "--honor-pack-keep");
710 strvec_push(&cmd
.args
, "--non-empty");
711 strvec_push(&cmd
.args
, "--max-pack-size=0");
715 ret
= start_command(&cmd
);
720 * names has a confusing double use: it both provides the list
721 * of just-written new packs, and accepts the name of the cruft
722 * pack we are writing.
724 * By the time it is read here, it contains only the pack(s)
725 * that were just written, which is exactly the set of packs we
726 * want to consider kept.
728 * If `--expire-to` is given, the double-use served by `names`
729 * ensures that the pack written to `--expire-to` excludes any
730 * objects contained in the cruft pack.
732 in
= xfdopen(cmd
.in
, "w");
733 for_each_string_list_item(item
, names
)
734 fprintf(in
, "%s-%s.pack\n", pack_prefix
, item
->string
);
735 for_each_string_list_item(item
, existing_packs
)
736 fprintf(in
, "-%s.pack\n", item
->string
);
737 for_each_string_list_item(item
, existing_kept_packs
)
738 fprintf(in
, "%s.pack\n", item
->string
);
741 out
= xfdopen(cmd
.out
, "r");
742 while (strbuf_getline_lf(&line
, out
) != EOF
) {
743 struct string_list_item
*item
;
745 if (line
.len
!= the_hash_algo
->hexsz
)
746 die(_("repack: Expecting full hex object ID lines only "
747 "from pack-objects."));
749 * avoid putting packs written outside of the repository in the
753 item
= string_list_append(names
, line
.buf
);
754 item
->util
= populate_pack_exts(line
.buf
);
759 strbuf_release(&line
);
761 return finish_command(&cmd
);
764 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
766 struct child_process cmd
= CHILD_PROCESS_INIT
;
767 struct string_list_item
*item
;
768 struct string_list names
= STRING_LIST_INIT_DUP
;
769 struct string_list existing_nonkept_packs
= STRING_LIST_INIT_DUP
;
770 struct string_list existing_kept_packs
= STRING_LIST_INIT_DUP
;
771 struct pack_geometry
*geometry
= NULL
;
772 struct strbuf line
= STRBUF_INIT
;
773 struct tempfile
*refs_snapshot
= NULL
;
778 /* variables to be filled by option parsing */
779 int delete_redundant
= 0;
780 const char *unpack_unreachable
= NULL
;
781 int keep_unreachable
= 0;
782 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
783 struct pack_objects_args po_args
= {NULL
};
784 struct pack_objects_args cruft_po_args
= {NULL
};
785 int geometric_factor
= 0;
787 const char *cruft_expiration
= NULL
;
788 const char *expire_to
= NULL
;
790 struct option builtin_repack_options
[] = {
791 OPT_BIT('a', NULL
, &pack_everything
,
792 N_("pack everything in a single pack"), ALL_INTO_ONE
),
793 OPT_BIT('A', NULL
, &pack_everything
,
794 N_("same as -a, and turn unreachable objects loose"),
795 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
796 OPT_BIT(0, "cruft", &pack_everything
,
797 N_("same as -a, pack unreachable cruft objects separately"),
799 OPT_STRING(0, "cruft-expiration", &cruft_expiration
, N_("approxidate"),
800 N_("with -C, expire objects older than this")),
801 OPT_BOOL('d', NULL
, &delete_redundant
,
802 N_("remove redundant packs, and run git-prune-packed")),
803 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
804 N_("pass --no-reuse-delta to git-pack-objects")),
805 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
806 N_("pass --no-reuse-object to git-pack-objects")),
807 OPT_NEGBIT('n', NULL
, &run_update_server_info
,
808 N_("do not run git-update-server-info"), 1),
809 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
810 OPT_BOOL('l', "local", &po_args
.local
,
811 N_("pass --local to git-pack-objects")),
812 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
813 N_("write bitmap index")),
814 OPT_BOOL('i', "delta-islands", &use_delta_islands
,
815 N_("pass --delta-islands to git-pack-objects")),
816 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
817 N_("with -A, do not loosen objects older than this")),
818 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
819 N_("with -a, repack unreachable objects")),
820 OPT_STRING(0, "window", &po_args
.window
, N_("n"),
821 N_("size of the window used for delta compression")),
822 OPT_STRING(0, "window-memory", &po_args
.window_memory
, N_("bytes"),
823 N_("same as the above, but limit memory size instead of entries count")),
824 OPT_STRING(0, "depth", &po_args
.depth
, N_("n"),
825 N_("limits the maximum delta depth")),
826 OPT_STRING(0, "threads", &po_args
.threads
, N_("n"),
827 N_("limits the maximum number of threads")),
828 OPT_STRING(0, "max-pack-size", &po_args
.max_pack_size
, N_("bytes"),
829 N_("maximum size of each packfile")),
830 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
831 N_("repack objects in packs marked with .keep")),
832 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
833 N_("do not repack this pack")),
834 OPT_INTEGER('g', "geometric", &geometric_factor
,
835 N_("find a geometric progression with factor <N>")),
836 OPT_BOOL('m', "write-midx", &write_midx
,
837 N_("write a multi-pack index of the resulting packs")),
838 OPT_STRING(0, "expire-to", &expire_to
, N_("dir"),
839 N_("pack prefix to store a pack containing pruned objects")),
843 git_config(repack_config
, &cruft_po_args
);
845 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
846 git_repack_usage
, 0);
848 if (delete_redundant
&& repository_format_precious_objects
)
849 die(_("cannot delete packs in a precious-objects repo"));
851 if (keep_unreachable
&&
852 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
853 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
855 if (pack_everything
& PACK_CRUFT
) {
856 pack_everything
|= ALL_INTO_ONE
;
858 if (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
))
859 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
860 if (keep_unreachable
)
861 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
864 if (write_bitmaps
< 0) {
866 (!(pack_everything
& ALL_INTO_ONE
) || !is_bare_repository()))
868 } else if (write_bitmaps
&&
869 git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0) &&
870 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0)) {
873 if (pack_kept_objects
< 0)
874 pack_kept_objects
= write_bitmaps
> 0 && !write_midx
;
876 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
) && !write_midx
)
877 die(_(incremental_bitmap_conflict_error
));
879 if (write_midx
&& write_bitmaps
) {
880 struct strbuf path
= STRBUF_INIT
;
882 strbuf_addf(&path
, "%s/%s_XXXXXX", get_object_directory(),
885 refs_snapshot
= xmks_tempfile(path
.buf
);
886 midx_snapshot_refs(refs_snapshot
);
888 strbuf_release(&path
);
891 packdir
= mkpathdup("%s/pack", get_object_directory());
892 packtmp_name
= xstrfmt(".tmp-%d-pack", (int)getpid());
893 packtmp
= mkpathdup("%s/%s", packdir
, packtmp_name
);
895 collect_pack_filenames(&existing_nonkept_packs
, &existing_kept_packs
,
898 if (geometric_factor
) {
900 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
901 init_pack_geometry(&geometry
, &existing_kept_packs
);
902 split_pack_geometry(geometry
, geometric_factor
);
905 prepare_pack_objects(&cmd
, &po_args
, packtmp
);
907 show_progress
= !po_args
.quiet
&& isatty(2);
909 strvec_push(&cmd
.args
, "--keep-true-parents");
910 if (!pack_kept_objects
)
911 strvec_push(&cmd
.args
, "--honor-pack-keep");
912 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
913 strvec_pushf(&cmd
.args
, "--keep-pack=%s",
914 keep_pack_list
.items
[i
].string
);
915 strvec_push(&cmd
.args
, "--non-empty");
918 * We need to grab all reachable objects, including those that
919 * are reachable from reflogs and the index.
921 * When repacking into a geometric progression of packs,
922 * however, we ask 'git pack-objects --stdin-packs', and it is
923 * not about packing objects based on reachability but about
924 * repacking all the objects in specified packs and loose ones
925 * (indeed, --stdin-packs is incompatible with these options).
927 strvec_push(&cmd
.args
, "--all");
928 strvec_push(&cmd
.args
, "--reflog");
929 strvec_push(&cmd
.args
, "--indexed-objects");
931 if (has_promisor_remote())
932 strvec_push(&cmd
.args
, "--exclude-promisor-objects");
934 if (write_bitmaps
> 0)
935 strvec_push(&cmd
.args
, "--write-bitmap-index");
936 else if (write_bitmaps
< 0)
937 strvec_push(&cmd
.args
, "--write-bitmap-index-quiet");
939 if (use_delta_islands
)
940 strvec_push(&cmd
.args
, "--delta-islands");
942 if (pack_everything
& ALL_INTO_ONE
) {
943 repack_promisor_objects(&po_args
, &names
);
945 if (existing_nonkept_packs
.nr
&& delete_redundant
&&
946 !(pack_everything
& PACK_CRUFT
)) {
947 for_each_string_list_item(item
, &names
) {
948 strvec_pushf(&cmd
.args
, "--keep-pack=%s-%s.pack",
949 packtmp_name
, item
->string
);
951 if (unpack_unreachable
) {
952 strvec_pushf(&cmd
.args
,
953 "--unpack-unreachable=%s",
955 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
956 strvec_push(&cmd
.args
,
957 "--unpack-unreachable");
958 } else if (keep_unreachable
) {
959 strvec_push(&cmd
.args
, "--keep-unreachable");
960 strvec_push(&cmd
.args
, "--pack-loose-unreachable");
963 } else if (geometry
) {
964 strvec_push(&cmd
.args
, "--stdin-packs");
965 strvec_push(&cmd
.args
, "--unpacked");
967 strvec_push(&cmd
.args
, "--unpacked");
968 strvec_push(&cmd
.args
, "--incremental");
976 ret
= start_command(&cmd
);
981 FILE *in
= xfdopen(cmd
.in
, "w");
983 * The resulting pack should contain all objects in packs that
984 * are going to be rolled up, but exclude objects in packs which
985 * are being left alone.
987 for (i
= 0; i
< geometry
->split
; i
++)
988 fprintf(in
, "%s\n", pack_basename(geometry
->pack
[i
]));
989 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++)
990 fprintf(in
, "^%s\n", pack_basename(geometry
->pack
[i
]));
994 out
= xfdopen(cmd
.out
, "r");
995 while (strbuf_getline_lf(&line
, out
) != EOF
) {
996 struct string_list_item
*item
;
998 if (line
.len
!= the_hash_algo
->hexsz
)
999 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
1000 item
= string_list_append(&names
, line
.buf
);
1001 item
->util
= populate_pack_exts(item
->string
);
1003 strbuf_release(&line
);
1005 ret
= finish_command(&cmd
);
1009 if (!names
.nr
&& !po_args
.quiet
)
1010 printf_ln(_("Nothing new to pack."));
1012 if (pack_everything
& PACK_CRUFT
) {
1013 const char *pack_prefix
;
1014 if (!skip_prefix(packtmp
, packdir
, &pack_prefix
))
1015 die(_("pack prefix %s does not begin with objdir %s"),
1017 if (*pack_prefix
== '/')
1020 if (!cruft_po_args
.window
)
1021 cruft_po_args
.window
= po_args
.window
;
1022 if (!cruft_po_args
.window_memory
)
1023 cruft_po_args
.window_memory
= po_args
.window_memory
;
1024 if (!cruft_po_args
.depth
)
1025 cruft_po_args
.depth
= po_args
.depth
;
1026 if (!cruft_po_args
.threads
)
1027 cruft_po_args
.threads
= po_args
.threads
;
1029 cruft_po_args
.local
= po_args
.local
;
1030 cruft_po_args
.quiet
= po_args
.quiet
;
1032 ret
= write_cruft_pack(&cruft_po_args
, packtmp
, pack_prefix
,
1033 cruft_expiration
, &names
,
1034 &existing_nonkept_packs
,
1035 &existing_kept_packs
);
1039 if (delete_redundant
&& expire_to
) {
1041 * If `--expire-to` is given with `-d`, it's possible
1042 * that we're about to prune some objects. With cruft
1043 * packs, pruning is implicit: any objects from existing
1044 * packs that weren't picked up by new packs are removed
1045 * when their packs are deleted.
1047 * Generate an additional cruft pack, with one twist:
1048 * `names` now includes the name of the cruft pack
1049 * written in the previous step. So the contents of
1050 * _this_ cruft pack exclude everything contained in the
1051 * existing cruft pack (that is, all of the unreachable
1052 * objects which are no older than
1053 * `--cruft-expiration`).
1055 * To make this work, cruft_expiration must become NULL
1056 * so that this cruft pack doesn't actually prune any
1057 * objects. If it were non-NULL, this call would always
1058 * generate an empty pack (since every object not in the
1059 * cruft pack generated above will have an mtime older
1060 * than the expiration).
1062 ret
= write_cruft_pack(&cruft_po_args
, expire_to
,
1066 &existing_nonkept_packs
,
1067 &existing_kept_packs
);
1073 string_list_sort(&names
);
1075 close_object_store(the_repository
->objects
);
1078 * Ok we have prepared all new packfiles.
1080 for_each_string_list_item(item
, &names
) {
1081 struct generated_pack_data
*data
= item
->util
;
1083 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
1086 fname
= mkpathdup("%s/pack-%s%s",
1087 packdir
, item
->string
, exts
[ext
].name
);
1089 if (data
->tempfiles
[ext
]) {
1090 const char *fname_old
= get_tempfile_path(data
->tempfiles
[ext
]);
1091 struct stat statbuffer
;
1093 if (!stat(fname_old
, &statbuffer
)) {
1094 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
1095 chmod(fname_old
, statbuffer
.st_mode
);
1098 if (rename_tempfile(&data
->tempfiles
[ext
], fname
))
1099 die_errno(_("renaming pack to '%s' failed"), fname
);
1100 } else if (!exts
[ext
].optional
)
1101 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1102 exts
[ext
].name
, packtmp
, item
->string
);
1103 else if (unlink(fname
) < 0 && errno
!= ENOENT
)
1104 die_errno(_("could not unlink: %s"), fname
);
1109 /* End of pack replacement. */
1111 if (delete_redundant
&& pack_everything
& ALL_INTO_ONE
) {
1112 const int hexsz
= the_hash_algo
->hexsz
;
1113 for_each_string_list_item(item
, &existing_nonkept_packs
) {
1115 size_t len
= strlen(item
->string
);
1118 sha1
= item
->string
+ len
- hexsz
;
1120 * Mark this pack for deletion, which ensures that this
1121 * pack won't be included in a MIDX (if `--write-midx`
1122 * was given) and that we will actually delete this pack
1123 * (if `-d` was given).
1125 if (!string_list_has_string(&names
, sha1
))
1126 item
->util
= (void*)(uintptr_t)((size_t)item
->util
| DELETE_PACK
);
1131 struct string_list include
= STRING_LIST_INIT_NODUP
;
1132 midx_included_packs(&include
, &existing_nonkept_packs
,
1133 &existing_kept_packs
, &names
, geometry
);
1135 ret
= write_midx_included_packs(&include
, geometry
,
1136 refs_snapshot
? get_tempfile_path(refs_snapshot
) : NULL
,
1137 show_progress
, write_bitmaps
> 0);
1139 if (!ret
&& write_bitmaps
)
1140 remove_redundant_bitmaps(&include
, packdir
);
1142 string_list_clear(&include
, 0);
1148 reprepare_packed_git(the_repository
);
1150 if (delete_redundant
) {
1152 for_each_string_list_item(item
, &existing_nonkept_packs
) {
1153 if (!((uintptr_t)item
->util
& DELETE_PACK
))
1155 remove_redundant_pack(packdir
, item
->string
);
1159 struct strbuf buf
= STRBUF_INIT
;
1162 for (i
= 0; i
< geometry
->split
; i
++) {
1163 struct packed_git
*p
= geometry
->pack
[i
];
1164 if (string_list_has_string(&names
,
1165 hash_to_hex(p
->hash
)))
1169 strbuf_addstr(&buf
, pack_basename(p
));
1170 strbuf_strip_suffix(&buf
, ".pack");
1172 if ((p
->pack_keep
) ||
1173 (string_list_has_string(&existing_kept_packs
,
1177 remove_redundant_pack(packdir
, buf
.buf
);
1179 strbuf_release(&buf
);
1182 opts
|= PRUNE_PACKED_VERBOSE
;
1183 prune_packed_objects(opts
);
1185 if (!keep_unreachable
&&
1186 (!(pack_everything
& LOOSEN_UNREACHABLE
) ||
1187 unpack_unreachable
) &&
1188 is_repository_shallow(the_repository
))
1189 prune_shallow(PRUNE_QUICK
);
1192 if (run_update_server_info
)
1193 update_server_info(0);
1195 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0)) {
1197 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0))
1198 flags
|= MIDX_WRITE_BITMAP
| MIDX_WRITE_REV_INDEX
;
1199 write_midx_file(get_object_directory(), NULL
, NULL
, flags
);
1203 string_list_clear(&names
, 1);
1204 string_list_clear(&existing_nonkept_packs
, 0);
1205 string_list_clear(&existing_kept_packs
, 0);
1206 clear_pack_geometry(geometry
);