]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/repack.c
Merge branch 'en/fetch-negotiation-default-fix'
[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 #include "pack.h"
18 #include "pack-bitmap.h"
19 #include "refs.h"
20
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 char *packdir, *packtmp_name, *packtmp;
26
27 static const char *const git_repack_usage[] = {
28 N_("git repack [<options>]"),
29 NULL
30 };
31
32 static const char incremental_bitmap_conflict_error[] = N_(
33 "Incremental repacks are incompatible with bitmap indexes. Use\n"
34 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
35 );
36
37
38 static int repack_config(const char *var, const char *value, void *cb)
39 {
40 if (!strcmp(var, "repack.usedeltabaseoffset")) {
41 delta_base_offset = git_config_bool(var, value);
42 return 0;
43 }
44 if (!strcmp(var, "repack.packkeptobjects")) {
45 pack_kept_objects = git_config_bool(var, value);
46 return 0;
47 }
48 if (!strcmp(var, "repack.writebitmaps") ||
49 !strcmp(var, "pack.writebitmaps")) {
50 write_bitmaps = git_config_bool(var, value);
51 return 0;
52 }
53 if (!strcmp(var, "repack.usedeltaislands")) {
54 use_delta_islands = git_config_bool(var, value);
55 return 0;
56 }
57 return git_default_config(var, value, cb);
58 }
59
60 /*
61 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
62 */
63 static void remove_temporary_files(void)
64 {
65 struct strbuf buf = STRBUF_INIT;
66 size_t dirlen, prefixlen;
67 DIR *dir;
68 struct dirent *e;
69
70 dir = opendir(packdir);
71 if (!dir)
72 return;
73
74 /* Point at the slash at the end of ".../objects/pack/" */
75 dirlen = strlen(packdir) + 1;
76 strbuf_addstr(&buf, packtmp);
77 /* Hold the length of ".tmp-%d-pack-" */
78 prefixlen = buf.len - dirlen;
79
80 while ((e = readdir(dir))) {
81 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
82 continue;
83 strbuf_setlen(&buf, dirlen);
84 strbuf_addstr(&buf, e->d_name);
85 unlink(buf.buf);
86 }
87 closedir(dir);
88 strbuf_release(&buf);
89 }
90
91 static void remove_pack_on_signal(int signo)
92 {
93 remove_temporary_files();
94 sigchain_pop(signo);
95 raise(signo);
96 }
97
98 /*
99 * Adds all packs hex strings to either fname_nonkept_list or
100 * fname_kept_list based on whether each pack has a corresponding
101 * .keep file or not. Packs without a .keep file are not to be kept
102 * if we are going to pack everything into one file.
103 */
104 static void collect_pack_filenames(struct string_list *fname_nonkept_list,
105 struct string_list *fname_kept_list,
106 const struct string_list *extra_keep)
107 {
108 DIR *dir;
109 struct dirent *e;
110 char *fname;
111
112 if (!(dir = opendir(packdir)))
113 return;
114
115 while ((e = readdir(dir)) != NULL) {
116 size_t len;
117 int i;
118
119 if (!strip_suffix(e->d_name, ".pack", &len))
120 continue;
121
122 for (i = 0; i < extra_keep->nr; i++)
123 if (!fspathcmp(e->d_name, extra_keep->items[i].string))
124 break;
125
126 fname = xmemdupz(e->d_name, len);
127
128 if ((extra_keep->nr > 0 && i < extra_keep->nr) ||
129 (file_exists(mkpath("%s/%s.keep", packdir, fname))))
130 string_list_append_nodup(fname_kept_list, fname);
131 else
132 string_list_append_nodup(fname_nonkept_list, fname);
133 }
134 closedir(dir);
135 }
136
137 static void remove_redundant_pack(const char *dir_name, const char *base_name)
138 {
139 struct strbuf buf = STRBUF_INIT;
140 struct multi_pack_index *m = get_local_multi_pack_index(the_repository);
141 strbuf_addf(&buf, "%s.pack", base_name);
142 if (m && midx_contains_pack(m, buf.buf))
143 clear_midx_file(the_repository);
144 strbuf_insertf(&buf, 0, "%s/", dir_name);
145 unlink_pack_path(buf.buf, 1);
146 strbuf_release(&buf);
147 }
148
149 struct pack_objects_args {
150 const char *window;
151 const char *window_memory;
152 const char *depth;
153 const char *threads;
154 const char *max_pack_size;
155 int no_reuse_delta;
156 int no_reuse_object;
157 int quiet;
158 int local;
159 };
160
161 static void prepare_pack_objects(struct child_process *cmd,
162 const struct pack_objects_args *args)
163 {
164 strvec_push(&cmd->args, "pack-objects");
165 if (args->window)
166 strvec_pushf(&cmd->args, "--window=%s", args->window);
167 if (args->window_memory)
168 strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
169 if (args->depth)
170 strvec_pushf(&cmd->args, "--depth=%s", args->depth);
171 if (args->threads)
172 strvec_pushf(&cmd->args, "--threads=%s", args->threads);
173 if (args->max_pack_size)
174 strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
175 if (args->no_reuse_delta)
176 strvec_pushf(&cmd->args, "--no-reuse-delta");
177 if (args->no_reuse_object)
178 strvec_pushf(&cmd->args, "--no-reuse-object");
179 if (args->local)
180 strvec_push(&cmd->args, "--local");
181 if (args->quiet)
182 strvec_push(&cmd->args, "--quiet");
183 if (delta_base_offset)
184 strvec_push(&cmd->args, "--delta-base-offset");
185 strvec_push(&cmd->args, packtmp);
186 cmd->git_cmd = 1;
187 cmd->out = -1;
188 }
189
190 /*
191 * Write oid to the given struct child_process's stdin, starting it first if
192 * necessary.
193 */
194 static int write_oid(const struct object_id *oid, struct packed_git *pack,
195 uint32_t pos, void *data)
196 {
197 struct child_process *cmd = data;
198
199 if (cmd->in == -1) {
200 if (start_command(cmd))
201 die(_("could not start pack-objects to repack promisor objects"));
202 }
203
204 xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
205 xwrite(cmd->in, "\n", 1);
206 return 0;
207 }
208
209 static struct {
210 const char *name;
211 unsigned optional:1;
212 } exts[] = {
213 {".pack"},
214 {".rev", 1},
215 {".bitmap", 1},
216 {".promisor", 1},
217 {".idx"},
218 };
219
220 static unsigned populate_pack_exts(char *name)
221 {
222 struct stat statbuf;
223 struct strbuf path = STRBUF_INIT;
224 unsigned ret = 0;
225 int i;
226
227 for (i = 0; i < ARRAY_SIZE(exts); i++) {
228 strbuf_reset(&path);
229 strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name);
230
231 if (stat(path.buf, &statbuf))
232 continue;
233
234 ret |= (1 << i);
235 }
236
237 strbuf_release(&path);
238 return ret;
239 }
240
241 static void repack_promisor_objects(const struct pack_objects_args *args,
242 struct string_list *names)
243 {
244 struct child_process cmd = CHILD_PROCESS_INIT;
245 FILE *out;
246 struct strbuf line = STRBUF_INIT;
247
248 prepare_pack_objects(&cmd, args);
249 cmd.in = -1;
250
251 /*
252 * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
253 * hints may result in suboptimal deltas in the resulting pack. See if
254 * the OIDs can be sent with fake paths such that pack-objects can use a
255 * {type -> existing pack order} ordering when computing deltas instead
256 * of a {type -> size} ordering, which may produce better deltas.
257 */
258 for_each_packed_object(write_oid, &cmd,
259 FOR_EACH_OBJECT_PROMISOR_ONLY);
260
261 if (cmd.in == -1) {
262 /* No packed objects; cmd was never started */
263 child_process_clear(&cmd);
264 return;
265 }
266
267 close(cmd.in);
268
269 out = xfdopen(cmd.out, "r");
270 while (strbuf_getline_lf(&line, out) != EOF) {
271 struct string_list_item *item;
272 char *promisor_name;
273
274 if (line.len != the_hash_algo->hexsz)
275 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
276 item = string_list_append(names, line.buf);
277
278 /*
279 * pack-objects creates the .pack and .idx files, but not the
280 * .promisor file. Create the .promisor file, which is empty.
281 *
282 * NEEDSWORK: fetch-pack sometimes generates non-empty
283 * .promisor files containing the ref names and associated
284 * hashes at the point of generation of the corresponding
285 * packfile, but this would not preserve their contents. Maybe
286 * concatenate the contents of all .promisor files instead of
287 * just creating a new empty file.
288 */
289 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
290 line.buf);
291 write_promisor_file(promisor_name, NULL, 0);
292
293 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
294
295 free(promisor_name);
296 }
297 fclose(out);
298 if (finish_command(&cmd))
299 die(_("could not finish pack-objects to repack promisor objects"));
300 }
301
302 #define ALL_INTO_ONE 1
303 #define LOOSEN_UNREACHABLE 2
304
305 struct pack_geometry {
306 struct packed_git **pack;
307 uint32_t pack_nr, pack_alloc;
308 uint32_t split;
309 };
310
311 static uint32_t geometry_pack_weight(struct packed_git *p)
312 {
313 if (open_pack_index(p))
314 die(_("cannot open index for %s"), p->pack_name);
315 return p->num_objects;
316 }
317
318 static int geometry_cmp(const void *va, const void *vb)
319 {
320 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
321 bw = geometry_pack_weight(*(struct packed_git **)vb);
322
323 if (aw < bw)
324 return -1;
325 if (aw > bw)
326 return 1;
327 return 0;
328 }
329
330 static void init_pack_geometry(struct pack_geometry **geometry_p)
331 {
332 struct packed_git *p;
333 struct pack_geometry *geometry;
334
335 *geometry_p = xcalloc(1, sizeof(struct pack_geometry));
336 geometry = *geometry_p;
337
338 for (p = get_all_packs(the_repository); p; p = p->next) {
339 if (!pack_kept_objects && p->pack_keep)
340 continue;
341
342 ALLOC_GROW(geometry->pack,
343 geometry->pack_nr + 1,
344 geometry->pack_alloc);
345
346 geometry->pack[geometry->pack_nr] = p;
347 geometry->pack_nr++;
348 }
349
350 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
351 }
352
353 static void split_pack_geometry(struct pack_geometry *geometry, int factor)
354 {
355 uint32_t i;
356 uint32_t split;
357 off_t total_size = 0;
358
359 if (!geometry->pack_nr) {
360 geometry->split = geometry->pack_nr;
361 return;
362 }
363
364 /*
365 * First, count the number of packs (in descending order of size) which
366 * already form a geometric progression.
367 */
368 for (i = geometry->pack_nr - 1; i > 0; i--) {
369 struct packed_git *ours = geometry->pack[i];
370 struct packed_git *prev = geometry->pack[i - 1];
371
372 if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
373 die(_("pack %s too large to consider in geometric "
374 "progression"),
375 prev->pack_name);
376
377 if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
378 break;
379 }
380
381 split = i;
382
383 if (split) {
384 /*
385 * Move the split one to the right, since the top element in the
386 * last-compared pair can't be in the progression. Only do this
387 * when we split in the middle of the array (otherwise if we got
388 * to the end, then the split is in the right place).
389 */
390 split++;
391 }
392
393 /*
394 * Then, anything to the left of 'split' must be in a new pack. But,
395 * creating that new pack may cause packs in the heavy half to no longer
396 * form a geometric progression.
397 *
398 * Compute an expected size of the new pack, and then determine how many
399 * packs in the heavy half need to be joined into it (if any) to restore
400 * the geometric progression.
401 */
402 for (i = 0; i < split; i++) {
403 struct packed_git *p = geometry->pack[i];
404
405 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
406 die(_("pack %s too large to roll up"), p->pack_name);
407 total_size += geometry_pack_weight(p);
408 }
409 for (i = split; i < geometry->pack_nr; i++) {
410 struct packed_git *ours = geometry->pack[i];
411
412 if (unsigned_mult_overflows(factor, total_size))
413 die(_("pack %s too large to roll up"), ours->pack_name);
414
415 if (geometry_pack_weight(ours) < factor * total_size) {
416 if (unsigned_add_overflows(total_size,
417 geometry_pack_weight(ours)))
418 die(_("pack %s too large to roll up"),
419 ours->pack_name);
420
421 split++;
422 total_size += geometry_pack_weight(ours);
423 } else
424 break;
425 }
426
427 geometry->split = split;
428 }
429
430 static struct packed_git *get_largest_active_pack(struct pack_geometry *geometry)
431 {
432 if (!geometry) {
433 /*
434 * No geometry means either an all-into-one repack (in which
435 * case there is only one pack left and it is the largest) or an
436 * incremental one.
437 *
438 * If repacking incrementally, then we could check the size of
439 * all packs to determine which should be preferred, but leave
440 * this for later.
441 */
442 return NULL;
443 }
444 if (geometry->split == geometry->pack_nr)
445 return NULL;
446 return geometry->pack[geometry->pack_nr - 1];
447 }
448
449 static void clear_pack_geometry(struct pack_geometry *geometry)
450 {
451 if (!geometry)
452 return;
453
454 free(geometry->pack);
455 geometry->pack_nr = 0;
456 geometry->pack_alloc = 0;
457 geometry->split = 0;
458 }
459
460 struct midx_snapshot_ref_data {
461 struct tempfile *f;
462 struct oidset seen;
463 int preferred;
464 };
465
466 static int midx_snapshot_ref_one(const char *refname,
467 const struct object_id *oid,
468 int flag, void *_data)
469 {
470 struct midx_snapshot_ref_data *data = _data;
471 struct object_id peeled;
472
473 if (!peel_iterated_oid(oid, &peeled))
474 oid = &peeled;
475
476 if (oidset_insert(&data->seen, oid))
477 return 0; /* already seen */
478
479 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
480 return 0;
481
482 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
483 oid_to_hex(oid));
484
485 return 0;
486 }
487
488 static void midx_snapshot_refs(struct tempfile *f)
489 {
490 struct midx_snapshot_ref_data data;
491 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
492
493 data.f = f;
494 data.preferred = 0;
495 oidset_init(&data.seen, 0);
496
497 if (!fdopen_tempfile(f, "w"))
498 die(_("could not open tempfile %s for writing"),
499 get_tempfile_path(f));
500
501 if (preferred) {
502 struct string_list_item *item;
503
504 data.preferred = 1;
505 for_each_string_list_item(item, preferred)
506 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
507 data.preferred = 0;
508 }
509
510 for_each_ref(midx_snapshot_ref_one, &data);
511
512 if (close_tempfile_gently(f)) {
513 int save_errno = errno;
514 delete_tempfile(&f);
515 errno = save_errno;
516 die_errno(_("could not close refs snapshot tempfile"));
517 }
518
519 oidset_clear(&data.seen);
520 }
521
522 static void midx_included_packs(struct string_list *include,
523 struct string_list *existing_nonkept_packs,
524 struct string_list *existing_kept_packs,
525 struct string_list *names,
526 struct pack_geometry *geometry)
527 {
528 struct string_list_item *item;
529
530 for_each_string_list_item(item, existing_kept_packs)
531 string_list_insert(include, xstrfmt("%s.idx", item->string));
532 for_each_string_list_item(item, names)
533 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
534 if (geometry) {
535 struct strbuf buf = STRBUF_INIT;
536 uint32_t i;
537 for (i = geometry->split; i < geometry->pack_nr; i++) {
538 struct packed_git *p = geometry->pack[i];
539
540 strbuf_addstr(&buf, pack_basename(p));
541 strbuf_strip_suffix(&buf, ".pack");
542 strbuf_addstr(&buf, ".idx");
543
544 string_list_insert(include, strbuf_detach(&buf, NULL));
545 }
546 } else {
547 for_each_string_list_item(item, existing_nonkept_packs) {
548 if (item->util)
549 continue;
550 string_list_insert(include, xstrfmt("%s.idx", item->string));
551 }
552 }
553 }
554
555 static int write_midx_included_packs(struct string_list *include,
556 struct pack_geometry *geometry,
557 const char *refs_snapshot,
558 int show_progress, int write_bitmaps)
559 {
560 struct child_process cmd = CHILD_PROCESS_INIT;
561 struct string_list_item *item;
562 struct packed_git *largest = get_largest_active_pack(geometry);
563 FILE *in;
564 int ret;
565
566 if (!include->nr)
567 return 0;
568
569 cmd.in = -1;
570 cmd.git_cmd = 1;
571
572 strvec_push(&cmd.args, "multi-pack-index");
573 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
574
575 if (show_progress)
576 strvec_push(&cmd.args, "--progress");
577 else
578 strvec_push(&cmd.args, "--no-progress");
579
580 if (write_bitmaps)
581 strvec_push(&cmd.args, "--bitmap");
582
583 if (largest)
584 strvec_pushf(&cmd.args, "--preferred-pack=%s",
585 pack_basename(largest));
586
587 if (refs_snapshot)
588 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
589
590 ret = start_command(&cmd);
591 if (ret)
592 return ret;
593
594 in = xfdopen(cmd.in, "w");
595 for_each_string_list_item(item, include)
596 fprintf(in, "%s\n", item->string);
597 fclose(in);
598
599 return finish_command(&cmd);
600 }
601
602 int cmd_repack(int argc, const char **argv, const char *prefix)
603 {
604 struct child_process cmd = CHILD_PROCESS_INIT;
605 struct string_list_item *item;
606 struct string_list names = STRING_LIST_INIT_DUP;
607 struct string_list rollback = STRING_LIST_INIT_NODUP;
608 struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
609 struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
610 struct pack_geometry *geometry = NULL;
611 struct strbuf line = STRBUF_INIT;
612 struct tempfile *refs_snapshot = NULL;
613 int i, ext, ret;
614 FILE *out;
615 int show_progress;
616
617 /* variables to be filled by option parsing */
618 int pack_everything = 0;
619 int delete_redundant = 0;
620 const char *unpack_unreachable = NULL;
621 int keep_unreachable = 0;
622 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
623 int no_update_server_info = 0;
624 struct pack_objects_args po_args = {NULL};
625 int geometric_factor = 0;
626 int write_midx = 0;
627
628 struct option builtin_repack_options[] = {
629 OPT_BIT('a', NULL, &pack_everything,
630 N_("pack everything in a single pack"), ALL_INTO_ONE),
631 OPT_BIT('A', NULL, &pack_everything,
632 N_("same as -a, and turn unreachable objects loose"),
633 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
634 OPT_BOOL('d', NULL, &delete_redundant,
635 N_("remove redundant packs, and run git-prune-packed")),
636 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
637 N_("pass --no-reuse-delta to git-pack-objects")),
638 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
639 N_("pass --no-reuse-object to git-pack-objects")),
640 OPT_BOOL('n', NULL, &no_update_server_info,
641 N_("do not run git-update-server-info")),
642 OPT__QUIET(&po_args.quiet, N_("be quiet")),
643 OPT_BOOL('l', "local", &po_args.local,
644 N_("pass --local to git-pack-objects")),
645 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
646 N_("write bitmap index")),
647 OPT_BOOL('i', "delta-islands", &use_delta_islands,
648 N_("pass --delta-islands to git-pack-objects")),
649 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
650 N_("with -A, do not loosen objects older than this")),
651 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
652 N_("with -a, repack unreachable objects")),
653 OPT_STRING(0, "window", &po_args.window, N_("n"),
654 N_("size of the window used for delta compression")),
655 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
656 N_("same as the above, but limit memory size instead of entries count")),
657 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
658 N_("limits the maximum delta depth")),
659 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
660 N_("limits the maximum number of threads")),
661 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
662 N_("maximum size of each packfile")),
663 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
664 N_("repack objects in packs marked with .keep")),
665 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
666 N_("do not repack this pack")),
667 OPT_INTEGER('g', "geometric", &geometric_factor,
668 N_("find a geometric progression with factor <N>")),
669 OPT_BOOL('m', "write-midx", &write_midx,
670 N_("write a multi-pack index of the resulting packs")),
671 OPT_END()
672 };
673
674 git_config(repack_config, NULL);
675
676 argc = parse_options(argc, argv, prefix, builtin_repack_options,
677 git_repack_usage, 0);
678
679 if (delete_redundant && repository_format_precious_objects)
680 die(_("cannot delete packs in a precious-objects repo"));
681
682 if (keep_unreachable &&
683 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
684 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A");
685
686 if (write_bitmaps < 0) {
687 if (!write_midx &&
688 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
689 write_bitmaps = 0;
690 } else if (write_bitmaps &&
691 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
692 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
693 write_bitmaps = 0;
694 }
695 if (pack_kept_objects < 0)
696 pack_kept_objects = write_bitmaps > 0 && !write_midx;
697
698 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
699 die(_(incremental_bitmap_conflict_error));
700
701 if (write_midx && write_bitmaps) {
702 struct strbuf path = STRBUF_INIT;
703
704 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
705 "bitmap-ref-tips");
706
707 refs_snapshot = xmks_tempfile(path.buf);
708 midx_snapshot_refs(refs_snapshot);
709
710 strbuf_release(&path);
711 }
712
713 if (geometric_factor) {
714 if (pack_everything)
715 die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
716 init_pack_geometry(&geometry);
717 split_pack_geometry(geometry, geometric_factor);
718 }
719
720 packdir = mkpathdup("%s/pack", get_object_directory());
721 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
722 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
723
724 sigchain_push_common(remove_pack_on_signal);
725
726 prepare_pack_objects(&cmd, &po_args);
727
728 show_progress = !po_args.quiet && isatty(2);
729
730 strvec_push(&cmd.args, "--keep-true-parents");
731 if (!pack_kept_objects)
732 strvec_push(&cmd.args, "--honor-pack-keep");
733 for (i = 0; i < keep_pack_list.nr; i++)
734 strvec_pushf(&cmd.args, "--keep-pack=%s",
735 keep_pack_list.items[i].string);
736 strvec_push(&cmd.args, "--non-empty");
737 if (!geometry) {
738 /*
739 * We need to grab all reachable objects, including those that
740 * are reachable from reflogs and the index.
741 *
742 * When repacking into a geometric progression of packs,
743 * however, we ask 'git pack-objects --stdin-packs', and it is
744 * not about packing objects based on reachability but about
745 * repacking all the objects in specified packs and loose ones
746 * (indeed, --stdin-packs is incompatible with these options).
747 */
748 strvec_push(&cmd.args, "--all");
749 strvec_push(&cmd.args, "--reflog");
750 strvec_push(&cmd.args, "--indexed-objects");
751 }
752 if (has_promisor_remote())
753 strvec_push(&cmd.args, "--exclude-promisor-objects");
754 if (!write_midx) {
755 if (write_bitmaps > 0)
756 strvec_push(&cmd.args, "--write-bitmap-index");
757 else if (write_bitmaps < 0)
758 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
759 }
760 if (use_delta_islands)
761 strvec_push(&cmd.args, "--delta-islands");
762
763 collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
764 &keep_pack_list);
765
766 if (pack_everything & ALL_INTO_ONE) {
767 repack_promisor_objects(&po_args, &names);
768
769 if (existing_nonkept_packs.nr && delete_redundant) {
770 for_each_string_list_item(item, &names) {
771 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
772 packtmp_name, item->string);
773 }
774 if (unpack_unreachable) {
775 strvec_pushf(&cmd.args,
776 "--unpack-unreachable=%s",
777 unpack_unreachable);
778 } else if (pack_everything & LOOSEN_UNREACHABLE) {
779 strvec_push(&cmd.args,
780 "--unpack-unreachable");
781 } else if (keep_unreachable) {
782 strvec_push(&cmd.args, "--keep-unreachable");
783 strvec_push(&cmd.args, "--pack-loose-unreachable");
784 }
785 }
786 } else if (geometry) {
787 strvec_push(&cmd.args, "--stdin-packs");
788 strvec_push(&cmd.args, "--unpacked");
789 } else {
790 strvec_push(&cmd.args, "--unpacked");
791 strvec_push(&cmd.args, "--incremental");
792 }
793
794 if (geometry)
795 cmd.in = -1;
796 else
797 cmd.no_stdin = 1;
798
799 ret = start_command(&cmd);
800 if (ret)
801 return ret;
802
803 if (geometry) {
804 FILE *in = xfdopen(cmd.in, "w");
805 /*
806 * The resulting pack should contain all objects in packs that
807 * are going to be rolled up, but exclude objects in packs which
808 * are being left alone.
809 */
810 for (i = 0; i < geometry->split; i++)
811 fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
812 for (i = geometry->split; i < geometry->pack_nr; i++)
813 fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
814 fclose(in);
815 }
816
817 out = xfdopen(cmd.out, "r");
818 while (strbuf_getline_lf(&line, out) != EOF) {
819 if (line.len != the_hash_algo->hexsz)
820 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
821 string_list_append(&names, line.buf);
822 }
823 fclose(out);
824 ret = finish_command(&cmd);
825 if (ret)
826 return ret;
827
828 if (!names.nr && !po_args.quiet)
829 printf_ln(_("Nothing new to pack."));
830
831 for_each_string_list_item(item, &names) {
832 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
833 }
834
835 close_object_store(the_repository->objects);
836
837 /*
838 * Ok we have prepared all new packfiles.
839 */
840 for_each_string_list_item(item, &names) {
841 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
842 char *fname, *fname_old;
843
844 fname = mkpathdup("%s/pack-%s%s",
845 packdir, item->string, exts[ext].name);
846 fname_old = mkpathdup("%s-%s%s",
847 packtmp, item->string, exts[ext].name);
848
849 if (((uintptr_t)item->util) & ((uintptr_t)1 << ext)) {
850 struct stat statbuffer;
851 if (!stat(fname_old, &statbuffer)) {
852 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
853 chmod(fname_old, statbuffer.st_mode);
854 }
855
856 if (rename(fname_old, fname))
857 die_errno(_("renaming '%s' failed"), fname_old);
858 } else if (!exts[ext].optional)
859 die(_("missing required file: %s"), fname_old);
860 else if (unlink(fname) < 0 && errno != ENOENT)
861 die_errno(_("could not unlink: %s"), fname);
862
863 free(fname);
864 free(fname_old);
865 }
866 }
867 /* End of pack replacement. */
868
869 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
870 const int hexsz = the_hash_algo->hexsz;
871 string_list_sort(&names);
872 for_each_string_list_item(item, &existing_nonkept_packs) {
873 char *sha1;
874 size_t len = strlen(item->string);
875 if (len < hexsz)
876 continue;
877 sha1 = item->string + len - hexsz;
878 /*
879 * Mark this pack for deletion, which ensures that this
880 * pack won't be included in a MIDX (if `--write-midx`
881 * was given) and that we will actually delete this pack
882 * (if `-d` was given).
883 */
884 item->util = (void*)(intptr_t)!string_list_has_string(&names, sha1);
885 }
886 }
887
888 if (write_midx) {
889 struct string_list include = STRING_LIST_INIT_NODUP;
890 midx_included_packs(&include, &existing_nonkept_packs,
891 &existing_kept_packs, &names, geometry);
892
893 ret = write_midx_included_packs(&include, geometry,
894 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
895 show_progress, write_bitmaps > 0);
896
897 string_list_clear(&include, 0);
898
899 if (ret)
900 return ret;
901 }
902
903 reprepare_packed_git(the_repository);
904
905 if (delete_redundant) {
906 int opts = 0;
907 for_each_string_list_item(item, &existing_nonkept_packs) {
908 if (!item->util)
909 continue;
910 remove_redundant_pack(packdir, item->string);
911 }
912
913 if (geometry) {
914 struct strbuf buf = STRBUF_INIT;
915
916 uint32_t i;
917 for (i = 0; i < geometry->split; i++) {
918 struct packed_git *p = geometry->pack[i];
919 if (string_list_has_string(&names,
920 hash_to_hex(p->hash)))
921 continue;
922
923 strbuf_reset(&buf);
924 strbuf_addstr(&buf, pack_basename(p));
925 strbuf_strip_suffix(&buf, ".pack");
926
927 remove_redundant_pack(packdir, buf.buf);
928 }
929 strbuf_release(&buf);
930 }
931 if (show_progress)
932 opts |= PRUNE_PACKED_VERBOSE;
933 prune_packed_objects(opts);
934
935 if (!keep_unreachable &&
936 (!(pack_everything & LOOSEN_UNREACHABLE) ||
937 unpack_unreachable) &&
938 is_repository_shallow(the_repository))
939 prune_shallow(PRUNE_QUICK);
940 }
941
942 if (!no_update_server_info)
943 update_server_info(0);
944 remove_temporary_files();
945
946 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
947 unsigned flags = 0;
948 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
949 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
950 write_midx_file(get_object_directory(), NULL, NULL, flags);
951 }
952
953 string_list_clear(&names, 0);
954 string_list_clear(&rollback, 0);
955 string_list_clear(&existing_nonkept_packs, 0);
956 string_list_clear(&existing_kept_packs, 0);
957 clear_pack_geometry(geometry);
958 strbuf_release(&line);
959
960 return 0;
961 }