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
repack_config(const char * var,const char * value,void * cb)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 */
remove_temporary_files(void)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
remove_pack_on_signal(int signo)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 */
collect_pack_filenames(struct string_list * fname_nonkept_list,struct string_list * fname_kept_list,const struct string_list * extra_keep)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
remove_redundant_pack(const char * dir_name,const char * base_name)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
prepare_pack_objects(struct child_process * cmd,const struct pack_objects_args * args)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 */
write_oid(const struct object_id * oid,struct packed_git * pack,uint32_t pos,void * data)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
populate_pack_exts(char * name)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
repack_promisor_objects(const struct pack_objects_args * args,struct string_list * names)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 return;
264
265 close(cmd.in);
266
267 out = xfdopen(cmd.out, "r");
268 while (strbuf_getline_lf(&line, out) != EOF) {
269 struct string_list_item *item;
270 char *promisor_name;
271
272 if (line.len != the_hash_algo->hexsz)
273 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
274 item = string_list_append(names, line.buf);
275
276 /*
277 * pack-objects creates the .pack and .idx files, but not the
278 * .promisor file. Create the .promisor file, which is empty.
279 *
280 * NEEDSWORK: fetch-pack sometimes generates non-empty
281 * .promisor files containing the ref names and associated
282 * hashes at the point of generation of the corresponding
283 * packfile, but this would not preserve their contents. Maybe
284 * concatenate the contents of all .promisor files instead of
285 * just creating a new empty file.
286 */
287 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
288 line.buf);
289 write_promisor_file(promisor_name, NULL, 0);
290
291 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
292
293 free(promisor_name);
294 }
295 fclose(out);
296 if (finish_command(&cmd))
297 die(_("could not finish pack-objects to repack promisor objects"));
298 }
299
300 #define ALL_INTO_ONE 1
301 #define LOOSEN_UNREACHABLE 2
302
303 struct pack_geometry {
304 struct packed_git **pack;
305 uint32_t pack_nr, pack_alloc;
306 uint32_t split;
307 };
308
geometry_pack_weight(struct packed_git * p)309 static uint32_t geometry_pack_weight(struct packed_git *p)
310 {
311 if (open_pack_index(p))
312 die(_("cannot open index for %s"), p->pack_name);
313 return p->num_objects;
314 }
315
geometry_cmp(const void * va,const void * vb)316 static int geometry_cmp(const void *va, const void *vb)
317 {
318 uint32_t aw = geometry_pack_weight(*(struct packed_git **)va),
319 bw = geometry_pack_weight(*(struct packed_git **)vb);
320
321 if (aw < bw)
322 return -1;
323 if (aw > bw)
324 return 1;
325 return 0;
326 }
327
init_pack_geometry(struct pack_geometry ** geometry_p)328 static void init_pack_geometry(struct pack_geometry **geometry_p)
329 {
330 struct packed_git *p;
331 struct pack_geometry *geometry;
332
333 *geometry_p = xcalloc(1, sizeof(struct pack_geometry));
334 geometry = *geometry_p;
335
336 for (p = get_all_packs(the_repository); p; p = p->next) {
337 if (!pack_kept_objects && p->pack_keep)
338 continue;
339
340 ALLOC_GROW(geometry->pack,
341 geometry->pack_nr + 1,
342 geometry->pack_alloc);
343
344 geometry->pack[geometry->pack_nr] = p;
345 geometry->pack_nr++;
346 }
347
348 QSORT(geometry->pack, geometry->pack_nr, geometry_cmp);
349 }
350
split_pack_geometry(struct pack_geometry * geometry,int factor)351 static void split_pack_geometry(struct pack_geometry *geometry, int factor)
352 {
353 uint32_t i;
354 uint32_t split;
355 off_t total_size = 0;
356
357 if (!geometry->pack_nr) {
358 geometry->split = geometry->pack_nr;
359 return;
360 }
361
362 /*
363 * First, count the number of packs (in descending order of size) which
364 * already form a geometric progression.
365 */
366 for (i = geometry->pack_nr - 1; i > 0; i--) {
367 struct packed_git *ours = geometry->pack[i];
368 struct packed_git *prev = geometry->pack[i - 1];
369
370 if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
371 die(_("pack %s too large to consider in geometric "
372 "progression"),
373 prev->pack_name);
374
375 if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
376 break;
377 }
378
379 split = i;
380
381 if (split) {
382 /*
383 * Move the split one to the right, since the top element in the
384 * last-compared pair can't be in the progression. Only do this
385 * when we split in the middle of the array (otherwise if we got
386 * to the end, then the split is in the right place).
387 */
388 split++;
389 }
390
391 /*
392 * Then, anything to the left of 'split' must be in a new pack. But,
393 * creating that new pack may cause packs in the heavy half to no longer
394 * form a geometric progression.
395 *
396 * Compute an expected size of the new pack, and then determine how many
397 * packs in the heavy half need to be joined into it (if any) to restore
398 * the geometric progression.
399 */
400 for (i = 0; i < split; i++) {
401 struct packed_git *p = geometry->pack[i];
402
403 if (unsigned_add_overflows(total_size, geometry_pack_weight(p)))
404 die(_("pack %s too large to roll up"), p->pack_name);
405 total_size += geometry_pack_weight(p);
406 }
407 for (i = split; i < geometry->pack_nr; i++) {
408 struct packed_git *ours = geometry->pack[i];
409
410 if (unsigned_mult_overflows(factor, total_size))
411 die(_("pack %s too large to roll up"), ours->pack_name);
412
413 if (geometry_pack_weight(ours) < factor * total_size) {
414 if (unsigned_add_overflows(total_size,
415 geometry_pack_weight(ours)))
416 die(_("pack %s too large to roll up"),
417 ours->pack_name);
418
419 split++;
420 total_size += geometry_pack_weight(ours);
421 } else
422 break;
423 }
424
425 geometry->split = split;
426 }
427
get_largest_active_pack(struct pack_geometry * geometry)428 static struct packed_git *get_largest_active_pack(struct pack_geometry *geometry)
429 {
430 if (!geometry) {
431 /*
432 * No geometry means either an all-into-one repack (in which
433 * case there is only one pack left and it is the largest) or an
434 * incremental one.
435 *
436 * If repacking incrementally, then we could check the size of
437 * all packs to determine which should be preferred, but leave
438 * this for later.
439 */
440 return NULL;
441 }
442 if (geometry->split == geometry->pack_nr)
443 return NULL;
444 return geometry->pack[geometry->pack_nr - 1];
445 }
446
clear_pack_geometry(struct pack_geometry * geometry)447 static void clear_pack_geometry(struct pack_geometry *geometry)
448 {
449 if (!geometry)
450 return;
451
452 free(geometry->pack);
453 geometry->pack_nr = 0;
454 geometry->pack_alloc = 0;
455 geometry->split = 0;
456 }
457
458 struct midx_snapshot_ref_data {
459 struct tempfile *f;
460 struct oidset seen;
461 int preferred;
462 };
463
midx_snapshot_ref_one(const char * refname,const struct object_id * oid,int flag,void * _data)464 static int midx_snapshot_ref_one(const char *refname,
465 const struct object_id *oid,
466 int flag, void *_data)
467 {
468 struct midx_snapshot_ref_data *data = _data;
469 struct object_id peeled;
470
471 if (!peel_iterated_oid(oid, &peeled))
472 oid = &peeled;
473
474 if (oidset_insert(&data->seen, oid))
475 return 0; /* already seen */
476
477 if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT)
478 return 0;
479
480 fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "",
481 oid_to_hex(oid));
482
483 return 0;
484 }
485
midx_snapshot_refs(struct tempfile * f)486 static void midx_snapshot_refs(struct tempfile *f)
487 {
488 struct midx_snapshot_ref_data data;
489 const struct string_list *preferred = bitmap_preferred_tips(the_repository);
490
491 data.f = f;
492 data.preferred = 0;
493 oidset_init(&data.seen, 0);
494
495 if (!fdopen_tempfile(f, "w"))
496 die(_("could not open tempfile %s for writing"),
497 get_tempfile_path(f));
498
499 if (preferred) {
500 struct string_list_item *item;
501
502 data.preferred = 1;
503 for_each_string_list_item(item, preferred)
504 for_each_ref_in(item->string, midx_snapshot_ref_one, &data);
505 data.preferred = 0;
506 }
507
508 for_each_ref(midx_snapshot_ref_one, &data);
509
510 if (close_tempfile_gently(f)) {
511 int save_errno = errno;
512 delete_tempfile(&f);
513 errno = save_errno;
514 die_errno(_("could not close refs snapshot tempfile"));
515 }
516
517 oidset_clear(&data.seen);
518 }
519
midx_included_packs(struct string_list * include,struct string_list * existing_nonkept_packs,struct string_list * existing_kept_packs,struct string_list * names,struct pack_geometry * geometry)520 static void midx_included_packs(struct string_list *include,
521 struct string_list *existing_nonkept_packs,
522 struct string_list *existing_kept_packs,
523 struct string_list *names,
524 struct pack_geometry *geometry)
525 {
526 struct string_list_item *item;
527
528 for_each_string_list_item(item, existing_kept_packs)
529 string_list_insert(include, xstrfmt("%s.idx", item->string));
530 for_each_string_list_item(item, names)
531 string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
532 if (geometry) {
533 struct strbuf buf = STRBUF_INIT;
534 uint32_t i;
535 for (i = geometry->split; i < geometry->pack_nr; i++) {
536 struct packed_git *p = geometry->pack[i];
537
538 strbuf_addstr(&buf, pack_basename(p));
539 strbuf_strip_suffix(&buf, ".pack");
540 strbuf_addstr(&buf, ".idx");
541
542 string_list_insert(include, strbuf_detach(&buf, NULL));
543 }
544 } else {
545 for_each_string_list_item(item, existing_nonkept_packs) {
546 if (item->util)
547 continue;
548 string_list_insert(include, xstrfmt("%s.idx", item->string));
549 }
550 }
551 }
552
write_midx_included_packs(struct string_list * include,struct pack_geometry * geometry,const char * refs_snapshot,int show_progress,int write_bitmaps)553 static int write_midx_included_packs(struct string_list *include,
554 struct pack_geometry *geometry,
555 const char *refs_snapshot,
556 int show_progress, int write_bitmaps)
557 {
558 struct child_process cmd = CHILD_PROCESS_INIT;
559 struct string_list_item *item;
560 struct packed_git *largest = get_largest_active_pack(geometry);
561 FILE *in;
562 int ret;
563
564 if (!include->nr)
565 return 0;
566
567 cmd.in = -1;
568 cmd.git_cmd = 1;
569
570 strvec_push(&cmd.args, "multi-pack-index");
571 strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL);
572
573 if (show_progress)
574 strvec_push(&cmd.args, "--progress");
575 else
576 strvec_push(&cmd.args, "--no-progress");
577
578 if (write_bitmaps)
579 strvec_push(&cmd.args, "--bitmap");
580
581 if (largest)
582 strvec_pushf(&cmd.args, "--preferred-pack=%s",
583 pack_basename(largest));
584
585 if (refs_snapshot)
586 strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot);
587
588 ret = start_command(&cmd);
589 if (ret)
590 return ret;
591
592 in = xfdopen(cmd.in, "w");
593 for_each_string_list_item(item, include)
594 fprintf(in, "%s\n", item->string);
595 fclose(in);
596
597 return finish_command(&cmd);
598 }
599
cmd_repack(int argc,const char ** argv,const char * prefix)600 int cmd_repack(int argc, const char **argv, const char *prefix)
601 {
602 struct child_process cmd = CHILD_PROCESS_INIT;
603 struct string_list_item *item;
604 struct string_list names = STRING_LIST_INIT_DUP;
605 struct string_list rollback = STRING_LIST_INIT_NODUP;
606 struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
607 struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
608 struct pack_geometry *geometry = NULL;
609 struct strbuf line = STRBUF_INIT;
610 struct tempfile *refs_snapshot = NULL;
611 int i, ext, ret;
612 FILE *out;
613 int show_progress = isatty(2);
614
615 /* variables to be filled by option parsing */
616 int pack_everything = 0;
617 int delete_redundant = 0;
618 const char *unpack_unreachable = NULL;
619 int keep_unreachable = 0;
620 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
621 int no_update_server_info = 0;
622 struct pack_objects_args po_args = {NULL};
623 int geometric_factor = 0;
624 int write_midx = 0;
625
626 struct option builtin_repack_options[] = {
627 OPT_BIT('a', NULL, &pack_everything,
628 N_("pack everything in a single pack"), ALL_INTO_ONE),
629 OPT_BIT('A', NULL, &pack_everything,
630 N_("same as -a, and turn unreachable objects loose"),
631 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
632 OPT_BOOL('d', NULL, &delete_redundant,
633 N_("remove redundant packs, and run git-prune-packed")),
634 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
635 N_("pass --no-reuse-delta to git-pack-objects")),
636 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
637 N_("pass --no-reuse-object to git-pack-objects")),
638 OPT_BOOL('n', NULL, &no_update_server_info,
639 N_("do not run git-update-server-info")),
640 OPT__QUIET(&po_args.quiet, N_("be quiet")),
641 OPT_BOOL('l', "local", &po_args.local,
642 N_("pass --local to git-pack-objects")),
643 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
644 N_("write bitmap index")),
645 OPT_BOOL('i', "delta-islands", &use_delta_islands,
646 N_("pass --delta-islands to git-pack-objects")),
647 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
648 N_("with -A, do not loosen objects older than this")),
649 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
650 N_("with -a, repack unreachable objects")),
651 OPT_STRING(0, "window", &po_args.window, N_("n"),
652 N_("size of the window used for delta compression")),
653 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
654 N_("same as the above, but limit memory size instead of entries count")),
655 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
656 N_("limits the maximum delta depth")),
657 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
658 N_("limits the maximum number of threads")),
659 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
660 N_("maximum size of each packfile")),
661 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
662 N_("repack objects in packs marked with .keep")),
663 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
664 N_("do not repack this pack")),
665 OPT_INTEGER('g', "geometric", &geometric_factor,
666 N_("find a geometric progression with factor <N>")),
667 OPT_BOOL('m', "write-midx", &write_midx,
668 N_("write a multi-pack index of the resulting packs")),
669 OPT_END()
670 };
671
672 git_config(repack_config, NULL);
673
674 argc = parse_options(argc, argv, prefix, builtin_repack_options,
675 git_repack_usage, 0);
676
677 if (delete_redundant && repository_format_precious_objects)
678 die(_("cannot delete packs in a precious-objects repo"));
679
680 if (keep_unreachable &&
681 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
682 die(_("--keep-unreachable and -A are incompatible"));
683
684 if (write_bitmaps < 0) {
685 if (!write_midx &&
686 (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository()))
687 write_bitmaps = 0;
688 } else if (write_bitmaps &&
689 git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) &&
690 git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) {
691 write_bitmaps = 0;
692 }
693 if (pack_kept_objects < 0)
694 pack_kept_objects = write_bitmaps > 0;
695
696 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx)
697 die(_(incremental_bitmap_conflict_error));
698
699 if (write_midx && write_bitmaps) {
700 struct strbuf path = STRBUF_INIT;
701
702 strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(),
703 "bitmap-ref-tips");
704
705 refs_snapshot = xmks_tempfile(path.buf);
706 midx_snapshot_refs(refs_snapshot);
707
708 strbuf_release(&path);
709 }
710
711 if (geometric_factor) {
712 if (pack_everything)
713 die(_("--geometric is incompatible with -A, -a"));
714 init_pack_geometry(&geometry);
715 split_pack_geometry(geometry, geometric_factor);
716 }
717
718 packdir = mkpathdup("%s/pack", get_object_directory());
719 packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid());
720 packtmp = mkpathdup("%s/%s", packdir, packtmp_name);
721
722 sigchain_push_common(remove_pack_on_signal);
723
724 prepare_pack_objects(&cmd, &po_args);
725
726 strvec_push(&cmd.args, "--keep-true-parents");
727 if (!pack_kept_objects)
728 strvec_push(&cmd.args, "--honor-pack-keep");
729 for (i = 0; i < keep_pack_list.nr; i++)
730 strvec_pushf(&cmd.args, "--keep-pack=%s",
731 keep_pack_list.items[i].string);
732 strvec_push(&cmd.args, "--non-empty");
733 if (!geometry) {
734 /*
735 * We need to grab all reachable objects, including those that
736 * are reachable from reflogs and the index.
737 *
738 * When repacking into a geometric progression of packs,
739 * however, we ask 'git pack-objects --stdin-packs', and it is
740 * not about packing objects based on reachability but about
741 * repacking all the objects in specified packs and loose ones
742 * (indeed, --stdin-packs is incompatible with these options).
743 */
744 strvec_push(&cmd.args, "--all");
745 strvec_push(&cmd.args, "--reflog");
746 strvec_push(&cmd.args, "--indexed-objects");
747 }
748 if (has_promisor_remote())
749 strvec_push(&cmd.args, "--exclude-promisor-objects");
750 if (!write_midx) {
751 if (write_bitmaps > 0)
752 strvec_push(&cmd.args, "--write-bitmap-index");
753 else if (write_bitmaps < 0)
754 strvec_push(&cmd.args, "--write-bitmap-index-quiet");
755 }
756 if (use_delta_islands)
757 strvec_push(&cmd.args, "--delta-islands");
758
759 collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
760 &keep_pack_list);
761
762 if (pack_everything & ALL_INTO_ONE) {
763 repack_promisor_objects(&po_args, &names);
764
765 if (existing_nonkept_packs.nr && delete_redundant) {
766 for_each_string_list_item(item, &names) {
767 strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack",
768 packtmp_name, item->string);
769 }
770 if (unpack_unreachable) {
771 strvec_pushf(&cmd.args,
772 "--unpack-unreachable=%s",
773 unpack_unreachable);
774 } else if (pack_everything & LOOSEN_UNREACHABLE) {
775 strvec_push(&cmd.args,
776 "--unpack-unreachable");
777 } else if (keep_unreachable) {
778 strvec_push(&cmd.args, "--keep-unreachable");
779 strvec_push(&cmd.args, "--pack-loose-unreachable");
780 }
781 }
782 } else if (geometry) {
783 strvec_push(&cmd.args, "--stdin-packs");
784 strvec_push(&cmd.args, "--unpacked");
785 } else {
786 strvec_push(&cmd.args, "--unpacked");
787 strvec_push(&cmd.args, "--incremental");
788 }
789
790 if (geometry)
791 cmd.in = -1;
792 else
793 cmd.no_stdin = 1;
794
795 ret = start_command(&cmd);
796 if (ret)
797 return ret;
798
799 if (geometry) {
800 FILE *in = xfdopen(cmd.in, "w");
801 /*
802 * The resulting pack should contain all objects in packs that
803 * are going to be rolled up, but exclude objects in packs which
804 * are being left alone.
805 */
806 for (i = 0; i < geometry->split; i++)
807 fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
808 for (i = geometry->split; i < geometry->pack_nr; i++)
809 fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
810 fclose(in);
811 }
812
813 out = xfdopen(cmd.out, "r");
814 while (strbuf_getline_lf(&line, out) != EOF) {
815 if (line.len != the_hash_algo->hexsz)
816 die(_("repack: Expecting full hex object ID lines only from pack-objects."));
817 string_list_append(&names, line.buf);
818 }
819 fclose(out);
820 ret = finish_command(&cmd);
821 if (ret)
822 return ret;
823
824 if (!names.nr && !po_args.quiet)
825 printf_ln(_("Nothing new to pack."));
826
827 for_each_string_list_item(item, &names) {
828 item->util = (void *)(uintptr_t)populate_pack_exts(item->string);
829 }
830
831 close_object_store(the_repository->objects);
832
833 /*
834 * Ok we have prepared all new packfiles.
835 */
836 for_each_string_list_item(item, &names) {
837 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
838 char *fname, *fname_old;
839
840 fname = mkpathdup("%s/pack-%s%s",
841 packdir, item->string, exts[ext].name);
842 fname_old = mkpathdup("%s-%s%s",
843 packtmp, item->string, exts[ext].name);
844
845 if (((uintptr_t)item->util) & (1 << ext)) {
846 struct stat statbuffer;
847 if (!stat(fname_old, &statbuffer)) {
848 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
849 chmod(fname_old, statbuffer.st_mode);
850 }
851
852 if (rename(fname_old, fname))
853 die_errno(_("renaming '%s' failed"), fname_old);
854 } else if (!exts[ext].optional)
855 die(_("missing required file: %s"), fname_old);
856 else if (unlink(fname) < 0 && errno != ENOENT)
857 die_errno(_("could not unlink: %s"), fname);
858
859 free(fname);
860 free(fname_old);
861 }
862 }
863 /* End of pack replacement. */
864
865 if (delete_redundant && pack_everything & ALL_INTO_ONE) {
866 const int hexsz = the_hash_algo->hexsz;
867 string_list_sort(&names);
868 for_each_string_list_item(item, &existing_nonkept_packs) {
869 char *sha1;
870 size_t len = strlen(item->string);
871 if (len < hexsz)
872 continue;
873 sha1 = item->string + len - hexsz;
874 /*
875 * Mark this pack for deletion, which ensures that this
876 * pack won't be included in a MIDX (if `--write-midx`
877 * was given) and that we will actually delete this pack
878 * (if `-d` was given).
879 */
880 item->util = (void*)(intptr_t)!string_list_has_string(&names, sha1);
881 }
882 }
883
884 if (write_midx) {
885 struct string_list include = STRING_LIST_INIT_NODUP;
886 midx_included_packs(&include, &existing_nonkept_packs,
887 &existing_kept_packs, &names, geometry);
888
889 ret = write_midx_included_packs(&include, geometry,
890 refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
891 show_progress, write_bitmaps > 0);
892
893 string_list_clear(&include, 0);
894
895 if (ret)
896 return ret;
897 }
898
899 reprepare_packed_git(the_repository);
900
901 if (delete_redundant) {
902 int opts = 0;
903 for_each_string_list_item(item, &existing_nonkept_packs) {
904 if (!item->util)
905 continue;
906 remove_redundant_pack(packdir, item->string);
907 }
908
909 if (geometry) {
910 struct strbuf buf = STRBUF_INIT;
911
912 uint32_t i;
913 for (i = 0; i < geometry->split; i++) {
914 struct packed_git *p = geometry->pack[i];
915 if (string_list_has_string(&names,
916 hash_to_hex(p->hash)))
917 continue;
918
919 strbuf_reset(&buf);
920 strbuf_addstr(&buf, pack_basename(p));
921 strbuf_strip_suffix(&buf, ".pack");
922
923 remove_redundant_pack(packdir, buf.buf);
924 }
925 strbuf_release(&buf);
926 }
927 if (!po_args.quiet && show_progress)
928 opts |= PRUNE_PACKED_VERBOSE;
929 prune_packed_objects(opts);
930
931 if (!keep_unreachable &&
932 (!(pack_everything & LOOSEN_UNREACHABLE) ||
933 unpack_unreachable) &&
934 is_repository_shallow(the_repository))
935 prune_shallow(PRUNE_QUICK);
936 }
937
938 if (!no_update_server_info)
939 update_server_info(0);
940 remove_temporary_files();
941
942 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) {
943 unsigned flags = 0;
944 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0))
945 flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX;
946 write_midx_file(get_object_directory(), NULL, NULL, flags);
947 }
948
949 string_list_clear(&names, 0);
950 string_list_clear(&rollback, 0);
951 string_list_clear(&existing_nonkept_packs, 0);
952 string_list_clear(&existing_kept_packs, 0);
953 clear_pack_geometry(geometry);
954 strbuf_release(&line);
955
956 return 0;
957 }
958