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 static int delta_base_offset
= 1;
22 static int pack_kept_objects
= -1;
23 static int write_bitmaps
= -1;
24 static int use_delta_islands
;
25 static int run_update_server_info
= 1;
26 static char *packdir
, *packtmp_name
, *packtmp
;
28 static const char *const git_repack_usage
[] = {
29 N_("git repack [<options>]"),
33 static const char incremental_bitmap_conflict_error
[] = N_(
34 "Incremental repacks are incompatible with bitmap indexes. Use\n"
35 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
39 static int repack_config(const char *var
, const char *value
, void *cb
)
41 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
42 delta_base_offset
= git_config_bool(var
, value
);
45 if (!strcmp(var
, "repack.packkeptobjects")) {
46 pack_kept_objects
= git_config_bool(var
, value
);
49 if (!strcmp(var
, "repack.writebitmaps") ||
50 !strcmp(var
, "pack.writebitmaps")) {
51 write_bitmaps
= git_config_bool(var
, value
);
54 if (!strcmp(var
, "repack.usedeltaislands")) {
55 use_delta_islands
= git_config_bool(var
, value
);
58 if (strcmp(var
, "repack.updateserverinfo") == 0) {
59 run_update_server_info
= git_config_bool(var
, value
);
62 return git_default_config(var
, value
, cb
);
66 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
68 static void remove_temporary_files(void)
70 struct strbuf buf
= STRBUF_INIT
;
71 size_t dirlen
, prefixlen
;
75 dir
= opendir(packdir
);
79 /* Point at the slash at the end of ".../objects/pack/" */
80 dirlen
= strlen(packdir
) + 1;
81 strbuf_addstr(&buf
, packtmp
);
82 /* Hold the length of ".tmp-%d-pack-" */
83 prefixlen
= buf
.len
- dirlen
;
85 while ((e
= readdir(dir
))) {
86 if (strncmp(e
->d_name
, buf
.buf
+ dirlen
, prefixlen
))
88 strbuf_setlen(&buf
, dirlen
);
89 strbuf_addstr(&buf
, e
->d_name
);
96 static void remove_pack_on_signal(int signo
)
98 remove_temporary_files();
104 * Adds all packs hex strings to either fname_nonkept_list or
105 * fname_kept_list based on whether each pack has a corresponding
106 * .keep file or not. Packs without a .keep file are not to be kept
107 * if we are going to pack everything into one file.
109 static void collect_pack_filenames(struct string_list
*fname_nonkept_list
,
110 struct string_list
*fname_kept_list
,
111 const struct string_list
*extra_keep
)
117 if (!(dir
= opendir(packdir
)))
120 while ((e
= readdir(dir
)) != NULL
) {
124 if (!strip_suffix(e
->d_name
, ".pack", &len
))
127 for (i
= 0; i
< extra_keep
->nr
; i
++)
128 if (!fspathcmp(e
->d_name
, extra_keep
->items
[i
].string
))
131 fname
= xmemdupz(e
->d_name
, len
);
133 if ((extra_keep
->nr
> 0 && i
< extra_keep
->nr
) ||
134 (file_exists(mkpath("%s/%s.keep", packdir
, fname
))))
135 string_list_append_nodup(fname_kept_list
, fname
);
137 string_list_append_nodup(fname_nonkept_list
, fname
);
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 struct pack_objects_args
{
156 const char *window_memory
;
159 const char *max_pack_size
;
166 static void prepare_pack_objects(struct child_process
*cmd
,
167 const struct pack_objects_args
*args
)
169 strvec_push(&cmd
->args
, "pack-objects");
171 strvec_pushf(&cmd
->args
, "--window=%s", args
->window
);
172 if (args
->window_memory
)
173 strvec_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
175 strvec_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
177 strvec_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
178 if (args
->max_pack_size
)
179 strvec_pushf(&cmd
->args
, "--max-pack-size=%s", args
->max_pack_size
);
180 if (args
->no_reuse_delta
)
181 strvec_pushf(&cmd
->args
, "--no-reuse-delta");
182 if (args
->no_reuse_object
)
183 strvec_pushf(&cmd
->args
, "--no-reuse-object");
185 strvec_push(&cmd
->args
, "--local");
187 strvec_push(&cmd
->args
, "--quiet");
188 if (delta_base_offset
)
189 strvec_push(&cmd
->args
, "--delta-base-offset");
190 strvec_push(&cmd
->args
, packtmp
);
196 * Write oid to the given struct child_process's stdin, starting it first if
199 static int write_oid(const struct object_id
*oid
, struct packed_git
*pack
,
200 uint32_t pos
, void *data
)
202 struct child_process
*cmd
= data
;
205 if (start_command(cmd
))
206 die(_("could not start pack-objects to repack promisor objects"));
209 xwrite(cmd
->in
, oid_to_hex(oid
), the_hash_algo
->hexsz
);
210 xwrite(cmd
->in
, "\n", 1);
225 static unsigned populate_pack_exts(char *name
)
228 struct strbuf path
= STRBUF_INIT
;
232 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
234 strbuf_addf(&path
, "%s-%s%s", packtmp
, name
, exts
[i
].name
);
236 if (stat(path
.buf
, &statbuf
))
242 strbuf_release(&path
);
246 static void repack_promisor_objects(const struct pack_objects_args
*args
,
247 struct string_list
*names
)
249 struct child_process cmd
= CHILD_PROCESS_INIT
;
251 struct strbuf line
= STRBUF_INIT
;
253 prepare_pack_objects(&cmd
, args
);
257 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
258 * hints may result in suboptimal deltas in the resulting pack. See if
259 * the OIDs can be sent with fake paths such that pack-objects can use a
260 * {type -> existing pack order} ordering when computing deltas instead
261 * of a {type -> size} ordering, which may produce better deltas.
263 for_each_packed_object(write_oid
, &cmd
,
264 FOR_EACH_OBJECT_PROMISOR_ONLY
);
267 /* No packed objects; cmd was never started */
268 child_process_clear(&cmd
);
274 out
= xfdopen(cmd
.out
, "r");
275 while (strbuf_getline_lf(&line
, out
) != EOF
) {
276 struct string_list_item
*item
;
279 if (line
.len
!= the_hash_algo
->hexsz
)
280 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
281 item
= string_list_append(names
, line
.buf
);
284 * pack-objects creates the .pack and .idx files, but not the
285 * .promisor file. Create the .promisor file, which is empty.
287 * NEEDSWORK: fetch-pack sometimes generates non-empty
288 * .promisor files containing the ref names and associated
289 * hashes at the point of generation of the corresponding
290 * packfile, but this would not preserve their contents. Maybe
291 * concatenate the contents of all .promisor files instead of
292 * just creating a new empty file.
294 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
296 write_promisor_file(promisor_name
, NULL
, 0);
298 item
->util
= (void *)(uintptr_t)populate_pack_exts(item
->string
);
303 if (finish_command(&cmd
))
304 die(_("could not finish pack-objects to repack promisor objects"));
307 #define ALL_INTO_ONE 1
308 #define LOOSEN_UNREACHABLE 2
310 struct pack_geometry
{
311 struct packed_git
**pack
;
312 uint32_t pack_nr
, pack_alloc
;
316 static uint32_t geometry_pack_weight(struct packed_git
*p
)
318 if (open_pack_index(p
))
319 die(_("cannot open index for %s"), p
->pack_name
);
320 return p
->num_objects
;
323 static int geometry_cmp(const void *va
, const void *vb
)
325 uint32_t aw
= geometry_pack_weight(*(struct packed_git
**)va
),
326 bw
= geometry_pack_weight(*(struct packed_git
**)vb
);
335 static void init_pack_geometry(struct pack_geometry
**geometry_p
)
337 struct packed_git
*p
;
338 struct pack_geometry
*geometry
;
340 *geometry_p
= xcalloc(1, sizeof(struct pack_geometry
));
341 geometry
= *geometry_p
;
343 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
344 if (!pack_kept_objects
&& p
->pack_keep
)
347 ALLOC_GROW(geometry
->pack
,
348 geometry
->pack_nr
+ 1,
349 geometry
->pack_alloc
);
351 geometry
->pack
[geometry
->pack_nr
] = p
;
355 QSORT(geometry
->pack
, geometry
->pack_nr
, geometry_cmp
);
358 static void split_pack_geometry(struct pack_geometry
*geometry
, int factor
)
362 off_t total_size
= 0;
364 if (!geometry
->pack_nr
) {
365 geometry
->split
= geometry
->pack_nr
;
370 * First, count the number of packs (in descending order of size) which
371 * already form a geometric progression.
373 for (i
= geometry
->pack_nr
- 1; i
> 0; i
--) {
374 struct packed_git
*ours
= geometry
->pack
[i
];
375 struct packed_git
*prev
= geometry
->pack
[i
- 1];
377 if (unsigned_mult_overflows(factor
, geometry_pack_weight(prev
)))
378 die(_("pack %s too large to consider in geometric "
382 if (geometry_pack_weight(ours
) < factor
* geometry_pack_weight(prev
))
390 * Move the split one to the right, since the top element in the
391 * last-compared pair can't be in the progression. Only do this
392 * when we split in the middle of the array (otherwise if we got
393 * to the end, then the split is in the right place).
399 * Then, anything to the left of 'split' must be in a new pack. But,
400 * creating that new pack may cause packs in the heavy half to no longer
401 * form a geometric progression.
403 * Compute an expected size of the new pack, and then determine how many
404 * packs in the heavy half need to be joined into it (if any) to restore
405 * the geometric progression.
407 for (i
= 0; i
< split
; i
++) {
408 struct packed_git
*p
= geometry
->pack
[i
];
410 if (unsigned_add_overflows(total_size
, geometry_pack_weight(p
)))
411 die(_("pack %s too large to roll up"), p
->pack_name
);
412 total_size
+= geometry_pack_weight(p
);
414 for (i
= split
; i
< geometry
->pack_nr
; i
++) {
415 struct packed_git
*ours
= geometry
->pack
[i
];
417 if (unsigned_mult_overflows(factor
, total_size
))
418 die(_("pack %s too large to roll up"), ours
->pack_name
);
420 if (geometry_pack_weight(ours
) < factor
* total_size
) {
421 if (unsigned_add_overflows(total_size
,
422 geometry_pack_weight(ours
)))
423 die(_("pack %s too large to roll up"),
427 total_size
+= geometry_pack_weight(ours
);
432 geometry
->split
= split
;
435 static struct packed_git
*get_largest_active_pack(struct pack_geometry
*geometry
)
439 * No geometry means either an all-into-one repack (in which
440 * case there is only one pack left and it is the largest) or an
443 * If repacking incrementally, then we could check the size of
444 * all packs to determine which should be preferred, but leave
449 if (geometry
->split
== geometry
->pack_nr
)
451 return geometry
->pack
[geometry
->pack_nr
- 1];
454 static void clear_pack_geometry(struct pack_geometry
*geometry
)
459 free(geometry
->pack
);
460 geometry
->pack_nr
= 0;
461 geometry
->pack_alloc
= 0;
465 struct midx_snapshot_ref_data
{
471 static int midx_snapshot_ref_one(const char *refname
,
472 const struct object_id
*oid
,
473 int flag
, void *_data
)
475 struct midx_snapshot_ref_data
*data
= _data
;
476 struct object_id peeled
;
478 if (!peel_iterated_oid(oid
, &peeled
))
481 if (oidset_insert(&data
->seen
, oid
))
482 return 0; /* already seen */
484 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
487 fprintf(data
->f
->fp
, "%s%s\n", data
->preferred
? "+" : "",
493 static void midx_snapshot_refs(struct tempfile
*f
)
495 struct midx_snapshot_ref_data data
;
496 const struct string_list
*preferred
= bitmap_preferred_tips(the_repository
);
500 oidset_init(&data
.seen
, 0);
502 if (!fdopen_tempfile(f
, "w"))
503 die(_("could not open tempfile %s for writing"),
504 get_tempfile_path(f
));
507 struct string_list_item
*item
;
510 for_each_string_list_item(item
, preferred
)
511 for_each_ref_in(item
->string
, midx_snapshot_ref_one
, &data
);
515 for_each_ref(midx_snapshot_ref_one
, &data
);
517 if (close_tempfile_gently(f
)) {
518 int save_errno
= errno
;
521 die_errno(_("could not close refs snapshot tempfile"));
524 oidset_clear(&data
.seen
);
527 static void midx_included_packs(struct string_list
*include
,
528 struct string_list
*existing_nonkept_packs
,
529 struct string_list
*existing_kept_packs
,
530 struct string_list
*names
,
531 struct pack_geometry
*geometry
)
533 struct string_list_item
*item
;
535 for_each_string_list_item(item
, existing_kept_packs
)
536 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
537 for_each_string_list_item(item
, names
)
538 string_list_insert(include
, xstrfmt("pack-%s.idx", item
->string
));
540 struct strbuf buf
= STRBUF_INIT
;
542 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++) {
543 struct packed_git
*p
= geometry
->pack
[i
];
545 strbuf_addstr(&buf
, pack_basename(p
));
546 strbuf_strip_suffix(&buf
, ".pack");
547 strbuf_addstr(&buf
, ".idx");
549 string_list_insert(include
, strbuf_detach(&buf
, NULL
));
552 for_each_string_list_item(item
, existing_nonkept_packs
) {
555 string_list_insert(include
, xstrfmt("%s.idx", item
->string
));
560 static int write_midx_included_packs(struct string_list
*include
,
561 struct pack_geometry
*geometry
,
562 const char *refs_snapshot
,
563 int show_progress
, int write_bitmaps
)
565 struct child_process cmd
= CHILD_PROCESS_INIT
;
566 struct string_list_item
*item
;
567 struct packed_git
*largest
= get_largest_active_pack(geometry
);
577 strvec_push(&cmd
.args
, "multi-pack-index");
578 strvec_pushl(&cmd
.args
, "write", "--stdin-packs", NULL
);
581 strvec_push(&cmd
.args
, "--progress");
583 strvec_push(&cmd
.args
, "--no-progress");
586 strvec_push(&cmd
.args
, "--bitmap");
589 strvec_pushf(&cmd
.args
, "--preferred-pack=%s",
590 pack_basename(largest
));
593 strvec_pushf(&cmd
.args
, "--refs-snapshot=%s", refs_snapshot
);
595 ret
= start_command(&cmd
);
599 in
= xfdopen(cmd
.in
, "w");
600 for_each_string_list_item(item
, include
)
601 fprintf(in
, "%s\n", item
->string
);
604 return finish_command(&cmd
);
607 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
609 struct child_process cmd
= CHILD_PROCESS_INIT
;
610 struct string_list_item
*item
;
611 struct string_list names
= STRING_LIST_INIT_DUP
;
612 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
613 struct string_list existing_nonkept_packs
= STRING_LIST_INIT_DUP
;
614 struct string_list existing_kept_packs
= STRING_LIST_INIT_DUP
;
615 struct pack_geometry
*geometry
= NULL
;
616 struct strbuf line
= STRBUF_INIT
;
617 struct tempfile
*refs_snapshot
= NULL
;
622 /* variables to be filled by option parsing */
623 int pack_everything
= 0;
624 int delete_redundant
= 0;
625 const char *unpack_unreachable
= NULL
;
626 int keep_unreachable
= 0;
627 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
628 struct pack_objects_args po_args
= {NULL
};
629 int geometric_factor
= 0;
632 struct option builtin_repack_options
[] = {
633 OPT_BIT('a', NULL
, &pack_everything
,
634 N_("pack everything in a single pack"), ALL_INTO_ONE
),
635 OPT_BIT('A', NULL
, &pack_everything
,
636 N_("same as -a, and turn unreachable objects loose"),
637 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
638 OPT_BOOL('d', NULL
, &delete_redundant
,
639 N_("remove redundant packs, and run git-prune-packed")),
640 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
641 N_("pass --no-reuse-delta to git-pack-objects")),
642 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
643 N_("pass --no-reuse-object to git-pack-objects")),
644 OPT_NEGBIT('n', NULL
, &run_update_server_info
,
645 N_("do not run git-update-server-info"), 1),
646 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
647 OPT_BOOL('l', "local", &po_args
.local
,
648 N_("pass --local to git-pack-objects")),
649 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
650 N_("write bitmap index")),
651 OPT_BOOL('i', "delta-islands", &use_delta_islands
,
652 N_("pass --delta-islands to git-pack-objects")),
653 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
654 N_("with -A, do not loosen objects older than this")),
655 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
656 N_("with -a, repack unreachable objects")),
657 OPT_STRING(0, "window", &po_args
.window
, N_("n"),
658 N_("size of the window used for delta compression")),
659 OPT_STRING(0, "window-memory", &po_args
.window_memory
, N_("bytes"),
660 N_("same as the above, but limit memory size instead of entries count")),
661 OPT_STRING(0, "depth", &po_args
.depth
, N_("n"),
662 N_("limits the maximum delta depth")),
663 OPT_STRING(0, "threads", &po_args
.threads
, N_("n"),
664 N_("limits the maximum number of threads")),
665 OPT_STRING(0, "max-pack-size", &po_args
.max_pack_size
, N_("bytes"),
666 N_("maximum size of each packfile")),
667 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
668 N_("repack objects in packs marked with .keep")),
669 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
670 N_("do not repack this pack")),
671 OPT_INTEGER('g', "geometric", &geometric_factor
,
672 N_("find a geometric progression with factor <N>")),
673 OPT_BOOL('m', "write-midx", &write_midx
,
674 N_("write a multi-pack index of the resulting packs")),
678 git_config(repack_config
, NULL
);
680 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
681 git_repack_usage
, 0);
683 if (delete_redundant
&& repository_format_precious_objects
)
684 die(_("cannot delete packs in a precious-objects repo"));
686 if (keep_unreachable
&&
687 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
688 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
690 if (write_bitmaps
< 0) {
692 (!(pack_everything
& ALL_INTO_ONE
) || !is_bare_repository()))
694 } else if (write_bitmaps
&&
695 git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0) &&
696 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0)) {
699 if (pack_kept_objects
< 0)
700 pack_kept_objects
= write_bitmaps
> 0 && !write_midx
;
702 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
) && !write_midx
)
703 die(_(incremental_bitmap_conflict_error
));
705 if (write_midx
&& write_bitmaps
) {
706 struct strbuf path
= STRBUF_INIT
;
708 strbuf_addf(&path
, "%s/%s_XXXXXX", get_object_directory(),
711 refs_snapshot
= xmks_tempfile(path
.buf
);
712 midx_snapshot_refs(refs_snapshot
);
714 strbuf_release(&path
);
717 if (geometric_factor
) {
719 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
720 init_pack_geometry(&geometry
);
721 split_pack_geometry(geometry
, geometric_factor
);
724 packdir
= mkpathdup("%s/pack", get_object_directory());
725 packtmp_name
= xstrfmt(".tmp-%d-pack", (int)getpid());
726 packtmp
= mkpathdup("%s/%s", packdir
, packtmp_name
);
728 sigchain_push_common(remove_pack_on_signal
);
730 prepare_pack_objects(&cmd
, &po_args
);
732 show_progress
= !po_args
.quiet
&& isatty(2);
734 strvec_push(&cmd
.args
, "--keep-true-parents");
735 if (!pack_kept_objects
)
736 strvec_push(&cmd
.args
, "--honor-pack-keep");
737 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
738 strvec_pushf(&cmd
.args
, "--keep-pack=%s",
739 keep_pack_list
.items
[i
].string
);
740 strvec_push(&cmd
.args
, "--non-empty");
743 * We need to grab all reachable objects, including those that
744 * are reachable from reflogs and the index.
746 * When repacking into a geometric progression of packs,
747 * however, we ask 'git pack-objects --stdin-packs', and it is
748 * not about packing objects based on reachability but about
749 * repacking all the objects in specified packs and loose ones
750 * (indeed, --stdin-packs is incompatible with these options).
752 strvec_push(&cmd
.args
, "--all");
753 strvec_push(&cmd
.args
, "--reflog");
754 strvec_push(&cmd
.args
, "--indexed-objects");
756 if (has_promisor_remote())
757 strvec_push(&cmd
.args
, "--exclude-promisor-objects");
759 if (write_bitmaps
> 0)
760 strvec_push(&cmd
.args
, "--write-bitmap-index");
761 else if (write_bitmaps
< 0)
762 strvec_push(&cmd
.args
, "--write-bitmap-index-quiet");
764 if (use_delta_islands
)
765 strvec_push(&cmd
.args
, "--delta-islands");
767 collect_pack_filenames(&existing_nonkept_packs
, &existing_kept_packs
,
770 if (pack_everything
& ALL_INTO_ONE
) {
771 repack_promisor_objects(&po_args
, &names
);
773 if (existing_nonkept_packs
.nr
&& delete_redundant
) {
774 for_each_string_list_item(item
, &names
) {
775 strvec_pushf(&cmd
.args
, "--keep-pack=%s-%s.pack",
776 packtmp_name
, item
->string
);
778 if (unpack_unreachable
) {
779 strvec_pushf(&cmd
.args
,
780 "--unpack-unreachable=%s",
782 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
783 strvec_push(&cmd
.args
,
784 "--unpack-unreachable");
785 } else if (keep_unreachable
) {
786 strvec_push(&cmd
.args
, "--keep-unreachable");
787 strvec_push(&cmd
.args
, "--pack-loose-unreachable");
790 } else if (geometry
) {
791 strvec_push(&cmd
.args
, "--stdin-packs");
792 strvec_push(&cmd
.args
, "--unpacked");
794 strvec_push(&cmd
.args
, "--unpacked");
795 strvec_push(&cmd
.args
, "--incremental");
803 ret
= start_command(&cmd
);
808 FILE *in
= xfdopen(cmd
.in
, "w");
810 * The resulting pack should contain all objects in packs that
811 * are going to be rolled up, but exclude objects in packs which
812 * are being left alone.
814 for (i
= 0; i
< geometry
->split
; i
++)
815 fprintf(in
, "%s\n", pack_basename(geometry
->pack
[i
]));
816 for (i
= geometry
->split
; i
< geometry
->pack_nr
; i
++)
817 fprintf(in
, "^%s\n", pack_basename(geometry
->pack
[i
]));
821 out
= xfdopen(cmd
.out
, "r");
822 while (strbuf_getline_lf(&line
, out
) != EOF
) {
823 if (line
.len
!= the_hash_algo
->hexsz
)
824 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
825 string_list_append(&names
, line
.buf
);
828 ret
= finish_command(&cmd
);
832 if (!names
.nr
&& !po_args
.quiet
)
833 printf_ln(_("Nothing new to pack."));
835 for_each_string_list_item(item
, &names
) {
836 item
->util
= (void *)(uintptr_t)populate_pack_exts(item
->string
);
839 close_object_store(the_repository
->objects
);
842 * Ok we have prepared all new packfiles.
844 for_each_string_list_item(item
, &names
) {
845 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
846 char *fname
, *fname_old
;
848 fname
= mkpathdup("%s/pack-%s%s",
849 packdir
, item
->string
, exts
[ext
].name
);
850 fname_old
= mkpathdup("%s-%s%s",
851 packtmp
, item
->string
, exts
[ext
].name
);
853 if (((uintptr_t)item
->util
) & ((uintptr_t)1 << ext
)) {
854 struct stat statbuffer
;
855 if (!stat(fname_old
, &statbuffer
)) {
856 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
857 chmod(fname_old
, statbuffer
.st_mode
);
860 if (rename(fname_old
, fname
))
861 die_errno(_("renaming '%s' failed"), fname_old
);
862 } else if (!exts
[ext
].optional
)
863 die(_("missing required file: %s"), fname_old
);
864 else if (unlink(fname
) < 0 && errno
!= ENOENT
)
865 die_errno(_("could not unlink: %s"), fname
);
871 /* End of pack replacement. */
873 if (delete_redundant
&& pack_everything
& ALL_INTO_ONE
) {
874 const int hexsz
= the_hash_algo
->hexsz
;
875 string_list_sort(&names
);
876 for_each_string_list_item(item
, &existing_nonkept_packs
) {
878 size_t len
= strlen(item
->string
);
881 sha1
= item
->string
+ len
- hexsz
;
883 * Mark this pack for deletion, which ensures that this
884 * pack won't be included in a MIDX (if `--write-midx`
885 * was given) and that we will actually delete this pack
886 * (if `-d` was given).
888 item
->util
= (void*)(intptr_t)!string_list_has_string(&names
, sha1
);
893 struct string_list include
= STRING_LIST_INIT_NODUP
;
894 midx_included_packs(&include
, &existing_nonkept_packs
,
895 &existing_kept_packs
, &names
, geometry
);
897 ret
= write_midx_included_packs(&include
, geometry
,
898 refs_snapshot
? get_tempfile_path(refs_snapshot
) : NULL
,
899 show_progress
, write_bitmaps
> 0);
901 string_list_clear(&include
, 0);
907 reprepare_packed_git(the_repository
);
909 if (delete_redundant
) {
911 for_each_string_list_item(item
, &existing_nonkept_packs
) {
914 remove_redundant_pack(packdir
, item
->string
);
918 struct strbuf buf
= STRBUF_INIT
;
921 for (i
= 0; i
< geometry
->split
; i
++) {
922 struct packed_git
*p
= geometry
->pack
[i
];
923 if (string_list_has_string(&names
,
924 hash_to_hex(p
->hash
)))
928 strbuf_addstr(&buf
, pack_basename(p
));
929 strbuf_strip_suffix(&buf
, ".pack");
931 remove_redundant_pack(packdir
, buf
.buf
);
933 strbuf_release(&buf
);
936 opts
|= PRUNE_PACKED_VERBOSE
;
937 prune_packed_objects(opts
);
939 if (!keep_unreachable
&&
940 (!(pack_everything
& LOOSEN_UNREACHABLE
) ||
941 unpack_unreachable
) &&
942 is_repository_shallow(the_repository
))
943 prune_shallow(PRUNE_QUICK
);
946 if (run_update_server_info
)
947 update_server_info(0);
948 remove_temporary_files();
950 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0)) {
952 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP
, 0))
953 flags
|= MIDX_WRITE_BITMAP
| MIDX_WRITE_REV_INDEX
;
954 write_midx_file(get_object_directory(), NULL
, NULL
, flags
);
957 string_list_clear(&names
, 0);
958 string_list_clear(&rollback
, 0);
959 string_list_clear(&existing_nonkept_packs
, 0);
960 string_list_clear(&existing_kept_packs
, 0);
961 clear_pack_geometry(geometry
);
962 strbuf_release(&line
);