5 #include "environment.h"
8 #include "parse-options.h"
9 #include "run-command.h"
12 #include "string-list.h"
16 #include "prune-packed.h"
17 #include "object-store.h"
18 #include "promisor-remote.h"
21 #include "pack-bitmap.h"
24 #define ALL_INTO_ONE 1
25 #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
, void *cb
)
63 struct pack_objects_args
*cruft_po_args
= cb
;
64 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
65 delta_base_offset
= git_config_bool(var
, value
);
68 if (!strcmp(var
, "repack.packkeptobjects")) {
69 pack_kept_objects
= git_config_bool(var
, value
);
72 if (!strcmp(var
, "repack.writebitmaps") ||
73 !strcmp(var
, "pack.writebitmaps")) {
74 write_bitmaps
= git_config_bool(var
, value
);
77 if (!strcmp(var
, "repack.usedeltaislands")) {
78 use_delta_islands
= git_config_bool(var
, value
);
81 if (strcmp(var
, "repack.updateserverinfo") == 0) {
82 run_update_server_info
= git_config_bool(var
, value
);
85 if (!strcmp(var
, "repack.cruftwindow"))
86 return git_config_string(&cruft_po_args
->window
, var
, value
);
87 if (!strcmp(var
, "repack.cruftwindowmemory"))
88 return git_config_string(&cruft_po_args
->window_memory
, var
, value
);
89 if (!strcmp(var
, "repack.cruftdepth"))
90 return git_config_string(&cruft_po_args
->depth
, var
, value
);
91 if (!strcmp(var
, "repack.cruftthreads"))
92 return git_config_string(&cruft_po_args
->threads
, var
, value
);
93 return git_default_config(var
, value
, cb
);
97 * Adds all packs hex strings to either fname_nonkept_list or
98 * fname_kept_list based on whether each pack has a corresponding
99 * .keep file or not. Packs without a .keep file are not to be kept
100 * if we are going to pack everything into one file.
102 static void collect_pack_filenames(struct string_list
*fname_nonkept_list
,
103 struct string_list
*fname_kept_list
,
104 const struct string_list
*extra_keep
)
110 if (!(dir
= opendir(packdir
)))
113 while ((e
= readdir(dir
)) != NULL
) {
117 if (!strip_suffix(e
->d_name
, ".pack", &len
))
120 for (i
= 0; i
< extra_keep
->nr
; i
++)
121 if (!fspathcmp(e
->d_name
, extra_keep
->items
[i
].string
))
124 fname
= xmemdupz(e
->d_name
, len
);
126 if ((extra_keep
->nr
> 0 && i
< extra_keep
->nr
) ||
127 (file_exists(mkpath("%s/%s.keep", packdir
, fname
)))) {
128 string_list_append_nodup(fname_kept_list
, fname
);
130 struct string_list_item
*item
;
131 item
= string_list_append_nodup(fname_nonkept_list
,
133 if (file_exists(mkpath("%s/%s.mtimes", packdir
, fname
)))
134 item
->util
= (void*)(uintptr_t)CRUFT_PACK
;
139 string_list_sort(fname_kept_list
);
142 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
144 struct strbuf buf
= STRBUF_INIT
;
145 struct multi_pack_index
*m
= get_local_multi_pack_index(the_repository
);
146 strbuf_addf(&buf
, "%s.pack", base_name
);
147 if (m
&& midx_contains_pack(m
, buf
.buf
))
148 clear_midx_file(the_repository
);
149 strbuf_insertf(&buf
, 0, "%s/", dir_name
);
150 unlink_pack_path(buf
.buf
, 1);
151 strbuf_release(&buf
);
154 static void prepare_pack_objects(struct child_process
*cmd
,
155 const struct pack_objects_args
*args
,
158 strvec_push(&cmd
->args
, "pack-objects");
160 strvec_pushf(&cmd
->args
, "--window=%s", args
->window
);
161 if (args
->window_memory
)
162 strvec_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
164 strvec_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
166 strvec_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
167 if (args
->max_pack_size
)
168 strvec_pushf(&cmd
->args
, "--max-pack-size=%s", args
->max_pack_size
);
169 if (args
->no_reuse_delta
)
170 strvec_pushf(&cmd
->args
, "--no-reuse-delta");
171 if (args
->no_reuse_object
)
172 strvec_pushf(&cmd
->args
, "--no-reuse-object");
174 strvec_push(&cmd
->args
, "--local");
176 strvec_push(&cmd
->args
, "--quiet");
177 if (delta_base_offset
)
178 strvec_push(&cmd
->args
, "--delta-base-offset");
179 strvec_push(&cmd
->args
, out
);
185 * Write oid to the given struct child_process's stdin, starting it first if
188 static int write_oid(const struct object_id
*oid
,
189 struct packed_git
*pack UNUSED
,
190 uint32_t pos UNUSED
, void *data
)
192 struct child_process
*cmd
= data
;
195 if (start_command(cmd
))
196 die(_("could not start pack-objects to repack promisor objects"));
199 xwrite(cmd
->in
, oid_to_hex(oid
), the_hash_algo
->hexsz
);
200 xwrite(cmd
->in
, "\n", 1);
216 struct generated_pack_data
{
217 struct tempfile
*tempfiles
[ARRAY_SIZE(exts
)];
220 static struct generated_pack_data
*populate_pack_exts(const char *name
)
223 struct strbuf path
= STRBUF_INIT
;
224 struct generated_pack_data
*data
= xcalloc(1, sizeof(*data
));
227 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
229 strbuf_addf(&path
, "%s-%s%s", packtmp
, name
, exts
[i
].name
);
231 if (stat(path
.buf
, &statbuf
))
234 data
->tempfiles
[i
] = register_tempfile(path
.buf
);
237 strbuf_release(&path
);
241 static void repack_promisor_objects(const struct pack_objects_args
*args
,
242 struct string_list
*names
)
244 struct child_process cmd
= CHILD_PROCESS_INIT
;
246 struct strbuf line
= STRBUF_INIT
;
248 prepare_pack_objects(&cmd
, args
, packtmp
);
252 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
253 * hints may result in suboptimal deltas in the resulting pack. See if
254 * the OIDs can be sent with fake paths such that pack-objects can use a
255 * {type -> existing pack order} ordering when computing deltas instead
256 * of a {type -> size} ordering, which may produce better deltas.
258 for_each_packed_object(write_oid
, &cmd
,
259 FOR_EACH_OBJECT_PROMISOR_ONLY
);
262 /* No packed objects; cmd was never started */
263 child_process_clear(&cmd
);
269 out
= xfdopen(cmd
.out
, "r");
270 while (strbuf_getline_lf(&line
, out
) != EOF
) {
271 struct string_list_item
*item
;
274 if (line
.len
!= the_hash_algo
->hexsz
)
275 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
276 item
= string_list_append(names
, line
.buf
);
279 * pack-objects creates the .pack and .idx files, but not the
280 * .promisor file. Create the .promisor file, which is empty.
282 * NEEDSWORK: fetch-pack sometimes generates non-empty
283 * .promisor files containing the ref names and associated
284 * hashes at the point of generation of the corresponding
285 * packfile, but this would not preserve their contents. Maybe
286 * concatenate the contents of all .promisor files instead of
287 * just creating a new empty file.
289 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
291 write_promisor_file(promisor_name
, NULL
, 0);
293 item
->util
= populate_pack_exts(item
->string
);
298 if (finish_command(&cmd
))
299 die(_("could not finish pack-objects to repack promisor objects"));
302 struct pack_geometry
{
303 struct packed_git
**pack
;
304 uint32_t pack_nr
, pack_alloc
;
308 static uint32_t geometry_pack_weight(struct packed_git
*p
)
310 if (open_pack_index(p
))
311 die(_("cannot open index for %s"), p
->pack_name
);
312 return p
->num_objects
;
315 static int geometry_cmp(const void *va
, const void *vb
)
317 uint32_t aw
= geometry_pack_weight(*(struct packed_git
**)va
),
318 bw
= geometry_pack_weight(*(struct packed_git
**)vb
);
327 static void init_pack_geometry(struct pack_geometry
**geometry_p
,
328 struct string_list
*existing_kept_packs
)
330 struct packed_git
*p
;
331 struct pack_geometry
*geometry
;
332 struct strbuf buf
= STRBUF_INIT
;
334 *geometry_p
= xcalloc(1, sizeof(struct pack_geometry
));
335 geometry
= *geometry_p
;
337 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
338 if (!pack_kept_objects
) {
340 * Any pack that has its pack_keep bit set will appear
341 * in existing_kept_packs below, but this saves us from
342 * doing a more expensive check.
348 * The pack may be kept via the --keep-pack option;
349 * check 'existing_kept_packs' to determine whether to
353 strbuf_addstr(&buf
, pack_basename(p
));
354 strbuf_strip_suffix(&buf
, ".pack");
356 if (string_list_has_string(existing_kept_packs
, buf
.buf
))
362 ALLOC_GROW(geometry
->pack
,
363 geometry
->pack_nr
+ 1,
364 geometry
->pack_alloc
);
366 geometry
->pack
[geometry
->pack_nr
] = p
;
370 QSORT(geometry
->pack
, geometry
->pack_nr
, geometry_cmp
);
371 strbuf_release(&buf
);
374 static void split_pack_geometry(struct pack_geometry
*geometry
, int factor
)
378 off_t total_size
= 0;
380 if (!geometry
->pack_nr
) {
381 geometry
->split
= geometry
->pack_nr
;
386 * First, count the number of packs (in descending order of size) which
387 * already form a geometric progression.
389 for (i
= geometry
->pack_nr
- 1; i
> 0; i
--) {
390 struct packed_git
*ours
= geometry
->pack
[i
];
391 struct packed_git
*prev
= geometry
->pack
[i
- 1];
393 if (unsigned_mult_overflows(factor
, geometry_pack_weight(prev
)))
394 die(_("pack %s too large to consider in geometric "
398 if (geometry_pack_weight(ours
) < factor
* geometry_pack_weight(prev
))
406 * Move the split one to the right, since the top element in the
407 * last-compared pair can't be in the progression. Only do this
408 * when we split in the middle of the array (otherwise if we got
409 * to the end, then the split is in the right place).
415 * Then, anything to the left of 'split' must be in a new pack. But,
416 * creating that new pack may cause packs in the heavy half to no longer
417 * form a geometric progression.
419 * Compute an expected size of the new pack, and then determine how many
420 * packs in the heavy half need to be joined into it (if any) to restore
421 * the geometric progression.
423 for (i
= 0; i
< split
; i
++) {
424 struct packed_git
*p
= geometry
->pack
[i
];
426 if (unsigned_add_overflows(total_size
, geometry_pack_weight(p
)))
427 die(_("pack %s too large to roll up"), p
->pack_name
);
428 total_size
+= geometry_pack_weight(p
);
430 for (i
= split
; i
< geometry
->pack_nr
; i
++) {
431 struct packed_git
*ours
= geometry
->pack
[i
];
433 if (unsigned_mult_overflows(factor
, total_size
))
434 die(_("pack %s too large to roll up"), ours
->pack_name
);
436 if (geometry_pack_weight(ours
) < factor
* total_size
) {
437 if (unsigned_add_overflows(total_size
,
438 geometry_pack_weight(ours
)))
439 die(_("pack %s too large to roll up"),
443 total_size
+= geometry_pack_weight(ours
);
448 geometry
->split
= split
;
451 static struct packed_git
*get_largest_active_pack(struct pack_geometry
*geometry
)
455 * No geometry means either an all-into-one repack (in which
456 * case there is only one pack left and it is the largest) or an
459 * If repacking incrementally, then we could check the size of
460 * all packs to determine which should be preferred, but leave
465 if (geometry
->split
== geometry
->pack_nr
)
467 return geometry
->pack
[geometry
->pack_nr
- 1];
470 static void clear_pack_geometry(struct pack_geometry
*geometry
)
475 free(geometry
->pack
);
476 geometry
->pack_nr
= 0;
477 geometry
->pack_alloc
= 0;
481 struct midx_snapshot_ref_data
{
487 static int midx_snapshot_ref_one(const char *refname UNUSED
,
488 const struct object_id
*oid
,
489 int flag UNUSED
, void *_data
)
491 struct midx_snapshot_ref_data
*data
= _data
;
492 struct object_id peeled
;
494 if (!peel_iterated_oid(oid
, &peeled
))
497 if (oidset_insert(&data
->seen
, oid
))
498 return 0; /* already seen */
500 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
503 fprintf(data
->f
->fp
, "%s%s\n", data
->preferred
? "+" : "",
509 static void midx_snapshot_refs(struct tempfile
*f
)
511 struct midx_snapshot_ref_data data
;
512 const struct string_list
*preferred
= bitmap_preferred_tips(the_repository
);
516 oidset_init(&data
.seen
, 0);
518 if (!fdopen_tempfile(f
, "w"))
519 die(_("could not open tempfile %s for writing"),
520 get_tempfile_path(f
));
523 struct string_list_item
*item
;
526 for_each_string_list_item(item
, preferred
)
527 for_each_ref_in(item
->string
, midx_snapshot_ref_one
, &data
);
531 for_each_ref(midx_snapshot_ref_one
, &data
);
533 if (close_tempfile_gently(f
)) {
534 int save_errno
= errno
;
537 die_errno(_("could not close refs snapshot tempfile"));
540 oidset_clear(&data
.seen
);
543 static void midx_included_packs(struct string_list
*include
,
544 struct string_list
*existing_nonkept_packs
,
545 struct string_list
*existing_kept_packs
,
546 struct string_list
*names
,
547 struct pack_geometry
*geometry
)
549 struct string_list_item
*item
;
551 for_each_string_list_item(item
, existing_kept_packs
)
552 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
553 for_each_string_list_item(item
, names
)
554 string_list_insert(include
, xstrfmt("pack-%s.idx", item
->string
));
556 struct strbuf buf
= STRBUF_INIT
;
558 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++) {
559 struct packed_git
*p
= geometry
->pack
[i
];
561 strbuf_addstr(&buf
, pack_basename(p
));
562 strbuf_strip_suffix(&buf
, ".pack");
563 strbuf_addstr(&buf
, ".idx");
565 string_list_insert(include
, strbuf_detach(&buf
, NULL
));
568 for_each_string_list_item(item
, existing_nonkept_packs
) {
569 if (!((uintptr_t)item
->util
& CRUFT_PACK
)) {
571 * no need to check DELETE_PACK, since we're not
572 * doing an ALL_INTO_ONE repack
576 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
579 for_each_string_list_item(item
, existing_nonkept_packs
) {
580 if ((uintptr_t)item
->util
& DELETE_PACK
)
582 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
587 static int write_midx_included_packs(struct string_list
*include
,
588 struct pack_geometry
*geometry
,
589 const char *refs_snapshot
,
590 int show_progress
, int write_bitmaps
)
592 struct child_process cmd
= CHILD_PROCESS_INIT
;
593 struct string_list_item
*item
;
594 struct packed_git
*largest
= get_largest_active_pack(geometry
);
604 strvec_push(&cmd
.args
, "multi-pack-index");
605 strvec_pushl(&cmd
.args
, "write", "--stdin-packs", NULL
);
608 strvec_push(&cmd
.args
, "--progress");
610 strvec_push(&cmd
.args
, "--no-progress");
613 strvec_push(&cmd
.args
, "--bitmap");
616 strvec_pushf(&cmd
.args
, "--preferred-pack=%s",
617 pack_basename(largest
));
620 strvec_pushf(&cmd
.args
, "--refs-snapshot=%s", refs_snapshot
);
622 ret
= start_command(&cmd
);
626 in
= xfdopen(cmd
.in
, "w");
627 for_each_string_list_item(item
, include
)
628 fprintf(in
, "%s\n", item
->string
);
631 return finish_command(&cmd
);
634 static void remove_redundant_bitmaps(struct string_list
*include
,
637 struct strbuf path
= STRBUF_INIT
;
638 struct string_list_item
*item
;
641 strbuf_addstr(&path
, packdir
);
642 strbuf_addch(&path
, '/');
643 packdir_len
= path
.len
;
646 * Remove any pack bitmaps corresponding to packs which are now
647 * included in the MIDX.
649 for_each_string_list_item(item
, include
) {
650 strbuf_addstr(&path
, item
->string
);
651 strbuf_strip_suffix(&path
, ".idx");
652 strbuf_addstr(&path
, ".bitmap");
654 if (unlink(path
.buf
) && errno
!= ENOENT
)
655 warning_errno(_("could not remove stale bitmap: %s"),
658 strbuf_setlen(&path
, packdir_len
);
660 strbuf_release(&path
);
663 static int write_cruft_pack(const struct pack_objects_args
*args
,
664 const char *destination
,
665 const char *pack_prefix
,
666 const char *cruft_expiration
,
667 struct string_list
*names
,
668 struct string_list
*existing_packs
,
669 struct string_list
*existing_kept_packs
)
671 struct child_process cmd
= CHILD_PROCESS_INIT
;
672 struct strbuf line
= STRBUF_INIT
;
673 struct string_list_item
*item
;
677 int local
= skip_prefix(destination
, packdir
, &scratch
);
679 prepare_pack_objects(&cmd
, args
, destination
);
681 strvec_push(&cmd
.args
, "--cruft");
682 if (cruft_expiration
)
683 strvec_pushf(&cmd
.args
, "--cruft-expiration=%s",
686 strvec_push(&cmd
.args
, "--honor-pack-keep");
687 strvec_push(&cmd
.args
, "--non-empty");
688 strvec_push(&cmd
.args
, "--max-pack-size=0");
692 ret
= start_command(&cmd
);
697 * names has a confusing double use: it both provides the list
698 * of just-written new packs, and accepts the name of the cruft
699 * pack we are writing.
701 * By the time it is read here, it contains only the pack(s)
702 * that were just written, which is exactly the set of packs we
703 * want to consider kept.
705 * If `--expire-to` is given, the double-use served by `names`
706 * ensures that the pack written to `--expire-to` excludes any
707 * objects contained in the cruft pack.
709 in
= xfdopen(cmd
.in
, "w");
710 for_each_string_list_item(item
, names
)
711 fprintf(in
, "%s-%s.pack\n", pack_prefix
, item
->string
);
712 for_each_string_list_item(item
, existing_packs
)
713 fprintf(in
, "-%s.pack\n", item
->string
);
714 for_each_string_list_item(item
, existing_kept_packs
)
715 fprintf(in
, "%s.pack\n", item
->string
);
718 out
= xfdopen(cmd
.out
, "r");
719 while (strbuf_getline_lf(&line
, out
) != EOF
) {
720 struct string_list_item
*item
;
722 if (line
.len
!= the_hash_algo
->hexsz
)
723 die(_("repack: Expecting full hex object ID lines only "
724 "from pack-objects."));
726 * avoid putting packs written outside of the repository in the
730 item
= string_list_append(names
, line
.buf
);
731 item
->util
= populate_pack_exts(line
.buf
);
736 strbuf_release(&line
);
738 return finish_command(&cmd
);
741 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
743 struct child_process cmd
= CHILD_PROCESS_INIT
;
744 struct string_list_item
*item
;
745 struct string_list names
= STRING_LIST_INIT_DUP
;
746 struct string_list existing_nonkept_packs
= STRING_LIST_INIT_DUP
;
747 struct string_list existing_kept_packs
= STRING_LIST_INIT_DUP
;
748 struct pack_geometry
*geometry
= NULL
;
749 struct strbuf line
= STRBUF_INIT
;
750 struct tempfile
*refs_snapshot
= NULL
;
755 /* variables to be filled by option parsing */
756 int delete_redundant
= 0;
757 const char *unpack_unreachable
= NULL
;
758 int keep_unreachable
= 0;
759 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
760 struct pack_objects_args po_args
= {NULL
};
761 struct pack_objects_args cruft_po_args
= {NULL
};
762 int geometric_factor
= 0;
764 const char *cruft_expiration
= NULL
;
765 const char *expire_to
= NULL
;
767 struct option builtin_repack_options
[] = {
768 OPT_BIT('a', NULL
, &pack_everything
,
769 N_("pack everything in a single pack"), ALL_INTO_ONE
),
770 OPT_BIT('A', NULL
, &pack_everything
,
771 N_("same as -a, and turn unreachable objects loose"),
772 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
773 OPT_BIT(0, "cruft", &pack_everything
,
774 N_("same as -a, pack unreachable cruft objects separately"),
776 OPT_STRING(0, "cruft-expiration", &cruft_expiration
, N_("approxidate"),
777 N_("with -C, expire objects older than this")),
778 OPT_BOOL('d', NULL
, &delete_redundant
,
779 N_("remove redundant packs, and run git-prune-packed")),
780 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
781 N_("pass --no-reuse-delta to git-pack-objects")),
782 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
783 N_("pass --no-reuse-object to git-pack-objects")),
784 OPT_NEGBIT('n', NULL
, &run_update_server_info
,
785 N_("do not run git-update-server-info"), 1),
786 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
787 OPT_BOOL('l', "local", &po_args
.local
,
788 N_("pass --local to git-pack-objects")),
789 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
790 N_("write bitmap index")),
791 OPT_BOOL('i', "delta-islands", &use_delta_islands
,
792 N_("pass --delta-islands to git-pack-objects")),
793 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
794 N_("with -A, do not loosen objects older than this")),
795 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
796 N_("with -a, repack unreachable objects")),
797 OPT_STRING(0, "window", &po_args
.window
, N_("n"),
798 N_("size of the window used for delta compression")),
799 OPT_STRING(0, "window-memory", &po_args
.window_memory
, N_("bytes"),
800 N_("same as the above, but limit memory size instead of entries count")),
801 OPT_STRING(0, "depth", &po_args
.depth
, N_("n"),
802 N_("limits the maximum delta depth")),
803 OPT_STRING(0, "threads", &po_args
.threads
, N_("n"),
804 N_("limits the maximum number of threads")),
805 OPT_STRING(0, "max-pack-size", &po_args
.max_pack_size
, N_("bytes"),
806 N_("maximum size of each packfile")),
807 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
808 N_("repack objects in packs marked with .keep")),
809 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
810 N_("do not repack this pack")),
811 OPT_INTEGER('g', "geometric", &geometric_factor
,
812 N_("find a geometric progression with factor <N>")),
813 OPT_BOOL('m', "write-midx", &write_midx
,
814 N_("write a multi-pack index of the resulting packs")),
815 OPT_STRING(0, "expire-to", &expire_to
, N_("dir"),
816 N_("pack prefix to store a pack containing pruned objects")),
820 git_config(repack_config
, &cruft_po_args
);
822 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
823 git_repack_usage
, 0);
825 if (delete_redundant
&& repository_format_precious_objects
)
826 die(_("cannot delete packs in a precious-objects repo"));
828 if (keep_unreachable
&&
829 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
830 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
832 if (pack_everything
& PACK_CRUFT
) {
833 pack_everything
|= ALL_INTO_ONE
;
835 if (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
))
836 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
837 if (keep_unreachable
)
838 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
841 if (write_bitmaps
< 0) {
843 (!(pack_everything
& ALL_INTO_ONE
) || !is_bare_repository()))
845 } else if (write_bitmaps
&&
846 git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0) &&
847 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0)) {
850 if (pack_kept_objects
< 0)
851 pack_kept_objects
= write_bitmaps
> 0 && !write_midx
;
853 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
) && !write_midx
)
854 die(_(incremental_bitmap_conflict_error
));
856 if (write_midx
&& write_bitmaps
) {
857 struct strbuf path
= STRBUF_INIT
;
859 strbuf_addf(&path
, "%s/%s_XXXXXX", get_object_directory(),
862 refs_snapshot
= xmks_tempfile(path
.buf
);
863 midx_snapshot_refs(refs_snapshot
);
865 strbuf_release(&path
);
868 packdir
= mkpathdup("%s/pack", get_object_directory());
869 packtmp_name
= xstrfmt(".tmp-%d-pack", (int)getpid());
870 packtmp
= mkpathdup("%s/%s", packdir
, packtmp_name
);
872 collect_pack_filenames(&existing_nonkept_packs
, &existing_kept_packs
,
875 if (geometric_factor
) {
877 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
878 init_pack_geometry(&geometry
, &existing_kept_packs
);
879 split_pack_geometry(geometry
, geometric_factor
);
882 prepare_pack_objects(&cmd
, &po_args
, packtmp
);
884 show_progress
= !po_args
.quiet
&& isatty(2);
886 strvec_push(&cmd
.args
, "--keep-true-parents");
887 if (!pack_kept_objects
)
888 strvec_push(&cmd
.args
, "--honor-pack-keep");
889 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
890 strvec_pushf(&cmd
.args
, "--keep-pack=%s",
891 keep_pack_list
.items
[i
].string
);
892 strvec_push(&cmd
.args
, "--non-empty");
895 * We need to grab all reachable objects, including those that
896 * are reachable from reflogs and the index.
898 * When repacking into a geometric progression of packs,
899 * however, we ask 'git pack-objects --stdin-packs', and it is
900 * not about packing objects based on reachability but about
901 * repacking all the objects in specified packs and loose ones
902 * (indeed, --stdin-packs is incompatible with these options).
904 strvec_push(&cmd
.args
, "--all");
905 strvec_push(&cmd
.args
, "--reflog");
906 strvec_push(&cmd
.args
, "--indexed-objects");
908 if (repo_has_promisor_remote(the_repository
))
909 strvec_push(&cmd
.args
, "--exclude-promisor-objects");
911 if (write_bitmaps
> 0)
912 strvec_push(&cmd
.args
, "--write-bitmap-index");
913 else if (write_bitmaps
< 0)
914 strvec_push(&cmd
.args
, "--write-bitmap-index-quiet");
916 if (use_delta_islands
)
917 strvec_push(&cmd
.args
, "--delta-islands");
919 if (pack_everything
& ALL_INTO_ONE
) {
920 repack_promisor_objects(&po_args
, &names
);
922 if (existing_nonkept_packs
.nr
&& delete_redundant
&&
923 !(pack_everything
& PACK_CRUFT
)) {
924 for_each_string_list_item(item
, &names
) {
925 strvec_pushf(&cmd
.args
, "--keep-pack=%s-%s.pack",
926 packtmp_name
, item
->string
);
928 if (unpack_unreachable
) {
929 strvec_pushf(&cmd
.args
,
930 "--unpack-unreachable=%s",
932 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
933 strvec_push(&cmd
.args
,
934 "--unpack-unreachable");
935 } else if (keep_unreachable
) {
936 strvec_push(&cmd
.args
, "--keep-unreachable");
937 strvec_push(&cmd
.args
, "--pack-loose-unreachable");
940 } else if (geometry
) {
941 strvec_push(&cmd
.args
, "--stdin-packs");
942 strvec_push(&cmd
.args
, "--unpacked");
944 strvec_push(&cmd
.args
, "--unpacked");
945 strvec_push(&cmd
.args
, "--incremental");
953 ret
= start_command(&cmd
);
958 FILE *in
= xfdopen(cmd
.in
, "w");
960 * The resulting pack should contain all objects in packs that
961 * are going to be rolled up, but exclude objects in packs which
962 * are being left alone.
964 for (i
= 0; i
< geometry
->split
; i
++)
965 fprintf(in
, "%s\n", pack_basename(geometry
->pack
[i
]));
966 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++)
967 fprintf(in
, "^%s\n", pack_basename(geometry
->pack
[i
]));
971 out
= xfdopen(cmd
.out
, "r");
972 while (strbuf_getline_lf(&line
, out
) != EOF
) {
973 struct string_list_item
*item
;
975 if (line
.len
!= the_hash_algo
->hexsz
)
976 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
977 item
= string_list_append(&names
, line
.buf
);
978 item
->util
= populate_pack_exts(item
->string
);
980 strbuf_release(&line
);
982 ret
= finish_command(&cmd
);
986 if (!names
.nr
&& !po_args
.quiet
)
987 printf_ln(_("Nothing new to pack."));
989 if (pack_everything
& PACK_CRUFT
) {
990 const char *pack_prefix
;
991 if (!skip_prefix(packtmp
, packdir
, &pack_prefix
))
992 die(_("pack prefix %s does not begin with objdir %s"),
994 if (*pack_prefix
== '/')
997 if (!cruft_po_args
.window
)
998 cruft_po_args
.window
= po_args
.window
;
999 if (!cruft_po_args
.window_memory
)
1000 cruft_po_args
.window_memory
= po_args
.window_memory
;
1001 if (!cruft_po_args
.depth
)
1002 cruft_po_args
.depth
= po_args
.depth
;
1003 if (!cruft_po_args
.threads
)
1004 cruft_po_args
.threads
= po_args
.threads
;
1006 cruft_po_args
.local
= po_args
.local
;
1007 cruft_po_args
.quiet
= po_args
.quiet
;
1009 ret
= write_cruft_pack(&cruft_po_args
, packtmp
, pack_prefix
,
1010 cruft_expiration
, &names
,
1011 &existing_nonkept_packs
,
1012 &existing_kept_packs
);
1016 if (delete_redundant
&& expire_to
) {
1018 * If `--expire-to` is given with `-d`, it's possible
1019 * that we're about to prune some objects. With cruft
1020 * packs, pruning is implicit: any objects from existing
1021 * packs that weren't picked up by new packs are removed
1022 * when their packs are deleted.
1024 * Generate an additional cruft pack, with one twist:
1025 * `names` now includes the name of the cruft pack
1026 * written in the previous step. So the contents of
1027 * _this_ cruft pack exclude everything contained in the
1028 * existing cruft pack (that is, all of the unreachable
1029 * objects which are no older than
1030 * `--cruft-expiration`).
1032 * To make this work, cruft_expiration must become NULL
1033 * so that this cruft pack doesn't actually prune any
1034 * objects. If it were non-NULL, this call would always
1035 * generate an empty pack (since every object not in the
1036 * cruft pack generated above will have an mtime older
1037 * than the expiration).
1039 ret
= write_cruft_pack(&cruft_po_args
, expire_to
,
1043 &existing_nonkept_packs
,
1044 &existing_kept_packs
);
1050 string_list_sort(&names
);
1052 close_object_store(the_repository
->objects
);
1055 * Ok we have prepared all new packfiles.
1057 for_each_string_list_item(item
, &names
) {
1058 struct generated_pack_data
*data
= item
->util
;
1060 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
1063 fname
= mkpathdup("%s/pack-%s%s",
1064 packdir
, item
->string
, exts
[ext
].name
);
1066 if (data
->tempfiles
[ext
]) {
1067 const char *fname_old
= get_tempfile_path(data
->tempfiles
[ext
]);
1068 struct stat statbuffer
;
1070 if (!stat(fname_old
, &statbuffer
)) {
1071 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
1072 chmod(fname_old
, statbuffer
.st_mode
);
1075 if (rename_tempfile(&data
->tempfiles
[ext
], fname
))
1076 die_errno(_("renaming pack to '%s' failed"), fname
);
1077 } else if (!exts
[ext
].optional
)
1078 die(_("pack-objects did not write a '%s' file for pack %s-%s"),
1079 exts
[ext
].name
, packtmp
, item
->string
);
1080 else if (unlink(fname
) < 0 && errno
!= ENOENT
)
1081 die_errno(_("could not unlink: %s"), fname
);
1086 /* End of pack replacement. */
1088 if (delete_redundant
&& pack_everything
& ALL_INTO_ONE
) {
1089 const int hexsz
= the_hash_algo
->hexsz
;
1090 for_each_string_list_item(item
, &existing_nonkept_packs
) {
1092 size_t len
= strlen(item
->string
);
1095 sha1
= item
->string
+ len
- hexsz
;
1097 * Mark this pack for deletion, which ensures that this
1098 * pack won't be included in a MIDX (if `--write-midx`
1099 * was given) and that we will actually delete this pack
1100 * (if `-d` was given).
1102 if (!string_list_has_string(&names
, sha1
))
1103 item
->util
= (void*)(uintptr_t)((size_t)item
->util
| DELETE_PACK
);
1108 struct string_list include
= STRING_LIST_INIT_NODUP
;
1109 midx_included_packs(&include
, &existing_nonkept_packs
,
1110 &existing_kept_packs
, &names
, geometry
);
1112 ret
= write_midx_included_packs(&include
, geometry
,
1113 refs_snapshot
? get_tempfile_path(refs_snapshot
) : NULL
,
1114 show_progress
, write_bitmaps
> 0);
1116 if (!ret
&& write_bitmaps
)
1117 remove_redundant_bitmaps(&include
, packdir
);
1119 string_list_clear(&include
, 0);
1125 reprepare_packed_git(the_repository
);
1127 if (delete_redundant
) {
1129 for_each_string_list_item(item
, &existing_nonkept_packs
) {
1130 if (!((uintptr_t)item
->util
& DELETE_PACK
))
1132 remove_redundant_pack(packdir
, item
->string
);
1136 struct strbuf buf
= STRBUF_INIT
;
1139 for (i
= 0; i
< geometry
->split
; i
++) {
1140 struct packed_git
*p
= geometry
->pack
[i
];
1141 if (string_list_has_string(&names
,
1142 hash_to_hex(p
->hash
)))
1146 strbuf_addstr(&buf
, pack_basename(p
));
1147 strbuf_strip_suffix(&buf
, ".pack");
1149 if ((p
->pack_keep
) ||
1150 (string_list_has_string(&existing_kept_packs
,
1154 remove_redundant_pack(packdir
, buf
.buf
);
1156 strbuf_release(&buf
);
1159 opts
|= PRUNE_PACKED_VERBOSE
;
1160 prune_packed_objects(opts
);
1162 if (!keep_unreachable
&&
1163 (!(pack_everything
& LOOSEN_UNREACHABLE
) ||
1164 unpack_unreachable
) &&
1165 is_repository_shallow(the_repository
))
1166 prune_shallow(PRUNE_QUICK
);
1169 if (run_update_server_info
)
1170 update_server_info(0);
1172 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0)) {
1174 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0))
1175 flags
|= MIDX_WRITE_BITMAP
| MIDX_WRITE_REV_INDEX
;
1176 write_midx_file(get_object_directory(), NULL
, NULL
, flags
);
1180 string_list_clear(&names
, 1);
1181 string_list_clear(&existing_nonkept_packs
, 0);
1182 string_list_clear(&existing_kept_packs
, 0);
1183 clear_pack_geometry(geometry
);