]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/repack.c
Merge branch 'ea/blame-use-oideq'
[thirdparty/git.git] / builtin / repack.c
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 "strvec.h"
11 #include "midx.h"
12 #include "packfile.h"
13 #include "prune-packed.h"
14 #include "object-store.h"
15 #include "promisor-remote.h"
16 #include "shallow.h"
17
18 static int delta_base_offset = 1;
19 static int pack_kept_objects = -1;
20 static int write_bitmaps = -1;
21 static int use_delta_islands;
22 static char *packdir, *packtmp;
23
24 static const char *const git_repack_usage[] = {
25 N_("git repack [<options>]"),
26 NULL
27 };
28
29 static const char incremental_bitmap_conflict_error[] = N_(
30 "Incremental repacks are incompatible with bitmap indexes. Use\n"
31 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
32 );
33
34
35 static int repack_config(const char *var, const char *value, void *cb)
36 {
37 if (!strcmp(var, "repack.usedeltabaseoffset")) {
38 delta_base_offset = git_config_bool(var, value);
39 return 0;
40 }
41 if (!strcmp(var, "repack.packkeptobjects")) {
42 pack_kept_objects = git_config_bool(var, value);
43 return 0;
44 }
45 if (!strcmp(var, "repack.writebitmaps") ||
46 !strcmp(var, "pack.writebitmaps")) {
47 write_bitmaps = git_config_bool(var, value);
48 return 0;
49 }
50 if (!strcmp(var, "repack.usedeltaislands")) {
51 use_delta_islands = git_config_bool(var, value);
52 return 0;
53 }
54 return git_default_config(var, value, cb);
55 }
56
57 /*
58 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
59 */
60 static void remove_temporary_files(void)
61 {
62 struct strbuf buf = STRBUF_INIT;
63 size_t dirlen, prefixlen;
64 DIR *dir;
65 struct dirent *e;
66
67 dir = opendir(packdir);
68 if (!dir)
69 return;
70
71 /* Point at the slash at the end of ".../objects/pack/" */
72 dirlen = strlen(packdir) + 1;
73 strbuf_addstr(&buf, packtmp);
74 /* Hold the length of ".tmp-%d-pack-" */
75 prefixlen = buf.len - dirlen;
76
77 while ((e = readdir(dir))) {
78 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
79 continue;
80 strbuf_setlen(&buf, dirlen);
81 strbuf_addstr(&buf, e->d_name);
82 unlink(buf.buf);
83 }
84 closedir(dir);
85 strbuf_release(&buf);
86 }
87
88 static void remove_pack_on_signal(int signo)
89 {
90 remove_temporary_files();
91 sigchain_pop(signo);
92 raise(signo);
93 }
94
95 /*
96 * Adds all packs hex strings to the fname list, which do not
97 * have a corresponding .keep file. These packs are not to
98 * be kept if we are going to pack everything into one file.
99 */
100 static void get_non_kept_pack_filenames(struct string_list *fname_list,
101 const struct string_list *extra_keep)
102 {
103 DIR *dir;
104 struct dirent *e;
105 char *fname;
106
107 if (!(dir = opendir(packdir)))
108 return;
109
110 while ((e = readdir(dir)) != NULL) {
111 size_t len;
112 int i;
113
114 for (i = 0; i < extra_keep->nr; i++)
115 if (!fspathcmp(e->d_name, extra_keep->items[i].string))
116 break;
117 if (extra_keep->nr > 0 && i < extra_keep->nr)
118 continue;
119
120 if (!strip_suffix(e->d_name, ".pack", &len))
121 continue;
122
123 fname = xmemdupz(e->d_name, len);
124
125 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
126 string_list_append_nodup(fname_list, fname);
127 else
128 free(fname);
129 }
130 closedir(dir);
131 }
132
133 static void remove_redundant_pack(const char *dir_name, const char *base_name)
134 {
135 struct strbuf buf = STRBUF_INIT;
136 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
137 strbuf_addf(&buf, "%s.pack", base_name);
138 if (m && midx_contains_pack(m, buf.buf))
139 clear_midx_file(the_repository);
140 strbuf_insertf(&buf, 0, "%s/", dir_name);
141 unlink_pack_path(buf.buf, 1);
142 strbuf_release(&buf);
143 }
144
145 struct pack_objects_args {
146 const char *window;
147 const char *window_memory;
148 const char *depth;
149 const char *threads;
150 const char *max_pack_size;
151 int no_reuse_delta;
152 int no_reuse_object;
153 int quiet;
154 int local;
155 };
156
157 static void prepare_pack_objects(struct child_process *cmd,
158 const struct pack_objects_args *args)
159 {
160 strvec_push(&cmd->args, "pack-objects");
161 if (args->window)
162 strvec_pushf(&cmd->args, "--window=%s", args->window);
163 if (args->window_memory)
164 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
165 if (args->depth)
166 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
167 if (args->threads)
168 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
169 if (args->max_pack_size)
170 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
171 if (args->no_reuse_delta)
172 strvec_pushf(&cmd->args, "--no-reuse-delta");
173 if (args->no_reuse_object)
174 strvec_pushf(&cmd->args, "--no-reuse-object");
175 if (args->local)
176 strvec_push(&cmd->args, "--local");
177 if (args->quiet)
178 strvec_push(&cmd->args, "--quiet");
179 if (delta_base_offset)
180 strvec_push(&cmd->args, "--delta-base-offset");
181 strvec_push(&cmd->args, packtmp);
182 cmd->git_cmd = 1;
183 cmd->out = -1;
184 }
185
186 /*
187 * Write oid to the given struct child_process's stdin, starting it first if
188 * necessary.
189 */
190 static int write_oid(const struct object_id *oid, struct packed_git *pack,
191 uint32_t pos, void *data)
192 {
193 struct child_process *cmd = data;
194
195 if (cmd->in == -1) {
196 if (start_command(cmd))
197 die(_("could not start pack-objects to repack promisor objects"));
198 }
199
200 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
201 xwrite(cmd->in, "\n", 1);
202 return 0;
203 }
204
205 static void repack_promisor_objects(const struct pack_objects_args *args,
206 struct string_list *names)
207 {
208 struct child_process cmd = CHILD_PROCESS_INIT;
209 FILE *out;
210 struct strbuf line = STRBUF_INIT;
211
212 prepare_pack_objects(&cmd, args);
213 cmd.in = -1;
214
215 /*
216 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
217 * hints may result in suboptimal deltas in the resulting pack. See if
218 * the OIDs can be sent with fake paths such that pack-objects can use a
219 * {type -> existing pack order} ordering when computing deltas instead
220 * of a {type -> size} ordering, which may produce better deltas.
221 */
222 for_each_packed_object(write_oid, &cmd,
223 FOR_EACH_OBJECT_PROMISOR_ONLY);
224
225 if (cmd.in == -1)
226 /* No packed objects; cmd was never started */
227 return;
228
229 close(cmd.in);
230
231 out = xfdopen(cmd.out, "r");
232 while (strbuf_getline_lf(&line, out) != EOF) {
233 char *promisor_name;
234 int fd;
235 if (line.len != the_hash_algo->hexsz)
236 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
237 string_list_append(names, line.buf);
238
239 /*
240 * pack-objects creates the .pack and .idx files, but not the
241 * .promisor file. Create the .promisor file, which is empty.
242 *
243 * NEEDSWORK: fetch-pack sometimes generates non-empty
244 * .promisor files containing the ref names and associated
245 * hashes at the point of generation of the corresponding
246 * packfile, but this would not preserve their contents. Maybe
247 * concatenate the contents of all .promisor files instead of
248 * just creating a new empty file.
249 */
250 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
251 line.buf);
252 fd = open(promisor_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
253 if (fd < 0)
254 die_errno(_("unable to create '%s'"), promisor_name);
255 close(fd);
256 free(promisor_name);
257 }
258 fclose(out);
259 if (finish_command(&cmd))
260 die(_("could not finish pack-objects to repack promisor objects"));
261 }
262
263 #define ALL_INTO_ONE 1
264 #define LOOSEN_UNREACHABLE 2
265
266 int cmd_repack(int argc, const char **argv, const char *prefix)
267 {
268 struct {
269 const char *name;
270 unsigned optional:1;
271 } exts[] = {
272 {".pack"},
273 {".idx"},
274 {".bitmap", 1},
275 {".promisor", 1},
276 };
277 struct child_process cmd = CHILD_PROCESS_INIT;
278 struct string_list_item *item;
279 struct string_list names = STRING_LIST_INIT_DUP;
280 struct string_list rollback = STRING_LIST_INIT_NODUP;
281 struct string_list existing_packs = STRING_LIST_INIT_DUP;
282 struct strbuf line = STRBUF_INIT;
283 int i, ext, ret, failed;
284 FILE *out;
285
286 /* variables to be filled by option parsing */
287 int pack_everything = 0;
288 int delete_redundant = 0;
289 const char *unpack_unreachable = NULL;
290 int keep_unreachable = 0;
291 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
292 int no_update_server_info = 0;
293 struct pack_objects_args po_args = {NULL};
294
295 struct option builtin_repack_options[] = {
296 OPT_BIT('a', NULL, &pack_everything,
297 N_("pack everything in a single pack"), ALL_INTO_ONE),
298 OPT_BIT('A', NULL, &pack_everything,
299 N_("same as -a, and turn unreachable objects loose"),
300 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
301 OPT_BOOL('d', NULL, &delete_redundant,
302 N_("remove redundant packs, and run git-prune-packed")),
303 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
304 N_("pass --no-reuse-delta to git-pack-objects")),
305 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
306 N_("pass --no-reuse-object to git-pack-objects")),
307 OPT_BOOL('n', NULL, &no_update_server_info,
308 N_("do not run git-update-server-info")),
309 OPT__QUIET(&po_args.quiet, N_("be quiet")),
310 OPT_BOOL('l', "local", &po_args.local,
311 N_("pass --local to git-pack-objects")),
312 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
313 N_("write bitmap index")),
314 OPT_BOOL('i', "delta-islands", &use_delta_islands,
315 N_("pass --delta-islands to git-pack-objects")),
316 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
317 N_("with -A, do not loosen objects older than this")),
318 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
319 N_("with -a, repack unreachable objects")),
320 OPT_STRING(0, "window", &po_args.window, N_("n"),
321 N_("size of the window used for delta compression")),
322 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
323 N_("same as the above, but limit memory size instead of entries count")),
324 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
325 N_("limits the maximum delta depth")),
326 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
327 N_("limits the maximum number of threads")),
328 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
329 N_("maximum size of each packfile")),
330 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
331 N_("repack objects in packs marked with .keep")),
332 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
333 N_("do not repack this pack")),
334 OPT_END()
335 };
336
337 git_config(repack_config, NULL);
338
339 argc = parse_options(argc, argv, prefix, builtin_repack_options,
340 git_repack_usage, 0);
341
342 if (delete_redundant && repository_format_precious_objects)
343 die(_("cannot delete packs in a precious-objects repo"));
344
345 if (keep_unreachable &&
346 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
347 die(_("--keep-unreachable and -A are incompatible"));
348
349 if (write_bitmaps < 0) {
350 if (!(pack_everything & ALL_INTO_ONE) ||
351 !is_bare_repository())
352 write_bitmaps = 0;
353 }
354 if (pack_kept_objects < 0)
355 pack_kept_objects = write_bitmaps > 0;
356
357 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))
358 die(_(incremental_bitmap_conflict_error));
359
360 packdir = mkpathdup("%s/pack", get_object_directory());
361 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
362
363 sigchain_push_common(remove_pack_on_signal);
364
365 prepare_pack_objects(&cmd, &po_args);
366
367 strvec_push(&cmd.args, "--keep-true-parents");
368 if (!pack_kept_objects)
369 strvec_push(&cmd.args, "--honor-pack-keep");
370 for (i = 0; i < keep_pack_list.nr; i++)
371 strvec_pushf(&cmd.args, "--keep-pack=%s",
372 keep_pack_list.items[i].string);
373 strvec_push(&cmd.args, "--non-empty");
374 strvec_push(&cmd.args, "--all");
375 strvec_push(&cmd.args, "--reflog");
376 strvec_push(&cmd.args, "--indexed-objects");
377 if (has_promisor_remote())
378 strvec_push(&cmd.args, "--exclude-promisor-objects");
379 if (write_bitmaps > 0)
380 strvec_push(&cmd.args, "--write-bitmap-index");
381 else if (write_bitmaps < 0)
382 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
383 if (use_delta_islands)
384 strvec_push(&cmd.args, "--delta-islands");
385
386 if (pack_everything & ALL_INTO_ONE) {
387 get_non_kept_pack_filenames(&existing_packs, &keep_pack_list);
388
389 repack_promisor_objects(&po_args, &names);
390
391 if (existing_packs.nr && delete_redundant) {
392 if (unpack_unreachable) {
393 strvec_pushf(&cmd.args,
394 "--unpack-unreachable=%s",
395 unpack_unreachable);
396 strvec_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
397 } else if (pack_everything & LOOSEN_UNREACHABLE) {
398 strvec_push(&cmd.args,
399 "--unpack-unreachable");
400 } else if (keep_unreachable) {
401 strvec_push(&cmd.args, "--keep-unreachable");
402 strvec_push(&cmd.args, "--pack-loose-unreachable");
403 } else {
404 strvec_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
405 }
406 }
407 } else {
408 strvec_push(&cmd.args, "--unpacked");
409 strvec_push(&cmd.args, "--incremental");
410 }
411
412 cmd.no_stdin = 1;
413
414 ret = start_command(&cmd);
415 if (ret)
416 return ret;
417
418 out = xfdopen(cmd.out, "r");
419 while (strbuf_getline_lf(&line, out) != EOF) {
420 if (line.len != the_hash_algo->hexsz)
421 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
422 string_list_append(&names, line.buf);
423 }
424 fclose(out);
425 ret = finish_command(&cmd);
426 if (ret)
427 return ret;
428
429 if (!names.nr && !po_args.quiet)
430 printf_ln(_("Nothing new to pack."));
431
432 close_object_store(the_repository->objects);
433
434 /*
435 * Ok we have prepared all new packfiles.
436 * First see if there are packs of the same name and if so
437 * if we can move them out of the way (this can happen if we
438 * repacked immediately after packing fully.
439 */
440 failed = 0;
441 for_each_string_list_item(item, &names) {
442 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
443 char *fname, *fname_old;
444
445 fname = mkpathdup("%s/pack-%s%s", packdir,
446 item->string, exts[ext].name);
447 if (!file_exists(fname)) {
448 free(fname);
449 continue;
450 }
451
452 fname_old = mkpathdup("%s/old-%s%s", packdir,
453 item->string, exts[ext].name);
454 if (file_exists(fname_old))
455 if (unlink(fname_old))
456 failed = 1;
457
458 if (!failed && rename(fname, fname_old)) {
459 free(fname);
460 free(fname_old);
461 failed = 1;
462 break;
463 } else {
464 string_list_append(&rollback, fname);
465 free(fname_old);
466 }
467 }
468 if (failed)
469 break;
470 }
471 if (failed) {
472 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
473 for_each_string_list_item(item, &rollback) {
474 char *fname, *fname_old;
475 fname = mkpathdup("%s/%s", packdir, item->string);
476 fname_old = mkpathdup("%s/old-%s", packdir, item->string);
477 if (rename(fname_old, fname))
478 string_list_append(&rollback_failure, fname);
479 free(fname);
480 free(fname_old);
481 }
482
483 if (rollback_failure.nr) {
484 int i;
485 fprintf(stderr,
486 _("WARNING: Some packs in use have been renamed by\n"
487 "WARNING: prefixing old- to their name, in order to\n"
488 "WARNING: replace them with the new version of the\n"
489 "WARNING: file. But the operation failed, and the\n"
490 "WARNING: attempt to rename them back to their\n"
491 "WARNING: original names also failed.\n"
492 "WARNING: Please rename them in %s manually:\n"), packdir);
493 for (i = 0; i < rollback_failure.nr; i++)
494 fprintf(stderr, "WARNING: old-%s -> %s\n",
495 rollback_failure.items[i].string,
496 rollback_failure.items[i].string);
497 }
498 exit(1);
499 }
500
501 /* Now the ones with the same name are out of the way... */
502 for_each_string_list_item(item, &names) {
503 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
504 char *fname, *fname_old;
505 struct stat statbuffer;
506 int exists = 0;
507 fname = mkpathdup("%s/pack-%s%s",
508 packdir, item->string, exts[ext].name);
509 fname_old = mkpathdup("%s-%s%s",
510 packtmp, item->string, exts[ext].name);
511 if (!stat(fname_old, &statbuffer)) {
512 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
513 chmod(fname_old, statbuffer.st_mode);
514 exists = 1;
515 }
516 if (exists || !exts[ext].optional) {
517 if (rename(fname_old, fname))
518 die_errno(_("renaming '%s' failed"), fname_old);
519 }
520 free(fname);
521 free(fname_old);
522 }
523 }
524
525 /* Remove the "old-" files */
526 for_each_string_list_item(item, &names) {
527 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
528 char *fname;
529 fname = mkpathdup("%s/old-%s%s",
530 packdir,
531 item->string,
532 exts[ext].name);
533 if (remove_path(fname))
534 warning(_("failed to remove '%s'"), fname);
535 free(fname);
536 }
537 }
538
539 /* End of pack replacement. */
540
541 reprepare_packed_git(the_repository);
542
543 if (delete_redundant) {
544 const int hexsz = the_hash_algo->hexsz;
545 int opts = 0;
546 string_list_sort(&names);
547 for_each_string_list_item(item, &existing_packs) {
548 char *sha1;
549 size_t len = strlen(item->string);
550 if (len < hexsz)
551 continue;
552 sha1 = item->string + len - hexsz;
553 if (!string_list_has_string(&names, sha1))
554 remove_redundant_pack(packdir, item->string);
555 }
556 if (!po_args.quiet && isatty(2))
557 opts |= PRUNE_PACKED_VERBOSE;
558 prune_packed_objects(opts);
559
560 if (!keep_unreachable &&
561 (!(pack_everything & LOOSEN_UNREACHABLE) ||
562 unpack_unreachable) &&
563 is_repository_shallow(the_repository))
564 prune_shallow(PRUNE_QUICK);
565 }
566
567 if (!no_update_server_info)
568 update_server_info(0);
569 remove_temporary_files();
570
571 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0))
572 write_midx_file(get_object_directory(), 0);
573
574 string_list_clear(&names, 0);
575 string_list_clear(&rollback, 0);
576 string_list_clear(&existing_packs, 0);
577 strbuf_release(&line);
578
579 return 0;
580 }