5 #include "parse-options.h"
6 #include "run-command.h"
9 #include "string-list.h"
10 #include "argv-array.h"
13 #include "object-store.h"
15 static int delta_base_offset
= 1;
16 static int pack_kept_objects
= -1;
17 static int write_bitmaps
;
18 static char *packdir
, *packtmp
;
20 static const char *const git_repack_usage
[] = {
21 N_("git repack [<options>]"),
25 static const char incremental_bitmap_conflict_error
[] = N_(
26 "Incremental repacks are incompatible with bitmap indexes. Use\n"
27 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
31 static int repack_config(const char *var
, const char *value
, void *cb
)
33 if (!strcmp(var
, "repack.usedeltabaseoffset")) {
34 delta_base_offset
= git_config_bool(var
, value
);
37 if (!strcmp(var
, "repack.packkeptobjects")) {
38 pack_kept_objects
= git_config_bool(var
, value
);
41 if (!strcmp(var
, "repack.writebitmaps") ||
42 !strcmp(var
, "pack.writebitmaps")) {
43 write_bitmaps
= git_config_bool(var
, value
);
46 return git_default_config(var
, value
, cb
);
50 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
52 static void remove_temporary_files(void)
54 struct strbuf buf
= STRBUF_INIT
;
55 size_t dirlen
, prefixlen
;
59 dir
= opendir(packdir
);
63 /* Point at the slash at the end of ".../objects/pack/" */
64 dirlen
= strlen(packdir
) + 1;
65 strbuf_addstr(&buf
, packtmp
);
66 /* Hold the length of ".tmp-%d-pack-" */
67 prefixlen
= buf
.len
- dirlen
;
69 while ((e
= readdir(dir
))) {
70 if (strncmp(e
->d_name
, buf
.buf
+ dirlen
, prefixlen
))
72 strbuf_setlen(&buf
, dirlen
);
73 strbuf_addstr(&buf
, e
->d_name
);
80 static void remove_pack_on_signal(int signo
)
82 remove_temporary_files();
88 * Adds all packs hex strings to the fname list, which do not
89 * have a corresponding .keep file. These packs are not to
90 * be kept if we are going to pack everything into one file.
92 static void get_non_kept_pack_filenames(struct string_list
*fname_list
,
93 const struct string_list
*extra_keep
)
99 if (!(dir
= opendir(packdir
)))
102 while ((e
= readdir(dir
)) != NULL
) {
106 for (i
= 0; i
< extra_keep
->nr
; i
++)
107 if (!fspathcmp(e
->d_name
, extra_keep
->items
[i
].string
))
109 if (extra_keep
->nr
> 0 && i
< extra_keep
->nr
)
112 if (!strip_suffix(e
->d_name
, ".pack", &len
))
115 fname
= xmemdupz(e
->d_name
, len
);
117 if (!file_exists(mkpath("%s/%s.keep", packdir
, fname
)))
118 string_list_append_nodup(fname_list
, fname
);
125 static void remove_redundant_pack(const char *dir_name
, const char *base_name
)
127 const char *exts
[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
129 struct strbuf buf
= STRBUF_INIT
;
132 strbuf_addf(&buf
, "%s/%s", dir_name
, base_name
);
135 for (i
= 0; i
< ARRAY_SIZE(exts
); i
++) {
136 strbuf_setlen(&buf
, plen
);
137 strbuf_addstr(&buf
, exts
[i
]);
140 strbuf_release(&buf
);
143 struct pack_objects_args
{
145 const char *window_memory
;
148 const char *max_pack_size
;
155 static void prepare_pack_objects(struct child_process
*cmd
,
156 const struct pack_objects_args
*args
)
158 argv_array_push(&cmd
->args
, "pack-objects");
160 argv_array_pushf(&cmd
->args
, "--window=%s", args
->window
);
161 if (args
->window_memory
)
162 argv_array_pushf(&cmd
->args
, "--window-memory=%s", args
->window_memory
);
164 argv_array_pushf(&cmd
->args
, "--depth=%s", args
->depth
);
166 argv_array_pushf(&cmd
->args
, "--threads=%s", args
->threads
);
167 if (args
->max_pack_size
)
168 argv_array_pushf(&cmd
->args
, "--max-pack-size=%s", args
->max_pack_size
);
169 if (args
->no_reuse_delta
)
170 argv_array_pushf(&cmd
->args
, "--no-reuse-delta");
171 if (args
->no_reuse_object
)
172 argv_array_pushf(&cmd
->args
, "--no-reuse-object");
174 argv_array_push(&cmd
->args
, "--local");
176 argv_array_push(&cmd
->args
, "--quiet");
177 if (delta_base_offset
)
178 argv_array_push(&cmd
->args
, "--delta-base-offset");
179 argv_array_push(&cmd
->args
, packtmp
);
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
, struct packed_git
*pack
,
189 uint32_t pos
, void *data
)
191 struct child_process
*cmd
= data
;
194 if (start_command(cmd
))
195 die("Could not start pack-objects to repack promisor objects");
198 xwrite(cmd
->in
, oid_to_hex(oid
), GIT_SHA1_HEXSZ
);
199 xwrite(cmd
->in
, "\n", 1);
203 static void repack_promisor_objects(const struct pack_objects_args
*args
,
204 struct string_list
*names
)
206 struct child_process cmd
= CHILD_PROCESS_INIT
;
208 struct strbuf line
= STRBUF_INIT
;
210 prepare_pack_objects(&cmd
, args
);
214 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
215 * hints may result in suboptimal deltas in the resulting pack. See if
216 * the OIDs can be sent with fake paths such that pack-objects can use a
217 * {type -> existing pack order} ordering when computing deltas instead
218 * of a {type -> size} ordering, which may produce better deltas.
220 for_each_packed_object(write_oid
, &cmd
,
221 FOR_EACH_OBJECT_PROMISOR_ONLY
);
224 /* No packed objects; cmd was never started */
229 out
= xfdopen(cmd
.out
, "r");
230 while (strbuf_getline_lf(&line
, out
) != EOF
) {
234 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
235 string_list_append(names
, line
.buf
);
238 * pack-objects creates the .pack and .idx files, but not the
239 * .promisor file. Create the .promisor file, which is empty.
241 promisor_name
= mkpathdup("%s-%s.promisor", packtmp
,
243 fd
= open(promisor_name
, O_CREAT
|O_EXCL
|O_WRONLY
, 0600);
245 die_errno("unable to create '%s'", promisor_name
);
250 if (finish_command(&cmd
))
251 die("Could not finish pack-objects to repack promisor objects");
254 #define ALL_INTO_ONE 1
255 #define LOOSEN_UNREACHABLE 2
257 int cmd_repack(int argc
, const char **argv
, const char *prefix
)
268 struct child_process cmd
= CHILD_PROCESS_INIT
;
269 struct string_list_item
*item
;
270 struct string_list names
= STRING_LIST_INIT_DUP
;
271 struct string_list rollback
= STRING_LIST_INIT_NODUP
;
272 struct string_list existing_packs
= STRING_LIST_INIT_DUP
;
273 struct strbuf line
= STRBUF_INIT
;
274 int i
, ext
, ret
, failed
;
277 /* variables to be filled by option parsing */
278 int pack_everything
= 0;
279 int delete_redundant
= 0;
280 const char *unpack_unreachable
= NULL
;
281 int keep_unreachable
= 0;
282 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
283 int no_update_server_info
= 0;
284 int midx_cleared
= 0;
285 struct pack_objects_args po_args
= {NULL
};
287 struct option builtin_repack_options
[] = {
288 OPT_BIT('a', NULL
, &pack_everything
,
289 N_("pack everything in a single pack"), ALL_INTO_ONE
),
290 OPT_BIT('A', NULL
, &pack_everything
,
291 N_("same as -a, and turn unreachable objects loose"),
292 LOOSEN_UNREACHABLE
| ALL_INTO_ONE
),
293 OPT_BOOL('d', NULL
, &delete_redundant
,
294 N_("remove redundant packs, and run git-prune-packed")),
295 OPT_BOOL('f', NULL
, &po_args
.no_reuse_delta
,
296 N_("pass --no-reuse-delta to git-pack-objects")),
297 OPT_BOOL('F', NULL
, &po_args
.no_reuse_object
,
298 N_("pass --no-reuse-object to git-pack-objects")),
299 OPT_BOOL('n', NULL
, &no_update_server_info
,
300 N_("do not run git-update-server-info")),
301 OPT__QUIET(&po_args
.quiet
, N_("be quiet")),
302 OPT_BOOL('l', "local", &po_args
.local
,
303 N_("pass --local to git-pack-objects")),
304 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps
,
305 N_("write bitmap index")),
306 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable
, N_("approxidate"),
307 N_("with -A, do not loosen objects older than this")),
308 OPT_BOOL('k', "keep-unreachable", &keep_unreachable
,
309 N_("with -a, repack unreachable objects")),
310 OPT_STRING(0, "window", &po_args
.window
, N_("n"),
311 N_("size of the window used for delta compression")),
312 OPT_STRING(0, "window-memory", &po_args
.window_memory
, N_("bytes"),
313 N_("same as the above, but limit memory size instead of entries count")),
314 OPT_STRING(0, "depth", &po_args
.depth
, N_("n"),
315 N_("limits the maximum delta depth")),
316 OPT_STRING(0, "threads", &po_args
.threads
, N_("n"),
317 N_("limits the maximum number of threads")),
318 OPT_STRING(0, "max-pack-size", &po_args
.max_pack_size
, N_("bytes"),
319 N_("maximum size of each packfile")),
320 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects
,
321 N_("repack objects in packs marked with .keep")),
322 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
323 N_("do not repack this pack")),
327 git_config(repack_config
, NULL
);
329 argc
= parse_options(argc
, argv
, prefix
, builtin_repack_options
,
330 git_repack_usage
, 0);
332 if (delete_redundant
&& repository_format_precious_objects
)
333 die(_("cannot delete packs in a precious-objects repo"));
335 if (keep_unreachable
&&
336 (unpack_unreachable
|| (pack_everything
& LOOSEN_UNREACHABLE
)))
337 die(_("--keep-unreachable and -A are incompatible"));
339 if (pack_kept_objects
< 0)
340 pack_kept_objects
= write_bitmaps
;
342 if (write_bitmaps
&& !(pack_everything
& ALL_INTO_ONE
))
343 die(_(incremental_bitmap_conflict_error
));
345 packdir
= mkpathdup("%s/pack", get_object_directory());
346 packtmp
= mkpathdup("%s/.tmp-%d-pack", packdir
, (int)getpid());
348 sigchain_push_common(remove_pack_on_signal
);
350 prepare_pack_objects(&cmd
, &po_args
);
352 argv_array_push(&cmd
.args
, "--keep-true-parents");
353 if (!pack_kept_objects
)
354 argv_array_push(&cmd
.args
, "--honor-pack-keep");
355 for (i
= 0; i
< keep_pack_list
.nr
; i
++)
356 argv_array_pushf(&cmd
.args
, "--keep-pack=%s",
357 keep_pack_list
.items
[i
].string
);
358 argv_array_push(&cmd
.args
, "--non-empty");
359 argv_array_push(&cmd
.args
, "--all");
360 argv_array_push(&cmd
.args
, "--reflog");
361 argv_array_push(&cmd
.args
, "--indexed-objects");
362 if (repository_format_partial_clone
)
363 argv_array_push(&cmd
.args
, "--exclude-promisor-objects");
365 argv_array_push(&cmd
.args
, "--write-bitmap-index");
367 if (pack_everything
& ALL_INTO_ONE
) {
368 get_non_kept_pack_filenames(&existing_packs
, &keep_pack_list
);
370 repack_promisor_objects(&po_args
, &names
);
372 if (existing_packs
.nr
&& delete_redundant
) {
373 if (unpack_unreachable
) {
374 argv_array_pushf(&cmd
.args
,
375 "--unpack-unreachable=%s",
377 argv_array_push(&cmd
.env_array
, "GIT_REF_PARANOIA=1");
378 } else if (pack_everything
& LOOSEN_UNREACHABLE
) {
379 argv_array_push(&cmd
.args
,
380 "--unpack-unreachable");
381 } else if (keep_unreachable
) {
382 argv_array_push(&cmd
.args
, "--keep-unreachable");
383 argv_array_push(&cmd
.args
, "--pack-loose-unreachable");
385 argv_array_push(&cmd
.env_array
, "GIT_REF_PARANOIA=1");
389 argv_array_push(&cmd
.args
, "--unpacked");
390 argv_array_push(&cmd
.args
, "--incremental");
395 ret
= start_command(&cmd
);
399 out
= xfdopen(cmd
.out
, "r");
400 while (strbuf_getline_lf(&line
, out
) != EOF
) {
402 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
403 string_list_append(&names
, line
.buf
);
406 ret
= finish_command(&cmd
);
410 if (!names
.nr
&& !po_args
.quiet
)
411 printf("Nothing new to pack.\n");
414 * Ok we have prepared all new packfiles.
415 * First see if there are packs of the same name and if so
416 * if we can move them out of the way (this can happen if we
417 * repacked immediately after packing fully.
420 for_each_string_list_item(item
, &names
) {
421 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
422 char *fname
, *fname_old
;
425 /* if we move a packfile, it will invalidated the midx */
426 clear_midx_file(get_object_directory());
430 fname
= mkpathdup("%s/pack-%s%s", packdir
,
431 item
->string
, exts
[ext
].name
);
432 if (!file_exists(fname
)) {
437 fname_old
= mkpathdup("%s/old-%s%s", packdir
,
438 item
->string
, exts
[ext
].name
);
439 if (file_exists(fname_old
))
440 if (unlink(fname_old
))
443 if (!failed
&& rename(fname
, fname_old
)) {
449 string_list_append(&rollback
, fname
);
457 struct string_list rollback_failure
= STRING_LIST_INIT_DUP
;
458 for_each_string_list_item(item
, &rollback
) {
459 char *fname
, *fname_old
;
460 fname
= mkpathdup("%s/%s", packdir
, item
->string
);
461 fname_old
= mkpathdup("%s/old-%s", packdir
, item
->string
);
462 if (rename(fname_old
, fname
))
463 string_list_append(&rollback_failure
, fname
);
468 if (rollback_failure
.nr
) {
471 "WARNING: Some packs in use have been renamed by\n"
472 "WARNING: prefixing old- to their name, in order to\n"
473 "WARNING: replace them with the new version of the\n"
474 "WARNING: file. But the operation failed, and the\n"
475 "WARNING: attempt to rename them back to their\n"
476 "WARNING: original names also failed.\n"
477 "WARNING: Please rename them in %s manually:\n", packdir
);
478 for (i
= 0; i
< rollback_failure
.nr
; i
++)
479 fprintf(stderr
, "WARNING: old-%s -> %s\n",
480 rollback_failure
.items
[i
].string
,
481 rollback_failure
.items
[i
].string
);
486 /* Now the ones with the same name are out of the way... */
487 for_each_string_list_item(item
, &names
) {
488 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
489 char *fname
, *fname_old
;
490 struct stat statbuffer
;
492 fname
= mkpathdup("%s/pack-%s%s",
493 packdir
, item
->string
, exts
[ext
].name
);
494 fname_old
= mkpathdup("%s-%s%s",
495 packtmp
, item
->string
, exts
[ext
].name
);
496 if (!stat(fname_old
, &statbuffer
)) {
497 statbuffer
.st_mode
&= ~(S_IWUSR
| S_IWGRP
| S_IWOTH
);
498 chmod(fname_old
, statbuffer
.st_mode
);
501 if (exists
|| !exts
[ext
].optional
) {
502 if (rename(fname_old
, fname
))
503 die_errno(_("renaming '%s' failed"), fname_old
);
510 /* Remove the "old-" files */
511 for_each_string_list_item(item
, &names
) {
512 for (ext
= 0; ext
< ARRAY_SIZE(exts
); ext
++) {
514 fname
= mkpathdup("%s/old-%s%s",
518 if (remove_path(fname
))
519 warning(_("failed to remove '%s'"), fname
);
524 /* End of pack replacement. */
526 reprepare_packed_git(the_repository
);
528 if (delete_redundant
) {
530 string_list_sort(&names
);
531 for_each_string_list_item(item
, &existing_packs
) {
533 size_t len
= strlen(item
->string
);
536 sha1
= item
->string
+ len
- 40;
537 if (!string_list_has_string(&names
, sha1
))
538 remove_redundant_pack(packdir
, item
->string
);
540 if (!po_args
.quiet
&& isatty(2))
541 opts
|= PRUNE_PACKED_VERBOSE
;
542 prune_packed_objects(opts
);
545 if (!no_update_server_info
)
546 update_server_info(0);
547 remove_temporary_files();
548 string_list_clear(&names
, 0);
549 string_list_clear(&rollback
, 0);
550 string_list_clear(&existing_packs
, 0);
551 strbuf_release(&line
);