Merge branch 'jk/trailer-fixes'
[alt-git.git] / builtin / repack.c
blob42be88e86ce6fd5541ad067c17f5b29a4d492feb
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "parse-options.h"
6 #include "run-command.h"
7 #include "sigchain.h"
8 #include "strbuf.h"
9 #include "string-list.h"
10 #include "argv-array.h"
11 #include "midx.h"
12 #include "packfile.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>]"),
22 NULL
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);
35 return 0;
37 if (!strcmp(var, "repack.packkeptobjects")) {
38 pack_kept_objects = git_config_bool(var, value);
39 return 0;
41 if (!strcmp(var, "repack.writebitmaps") ||
42 !strcmp(var, "pack.writebitmaps")) {
43 write_bitmaps = git_config_bool(var, value);
44 return 0;
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;
56 DIR *dir;
57 struct dirent *e;
59 dir = opendir(packdir);
60 if (!dir)
61 return;
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))
71 continue;
72 strbuf_setlen(&buf, dirlen);
73 strbuf_addstr(&buf, e->d_name);
74 unlink(buf.buf);
76 closedir(dir);
77 strbuf_release(&buf);
80 static void remove_pack_on_signal(int signo)
82 remove_temporary_files();
83 sigchain_pop(signo);
84 raise(signo);
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)
95 DIR *dir;
96 struct dirent *e;
97 char *fname;
99 if (!(dir = opendir(packdir)))
100 return;
102 while ((e = readdir(dir)) != NULL) {
103 size_t len;
104 int i;
106 for (i = 0; i < extra_keep->nr; i++)
107 if (!fspathcmp(e->d_name, extra_keep->items[i].string))
108 break;
109 if (extra_keep->nr > 0 && i < extra_keep->nr)
110 continue;
112 if (!strip_suffix(e->d_name, ".pack", &len))
113 continue;
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);
119 else
120 free(fname);
122 closedir(dir);
125 static void remove_redundant_pack(const char *dir_name, const char *base_name)
127 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap", ".promisor"};
128 int i;
129 struct strbuf buf = STRBUF_INIT;
130 size_t plen;
132 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
133 plen = buf.len;
135 for (i = 0; i < ARRAY_SIZE(exts); i++) {
136 strbuf_setlen(&buf, plen);
137 strbuf_addstr(&buf, exts[i]);
138 unlink(buf.buf);
140 strbuf_release(&buf);
143 struct pack_objects_args {
144 const char *window;
145 const char *window_memory;
146 const char *depth;
147 const char *threads;
148 const char *max_pack_size;
149 int no_reuse_delta;
150 int no_reuse_object;
151 int quiet;
152 int local;
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");
159 if (args->window)
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);
163 if (args->depth)
164 argv_array_pushf(&cmd->args, "--depth=%s", args->depth);
165 if (args->threads)
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");
173 if (args->local)
174 argv_array_push(&cmd->args, "--local");
175 if (args->quiet)
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);
180 cmd->git_cmd = 1;
181 cmd->out = -1;
185 * Write oid to the given struct child_process's stdin, starting it first if
186 * necessary.
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;
193 if (cmd->in == -1) {
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);
200 return 0;
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;
207 FILE *out;
208 struct strbuf line = STRBUF_INIT;
210 prepare_pack_objects(&cmd, args);
211 cmd.in = -1;
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);
223 if (cmd.in == -1)
224 /* No packed objects; cmd was never started */
225 return;
227 close(cmd.in);
229 out = xfdopen(cmd.out, "r");
230 while (strbuf_getline_lf(&line, out) != EOF) {
231 char *promisor_name;
232 int fd;
233 if (line.len != 40)
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,
242 line.buf);
243 fd = open(promisor_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
244 if (fd < 0)
245 die_errno("unable to create '%s'", promisor_name);
246 close(fd);
247 free(promisor_name);
249 fclose(out);
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)
259 struct {
260 const char *name;
261 unsigned optional:1;
262 } exts[] = {
263 {".pack"},
264 {".idx"},
265 {".bitmap", 1},
266 {".promisor", 1},
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;
275 FILE *out;
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")),
324 OPT_END()
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");
364 if (write_bitmaps)
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",
376 unpack_unreachable);
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");
384 } else {
385 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
388 } else {
389 argv_array_push(&cmd.args, "--unpacked");
390 argv_array_push(&cmd.args, "--incremental");
393 cmd.no_stdin = 1;
395 ret = start_command(&cmd);
396 if (ret)
397 return ret;
399 out = xfdopen(cmd.out, "r");
400 while (strbuf_getline_lf(&line, out) != EOF) {
401 if (line.len != 40)
402 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
403 string_list_append(&names, line.buf);
405 fclose(out);
406 ret = finish_command(&cmd);
407 if (ret)
408 return ret;
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.
419 failed = 0;
420 for_each_string_list_item(item, &names) {
421 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
422 char *fname, *fname_old;
424 if (!midx_cleared) {
425 /* if we move a packfile, it will invalidated the midx */
426 clear_midx_file(get_object_directory());
427 midx_cleared = 1;
430 fname = mkpathdup("%s/pack-%s%s", packdir,
431 item->string, exts[ext].name);
432 if (!file_exists(fname)) {
433 free(fname);
434 continue;
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))
441 failed = 1;
443 if (!failed && rename(fname, fname_old)) {
444 free(fname);
445 free(fname_old);
446 failed = 1;
447 break;
448 } else {
449 string_list_append(&rollback, fname);
450 free(fname_old);
453 if (failed)
454 break;
456 if (failed) {
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);
464 free(fname);
465 free(fname_old);
468 if (rollback_failure.nr) {
469 int i;
470 fprintf(stderr,
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);
483 exit(1);
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;
491 int exists = 0;
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);
499 exists = 1;
501 if (exists || !exts[ext].optional) {
502 if (rename(fname_old, fname))
503 die_errno(_("renaming '%s' failed"), fname_old);
505 free(fname);
506 free(fname_old);
510 /* Remove the "old-" files */
511 for_each_string_list_item(item, &names) {
512 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
513 char *fname;
514 fname = mkpathdup("%s/old-%s%s",
515 packdir,
516 item->string,
517 exts[ext].name);
518 if (remove_path(fname))
519 warning(_("failed to remove '%s'"), fname);
520 free(fname);
524 /* End of pack replacement. */
526 reprepare_packed_git(the_repository);
528 if (delete_redundant) {
529 int opts = 0;
530 string_list_sort(&names);
531 for_each_string_list_item(item, &existing_packs) {
532 char *sha1;
533 size_t len = strlen(item->string);
534 if (len < 40)
535 continue;
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);
553 return 0;