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"
19 static int delta_base_offset
= 1;
20 static int pack_kept_objects
= -1;
21 static int write_bitmaps
= -1;
22 static int use_delta_islands
;
23 static char *packdir
, *packtmp
;
25 static const char *const git_repack_usage
[] = {
26 N_("git repack [<options>]"),
30 static const char incremental_bitmap_conflict_error
[] = N_(
31 "Incremental repacks are incompatible with bitmap indexes. Use\n"
32 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
36 static int repack_config(const char *var
, const char *value
, void *cb
)
38 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
39 delta_base_offset
= git_config_bool(var
, value
);
42 if (!strcmp(var
, "repack.packkeptobjects")) {
43 pack_kept_objects
= git_config_bool(var
, value
);
46 if (!strcmp(var
, "repack.writebitmaps") ||
47 !strcmp(var
, "pack.writebitmaps")) {
48 write_bitmaps
= git_config_bool(var
, value
);
51 if (!strcmp(var
, "repack.usedeltaislands")) {
52 use_delta_islands
= git_config_bool(var
, value
);
55 return git_default_config(var
, value
, cb
);
59 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
61 static void remove_temporary_files(void)
63 struct strbuf buf
= STRBUF_INIT
;
64 size_t dirlen
, prefixlen
;
68 dir
= opendir(packdir
);
72 /* Point at the slash at the end of ".../objects/pack/" */
73 dirlen
= strlen(packdir
) + 1;
74 strbuf_addstr(&buf
, packtmp
);
75 /* Hold the length of ".tmp-%d-pack-" */
76 prefixlen
= buf
.len
- dirlen
;
78 while ((e
= readdir(dir
))) {
79 if (strncmp(e
->d_name
, buf
.buf
+ dirlen
, prefixlen
))
81 strbuf_setlen(&buf
, dirlen
);
82 strbuf_addstr(&buf
, e
->d_name
);
89 static void remove_pack_on_signal(int signo
)
91 remove_temporary_files();
97 * Adds all packs hex strings to the fname list, which do not
98 * have a corresponding .keep file. These packs are not to
99 * be kept if we are going to pack everything into one file.
101 static void get_non_kept_pack_filenames(struct string_list
*fname_list
,
102 const struct string_list
*extra_keep
)
108 if (!(dir
= opendir(packdir
)))
111 while ((e
= readdir(dir
)) != NULL
) {
115 for (i
= 0; i
< extra_keep
->nr
; i
++)
116 if (!fspathcmp(e
->d_name
, extra_keep
->items
[i
].string
))
118 if (extra_keep
->nr
> 0 && i
< extra_keep
->nr
)
121 if (!strip_suffix(e
->d_name
, ".pack", &len
))
124 fname
= xmemdupz(e
->d_name
, len
);
126 if (!file_exists(mkpath("%s/%s.keep", packdir
, fname
)))
127 string_list_append_nodup(fname_list
, fname
);
134 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
136 struct strbuf buf
= STRBUF_INIT
;
137 struct multi_pack_index
*m
= get_local_multi_pack_index(the_repository
);
138 strbuf_addf(&buf
, "%s.pack", base_name
);
139 if (m
&& midx_contains_pack(m
, buf
.buf
))
140 clear_midx_file(the_repository
);
141 strbuf_insertf(&buf
, 0, "%s/", dir_name
);
142 unlink_pack_path(buf
.buf
, 1);
143 strbuf_release(&buf
);
146 struct pack_objects_args
{
148 const char *window_memory
;
151 const char *max_pack_size
;
158 static void prepare_pack_objects(struct child_process
*cmd
,
159 const struct pack_objects_args
*args
)
161 strvec_push(&cmd
->args
, "pack-objects");
163 strvec_pushf(&cmd
->args
, "--window=%s", args
->window
);
164 if (args
->window_memory
)
165 strvec_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
167 strvec_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
169 strvec_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
170 if (args
->max_pack_size
)
171 strvec_pushf(&cmd
->args
, "--max-pack-size=%s", args
->max_pack_size
);
172 if (args
->no_reuse_delta
)
173 strvec_pushf(&cmd
->args
, "--no-reuse-delta");
174 if (args
->no_reuse_object
)
175 strvec_pushf(&cmd
->args
, "--no-reuse-object");
177 strvec_push(&cmd
->args
, "--local");
179 strvec_push(&cmd
->args
, "--quiet");
180 if (delta_base_offset
)
181 strvec_push(&cmd
->args
, "--delta-base-offset");
182 strvec_push(&cmd
->args
, packtmp
);
188 * Write oid to the given struct child_process's stdin, starting it first if
191 static int write_oid(const struct object_id
*oid
, struct packed_git
*pack
,
192 uint32_t pos
, void *data
)
194 struct child_process
*cmd
= data
;
197 if (start_command(cmd
))
198 die(_("could not start pack-objects to repack promisor objects"));
201 xwrite(cmd
->in
, oid_to_hex(oid
), the_hash_algo
->hexsz
);
202 xwrite(cmd
->in
, "\n", 1);
216 static unsigned populate_pack_exts(char *name
)
219 struct strbuf path
= STRBUF_INIT
;
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
))
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
);
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 */
263 out
= xfdopen(cmd
.out
, "r");
264 while (strbuf_getline_lf(&line
, out
) != EOF
) {
265 struct string_list_item
*item
;
268 if (line
.len
!= the_hash_algo
->hexsz
)
269 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
270 item
= string_list_append(names
, line
.buf
);
273 * pack-objects creates the .pack and .idx files, but not the
274 * .promisor file. Create the .promisor file, which is empty.
276 * NEEDSWORK: fetch-pack sometimes generates non-empty
277 * .promisor files containing the ref names and associated
278 * hashes at the point of generation of the corresponding
279 * packfile, but this would not preserve their contents. Maybe
280 * concatenate the contents of all .promisor files instead of
281 * just creating a new empty file.
283 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
285 write_promisor_file(promisor_name
, NULL
, 0);
287 item
->util
= (void *)(uintptr_t)populate_pack_exts(item
->string
);
292 if (finish_command(&cmd
))
293 die(_("could not finish pack-objects to repack promisor objects"));
296 #define ALL_INTO_ONE 1
297 #define LOOSEN_UNREACHABLE 2
299 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
301 struct child_process cmd
= CHILD_PROCESS_INIT
;
302 struct string_list_item
*item
;
303 struct string_list names
= STRING_LIST_INIT_DUP
;
304 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
305 struct string_list existing_packs
= STRING_LIST_INIT_DUP
;
306 struct strbuf line
= STRBUF_INIT
;
310 /* variables to be filled by option parsing */
311 int pack_everything
= 0;
312 int delete_redundant
= 0;
313 const char *unpack_unreachable
= NULL
;
314 int keep_unreachable
= 0;
315 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
316 int no_update_server_info
= 0;
317 struct pack_objects_args po_args
= {NULL
};
319 struct option builtin_repack_options
[] = {
320 OPT_BIT('a', NULL
, &pack_everything
,
321 N_("pack everything in a single pack"), ALL_INTO_ONE
),
322 OPT_BIT('A', NULL
, &pack_everything
,
323 N_("same as -a, and turn unreachable objects loose"),
324 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
325 OPT_BOOL('d', NULL
, &delete_redundant
,
326 N_("remove redundant packs, and run git-prune-packed")),
327 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
328 N_("pass --no-reuse-delta to git-pack-objects")),
329 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
330 N_("pass --no-reuse-object to git-pack-objects")),
331 OPT_BOOL('n', NULL
, &no_update_server_info
,
332 N_("do not run git-update-server-info")),
333 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
334 OPT_BOOL('l', "local", &po_args
.local
,
335 N_("pass --local to git-pack-objects")),
336 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
337 N_("write bitmap index")),
338 OPT_BOOL('i', "delta-islands", &use_delta_islands
,
339 N_("pass --delta-islands to git-pack-objects")),
340 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
341 N_("with -A, do not loosen objects older than this")),
342 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
343 N_("with -a, repack unreachable objects")),
344 OPT_STRING(0, "window", &po_args
.window
, N_("n"),
345 N_("size of the window used for delta compression")),
346 OPT_STRING(0, "window-memory", &po_args
.window_memory
, N_("bytes"),
347 N_("same as the above, but limit memory size instead of entries count")),
348 OPT_STRING(0, "depth", &po_args
.depth
, N_("n"),
349 N_("limits the maximum delta depth")),
350 OPT_STRING(0, "threads", &po_args
.threads
, N_("n"),
351 N_("limits the maximum number of threads")),
352 OPT_STRING(0, "max-pack-size", &po_args
.max_pack_size
, N_("bytes"),
353 N_("maximum size of each packfile")),
354 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
355 N_("repack objects in packs marked with .keep")),
356 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
357 N_("do not repack this pack")),
361 git_config(repack_config
, NULL
);
363 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
364 git_repack_usage
, 0);
366 if (delete_redundant
&& repository_format_precious_objects
)
367 die(_("cannot delete packs in a precious-objects repo"));
369 if (keep_unreachable
&&
370 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
371 die(_("--keep-unreachable and -A are incompatible"));
373 if (write_bitmaps
< 0) {
374 if (!(pack_everything
& ALL_INTO_ONE
) ||
375 !is_bare_repository())
378 if (pack_kept_objects
< 0)
379 pack_kept_objects
= write_bitmaps
> 0;
381 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
))
382 die(_(incremental_bitmap_conflict_error
));
384 packdir
= mkpathdup("%s/pack", get_object_directory());
385 packtmp
= mkpathdup("%s/.tmp-%d-pack", packdir
, (int)getpid());
387 sigchain_push_common(remove_pack_on_signal
);
389 prepare_pack_objects(&cmd
, &po_args
);
391 strvec_push(&cmd
.args
, "--keep-true-parents");
392 if (!pack_kept_objects
)
393 strvec_push(&cmd
.args
, "--honor-pack-keep");
394 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
395 strvec_pushf(&cmd
.args
, "--keep-pack=%s",
396 keep_pack_list
.items
[i
].string
);
397 strvec_push(&cmd
.args
, "--non-empty");
398 strvec_push(&cmd
.args
, "--all");
399 strvec_push(&cmd
.args
, "--reflog");
400 strvec_push(&cmd
.args
, "--indexed-objects");
401 if (has_promisor_remote())
402 strvec_push(&cmd
.args
, "--exclude-promisor-objects");
403 if (write_bitmaps
> 0)
404 strvec_push(&cmd
.args
, "--write-bitmap-index");
405 else if (write_bitmaps
< 0)
406 strvec_push(&cmd
.args
, "--write-bitmap-index-quiet");
407 if (use_delta_islands
)
408 strvec_push(&cmd
.args
, "--delta-islands");
410 if (pack_everything
& ALL_INTO_ONE
) {
411 get_non_kept_pack_filenames(&existing_packs
, &keep_pack_list
);
413 repack_promisor_objects(&po_args
, &names
);
415 if (existing_packs
.nr
&& delete_redundant
) {
416 if (unpack_unreachable
) {
417 strvec_pushf(&cmd
.args
,
418 "--unpack-unreachable=%s",
420 strvec_push(&cmd
.env_array
, "GIT_REF_PARANOIA=1");
421 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
422 strvec_push(&cmd
.args
,
423 "--unpack-unreachable");
424 } else if (keep_unreachable
) {
425 strvec_push(&cmd
.args
, "--keep-unreachable");
426 strvec_push(&cmd
.args
, "--pack-loose-unreachable");
428 strvec_push(&cmd
.env_array
, "GIT_REF_PARANOIA=1");
432 strvec_push(&cmd
.args
, "--unpacked");
433 strvec_push(&cmd
.args
, "--incremental");
438 ret
= start_command(&cmd
);
442 out
= xfdopen(cmd
.out
, "r");
443 while (strbuf_getline_lf(&line
, out
) != EOF
) {
444 if (line
.len
!= the_hash_algo
->hexsz
)
445 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
446 string_list_append(&names
, line
.buf
);
449 ret
= finish_command(&cmd
);
453 if (!names
.nr
&& !po_args
.quiet
)
454 printf_ln(_("Nothing new to pack."));
456 for_each_string_list_item(item
, &names
) {
457 item
->util
= (void *)(uintptr_t)populate_pack_exts(item
->string
);
460 close_object_store(the_repository
->objects
);
463 * Ok we have prepared all new packfiles.
465 for_each_string_list_item(item
, &names
) {
466 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
467 char *fname
, *fname_old
;
469 fname
= mkpathdup("%s/pack-%s%s",
470 packdir
, item
->string
, exts
[ext
].name
);
471 fname_old
= mkpathdup("%s-%s%s",
472 packtmp
, item
->string
, exts
[ext
].name
);
474 if (((uintptr_t)item
->util
) & (1 << ext
)) {
475 struct stat statbuffer
;
476 if (!stat(fname_old
, &statbuffer
)) {
477 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
478 chmod(fname_old
, statbuffer
.st_mode
);
481 if (rename(fname_old
, fname
))
482 die_errno(_("renaming '%s' failed"), fname_old
);
483 } else if (!exts
[ext
].optional
)
484 die(_("missing required file: %s"), fname_old
);
485 else if (unlink(fname
) < 0 && errno
!= ENOENT
)
486 die_errno(_("could not unlink: %s"), fname
);
492 /* End of pack replacement. */
494 reprepare_packed_git(the_repository
);
496 if (delete_redundant
) {
497 const int hexsz
= the_hash_algo
->hexsz
;
499 string_list_sort(&names
);
500 for_each_string_list_item(item
, &existing_packs
) {
502 size_t len
= strlen(item
->string
);
505 sha1
= item
->string
+ len
- hexsz
;
506 if (!string_list_has_string(&names
, sha1
))
507 remove_redundant_pack(packdir
, item
->string
);
509 if (!po_args
.quiet
&& isatty(2))
510 opts
|= PRUNE_PACKED_VERBOSE
;
511 prune_packed_objects(opts
);
513 if (!keep_unreachable
&&
514 (!(pack_everything
& LOOSEN_UNREACHABLE
) ||
515 unpack_unreachable
) &&
516 is_repository_shallow(the_repository
))
517 prune_shallow(PRUNE_QUICK
);
520 if (!no_update_server_info
)
521 update_server_info(0);
522 remove_temporary_files();
524 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0))
525 write_midx_file(get_object_directory(), 0);
527 string_list_clear(&names
, 0);
528 string_list_clear(&rollback
, 0);
529 string_list_clear(&existing_packs
, 0);
530 strbuf_release(&line
);