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
;
35 static char *cruft_expiration
;
37 static const char *const git_repack_usage
[] = {
38 N_("git repack [<options>]"),
42 static const char incremental_bitmap_conflict_error
[] = N_(
43 "Incremental repacks are incompatible with bitmap indexes. Use\n"
44 "--no-write-bitmap-index or disable the pack.writeBitmaps configuration."
47 struct pack_objects_args
{
49 const char *window_memory
;
52 const char *max_pack_size
;
59 static int repack_config(const char *var
, const char *value
, void *cb
)
61 struct pack_objects_args
*cruft_po_args
= cb
;
62 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
63 delta_base_offset
= git_config_bool(var
, value
);
66 if (!strcmp(var
, "repack.packkeptobjects")) {
67 pack_kept_objects
= git_config_bool(var
, value
);
70 if (!strcmp(var
, "repack.writebitmaps") ||
71 !strcmp(var
, "pack.writebitmaps")) {
72 write_bitmaps
= git_config_bool(var
, value
);
75 if (!strcmp(var
, "repack.usedeltaislands")) {
76 use_delta_islands
= git_config_bool(var
, value
);
79 if (strcmp(var
, "repack.updateserverinfo") == 0) {
80 run_update_server_info
= git_config_bool(var
, value
);
83 if (!strcmp(var
, "repack.cruftwindow"))
84 return git_config_string(&cruft_po_args
->window
, var
, value
);
85 if (!strcmp(var
, "repack.cruftwindowmemory"))
86 return git_config_string(&cruft_po_args
->window_memory
, var
, value
);
87 if (!strcmp(var
, "repack.cruftdepth"))
88 return git_config_string(&cruft_po_args
->depth
, var
, value
);
89 if (!strcmp(var
, "repack.cruftthreads"))
90 return git_config_string(&cruft_po_args
->threads
, var
, value
);
91 return git_default_config(var
, value
, cb
);
95 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
97 static void remove_temporary_files(void)
99 struct strbuf buf
= STRBUF_INIT
;
100 size_t dirlen
, prefixlen
;
104 dir
= opendir(packdir
);
108 /* Point at the slash at the end of ".../objects/pack/" */
109 dirlen
= strlen(packdir
) + 1;
110 strbuf_addstr(&buf
, packtmp
);
111 /* Hold the length of ".tmp-%d-pack-" */
112 prefixlen
= buf
.len
- dirlen
;
114 while ((e
= readdir(dir
))) {
115 if (strncmp(e
->d_name
, buf
.buf
+ dirlen
, prefixlen
))
117 strbuf_setlen(&buf
, dirlen
);
118 strbuf_addstr(&buf
, e
->d_name
);
122 strbuf_release(&buf
);
125 static void remove_pack_on_signal(int signo
)
127 remove_temporary_files();
133 * Adds all packs hex strings to either fname_nonkept_list or
134 * fname_kept_list based on whether each pack has a corresponding
135 * .keep file or not. Packs without a .keep file are not to be kept
136 * if we are going to pack everything into one file.
138 static void collect_pack_filenames(struct string_list
*fname_nonkept_list
,
139 struct string_list
*fname_kept_list
,
140 const struct string_list
*extra_keep
)
146 if (!(dir
= opendir(packdir
)))
149 while ((e
= readdir(dir
)) != NULL
) {
153 if (!strip_suffix(e
->d_name
, ".pack", &len
))
156 for (i
= 0; i
< extra_keep
->nr
; i
++)
157 if (!fspathcmp(e
->d_name
, extra_keep
->items
[i
].string
))
160 fname
= xmemdupz(e
->d_name
, len
);
162 if ((extra_keep
->nr
> 0 && i
< extra_keep
->nr
) ||
163 (file_exists(mkpath("%s/%s.keep", packdir
, fname
)))) {
164 string_list_append_nodup(fname_kept_list
, fname
);
166 struct string_list_item
*item
;
167 item
= string_list_append_nodup(fname_nonkept_list
,
169 if (file_exists(mkpath("%s/%s.mtimes", packdir
, fname
)))
170 item
->util
= (void*)(uintptr_t)CRUFT_PACK
;
175 string_list_sort(fname_kept_list
);
178 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
180 struct strbuf buf
= STRBUF_INIT
;
181 struct multi_pack_index
*m
= get_local_multi_pack_index(the_repository
);
182 strbuf_addf(&buf
, "%s.pack", base_name
);
183 if (m
&& midx_contains_pack(m
, buf
.buf
))
184 clear_midx_file(the_repository
);
185 strbuf_insertf(&buf
, 0, "%s/", dir_name
);
186 unlink_pack_path(buf
.buf
, 1);
187 strbuf_release(&buf
);
190 static void prepare_pack_objects(struct child_process
*cmd
,
191 const struct pack_objects_args
*args
)
193 strvec_push(&cmd
->args
, "pack-objects");
195 strvec_pushf(&cmd
->args
, "--window=%s", args
->window
);
196 if (args
->window_memory
)
197 strvec_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
199 strvec_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
201 strvec_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
202 if (args
->max_pack_size
)
203 strvec_pushf(&cmd
->args
, "--max-pack-size=%s", args
->max_pack_size
);
204 if (args
->no_reuse_delta
)
205 strvec_pushf(&cmd
->args
, "--no-reuse-delta");
206 if (args
->no_reuse_object
)
207 strvec_pushf(&cmd
->args
, "--no-reuse-object");
209 strvec_push(&cmd
->args
, "--local");
211 strvec_push(&cmd
->args
, "--quiet");
212 if (delta_base_offset
)
213 strvec_push(&cmd
->args
, "--delta-base-offset");
214 strvec_push(&cmd
->args
, packtmp
);
220 * Write oid to the given struct child_process's stdin, starting it first if
223 static int write_oid(const struct object_id
*oid
, struct packed_git
*pack
,
224 uint32_t pos
, void *data
)
226 struct child_process
*cmd
= data
;
229 if (start_command(cmd
))
230 die(_("could not start pack-objects to repack promisor objects"));
233 xwrite(cmd
->in
, oid_to_hex(oid
), the_hash_algo
->hexsz
);
234 xwrite(cmd
->in
, "\n", 1);
250 static unsigned populate_pack_exts(char *name
)
253 struct strbuf path
= STRBUF_INIT
;
257 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
259 strbuf_addf(&path
, "%s-%s%s", packtmp
, name
, exts
[i
].name
);
261 if (stat(path
.buf
, &statbuf
))
267 strbuf_release(&path
);
271 static void repack_promisor_objects(const struct pack_objects_args
*args
,
272 struct string_list
*names
)
274 struct child_process cmd
= CHILD_PROCESS_INIT
;
276 struct strbuf line
= STRBUF_INIT
;
278 prepare_pack_objects(&cmd
, args
);
282 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
283 * hints may result in suboptimal deltas in the resulting pack. See if
284 * the OIDs can be sent with fake paths such that pack-objects can use a
285 * {type -> existing pack order} ordering when computing deltas instead
286 * of a {type -> size} ordering, which may produce better deltas.
288 for_each_packed_object(write_oid
, &cmd
,
289 FOR_EACH_OBJECT_PROMISOR_ONLY
);
292 /* No packed objects; cmd was never started */
293 child_process_clear(&cmd
);
299 out
= xfdopen(cmd
.out
, "r");
300 while (strbuf_getline_lf(&line
, out
) != EOF
) {
301 struct string_list_item
*item
;
304 if (line
.len
!= the_hash_algo
->hexsz
)
305 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
306 item
= string_list_append(names
, line
.buf
);
309 * pack-objects creates the .pack and .idx files, but not the
310 * .promisor file. Create the .promisor file, which is empty.
312 * NEEDSWORK: fetch-pack sometimes generates non-empty
313 * .promisor files containing the ref names and associated
314 * hashes at the point of generation of the corresponding
315 * packfile, but this would not preserve their contents. Maybe
316 * concatenate the contents of all .promisor files instead of
317 * just creating a new empty file.
319 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
321 write_promisor_file(promisor_name
, NULL
, 0);
323 item
->util
= (void *)(uintptr_t)populate_pack_exts(item
->string
);
328 if (finish_command(&cmd
))
329 die(_("could not finish pack-objects to repack promisor objects"));
332 struct pack_geometry
{
333 struct packed_git
**pack
;
334 uint32_t pack_nr
, pack_alloc
;
338 static uint32_t geometry_pack_weight(struct packed_git
*p
)
340 if (open_pack_index(p
))
341 die(_("cannot open index for %s"), p
->pack_name
);
342 return p
->num_objects
;
345 static int geometry_cmp(const void *va
, const void *vb
)
347 uint32_t aw
= geometry_pack_weight(*(struct packed_git
**)va
),
348 bw
= geometry_pack_weight(*(struct packed_git
**)vb
);
357 static void init_pack_geometry(struct pack_geometry
**geometry_p
,
358 struct string_list
*existing_kept_packs
)
360 struct packed_git
*p
;
361 struct pack_geometry
*geometry
;
362 struct strbuf buf
= STRBUF_INIT
;
364 *geometry_p
= xcalloc(1, sizeof(struct pack_geometry
));
365 geometry
= *geometry_p
;
367 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
368 if (!pack_kept_objects
) {
370 * Any pack that has its pack_keep bit set will appear
371 * in existing_kept_packs below, but this saves us from
372 * doing a more expensive check.
378 * The pack may be kept via the --keep-pack option;
379 * check 'existing_kept_packs' to determine whether to
383 strbuf_addstr(&buf
, pack_basename(p
));
384 strbuf_strip_suffix(&buf
, ".pack");
386 if (string_list_has_string(existing_kept_packs
, buf
.buf
))
392 ALLOC_GROW(geometry
->pack
,
393 geometry
->pack_nr
+ 1,
394 geometry
->pack_alloc
);
396 geometry
->pack
[geometry
->pack_nr
] = p
;
400 QSORT(geometry
->pack
, geometry
->pack_nr
, geometry_cmp
);
401 strbuf_release(&buf
);
404 static void split_pack_geometry(struct pack_geometry
*geometry
, int factor
)
408 off_t total_size
= 0;
410 if (!geometry
->pack_nr
) {
411 geometry
->split
= geometry
->pack_nr
;
416 * First, count the number of packs (in descending order of size) which
417 * already form a geometric progression.
419 for (i
= geometry
->pack_nr
- 1; i
> 0; i
--) {
420 struct packed_git
*ours
= geometry
->pack
[i
];
421 struct packed_git
*prev
= geometry
->pack
[i
- 1];
423 if (unsigned_mult_overflows(factor
, geometry_pack_weight(prev
)))
424 die(_("pack %s too large to consider in geometric "
428 if (geometry_pack_weight(ours
) < factor
* geometry_pack_weight(prev
))
436 * Move the split one to the right, since the top element in the
437 * last-compared pair can't be in the progression. Only do this
438 * when we split in the middle of the array (otherwise if we got
439 * to the end, then the split is in the right place).
445 * Then, anything to the left of 'split' must be in a new pack. But,
446 * creating that new pack may cause packs in the heavy half to no longer
447 * form a geometric progression.
449 * Compute an expected size of the new pack, and then determine how many
450 * packs in the heavy half need to be joined into it (if any) to restore
451 * the geometric progression.
453 for (i
= 0; i
< split
; i
++) {
454 struct packed_git
*p
= geometry
->pack
[i
];
456 if (unsigned_add_overflows(total_size
, geometry_pack_weight(p
)))
457 die(_("pack %s too large to roll up"), p
->pack_name
);
458 total_size
+= geometry_pack_weight(p
);
460 for (i
= split
; i
< geometry
->pack_nr
; i
++) {
461 struct packed_git
*ours
= geometry
->pack
[i
];
463 if (unsigned_mult_overflows(factor
, total_size
))
464 die(_("pack %s too large to roll up"), ours
->pack_name
);
466 if (geometry_pack_weight(ours
) < factor
* total_size
) {
467 if (unsigned_add_overflows(total_size
,
468 geometry_pack_weight(ours
)))
469 die(_("pack %s too large to roll up"),
473 total_size
+= geometry_pack_weight(ours
);
478 geometry
->split
= split
;
481 static struct packed_git
*get_largest_active_pack(struct pack_geometry
*geometry
)
485 * No geometry means either an all-into-one repack (in which
486 * case there is only one pack left and it is the largest) or an
489 * If repacking incrementally, then we could check the size of
490 * all packs to determine which should be preferred, but leave
495 if (geometry
->split
== geometry
->pack_nr
)
497 return geometry
->pack
[geometry
->pack_nr
- 1];
500 static void clear_pack_geometry(struct pack_geometry
*geometry
)
505 free(geometry
->pack
);
506 geometry
->pack_nr
= 0;
507 geometry
->pack_alloc
= 0;
511 struct midx_snapshot_ref_data
{
517 static int midx_snapshot_ref_one(const char *refname
,
518 const struct object_id
*oid
,
519 int flag
, void *_data
)
521 struct midx_snapshot_ref_data
*data
= _data
;
522 struct object_id peeled
;
524 if (!peel_iterated_oid(oid
, &peeled
))
527 if (oidset_insert(&data
->seen
, oid
))
528 return 0; /* already seen */
530 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
533 fprintf(data
->f
->fp
, "%s%s\n", data
->preferred
? "+" : "",
539 static void midx_snapshot_refs(struct tempfile
*f
)
541 struct midx_snapshot_ref_data data
;
542 const struct string_list
*preferred
= bitmap_preferred_tips(the_repository
);
546 oidset_init(&data
.seen
, 0);
548 if (!fdopen_tempfile(f
, "w"))
549 die(_("could not open tempfile %s for writing"),
550 get_tempfile_path(f
));
553 struct string_list_item
*item
;
556 for_each_string_list_item(item
, preferred
)
557 for_each_ref_in(item
->string
, midx_snapshot_ref_one
, &data
);
561 for_each_ref(midx_snapshot_ref_one
, &data
);
563 if (close_tempfile_gently(f
)) {
564 int save_errno
= errno
;
567 die_errno(_("could not close refs snapshot tempfile"));
570 oidset_clear(&data
.seen
);
573 static void midx_included_packs(struct string_list
*include
,
574 struct string_list
*existing_nonkept_packs
,
575 struct string_list
*existing_kept_packs
,
576 struct string_list
*names
,
577 struct pack_geometry
*geometry
)
579 struct string_list_item
*item
;
581 for_each_string_list_item(item
, existing_kept_packs
)
582 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
583 for_each_string_list_item(item
, names
)
584 string_list_insert(include
, xstrfmt("pack-%s.idx", item
->string
));
586 struct strbuf buf
= STRBUF_INIT
;
588 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++) {
589 struct packed_git
*p
= geometry
->pack
[i
];
591 strbuf_addstr(&buf
, pack_basename(p
));
592 strbuf_strip_suffix(&buf
, ".pack");
593 strbuf_addstr(&buf
, ".idx");
595 string_list_insert(include
, strbuf_detach(&buf
, NULL
));
598 for_each_string_list_item(item
, existing_nonkept_packs
) {
599 if (!((uintptr_t)item
->util
& CRUFT_PACK
)) {
601 * no need to check DELETE_PACK, since we're not
602 * doing an ALL_INTO_ONE repack
606 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
609 for_each_string_list_item(item
, existing_nonkept_packs
) {
610 if ((uintptr_t)item
->util
& DELETE_PACK
)
612 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
617 static int write_midx_included_packs(struct string_list
*include
,
618 struct pack_geometry
*geometry
,
619 const char *refs_snapshot
,
620 int show_progress
, int write_bitmaps
)
622 struct child_process cmd
= CHILD_PROCESS_INIT
;
623 struct string_list_item
*item
;
624 struct packed_git
*largest
= get_largest_active_pack(geometry
);
634 strvec_push(&cmd
.args
, "multi-pack-index");
635 strvec_pushl(&cmd
.args
, "write", "--stdin-packs", NULL
);
638 strvec_push(&cmd
.args
, "--progress");
640 strvec_push(&cmd
.args
, "--no-progress");
643 strvec_push(&cmd
.args
, "--bitmap");
646 strvec_pushf(&cmd
.args
, "--preferred-pack=%s",
647 pack_basename(largest
));
650 strvec_pushf(&cmd
.args
, "--refs-snapshot=%s", refs_snapshot
);
652 ret
= start_command(&cmd
);
656 in
= xfdopen(cmd
.in
, "w");
657 for_each_string_list_item(item
, include
)
658 fprintf(in
, "%s\n", item
->string
);
661 return finish_command(&cmd
);
664 static int write_cruft_pack(const struct pack_objects_args
*args
,
665 const char *pack_prefix
,
666 struct string_list
*names
,
667 struct string_list
*existing_packs
,
668 struct string_list
*existing_kept_packs
)
670 struct child_process cmd
= CHILD_PROCESS_INIT
;
671 struct strbuf line
= STRBUF_INIT
;
672 struct string_list_item
*item
;
676 prepare_pack_objects(&cmd
, args
);
678 strvec_push(&cmd
.args
, "--cruft");
679 if (cruft_expiration
)
680 strvec_pushf(&cmd
.args
, "--cruft-expiration=%s",
683 strvec_push(&cmd
.args
, "--honor-pack-keep");
684 strvec_push(&cmd
.args
, "--non-empty");
685 strvec_push(&cmd
.args
, "--max-pack-size=0");
689 ret
= start_command(&cmd
);
694 * names has a confusing double use: it both provides the list
695 * of just-written new packs, and accepts the name of the cruft
696 * pack we are writing.
698 * By the time it is read here, it contains only the pack(s)
699 * that were just written, which is exactly the set of packs we
700 * want to consider kept.
702 in
= xfdopen(cmd
.in
, "w");
703 for_each_string_list_item(item
, names
)
704 fprintf(in
, "%s-%s.pack\n", pack_prefix
, item
->string
);
705 for_each_string_list_item(item
, existing_packs
)
706 fprintf(in
, "-%s.pack\n", item
->string
);
707 for_each_string_list_item(item
, existing_kept_packs
)
708 fprintf(in
, "%s.pack\n", item
->string
);
711 out
= xfdopen(cmd
.out
, "r");
712 while (strbuf_getline_lf(&line
, out
) != EOF
) {
713 if (line
.len
!= the_hash_algo
->hexsz
)
714 die(_("repack: Expecting full hex object ID lines only "
715 "from pack-objects."));
716 string_list_append(names
, line
.buf
);
720 strbuf_release(&line
);
722 return finish_command(&cmd
);
725 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
727 struct child_process cmd
= CHILD_PROCESS_INIT
;
728 struct string_list_item
*item
;
729 struct string_list names
= STRING_LIST_INIT_DUP
;
730 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
731 struct string_list existing_nonkept_packs
= STRING_LIST_INIT_DUP
;
732 struct string_list existing_kept_packs
= STRING_LIST_INIT_DUP
;
733 struct pack_geometry
*geometry
= NULL
;
734 struct strbuf line
= STRBUF_INIT
;
735 struct tempfile
*refs_snapshot
= NULL
;
740 /* variables to be filled by option parsing */
741 int delete_redundant
= 0;
742 const char *unpack_unreachable
= NULL
;
743 int keep_unreachable
= 0;
744 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
745 struct pack_objects_args po_args
= {NULL
};
746 struct pack_objects_args cruft_po_args
= {NULL
};
747 int geometric_factor
= 0;
750 struct option builtin_repack_options
[] = {
751 OPT_BIT('a', NULL
, &pack_everything
,
752 N_("pack everything in a single pack"), ALL_INTO_ONE
),
753 OPT_BIT('A', NULL
, &pack_everything
,
754 N_("same as -a, and turn unreachable objects loose"),
755 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
756 OPT_BIT(0, "cruft", &pack_everything
,
757 N_("same as -a, pack unreachable cruft objects separately"),
759 OPT_STRING(0, "cruft-expiration", &cruft_expiration
, N_("approxidate"),
760 N_("with -C, expire objects older than this")),
761 OPT_BOOL('d', NULL
, &delete_redundant
,
762 N_("remove redundant packs, and run git-prune-packed")),
763 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
764 N_("pass --no-reuse-delta to git-pack-objects")),
765 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
766 N_("pass --no-reuse-object to git-pack-objects")),
767 OPT_NEGBIT('n', NULL
, &run_update_server_info
,
768 N_("do not run git-update-server-info"), 1),
769 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
770 OPT_BOOL('l', "local", &po_args
.local
,
771 N_("pass --local to git-pack-objects")),
772 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
773 N_("write bitmap index")),
774 OPT_BOOL('i', "delta-islands", &use_delta_islands
,
775 N_("pass --delta-islands to git-pack-objects")),
776 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
777 N_("with -A, do not loosen objects older than this")),
778 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
779 N_("with -a, repack unreachable objects")),
780 OPT_STRING(0, "window", &po_args
.window
, N_("n"),
781 N_("size of the window used for delta compression")),
782 OPT_STRING(0, "window-memory", &po_args
.window_memory
, N_("bytes"),
783 N_("same as the above, but limit memory size instead of entries count")),
784 OPT_STRING(0, "depth", &po_args
.depth
, N_("n"),
785 N_("limits the maximum delta depth")),
786 OPT_STRING(0, "threads", &po_args
.threads
, N_("n"),
787 N_("limits the maximum number of threads")),
788 OPT_STRING(0, "max-pack-size", &po_args
.max_pack_size
, N_("bytes"),
789 N_("maximum size of each packfile")),
790 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
791 N_("repack objects in packs marked with .keep")),
792 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
793 N_("do not repack this pack")),
794 OPT_INTEGER('g', "geometric", &geometric_factor
,
795 N_("find a geometric progression with factor <N>")),
796 OPT_BOOL('m', "write-midx", &write_midx
,
797 N_("write a multi-pack index of the resulting packs")),
801 git_config(repack_config
, &cruft_po_args
);
803 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
804 git_repack_usage
, 0);
806 if (delete_redundant
&& repository_format_precious_objects
)
807 die(_("cannot delete packs in a precious-objects repo"));
809 if (keep_unreachable
&&
810 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
811 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
813 if (pack_everything
& PACK_CRUFT
) {
814 pack_everything
|= ALL_INTO_ONE
;
816 if (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
))
817 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A");
818 if (keep_unreachable
)
819 die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k");
822 if (write_bitmaps
< 0) {
824 (!(pack_everything
& ALL_INTO_ONE
) || !is_bare_repository()))
826 } else if (write_bitmaps
&&
827 git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0) &&
828 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0)) {
831 if (pack_kept_objects
< 0)
832 pack_kept_objects
= write_bitmaps
> 0 && !write_midx
;
834 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
) && !write_midx
)
835 die(_(incremental_bitmap_conflict_error
));
837 if (write_midx
&& write_bitmaps
) {
838 struct strbuf path
= STRBUF_INIT
;
840 strbuf_addf(&path
, "%s/%s_XXXXXX", get_object_directory(),
843 refs_snapshot
= xmks_tempfile(path
.buf
);
844 midx_snapshot_refs(refs_snapshot
);
846 strbuf_release(&path
);
849 packdir
= mkpathdup("%s/pack", get_object_directory());
850 packtmp_name
= xstrfmt(".tmp-%d-pack", (int)getpid());
851 packtmp
= mkpathdup("%s/%s", packdir
, packtmp_name
);
853 collect_pack_filenames(&existing_nonkept_packs
, &existing_kept_packs
,
856 if (geometric_factor
) {
858 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
859 init_pack_geometry(&geometry
, &existing_kept_packs
);
860 split_pack_geometry(geometry
, geometric_factor
);
863 sigchain_push_common(remove_pack_on_signal
);
865 prepare_pack_objects(&cmd
, &po_args
);
867 show_progress
= !po_args
.quiet
&& isatty(2);
869 strvec_push(&cmd
.args
, "--keep-true-parents");
870 if (!pack_kept_objects
)
871 strvec_push(&cmd
.args
, "--honor-pack-keep");
872 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
873 strvec_pushf(&cmd
.args
, "--keep-pack=%s",
874 keep_pack_list
.items
[i
].string
);
875 strvec_push(&cmd
.args
, "--non-empty");
878 * We need to grab all reachable objects, including those that
879 * are reachable from reflogs and the index.
881 * When repacking into a geometric progression of packs,
882 * however, we ask 'git pack-objects --stdin-packs', and it is
883 * not about packing objects based on reachability but about
884 * repacking all the objects in specified packs and loose ones
885 * (indeed, --stdin-packs is incompatible with these options).
887 strvec_push(&cmd
.args
, "--all");
888 strvec_push(&cmd
.args
, "--reflog");
889 strvec_push(&cmd
.args
, "--indexed-objects");
891 if (has_promisor_remote())
892 strvec_push(&cmd
.args
, "--exclude-promisor-objects");
894 if (write_bitmaps
> 0)
895 strvec_push(&cmd
.args
, "--write-bitmap-index");
896 else if (write_bitmaps
< 0)
897 strvec_push(&cmd
.args
, "--write-bitmap-index-quiet");
899 if (use_delta_islands
)
900 strvec_push(&cmd
.args
, "--delta-islands");
902 if (pack_everything
& ALL_INTO_ONE
) {
903 repack_promisor_objects(&po_args
, &names
);
905 if (existing_nonkept_packs
.nr
&& delete_redundant
&&
906 !(pack_everything
& PACK_CRUFT
)) {
907 for_each_string_list_item(item
, &names
) {
908 strvec_pushf(&cmd
.args
, "--keep-pack=%s-%s.pack",
909 packtmp_name
, item
->string
);
911 if (unpack_unreachable
) {
912 strvec_pushf(&cmd
.args
,
913 "--unpack-unreachable=%s",
915 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
916 strvec_push(&cmd
.args
,
917 "--unpack-unreachable");
918 } else if (keep_unreachable
) {
919 strvec_push(&cmd
.args
, "--keep-unreachable");
920 strvec_push(&cmd
.args
, "--pack-loose-unreachable");
923 } else if (geometry
) {
924 strvec_push(&cmd
.args
, "--stdin-packs");
925 strvec_push(&cmd
.args
, "--unpacked");
927 strvec_push(&cmd
.args
, "--unpacked");
928 strvec_push(&cmd
.args
, "--incremental");
936 ret
= start_command(&cmd
);
941 FILE *in
= xfdopen(cmd
.in
, "w");
943 * The resulting pack should contain all objects in packs that
944 * are going to be rolled up, but exclude objects in packs which
945 * are being left alone.
947 for (i
= 0; i
< geometry
->split
; i
++)
948 fprintf(in
, "%s\n", pack_basename(geometry
->pack
[i
]));
949 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++)
950 fprintf(in
, "^%s\n", pack_basename(geometry
->pack
[i
]));
954 out
= xfdopen(cmd
.out
, "r");
955 while (strbuf_getline_lf(&line
, out
) != EOF
) {
956 if (line
.len
!= the_hash_algo
->hexsz
)
957 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
958 string_list_append(&names
, line
.buf
);
961 ret
= finish_command(&cmd
);
965 if (!names
.nr
&& !po_args
.quiet
)
966 printf_ln(_("Nothing new to pack."));
968 if (pack_everything
& PACK_CRUFT
) {
969 const char *pack_prefix
;
970 if (!skip_prefix(packtmp
, packdir
, &pack_prefix
))
971 die(_("pack prefix %s does not begin with objdir %s"),
973 if (*pack_prefix
== '/')
976 if (!cruft_po_args
.window
)
977 cruft_po_args
.window
= po_args
.window
;
978 if (!cruft_po_args
.window_memory
)
979 cruft_po_args
.window_memory
= po_args
.window_memory
;
980 if (!cruft_po_args
.depth
)
981 cruft_po_args
.depth
= po_args
.depth
;
982 if (!cruft_po_args
.threads
)
983 cruft_po_args
.threads
= po_args
.threads
;
985 cruft_po_args
.local
= po_args
.local
;
986 cruft_po_args
.quiet
= po_args
.quiet
;
988 ret
= write_cruft_pack(&cruft_po_args
, pack_prefix
, &names
,
989 &existing_nonkept_packs
,
990 &existing_kept_packs
);
995 string_list_sort(&names
);
997 for_each_string_list_item(item
, &names
) {
998 item
->util
= (void *)(uintptr_t)populate_pack_exts(item
->string
);
1001 close_object_store(the_repository
->objects
);
1004 * Ok we have prepared all new packfiles.
1006 for_each_string_list_item(item
, &names
) {
1007 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
1008 char *fname
, *fname_old
;
1010 fname
= mkpathdup("%s/pack-%s%s",
1011 packdir
, item
->string
, exts
[ext
].name
);
1012 fname_old
= mkpathdup("%s-%s%s",
1013 packtmp
, item
->string
, exts
[ext
].name
);
1015 if (((uintptr_t)item
->util
) & ((uintptr_t)1 << ext
)) {
1016 struct stat statbuffer
;
1017 if (!stat(fname_old
, &statbuffer
)) {
1018 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
1019 chmod(fname_old
, statbuffer
.st_mode
);
1022 if (rename(fname_old
, fname
))
1023 die_errno(_("renaming '%s' failed"), fname_old
);
1024 } else if (!exts
[ext
].optional
)
1025 die(_("missing required file: %s"), fname_old
);
1026 else if (unlink(fname
) < 0 && errno
!= ENOENT
)
1027 die_errno(_("could not unlink: %s"), fname
);
1033 /* End of pack replacement. */
1035 if (delete_redundant
&& pack_everything
& ALL_INTO_ONE
) {
1036 const int hexsz
= the_hash_algo
->hexsz
;
1037 for_each_string_list_item(item
, &existing_nonkept_packs
) {
1039 size_t len
= strlen(item
->string
);
1042 sha1
= item
->string
+ len
- hexsz
;
1044 * Mark this pack for deletion, which ensures that this
1045 * pack won't be included in a MIDX (if `--write-midx`
1046 * was given) and that we will actually delete this pack
1047 * (if `-d` was given).
1049 if (!string_list_has_string(&names
, sha1
))
1050 item
->util
= (void*)(uintptr_t)((size_t)item
->util
| DELETE_PACK
);
1055 struct string_list include
= STRING_LIST_INIT_NODUP
;
1056 midx_included_packs(&include
, &existing_nonkept_packs
,
1057 &existing_kept_packs
, &names
, geometry
);
1059 ret
= write_midx_included_packs(&include
, geometry
,
1060 refs_snapshot
? get_tempfile_path(refs_snapshot
) : NULL
,
1061 show_progress
, write_bitmaps
> 0);
1063 string_list_clear(&include
, 0);
1069 reprepare_packed_git(the_repository
);
1071 if (delete_redundant
) {
1073 for_each_string_list_item(item
, &existing_nonkept_packs
) {
1074 if (!((uintptr_t)item
->util
& DELETE_PACK
))
1076 remove_redundant_pack(packdir
, item
->string
);
1080 struct strbuf buf
= STRBUF_INIT
;
1083 for (i
= 0; i
< geometry
->split
; i
++) {
1084 struct packed_git
*p
= geometry
->pack
[i
];
1085 if (string_list_has_string(&names
,
1086 hash_to_hex(p
->hash
)))
1090 strbuf_addstr(&buf
, pack_basename(p
));
1091 strbuf_strip_suffix(&buf
, ".pack");
1093 remove_redundant_pack(packdir
, buf
.buf
);
1095 strbuf_release(&buf
);
1098 opts
|= PRUNE_PACKED_VERBOSE
;
1099 prune_packed_objects(opts
);
1101 if (!keep_unreachable
&&
1102 (!(pack_everything
& LOOSEN_UNREACHABLE
) ||
1103 unpack_unreachable
) &&
1104 is_repository_shallow(the_repository
))
1105 prune_shallow(PRUNE_QUICK
);
1108 if (run_update_server_info
)
1109 update_server_info(0);
1110 remove_temporary_files();
1112 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0)) {
1114 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0))
1115 flags
|= MIDX_WRITE_BITMAP
| MIDX_WRITE_REV_INDEX
;
1116 write_midx_file(get_object_directory(), NULL
, NULL
, flags
);
1119 string_list_clear(&names
, 0);
1120 string_list_clear(&rollback
, 0);
1121 string_list_clear(&existing_nonkept_packs
, 0);
1122 string_list_clear(&existing_kept_packs
, 0);
1123 clear_pack_geometry(geometry
);
1124 strbuf_release(&line
);