]>
Commit | Line | Data |
---|---|---|
1 | #define USE_THE_REPOSITORY_VARIABLE | |
2 | #define DISABLE_SIGN_COMPARE_WARNINGS | |
3 | ||
4 | #include "builtin.h" | |
5 | #include "config.h" | |
6 | #include "dir.h" | |
7 | #include "environment.h" | |
8 | #include "gettext.h" | |
9 | #include "hex.h" | |
10 | #include "parse-options.h" | |
11 | #include "path.h" | |
12 | #include "run-command.h" | |
13 | #include "server-info.h" | |
14 | #include "strbuf.h" | |
15 | #include "string-list.h" | |
16 | #include "strvec.h" | |
17 | #include "midx.h" | |
18 | #include "packfile.h" | |
19 | #include "prune-packed.h" | |
20 | #include "object-store.h" | |
21 | #include "promisor-remote.h" | |
22 | #include "shallow.h" | |
23 | #include "pack.h" | |
24 | #include "pack-bitmap.h" | |
25 | #include "refs.h" | |
26 | #include "list-objects-filter-options.h" | |
27 | ||
28 | #define ALL_INTO_ONE 1 | |
29 | #define LOOSEN_UNREACHABLE 2 | |
30 | #define PACK_CRUFT 4 | |
31 | ||
32 | #define DELETE_PACK 1 | |
33 | #define RETAIN_PACK 2 | |
34 | ||
35 | static int pack_everything; | |
36 | static int delta_base_offset = 1; | |
37 | static int pack_kept_objects = -1; | |
38 | static int write_bitmaps = -1; | |
39 | static int use_delta_islands; | |
40 | static int run_update_server_info = 1; | |
41 | static char *packdir, *packtmp_name, *packtmp; | |
42 | ||
43 | static const char *const git_repack_usage[] = { | |
44 | N_("git repack [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [-m]\n" | |
45 | "[--window=<n>] [--depth=<n>] [--threads=<n>] [--keep-pack=<pack-name>]\n" | |
46 | "[--write-midx] [--name-hash-version=<n>] [--path-walk]"), | |
47 | NULL | |
48 | }; | |
49 | ||
50 | static const char incremental_bitmap_conflict_error[] = N_( | |
51 | "Incremental repacks are incompatible with bitmap indexes. Use\n" | |
52 | "--no-write-bitmap-index or disable the pack.writeBitmaps configuration." | |
53 | ); | |
54 | ||
55 | struct pack_objects_args { | |
56 | char *window; | |
57 | char *window_memory; | |
58 | char *depth; | |
59 | char *threads; | |
60 | unsigned long max_pack_size; | |
61 | int no_reuse_delta; | |
62 | int no_reuse_object; | |
63 | int quiet; | |
64 | int local; | |
65 | int name_hash_version; | |
66 | int path_walk; | |
67 | struct list_objects_filter_options filter_options; | |
68 | }; | |
69 | ||
70 | static int repack_config(const char *var, const char *value, | |
71 | const struct config_context *ctx, void *cb) | |
72 | { | |
73 | struct pack_objects_args *cruft_po_args = cb; | |
74 | if (!strcmp(var, "repack.usedeltabaseoffset")) { | |
75 | delta_base_offset = git_config_bool(var, value); | |
76 | return 0; | |
77 | } | |
78 | if (!strcmp(var, "repack.packkeptobjects")) { | |
79 | pack_kept_objects = git_config_bool(var, value); | |
80 | return 0; | |
81 | } | |
82 | if (!strcmp(var, "repack.writebitmaps") || | |
83 | !strcmp(var, "pack.writebitmaps")) { | |
84 | write_bitmaps = git_config_bool(var, value); | |
85 | return 0; | |
86 | } | |
87 | if (!strcmp(var, "repack.usedeltaislands")) { | |
88 | use_delta_islands = git_config_bool(var, value); | |
89 | return 0; | |
90 | } | |
91 | if (strcmp(var, "repack.updateserverinfo") == 0) { | |
92 | run_update_server_info = git_config_bool(var, value); | |
93 | return 0; | |
94 | } | |
95 | if (!strcmp(var, "repack.cruftwindow")) { | |
96 | free(cruft_po_args->window); | |
97 | return git_config_string(&cruft_po_args->window, var, value); | |
98 | } | |
99 | if (!strcmp(var, "repack.cruftwindowmemory")) { | |
100 | free(cruft_po_args->window_memory); | |
101 | return git_config_string(&cruft_po_args->window_memory, var, value); | |
102 | } | |
103 | if (!strcmp(var, "repack.cruftdepth")) { | |
104 | free(cruft_po_args->depth); | |
105 | return git_config_string(&cruft_po_args->depth, var, value); | |
106 | } | |
107 | if (!strcmp(var, "repack.cruftthreads")) { | |
108 | free(cruft_po_args->threads); | |
109 | return git_config_string(&cruft_po_args->threads, var, value); | |
110 | } | |
111 | return git_default_config(var, value, ctx, cb); | |
112 | } | |
113 | ||
114 | static void pack_objects_args_release(struct pack_objects_args *args) | |
115 | { | |
116 | free(args->window); | |
117 | free(args->window_memory); | |
118 | free(args->depth); | |
119 | free(args->threads); | |
120 | list_objects_filter_release(&args->filter_options); | |
121 | } | |
122 | ||
123 | struct existing_packs { | |
124 | struct string_list kept_packs; | |
125 | struct string_list non_kept_packs; | |
126 | struct string_list cruft_packs; | |
127 | }; | |
128 | ||
129 | #define EXISTING_PACKS_INIT { \ | |
130 | .kept_packs = STRING_LIST_INIT_DUP, \ | |
131 | .non_kept_packs = STRING_LIST_INIT_DUP, \ | |
132 | .cruft_packs = STRING_LIST_INIT_DUP, \ | |
133 | } | |
134 | ||
135 | static int has_existing_non_kept_packs(const struct existing_packs *existing) | |
136 | { | |
137 | return existing->non_kept_packs.nr || existing->cruft_packs.nr; | |
138 | } | |
139 | ||
140 | static void pack_mark_for_deletion(struct string_list_item *item) | |
141 | { | |
142 | item->util = (void*)((uintptr_t)item->util | DELETE_PACK); | |
143 | } | |
144 | ||
145 | static void pack_unmark_for_deletion(struct string_list_item *item) | |
146 | { | |
147 | item->util = (void*)((uintptr_t)item->util & ~DELETE_PACK); | |
148 | } | |
149 | ||
150 | static int pack_is_marked_for_deletion(struct string_list_item *item) | |
151 | { | |
152 | return (uintptr_t)item->util & DELETE_PACK; | |
153 | } | |
154 | ||
155 | static void pack_mark_retained(struct string_list_item *item) | |
156 | { | |
157 | item->util = (void*)((uintptr_t)item->util | RETAIN_PACK); | |
158 | } | |
159 | ||
160 | static int pack_is_retained(struct string_list_item *item) | |
161 | { | |
162 | return (uintptr_t)item->util & RETAIN_PACK; | |
163 | } | |
164 | ||
165 | static void mark_packs_for_deletion_1(struct string_list *names, | |
166 | struct string_list *list) | |
167 | { | |
168 | struct string_list_item *item; | |
169 | const int hexsz = the_hash_algo->hexsz; | |
170 | ||
171 | for_each_string_list_item(item, list) { | |
172 | char *sha1; | |
173 | size_t len = strlen(item->string); | |
174 | if (len < hexsz) | |
175 | continue; | |
176 | sha1 = item->string + len - hexsz; | |
177 | ||
178 | if (pack_is_retained(item)) { | |
179 | pack_unmark_for_deletion(item); | |
180 | } else if (!string_list_has_string(names, sha1)) { | |
181 | /* | |
182 | * Mark this pack for deletion, which ensures | |
183 | * that this pack won't be included in a MIDX | |
184 | * (if `--write-midx` was given) and that we | |
185 | * will actually delete this pack (if `-d` was | |
186 | * given). | |
187 | */ | |
188 | pack_mark_for_deletion(item); | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | static void retain_cruft_pack(struct existing_packs *existing, | |
194 | struct packed_git *cruft) | |
195 | { | |
196 | struct strbuf buf = STRBUF_INIT; | |
197 | struct string_list_item *item; | |
198 | ||
199 | strbuf_addstr(&buf, pack_basename(cruft)); | |
200 | strbuf_strip_suffix(&buf, ".pack"); | |
201 | ||
202 | item = string_list_lookup(&existing->cruft_packs, buf.buf); | |
203 | if (!item) | |
204 | BUG("could not find cruft pack '%s'", pack_basename(cruft)); | |
205 | ||
206 | pack_mark_retained(item); | |
207 | strbuf_release(&buf); | |
208 | } | |
209 | ||
210 | static void mark_packs_for_deletion(struct existing_packs *existing, | |
211 | struct string_list *names) | |
212 | ||
213 | { | |
214 | mark_packs_for_deletion_1(names, &existing->non_kept_packs); | |
215 | mark_packs_for_deletion_1(names, &existing->cruft_packs); | |
216 | } | |
217 | ||
218 | static void remove_redundant_pack(const char *dir_name, const char *base_name) | |
219 | { | |
220 | struct strbuf buf = STRBUF_INIT; | |
221 | struct multi_pack_index *m = get_local_multi_pack_index(the_repository); | |
222 | strbuf_addf(&buf, "%s.pack", base_name); | |
223 | if (m && midx_contains_pack(m, buf.buf)) | |
224 | clear_midx_file(the_repository); | |
225 | strbuf_insertf(&buf, 0, "%s/", dir_name); | |
226 | unlink_pack_path(buf.buf, 1); | |
227 | strbuf_release(&buf); | |
228 | } | |
229 | ||
230 | static void remove_redundant_packs_1(struct string_list *packs) | |
231 | { | |
232 | struct string_list_item *item; | |
233 | for_each_string_list_item(item, packs) { | |
234 | if (!pack_is_marked_for_deletion(item)) | |
235 | continue; | |
236 | remove_redundant_pack(packdir, item->string); | |
237 | } | |
238 | } | |
239 | ||
240 | static void remove_redundant_existing_packs(struct existing_packs *existing) | |
241 | { | |
242 | remove_redundant_packs_1(&existing->non_kept_packs); | |
243 | remove_redundant_packs_1(&existing->cruft_packs); | |
244 | } | |
245 | ||
246 | static void existing_packs_release(struct existing_packs *existing) | |
247 | { | |
248 | string_list_clear(&existing->kept_packs, 0); | |
249 | string_list_clear(&existing->non_kept_packs, 0); | |
250 | string_list_clear(&existing->cruft_packs, 0); | |
251 | } | |
252 | ||
253 | /* | |
254 | * Adds all packs hex strings (pack-$HASH) to either packs->non_kept | |
255 | * or packs->kept based on whether each pack has a corresponding | |
256 | * .keep file or not. Packs without a .keep file are not to be kept | |
257 | * if we are going to pack everything into one file. | |
258 | */ | |
259 | static void collect_pack_filenames(struct existing_packs *existing, | |
260 | const struct string_list *extra_keep) | |
261 | { | |
262 | struct packed_git *p; | |
263 | struct strbuf buf = STRBUF_INIT; | |
264 | ||
265 | for (p = get_all_packs(the_repository); p; p = p->next) { | |
266 | int i; | |
267 | const char *base; | |
268 | ||
269 | if (!p->pack_local) | |
270 | continue; | |
271 | ||
272 | base = pack_basename(p); | |
273 | ||
274 | for (i = 0; i < extra_keep->nr; i++) | |
275 | if (!fspathcmp(base, extra_keep->items[i].string)) | |
276 | break; | |
277 | ||
278 | strbuf_reset(&buf); | |
279 | strbuf_addstr(&buf, base); | |
280 | strbuf_strip_suffix(&buf, ".pack"); | |
281 | ||
282 | if ((extra_keep->nr > 0 && i < extra_keep->nr) || p->pack_keep) | |
283 | string_list_append(&existing->kept_packs, buf.buf); | |
284 | else if (p->is_cruft) | |
285 | string_list_append(&existing->cruft_packs, buf.buf); | |
286 | else | |
287 | string_list_append(&existing->non_kept_packs, buf.buf); | |
288 | } | |
289 | ||
290 | string_list_sort(&existing->kept_packs); | |
291 | string_list_sort(&existing->non_kept_packs); | |
292 | string_list_sort(&existing->cruft_packs); | |
293 | strbuf_release(&buf); | |
294 | } | |
295 | ||
296 | static void prepare_pack_objects(struct child_process *cmd, | |
297 | const struct pack_objects_args *args, | |
298 | const char *out) | |
299 | { | |
300 | strvec_push(&cmd->args, "pack-objects"); | |
301 | if (args->window) | |
302 | strvec_pushf(&cmd->args, "--window=%s", args->window); | |
303 | if (args->window_memory) | |
304 | strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory); | |
305 | if (args->depth) | |
306 | strvec_pushf(&cmd->args, "--depth=%s", args->depth); | |
307 | if (args->threads) | |
308 | strvec_pushf(&cmd->args, "--threads=%s", args->threads); | |
309 | if (args->max_pack_size) | |
310 | strvec_pushf(&cmd->args, "--max-pack-size=%lu", args->max_pack_size); | |
311 | if (args->no_reuse_delta) | |
312 | strvec_pushf(&cmd->args, "--no-reuse-delta"); | |
313 | if (args->no_reuse_object) | |
314 | strvec_pushf(&cmd->args, "--no-reuse-object"); | |
315 | if (args->name_hash_version) | |
316 | strvec_pushf(&cmd->args, "--name-hash-version=%d", args->name_hash_version); | |
317 | if (args->path_walk) | |
318 | strvec_pushf(&cmd->args, "--path-walk"); | |
319 | if (args->local) | |
320 | strvec_push(&cmd->args, "--local"); | |
321 | if (args->quiet) | |
322 | strvec_push(&cmd->args, "--quiet"); | |
323 | if (delta_base_offset) | |
324 | strvec_push(&cmd->args, "--delta-base-offset"); | |
325 | strvec_push(&cmd->args, out); | |
326 | cmd->git_cmd = 1; | |
327 | cmd->out = -1; | |
328 | } | |
329 | ||
330 | /* | |
331 | * Write oid to the given struct child_process's stdin, starting it first if | |
332 | * necessary. | |
333 | */ | |
334 | static int write_oid(const struct object_id *oid, | |
335 | struct packed_git *pack UNUSED, | |
336 | uint32_t pos UNUSED, void *data) | |
337 | { | |
338 | struct child_process *cmd = data; | |
339 | ||
340 | if (cmd->in == -1) { | |
341 | if (start_command(cmd)) | |
342 | die(_("could not start pack-objects to repack promisor objects")); | |
343 | } | |
344 | ||
345 | if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 || | |
346 | write_in_full(cmd->in, "\n", 1) < 0) | |
347 | die(_("failed to feed promisor objects to pack-objects")); | |
348 | return 0; | |
349 | } | |
350 | ||
351 | static struct { | |
352 | const char *name; | |
353 | unsigned optional:1; | |
354 | } exts[] = { | |
355 | {".pack"}, | |
356 | {".rev", 1}, | |
357 | {".mtimes", 1}, | |
358 | {".bitmap", 1}, | |
359 | {".promisor", 1}, | |
360 | {".idx"}, | |
361 | }; | |
362 | ||
363 | struct generated_pack_data { | |
364 | struct tempfile *tempfiles[ARRAY_SIZE(exts)]; | |
365 | }; | |
366 | ||
367 | static struct generated_pack_data *populate_pack_exts(const char *name) | |
368 | { | |
369 | struct stat statbuf; | |
370 | struct strbuf path = STRBUF_INIT; | |
371 | struct generated_pack_data *data = xcalloc(1, sizeof(*data)); | |
372 | int i; | |
373 | ||
374 | for (i = 0; i < ARRAY_SIZE(exts); i++) { | |
375 | strbuf_reset(&path); | |
376 | strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name); | |
377 | ||
378 | if (stat(path.buf, &statbuf)) | |
379 | continue; | |
380 | ||
381 | data->tempfiles[i] = register_tempfile(path.buf); | |
382 | } | |
383 | ||
384 | strbuf_release(&path); | |
385 | return data; | |
386 | } | |
387 | ||
388 | static int has_pack_ext(const struct generated_pack_data *data, | |
389 | const char *ext) | |
390 | { | |
391 | int i; | |
392 | for (i = 0; i < ARRAY_SIZE(exts); i++) { | |
393 | if (strcmp(exts[i].name, ext)) | |
394 | continue; | |
395 | return !!data->tempfiles[i]; | |
396 | } | |
397 | BUG("unknown pack extension: '%s'", ext); | |
398 | } | |
399 | ||
400 | static void repack_promisor_objects(const struct pack_objects_args *args, | |
401 | struct string_list *names) | |
402 | { | |
403 | struct child_process cmd = CHILD_PROCESS_INIT; | |
404 | FILE *out; | |
405 | struct strbuf line = STRBUF_INIT; | |
406 | ||
407 | prepare_pack_objects(&cmd, args, packtmp); | |
408 | cmd.in = -1; | |
409 | ||
410 | /* | |
411 | * NEEDSWORK: Giving pack-objects only the OIDs without any ordering | |
412 | * hints may result in suboptimal deltas in the resulting pack. See if | |
413 | * the OIDs can be sent with fake paths such that pack-objects can use a | |
414 | * {type -> existing pack order} ordering when computing deltas instead | |
415 | * of a {type -> size} ordering, which may produce better deltas. | |
416 | */ | |
417 | for_each_packed_object(the_repository, write_oid, &cmd, | |
418 | FOR_EACH_OBJECT_PROMISOR_ONLY); | |
419 | ||
420 | if (cmd.in == -1) { | |
421 | /* No packed objects; cmd was never started */ | |
422 | child_process_clear(&cmd); | |
423 | return; | |
424 | } | |
425 | ||
426 | close(cmd.in); | |
427 | ||
428 | out = xfdopen(cmd.out, "r"); | |
429 | while (strbuf_getline_lf(&line, out) != EOF) { | |
430 | struct string_list_item *item; | |
431 | char *promisor_name; | |
432 | ||
433 | if (line.len != the_hash_algo->hexsz) | |
434 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); | |
435 | item = string_list_append(names, line.buf); | |
436 | ||
437 | /* | |
438 | * pack-objects creates the .pack and .idx files, but not the | |
439 | * .promisor file. Create the .promisor file, which is empty. | |
440 | * | |
441 | * NEEDSWORK: fetch-pack sometimes generates non-empty | |
442 | * .promisor files containing the ref names and associated | |
443 | * hashes at the point of generation of the corresponding | |
444 | * packfile, but this would not preserve their contents. Maybe | |
445 | * concatenate the contents of all .promisor files instead of | |
446 | * just creating a new empty file. | |
447 | */ | |
448 | promisor_name = mkpathdup("%s-%s.promisor", packtmp, | |
449 | line.buf); | |
450 | write_promisor_file(promisor_name, NULL, 0); | |
451 | ||
452 | item->util = populate_pack_exts(item->string); | |
453 | ||
454 | free(promisor_name); | |
455 | } | |
456 | ||
457 | fclose(out); | |
458 | if (finish_command(&cmd)) | |
459 | die(_("could not finish pack-objects to repack promisor objects")); | |
460 | strbuf_release(&line); | |
461 | } | |
462 | ||
463 | struct pack_geometry { | |
464 | struct packed_git **pack; | |
465 | uint32_t pack_nr, pack_alloc; | |
466 | uint32_t split; | |
467 | ||
468 | int split_factor; | |
469 | }; | |
470 | ||
471 | static uint32_t geometry_pack_weight(struct packed_git *p) | |
472 | { | |
473 | if (open_pack_index(p)) | |
474 | die(_("cannot open index for %s"), p->pack_name); | |
475 | return p->num_objects; | |
476 | } | |
477 | ||
478 | static int geometry_cmp(const void *va, const void *vb) | |
479 | { | |
480 | uint32_t aw = geometry_pack_weight(*(struct packed_git **)va), | |
481 | bw = geometry_pack_weight(*(struct packed_git **)vb); | |
482 | ||
483 | if (aw < bw) | |
484 | return -1; | |
485 | if (aw > bw) | |
486 | return 1; | |
487 | return 0; | |
488 | } | |
489 | ||
490 | static void init_pack_geometry(struct pack_geometry *geometry, | |
491 | struct existing_packs *existing, | |
492 | const struct pack_objects_args *args) | |
493 | { | |
494 | struct packed_git *p; | |
495 | struct strbuf buf = STRBUF_INIT; | |
496 | ||
497 | for (p = get_all_packs(the_repository); p; p = p->next) { | |
498 | if (args->local && !p->pack_local) | |
499 | /* | |
500 | * When asked to only repack local packfiles we skip | |
501 | * over any packfiles that are borrowed from alternate | |
502 | * object directories. | |
503 | */ | |
504 | continue; | |
505 | ||
506 | if (!pack_kept_objects) { | |
507 | /* | |
508 | * Any pack that has its pack_keep bit set will | |
509 | * appear in existing->kept_packs below, but | |
510 | * this saves us from doing a more expensive | |
511 | * check. | |
512 | */ | |
513 | if (p->pack_keep) | |
514 | continue; | |
515 | ||
516 | /* | |
517 | * The pack may be kept via the --keep-pack | |
518 | * option; check 'existing->kept_packs' to | |
519 | * determine whether to ignore it. | |
520 | */ | |
521 | strbuf_reset(&buf); | |
522 | strbuf_addstr(&buf, pack_basename(p)); | |
523 | strbuf_strip_suffix(&buf, ".pack"); | |
524 | ||
525 | if (string_list_has_string(&existing->kept_packs, buf.buf)) | |
526 | continue; | |
527 | } | |
528 | if (p->is_cruft) | |
529 | continue; | |
530 | ||
531 | ALLOC_GROW(geometry->pack, | |
532 | geometry->pack_nr + 1, | |
533 | geometry->pack_alloc); | |
534 | ||
535 | geometry->pack[geometry->pack_nr] = p; | |
536 | geometry->pack_nr++; | |
537 | } | |
538 | ||
539 | QSORT(geometry->pack, geometry->pack_nr, geometry_cmp); | |
540 | strbuf_release(&buf); | |
541 | } | |
542 | ||
543 | static void split_pack_geometry(struct pack_geometry *geometry) | |
544 | { | |
545 | uint32_t i; | |
546 | uint32_t split; | |
547 | off_t total_size = 0; | |
548 | ||
549 | if (!geometry->pack_nr) { | |
550 | geometry->split = geometry->pack_nr; | |
551 | return; | |
552 | } | |
553 | ||
554 | /* | |
555 | * First, count the number of packs (in descending order of size) which | |
556 | * already form a geometric progression. | |
557 | */ | |
558 | for (i = geometry->pack_nr - 1; i > 0; i--) { | |
559 | struct packed_git *ours = geometry->pack[i]; | |
560 | struct packed_git *prev = geometry->pack[i - 1]; | |
561 | ||
562 | if (unsigned_mult_overflows(geometry->split_factor, | |
563 | geometry_pack_weight(prev))) | |
564 | die(_("pack %s too large to consider in geometric " | |
565 | "progression"), | |
566 | prev->pack_name); | |
567 | ||
568 | if (geometry_pack_weight(ours) < | |
569 | geometry->split_factor * geometry_pack_weight(prev)) | |
570 | break; | |
571 | } | |
572 | ||
573 | split = i; | |
574 | ||
575 | if (split) { | |
576 | /* | |
577 | * Move the split one to the right, since the top element in the | |
578 | * last-compared pair can't be in the progression. Only do this | |
579 | * when we split in the middle of the array (otherwise if we got | |
580 | * to the end, then the split is in the right place). | |
581 | */ | |
582 | split++; | |
583 | } | |
584 | ||
585 | /* | |
586 | * Then, anything to the left of 'split' must be in a new pack. But, | |
587 | * creating that new pack may cause packs in the heavy half to no longer | |
588 | * form a geometric progression. | |
589 | * | |
590 | * Compute an expected size of the new pack, and then determine how many | |
591 | * packs in the heavy half need to be joined into it (if any) to restore | |
592 | * the geometric progression. | |
593 | */ | |
594 | for (i = 0; i < split; i++) { | |
595 | struct packed_git *p = geometry->pack[i]; | |
596 | ||
597 | if (unsigned_add_overflows(total_size, geometry_pack_weight(p))) | |
598 | die(_("pack %s too large to roll up"), p->pack_name); | |
599 | total_size += geometry_pack_weight(p); | |
600 | } | |
601 | for (i = split; i < geometry->pack_nr; i++) { | |
602 | struct packed_git *ours = geometry->pack[i]; | |
603 | ||
604 | if (unsigned_mult_overflows(geometry->split_factor, | |
605 | total_size)) | |
606 | die(_("pack %s too large to roll up"), ours->pack_name); | |
607 | ||
608 | if (geometry_pack_weight(ours) < | |
609 | geometry->split_factor * total_size) { | |
610 | if (unsigned_add_overflows(total_size, | |
611 | geometry_pack_weight(ours))) | |
612 | die(_("pack %s too large to roll up"), | |
613 | ours->pack_name); | |
614 | ||
615 | split++; | |
616 | total_size += geometry_pack_weight(ours); | |
617 | } else | |
618 | break; | |
619 | } | |
620 | ||
621 | geometry->split = split; | |
622 | } | |
623 | ||
624 | static struct packed_git *get_preferred_pack(struct pack_geometry *geometry) | |
625 | { | |
626 | uint32_t i; | |
627 | ||
628 | if (!geometry) { | |
629 | /* | |
630 | * No geometry means either an all-into-one repack (in which | |
631 | * case there is only one pack left and it is the largest) or an | |
632 | * incremental one. | |
633 | * | |
634 | * If repacking incrementally, then we could check the size of | |
635 | * all packs to determine which should be preferred, but leave | |
636 | * this for later. | |
637 | */ | |
638 | return NULL; | |
639 | } | |
640 | if (geometry->split == geometry->pack_nr) | |
641 | return NULL; | |
642 | ||
643 | /* | |
644 | * The preferred pack is the largest pack above the split line. In | |
645 | * other words, it is the largest pack that does not get rolled up in | |
646 | * the geometric repack. | |
647 | */ | |
648 | for (i = geometry->pack_nr; i > geometry->split; i--) | |
649 | /* | |
650 | * A pack that is not local would never be included in a | |
651 | * multi-pack index. We thus skip over any non-local packs. | |
652 | */ | |
653 | if (geometry->pack[i - 1]->pack_local) | |
654 | return geometry->pack[i - 1]; | |
655 | ||
656 | return NULL; | |
657 | } | |
658 | ||
659 | static void geometry_remove_redundant_packs(struct pack_geometry *geometry, | |
660 | struct string_list *names, | |
661 | struct existing_packs *existing) | |
662 | { | |
663 | struct strbuf buf = STRBUF_INIT; | |
664 | uint32_t i; | |
665 | ||
666 | for (i = 0; i < geometry->split; i++) { | |
667 | struct packed_git *p = geometry->pack[i]; | |
668 | if (string_list_has_string(names, hash_to_hex(p->hash))) | |
669 | continue; | |
670 | ||
671 | strbuf_reset(&buf); | |
672 | strbuf_addstr(&buf, pack_basename(p)); | |
673 | strbuf_strip_suffix(&buf, ".pack"); | |
674 | ||
675 | if ((p->pack_keep) || | |
676 | (string_list_has_string(&existing->kept_packs, buf.buf))) | |
677 | continue; | |
678 | ||
679 | remove_redundant_pack(packdir, buf.buf); | |
680 | } | |
681 | ||
682 | strbuf_release(&buf); | |
683 | } | |
684 | ||
685 | static void free_pack_geometry(struct pack_geometry *geometry) | |
686 | { | |
687 | if (!geometry) | |
688 | return; | |
689 | ||
690 | free(geometry->pack); | |
691 | } | |
692 | ||
693 | struct midx_snapshot_ref_data { | |
694 | struct tempfile *f; | |
695 | struct oidset seen; | |
696 | int preferred; | |
697 | }; | |
698 | ||
699 | static int midx_snapshot_ref_one(const char *refname UNUSED, | |
700 | const char *referent UNUSED, | |
701 | const struct object_id *oid, | |
702 | int flag UNUSED, void *_data) | |
703 | { | |
704 | struct midx_snapshot_ref_data *data = _data; | |
705 | struct object_id peeled; | |
706 | ||
707 | if (!peel_iterated_oid(the_repository, oid, &peeled)) | |
708 | oid = &peeled; | |
709 | ||
710 | if (oidset_insert(&data->seen, oid)) | |
711 | return 0; /* already seen */ | |
712 | ||
713 | if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT) | |
714 | return 0; | |
715 | ||
716 | fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "", | |
717 | oid_to_hex(oid)); | |
718 | ||
719 | return 0; | |
720 | } | |
721 | ||
722 | static void midx_snapshot_refs(struct tempfile *f) | |
723 | { | |
724 | struct midx_snapshot_ref_data data; | |
725 | const struct string_list *preferred = bitmap_preferred_tips(the_repository); | |
726 | ||
727 | data.f = f; | |
728 | data.preferred = 0; | |
729 | oidset_init(&data.seen, 0); | |
730 | ||
731 | if (!fdopen_tempfile(f, "w")) | |
732 | die(_("could not open tempfile %s for writing"), | |
733 | get_tempfile_path(f)); | |
734 | ||
735 | if (preferred) { | |
736 | struct string_list_item *item; | |
737 | ||
738 | data.preferred = 1; | |
739 | for_each_string_list_item(item, preferred) | |
740 | refs_for_each_ref_in(get_main_ref_store(the_repository), | |
741 | item->string, | |
742 | midx_snapshot_ref_one, &data); | |
743 | data.preferred = 0; | |
744 | } | |
745 | ||
746 | refs_for_each_ref(get_main_ref_store(the_repository), | |
747 | midx_snapshot_ref_one, &data); | |
748 | ||
749 | if (close_tempfile_gently(f)) { | |
750 | int save_errno = errno; | |
751 | delete_tempfile(&f); | |
752 | errno = save_errno; | |
753 | die_errno(_("could not close refs snapshot tempfile")); | |
754 | } | |
755 | ||
756 | oidset_clear(&data.seen); | |
757 | } | |
758 | ||
759 | static void midx_included_packs(struct string_list *include, | |
760 | struct existing_packs *existing, | |
761 | struct string_list *names, | |
762 | struct pack_geometry *geometry) | |
763 | { | |
764 | struct string_list_item *item; | |
765 | struct strbuf buf = STRBUF_INIT; | |
766 | ||
767 | for_each_string_list_item(item, &existing->kept_packs) { | |
768 | strbuf_reset(&buf); | |
769 | strbuf_addf(&buf, "%s.idx", item->string); | |
770 | string_list_insert(include, buf.buf); | |
771 | } | |
772 | ||
773 | for_each_string_list_item(item, names) { | |
774 | strbuf_reset(&buf); | |
775 | strbuf_addf(&buf, "pack-%s.idx", item->string); | |
776 | string_list_insert(include, buf.buf); | |
777 | } | |
778 | ||
779 | if (geometry->split_factor) { | |
780 | uint32_t i; | |
781 | ||
782 | for (i = geometry->split; i < geometry->pack_nr; i++) { | |
783 | struct packed_git *p = geometry->pack[i]; | |
784 | ||
785 | /* | |
786 | * The multi-pack index never refers to packfiles part | |
787 | * of an alternate object database, so we skip these. | |
788 | * While git-multi-pack-index(1) would silently ignore | |
789 | * them anyway, this allows us to skip executing the | |
790 | * command completely when we have only non-local | |
791 | * packfiles. | |
792 | */ | |
793 | if (!p->pack_local) | |
794 | continue; | |
795 | ||
796 | strbuf_reset(&buf); | |
797 | strbuf_addstr(&buf, pack_basename(p)); | |
798 | strbuf_strip_suffix(&buf, ".pack"); | |
799 | strbuf_addstr(&buf, ".idx"); | |
800 | ||
801 | string_list_insert(include, buf.buf); | |
802 | } | |
803 | } else { | |
804 | for_each_string_list_item(item, &existing->non_kept_packs) { | |
805 | if (pack_is_marked_for_deletion(item)) | |
806 | continue; | |
807 | ||
808 | strbuf_reset(&buf); | |
809 | strbuf_addf(&buf, "%s.idx", item->string); | |
810 | string_list_insert(include, buf.buf); | |
811 | } | |
812 | } | |
813 | ||
814 | for_each_string_list_item(item, &existing->cruft_packs) { | |
815 | /* | |
816 | * When doing a --geometric repack, there is no need to check | |
817 | * for deleted packs, since we're by definition not doing an | |
818 | * ALL_INTO_ONE repack (hence no packs will be deleted). | |
819 | * Otherwise we must check for and exclude any packs which are | |
820 | * enqueued for deletion. | |
821 | * | |
822 | * So we could omit the conditional below in the --geometric | |
823 | * case, but doing so is unnecessary since no packs are marked | |
824 | * as pending deletion (since we only call | |
825 | * `mark_packs_for_deletion()` when doing an all-into-one | |
826 | * repack). | |
827 | */ | |
828 | if (pack_is_marked_for_deletion(item)) | |
829 | continue; | |
830 | ||
831 | strbuf_reset(&buf); | |
832 | strbuf_addf(&buf, "%s.idx", item->string); | |
833 | string_list_insert(include, buf.buf); | |
834 | } | |
835 | ||
836 | strbuf_release(&buf); | |
837 | } | |
838 | ||
839 | static int write_midx_included_packs(struct string_list *include, | |
840 | struct pack_geometry *geometry, | |
841 | struct string_list *names, | |
842 | const char *refs_snapshot, | |
843 | int show_progress, int write_bitmaps) | |
844 | { | |
845 | struct child_process cmd = CHILD_PROCESS_INIT; | |
846 | struct string_list_item *item; | |
847 | struct packed_git *preferred = get_preferred_pack(geometry); | |
848 | FILE *in; | |
849 | int ret; | |
850 | ||
851 | if (!include->nr) | |
852 | return 0; | |
853 | ||
854 | cmd.in = -1; | |
855 | cmd.git_cmd = 1; | |
856 | ||
857 | strvec_push(&cmd.args, "multi-pack-index"); | |
858 | strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL); | |
859 | ||
860 | if (show_progress) | |
861 | strvec_push(&cmd.args, "--progress"); | |
862 | else | |
863 | strvec_push(&cmd.args, "--no-progress"); | |
864 | ||
865 | if (write_bitmaps) | |
866 | strvec_push(&cmd.args, "--bitmap"); | |
867 | ||
868 | if (preferred) | |
869 | strvec_pushf(&cmd.args, "--preferred-pack=%s", | |
870 | pack_basename(preferred)); | |
871 | else if (names->nr) { | |
872 | /* The largest pack was repacked, meaning that either | |
873 | * one or two packs exist depending on whether the | |
874 | * repository has a cruft pack or not. | |
875 | * | |
876 | * Select the non-cruft one as preferred to encourage | |
877 | * pack-reuse among packs containing reachable objects | |
878 | * over unreachable ones. | |
879 | * | |
880 | * (Note we could write multiple packs here if | |
881 | * `--max-pack-size` was given, but any one of them | |
882 | * will suffice, so pick the first one.) | |
883 | */ | |
884 | for_each_string_list_item(item, names) { | |
885 | struct generated_pack_data *data = item->util; | |
886 | if (has_pack_ext(data, ".mtimes")) | |
887 | continue; | |
888 | ||
889 | strvec_pushf(&cmd.args, "--preferred-pack=pack-%s.pack", | |
890 | item->string); | |
891 | break; | |
892 | } | |
893 | } else { | |
894 | /* | |
895 | * No packs were kept, and no packs were written. The | |
896 | * only thing remaining are .keep packs (unless | |
897 | * --pack-kept-objects was given). | |
898 | * | |
899 | * Set the `--preferred-pack` arbitrarily here. | |
900 | */ | |
901 | ; | |
902 | } | |
903 | ||
904 | if (refs_snapshot) | |
905 | strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot); | |
906 | ||
907 | ret = start_command(&cmd); | |
908 | if (ret) | |
909 | return ret; | |
910 | ||
911 | in = xfdopen(cmd.in, "w"); | |
912 | for_each_string_list_item(item, include) | |
913 | fprintf(in, "%s\n", item->string); | |
914 | fclose(in); | |
915 | ||
916 | return finish_command(&cmd); | |
917 | } | |
918 | ||
919 | static void remove_redundant_bitmaps(struct string_list *include, | |
920 | const char *packdir) | |
921 | { | |
922 | struct strbuf path = STRBUF_INIT; | |
923 | struct string_list_item *item; | |
924 | size_t packdir_len; | |
925 | ||
926 | strbuf_addstr(&path, packdir); | |
927 | strbuf_addch(&path, '/'); | |
928 | packdir_len = path.len; | |
929 | ||
930 | /* | |
931 | * Remove any pack bitmaps corresponding to packs which are now | |
932 | * included in the MIDX. | |
933 | */ | |
934 | for_each_string_list_item(item, include) { | |
935 | strbuf_addstr(&path, item->string); | |
936 | strbuf_strip_suffix(&path, ".idx"); | |
937 | strbuf_addstr(&path, ".bitmap"); | |
938 | ||
939 | if (unlink(path.buf) && errno != ENOENT) | |
940 | warning_errno(_("could not remove stale bitmap: %s"), | |
941 | path.buf); | |
942 | ||
943 | strbuf_setlen(&path, packdir_len); | |
944 | } | |
945 | strbuf_release(&path); | |
946 | } | |
947 | ||
948 | static int finish_pack_objects_cmd(struct child_process *cmd, | |
949 | struct string_list *names, | |
950 | int local) | |
951 | { | |
952 | FILE *out; | |
953 | struct strbuf line = STRBUF_INIT; | |
954 | ||
955 | out = xfdopen(cmd->out, "r"); | |
956 | while (strbuf_getline_lf(&line, out) != EOF) { | |
957 | struct string_list_item *item; | |
958 | ||
959 | if (line.len != the_hash_algo->hexsz) | |
960 | die(_("repack: Expecting full hex object ID lines only " | |
961 | "from pack-objects.")); | |
962 | /* | |
963 | * Avoid putting packs written outside of the repository in the | |
964 | * list of names. | |
965 | */ | |
966 | if (local) { | |
967 | item = string_list_append(names, line.buf); | |
968 | item->util = populate_pack_exts(line.buf); | |
969 | } | |
970 | } | |
971 | fclose(out); | |
972 | ||
973 | strbuf_release(&line); | |
974 | ||
975 | return finish_command(cmd); | |
976 | } | |
977 | ||
978 | static int write_filtered_pack(const struct pack_objects_args *args, | |
979 | const char *destination, | |
980 | const char *pack_prefix, | |
981 | struct existing_packs *existing, | |
982 | struct string_list *names) | |
983 | { | |
984 | struct child_process cmd = CHILD_PROCESS_INIT; | |
985 | struct string_list_item *item; | |
986 | FILE *in; | |
987 | int ret; | |
988 | const char *caret; | |
989 | const char *scratch; | |
990 | int local = skip_prefix(destination, packdir, &scratch); | |
991 | ||
992 | prepare_pack_objects(&cmd, args, destination); | |
993 | ||
994 | strvec_push(&cmd.args, "--stdin-packs"); | |
995 | ||
996 | if (!pack_kept_objects) | |
997 | strvec_push(&cmd.args, "--honor-pack-keep"); | |
998 | for_each_string_list_item(item, &existing->kept_packs) | |
999 | strvec_pushf(&cmd.args, "--keep-pack=%s", item->string); | |
1000 | ||
1001 | cmd.in = -1; | |
1002 | ||
1003 | ret = start_command(&cmd); | |
1004 | if (ret) | |
1005 | return ret; | |
1006 | ||
1007 | /* | |
1008 | * Here 'names' contains only the pack(s) that were just | |
1009 | * written, which is exactly the packs we want to keep. Also | |
1010 | * 'existing_kept_packs' already contains the packs in | |
1011 | * 'keep_pack_list'. | |
1012 | */ | |
1013 | in = xfdopen(cmd.in, "w"); | |
1014 | for_each_string_list_item(item, names) | |
1015 | fprintf(in, "^%s-%s.pack\n", pack_prefix, item->string); | |
1016 | for_each_string_list_item(item, &existing->non_kept_packs) | |
1017 | fprintf(in, "%s.pack\n", item->string); | |
1018 | for_each_string_list_item(item, &existing->cruft_packs) | |
1019 | fprintf(in, "%s.pack\n", item->string); | |
1020 | caret = pack_kept_objects ? "" : "^"; | |
1021 | for_each_string_list_item(item, &existing->kept_packs) | |
1022 | fprintf(in, "%s%s.pack\n", caret, item->string); | |
1023 | fclose(in); | |
1024 | ||
1025 | return finish_pack_objects_cmd(&cmd, names, local); | |
1026 | } | |
1027 | ||
1028 | static void combine_small_cruft_packs(FILE *in, size_t combine_cruft_below_size, | |
1029 | struct existing_packs *existing) | |
1030 | { | |
1031 | struct packed_git *p; | |
1032 | struct strbuf buf = STRBUF_INIT; | |
1033 | size_t i; | |
1034 | ||
1035 | for (p = get_all_packs(the_repository); p; p = p->next) { | |
1036 | if (!(p->is_cruft && p->pack_local)) | |
1037 | continue; | |
1038 | ||
1039 | strbuf_reset(&buf); | |
1040 | strbuf_addstr(&buf, pack_basename(p)); | |
1041 | strbuf_strip_suffix(&buf, ".pack"); | |
1042 | ||
1043 | if (!string_list_has_string(&existing->cruft_packs, buf.buf)) | |
1044 | continue; | |
1045 | ||
1046 | if (p->pack_size < combine_cruft_below_size) { | |
1047 | fprintf(in, "-%s\n", pack_basename(p)); | |
1048 | } else { | |
1049 | retain_cruft_pack(existing, p); | |
1050 | fprintf(in, "%s\n", pack_basename(p)); | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | for (i = 0; i < existing->non_kept_packs.nr; i++) | |
1055 | fprintf(in, "-%s.pack\n", | |
1056 | existing->non_kept_packs.items[i].string); | |
1057 | ||
1058 | strbuf_release(&buf); | |
1059 | } | |
1060 | ||
1061 | static int write_cruft_pack(const struct pack_objects_args *args, | |
1062 | const char *destination, | |
1063 | const char *pack_prefix, | |
1064 | const char *cruft_expiration, | |
1065 | unsigned long combine_cruft_below_size, | |
1066 | struct string_list *names, | |
1067 | struct existing_packs *existing) | |
1068 | { | |
1069 | struct child_process cmd = CHILD_PROCESS_INIT; | |
1070 | struct string_list_item *item; | |
1071 | FILE *in; | |
1072 | int ret; | |
1073 | const char *scratch; | |
1074 | int local = skip_prefix(destination, packdir, &scratch); | |
1075 | ||
1076 | prepare_pack_objects(&cmd, args, destination); | |
1077 | ||
1078 | strvec_push(&cmd.args, "--cruft"); | |
1079 | if (cruft_expiration) | |
1080 | strvec_pushf(&cmd.args, "--cruft-expiration=%s", | |
1081 | cruft_expiration); | |
1082 | ||
1083 | strvec_push(&cmd.args, "--honor-pack-keep"); | |
1084 | strvec_push(&cmd.args, "--non-empty"); | |
1085 | ||
1086 | cmd.in = -1; | |
1087 | ||
1088 | ret = start_command(&cmd); | |
1089 | if (ret) | |
1090 | return ret; | |
1091 | ||
1092 | /* | |
1093 | * names has a confusing double use: it both provides the list | |
1094 | * of just-written new packs, and accepts the name of the cruft | |
1095 | * pack we are writing. | |
1096 | * | |
1097 | * By the time it is read here, it contains only the pack(s) | |
1098 | * that were just written, which is exactly the set of packs we | |
1099 | * want to consider kept. | |
1100 | * | |
1101 | * If `--expire-to` is given, the double-use served by `names` | |
1102 | * ensures that the pack written to `--expire-to` excludes any | |
1103 | * objects contained in the cruft pack. | |
1104 | */ | |
1105 | in = xfdopen(cmd.in, "w"); | |
1106 | for_each_string_list_item(item, names) | |
1107 | fprintf(in, "%s-%s.pack\n", pack_prefix, item->string); | |
1108 | if (combine_cruft_below_size && !cruft_expiration) { | |
1109 | combine_small_cruft_packs(in, combine_cruft_below_size, | |
1110 | existing); | |
1111 | } else { | |
1112 | for_each_string_list_item(item, &existing->non_kept_packs) | |
1113 | fprintf(in, "-%s.pack\n", item->string); | |
1114 | for_each_string_list_item(item, &existing->cruft_packs) | |
1115 | fprintf(in, "-%s.pack\n", item->string); | |
1116 | } | |
1117 | for_each_string_list_item(item, &existing->kept_packs) | |
1118 | fprintf(in, "%s.pack\n", item->string); | |
1119 | fclose(in); | |
1120 | ||
1121 | return finish_pack_objects_cmd(&cmd, names, local); | |
1122 | } | |
1123 | ||
1124 | static const char *find_pack_prefix(const char *packdir, const char *packtmp) | |
1125 | { | |
1126 | const char *pack_prefix; | |
1127 | if (!skip_prefix(packtmp, packdir, &pack_prefix)) | |
1128 | die(_("pack prefix %s does not begin with objdir %s"), | |
1129 | packtmp, packdir); | |
1130 | if (*pack_prefix == '/') | |
1131 | pack_prefix++; | |
1132 | return pack_prefix; | |
1133 | } | |
1134 | ||
1135 | int cmd_repack(int argc, | |
1136 | const char **argv, | |
1137 | const char *prefix, | |
1138 | struct repository *repo UNUSED) | |
1139 | { | |
1140 | struct child_process cmd = CHILD_PROCESS_INIT; | |
1141 | struct string_list_item *item; | |
1142 | struct string_list names = STRING_LIST_INIT_DUP; | |
1143 | struct existing_packs existing = EXISTING_PACKS_INIT; | |
1144 | struct pack_geometry geometry = { 0 }; | |
1145 | struct tempfile *refs_snapshot = NULL; | |
1146 | int i, ext, ret; | |
1147 | int show_progress; | |
1148 | ||
1149 | /* variables to be filled by option parsing */ | |
1150 | int delete_redundant = 0; | |
1151 | const char *unpack_unreachable = NULL; | |
1152 | int keep_unreachable = 0; | |
1153 | struct string_list keep_pack_list = STRING_LIST_INIT_NODUP; | |
1154 | struct pack_objects_args po_args = { 0 }; | |
1155 | struct pack_objects_args cruft_po_args = { 0 }; | |
1156 | int write_midx = 0; | |
1157 | const char *cruft_expiration = NULL; | |
1158 | const char *expire_to = NULL; | |
1159 | const char *filter_to = NULL; | |
1160 | const char *opt_window = NULL; | |
1161 | const char *opt_window_memory = NULL; | |
1162 | const char *opt_depth = NULL; | |
1163 | const char *opt_threads = NULL; | |
1164 | unsigned long combine_cruft_below_size = 0ul; | |
1165 | ||
1166 | struct option builtin_repack_options[] = { | |
1167 | OPT_BIT('a', NULL, &pack_everything, | |
1168 | N_("pack everything in a single pack"), ALL_INTO_ONE), | |
1169 | OPT_BIT('A', NULL, &pack_everything, | |
1170 | N_("same as -a, and turn unreachable objects loose"), | |
1171 | LOOSEN_UNREACHABLE | ALL_INTO_ONE), | |
1172 | OPT_BIT(0, "cruft", &pack_everything, | |
1173 | N_("same as -a, pack unreachable cruft objects separately"), | |
1174 | PACK_CRUFT), | |
1175 | OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"), | |
1176 | N_("with --cruft, expire objects older than this")), | |
1177 | OPT_UNSIGNED(0, "combine-cruft-below-size", | |
1178 | &combine_cruft_below_size, | |
1179 | N_("with --cruft, only repack cruft packs smaller than this")), | |
1180 | OPT_UNSIGNED(0, "max-cruft-size", &cruft_po_args.max_pack_size, | |
1181 | N_("with --cruft, limit the size of new cruft packs")), | |
1182 | OPT_BOOL('d', NULL, &delete_redundant, | |
1183 | N_("remove redundant packs, and run git-prune-packed")), | |
1184 | OPT_BOOL('f', NULL, &po_args.no_reuse_delta, | |
1185 | N_("pass --no-reuse-delta to git-pack-objects")), | |
1186 | OPT_BOOL('F', NULL, &po_args.no_reuse_object, | |
1187 | N_("pass --no-reuse-object to git-pack-objects")), | |
1188 | OPT_INTEGER(0, "name-hash-version", &po_args.name_hash_version, | |
1189 | N_("specify the name hash version to use for grouping similar objects by path")), | |
1190 | OPT_BOOL(0, "path-walk", &po_args.path_walk, | |
1191 | N_("pass --path-walk to git-pack-objects")), | |
1192 | OPT_NEGBIT('n', NULL, &run_update_server_info, | |
1193 | N_("do not run git-update-server-info"), 1), | |
1194 | OPT__QUIET(&po_args.quiet, N_("be quiet")), | |
1195 | OPT_BOOL('l', "local", &po_args.local, | |
1196 | N_("pass --local to git-pack-objects")), | |
1197 | OPT_BOOL('b', "write-bitmap-index", &write_bitmaps, | |
1198 | N_("write bitmap index")), | |
1199 | OPT_BOOL('i', "delta-islands", &use_delta_islands, | |
1200 | N_("pass --delta-islands to git-pack-objects")), | |
1201 | OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"), | |
1202 | N_("with -A, do not loosen objects older than this")), | |
1203 | OPT_BOOL('k', "keep-unreachable", &keep_unreachable, | |
1204 | N_("with -a, repack unreachable objects")), | |
1205 | OPT_STRING(0, "window", &opt_window, N_("n"), | |
1206 | N_("size of the window used for delta compression")), | |
1207 | OPT_STRING(0, "window-memory", &opt_window_memory, N_("bytes"), | |
1208 | N_("same as the above, but limit memory size instead of entries count")), | |
1209 | OPT_STRING(0, "depth", &opt_depth, N_("n"), | |
1210 | N_("limits the maximum delta depth")), | |
1211 | OPT_STRING(0, "threads", &opt_threads, N_("n"), | |
1212 | N_("limits the maximum number of threads")), | |
1213 | OPT_UNSIGNED(0, "max-pack-size", &po_args.max_pack_size, | |
1214 | N_("maximum size of each packfile")), | |
1215 | OPT_PARSE_LIST_OBJECTS_FILTER(&po_args.filter_options), | |
1216 | OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects, | |
1217 | N_("repack objects in packs marked with .keep")), | |
1218 | OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"), | |
1219 | N_("do not repack this pack")), | |
1220 | OPT_INTEGER('g', "geometric", &geometry.split_factor, | |
1221 | N_("find a geometric progression with factor <N>")), | |
1222 | OPT_BOOL('m', "write-midx", &write_midx, | |
1223 | N_("write a multi-pack index of the resulting packs")), | |
1224 | OPT_STRING(0, "expire-to", &expire_to, N_("dir"), | |
1225 | N_("pack prefix to store a pack containing pruned objects")), | |
1226 | OPT_STRING(0, "filter-to", &filter_to, N_("dir"), | |
1227 | N_("pack prefix to store a pack containing filtered out objects")), | |
1228 | OPT_END() | |
1229 | }; | |
1230 | ||
1231 | list_objects_filter_init(&po_args.filter_options); | |
1232 | ||
1233 | git_config(repack_config, &cruft_po_args); | |
1234 | ||
1235 | argc = parse_options(argc, argv, prefix, builtin_repack_options, | |
1236 | git_repack_usage, 0); | |
1237 | ||
1238 | po_args.window = xstrdup_or_null(opt_window); | |
1239 | po_args.window_memory = xstrdup_or_null(opt_window_memory); | |
1240 | po_args.depth = xstrdup_or_null(opt_depth); | |
1241 | po_args.threads = xstrdup_or_null(opt_threads); | |
1242 | ||
1243 | if (delete_redundant && repository_format_precious_objects) | |
1244 | die(_("cannot delete packs in a precious-objects repo")); | |
1245 | ||
1246 | die_for_incompatible_opt3(unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE), "-A", | |
1247 | keep_unreachable, "-k/--keep-unreachable", | |
1248 | pack_everything & PACK_CRUFT, "--cruft"); | |
1249 | ||
1250 | if (pack_everything & PACK_CRUFT) | |
1251 | pack_everything |= ALL_INTO_ONE; | |
1252 | ||
1253 | if (write_bitmaps < 0) { | |
1254 | if (!write_midx && | |
1255 | (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository())) | |
1256 | write_bitmaps = 0; | |
1257 | } | |
1258 | if (pack_kept_objects < 0) | |
1259 | pack_kept_objects = write_bitmaps > 0 && !write_midx; | |
1260 | ||
1261 | if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx) | |
1262 | die(_(incremental_bitmap_conflict_error)); | |
1263 | ||
1264 | if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) { | |
1265 | /* | |
1266 | * When asked to do a local repack, but we have | |
1267 | * packfiles that are inherited from an alternate, then | |
1268 | * we cannot guarantee that the multi-pack-index would | |
1269 | * have full coverage of all objects. We thus disable | |
1270 | * writing bitmaps in that case. | |
1271 | */ | |
1272 | warning(_("disabling bitmap writing, as some objects are not being packed")); | |
1273 | write_bitmaps = 0; | |
1274 | } | |
1275 | ||
1276 | if (write_midx && write_bitmaps) { | |
1277 | struct strbuf path = STRBUF_INIT; | |
1278 | ||
1279 | strbuf_addf(&path, "%s/%s_XXXXXX", repo_get_object_directory(the_repository), | |
1280 | "bitmap-ref-tips"); | |
1281 | ||
1282 | refs_snapshot = xmks_tempfile(path.buf); | |
1283 | midx_snapshot_refs(refs_snapshot); | |
1284 | ||
1285 | strbuf_release(&path); | |
1286 | } | |
1287 | ||
1288 | packdir = mkpathdup("%s/pack", repo_get_object_directory(the_repository)); | |
1289 | packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid()); | |
1290 | packtmp = mkpathdup("%s/%s", packdir, packtmp_name); | |
1291 | ||
1292 | collect_pack_filenames(&existing, &keep_pack_list); | |
1293 | ||
1294 | if (geometry.split_factor) { | |
1295 | if (pack_everything) | |
1296 | die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a"); | |
1297 | init_pack_geometry(&geometry, &existing, &po_args); | |
1298 | split_pack_geometry(&geometry); | |
1299 | } | |
1300 | ||
1301 | prepare_pack_objects(&cmd, &po_args, packtmp); | |
1302 | ||
1303 | show_progress = !po_args.quiet && isatty(2); | |
1304 | ||
1305 | strvec_push(&cmd.args, "--keep-true-parents"); | |
1306 | if (!pack_kept_objects) | |
1307 | strvec_push(&cmd.args, "--honor-pack-keep"); | |
1308 | for (i = 0; i < keep_pack_list.nr; i++) | |
1309 | strvec_pushf(&cmd.args, "--keep-pack=%s", | |
1310 | keep_pack_list.items[i].string); | |
1311 | strvec_push(&cmd.args, "--non-empty"); | |
1312 | if (!geometry.split_factor) { | |
1313 | /* | |
1314 | * We need to grab all reachable objects, including those that | |
1315 | * are reachable from reflogs and the index. | |
1316 | * | |
1317 | * When repacking into a geometric progression of packs, | |
1318 | * however, we ask 'git pack-objects --stdin-packs', and it is | |
1319 | * not about packing objects based on reachability but about | |
1320 | * repacking all the objects in specified packs and loose ones | |
1321 | * (indeed, --stdin-packs is incompatible with these options). | |
1322 | */ | |
1323 | strvec_push(&cmd.args, "--all"); | |
1324 | strvec_push(&cmd.args, "--reflog"); | |
1325 | strvec_push(&cmd.args, "--indexed-objects"); | |
1326 | } | |
1327 | if (repo_has_promisor_remote(the_repository)) | |
1328 | strvec_push(&cmd.args, "--exclude-promisor-objects"); | |
1329 | if (!write_midx) { | |
1330 | if (write_bitmaps > 0) | |
1331 | strvec_push(&cmd.args, "--write-bitmap-index"); | |
1332 | else if (write_bitmaps < 0) | |
1333 | strvec_push(&cmd.args, "--write-bitmap-index-quiet"); | |
1334 | } | |
1335 | if (use_delta_islands) | |
1336 | strvec_push(&cmd.args, "--delta-islands"); | |
1337 | ||
1338 | if (pack_everything & ALL_INTO_ONE) { | |
1339 | repack_promisor_objects(&po_args, &names); | |
1340 | ||
1341 | if (has_existing_non_kept_packs(&existing) && | |
1342 | delete_redundant && | |
1343 | !(pack_everything & PACK_CRUFT)) { | |
1344 | for_each_string_list_item(item, &names) { | |
1345 | strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack", | |
1346 | packtmp_name, item->string); | |
1347 | } | |
1348 | if (unpack_unreachable) { | |
1349 | strvec_pushf(&cmd.args, | |
1350 | "--unpack-unreachable=%s", | |
1351 | unpack_unreachable); | |
1352 | } else if (pack_everything & LOOSEN_UNREACHABLE) { | |
1353 | strvec_push(&cmd.args, | |
1354 | "--unpack-unreachable"); | |
1355 | } else if (keep_unreachable) { | |
1356 | strvec_push(&cmd.args, "--keep-unreachable"); | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | if (keep_unreachable && delete_redundant && | |
1361 | !(pack_everything & PACK_CRUFT)) | |
1362 | strvec_push(&cmd.args, "--pack-loose-unreachable"); | |
1363 | } else if (geometry.split_factor) { | |
1364 | strvec_push(&cmd.args, "--stdin-packs"); | |
1365 | strvec_push(&cmd.args, "--unpacked"); | |
1366 | } else { | |
1367 | strvec_push(&cmd.args, "--unpacked"); | |
1368 | strvec_push(&cmd.args, "--incremental"); | |
1369 | } | |
1370 | ||
1371 | if (po_args.filter_options.choice) | |
1372 | strvec_pushf(&cmd.args, "--filter=%s", | |
1373 | expand_list_objects_filter_spec(&po_args.filter_options)); | |
1374 | else if (filter_to) | |
1375 | die(_("option '%s' can only be used along with '%s'"), "--filter-to", "--filter"); | |
1376 | ||
1377 | if (geometry.split_factor) | |
1378 | cmd.in = -1; | |
1379 | else | |
1380 | cmd.no_stdin = 1; | |
1381 | ||
1382 | ret = start_command(&cmd); | |
1383 | if (ret) | |
1384 | goto cleanup; | |
1385 | ||
1386 | if (geometry.split_factor) { | |
1387 | FILE *in = xfdopen(cmd.in, "w"); | |
1388 | /* | |
1389 | * The resulting pack should contain all objects in packs that | |
1390 | * are going to be rolled up, but exclude objects in packs which | |
1391 | * are being left alone. | |
1392 | */ | |
1393 | for (i = 0; i < geometry.split; i++) | |
1394 | fprintf(in, "%s\n", pack_basename(geometry.pack[i])); | |
1395 | for (i = geometry.split; i < geometry.pack_nr; i++) | |
1396 | fprintf(in, "^%s\n", pack_basename(geometry.pack[i])); | |
1397 | fclose(in); | |
1398 | } | |
1399 | ||
1400 | ret = finish_pack_objects_cmd(&cmd, &names, 1); | |
1401 | if (ret) | |
1402 | goto cleanup; | |
1403 | ||
1404 | if (!names.nr && !po_args.quiet) | |
1405 | printf_ln(_("Nothing new to pack.")); | |
1406 | ||
1407 | if (pack_everything & PACK_CRUFT) { | |
1408 | const char *pack_prefix = find_pack_prefix(packdir, packtmp); | |
1409 | ||
1410 | if (!cruft_po_args.window) | |
1411 | cruft_po_args.window = xstrdup_or_null(po_args.window); | |
1412 | if (!cruft_po_args.window_memory) | |
1413 | cruft_po_args.window_memory = xstrdup_or_null(po_args.window_memory); | |
1414 | if (!cruft_po_args.depth) | |
1415 | cruft_po_args.depth = xstrdup_or_null(po_args.depth); | |
1416 | if (!cruft_po_args.threads) | |
1417 | cruft_po_args.threads = xstrdup_or_null(po_args.threads); | |
1418 | if (!cruft_po_args.max_pack_size) | |
1419 | cruft_po_args.max_pack_size = po_args.max_pack_size; | |
1420 | ||
1421 | cruft_po_args.local = po_args.local; | |
1422 | cruft_po_args.quiet = po_args.quiet; | |
1423 | ||
1424 | ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix, | |
1425 | cruft_expiration, | |
1426 | combine_cruft_below_size, &names, | |
1427 | &existing); | |
1428 | if (ret) | |
1429 | goto cleanup; | |
1430 | ||
1431 | if (delete_redundant && expire_to) { | |
1432 | /* | |
1433 | * If `--expire-to` is given with `-d`, it's possible | |
1434 | * that we're about to prune some objects. With cruft | |
1435 | * packs, pruning is implicit: any objects from existing | |
1436 | * packs that weren't picked up by new packs are removed | |
1437 | * when their packs are deleted. | |
1438 | * | |
1439 | * Generate an additional cruft pack, with one twist: | |
1440 | * `names` now includes the name of the cruft pack | |
1441 | * written in the previous step. So the contents of | |
1442 | * _this_ cruft pack exclude everything contained in the | |
1443 | * existing cruft pack (that is, all of the unreachable | |
1444 | * objects which are no older than | |
1445 | * `--cruft-expiration`). | |
1446 | * | |
1447 | * To make this work, cruft_expiration must become NULL | |
1448 | * so that this cruft pack doesn't actually prune any | |
1449 | * objects. If it were non-NULL, this call would always | |
1450 | * generate an empty pack (since every object not in the | |
1451 | * cruft pack generated above will have an mtime older | |
1452 | * than the expiration). | |
1453 | * | |
1454 | * Pretend we don't have a `--combine-cruft-below-size` | |
1455 | * argument, since we're not selectively combining | |
1456 | * anything based on size to generate the limbo cruft | |
1457 | * pack, but rather removing all cruft packs from the | |
1458 | * main repository regardless of size. | |
1459 | */ | |
1460 | ret = write_cruft_pack(&cruft_po_args, expire_to, | |
1461 | pack_prefix, | |
1462 | NULL, | |
1463 | 0ul, | |
1464 | &names, | |
1465 | &existing); | |
1466 | if (ret) | |
1467 | goto cleanup; | |
1468 | } | |
1469 | } | |
1470 | ||
1471 | if (po_args.filter_options.choice) { | |
1472 | if (!filter_to) | |
1473 | filter_to = packtmp; | |
1474 | ||
1475 | ret = write_filtered_pack(&po_args, | |
1476 | filter_to, | |
1477 | find_pack_prefix(packdir, packtmp), | |
1478 | &existing, | |
1479 | &names); | |
1480 | if (ret) | |
1481 | goto cleanup; | |
1482 | } | |
1483 | ||
1484 | string_list_sort(&names); | |
1485 | ||
1486 | close_object_store(the_repository->objects); | |
1487 | ||
1488 | /* | |
1489 | * Ok we have prepared all new packfiles. | |
1490 | */ | |
1491 | for_each_string_list_item(item, &names) { | |
1492 | struct generated_pack_data *data = item->util; | |
1493 | ||
1494 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { | |
1495 | char *fname; | |
1496 | ||
1497 | fname = mkpathdup("%s/pack-%s%s", | |
1498 | packdir, item->string, exts[ext].name); | |
1499 | ||
1500 | if (data->tempfiles[ext]) { | |
1501 | const char *fname_old = get_tempfile_path(data->tempfiles[ext]); | |
1502 | struct stat statbuffer; | |
1503 | ||
1504 | if (!stat(fname_old, &statbuffer)) { | |
1505 | statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); | |
1506 | chmod(fname_old, statbuffer.st_mode); | |
1507 | } | |
1508 | ||
1509 | if (rename_tempfile(&data->tempfiles[ext], fname)) | |
1510 | die_errno(_("renaming pack to '%s' failed"), fname); | |
1511 | } else if (!exts[ext].optional) | |
1512 | die(_("pack-objects did not write a '%s' file for pack %s-%s"), | |
1513 | exts[ext].name, packtmp, item->string); | |
1514 | else if (unlink(fname) < 0 && errno != ENOENT) | |
1515 | die_errno(_("could not unlink: %s"), fname); | |
1516 | ||
1517 | free(fname); | |
1518 | } | |
1519 | } | |
1520 | /* End of pack replacement. */ | |
1521 | ||
1522 | if (delete_redundant && pack_everything & ALL_INTO_ONE) | |
1523 | mark_packs_for_deletion(&existing, &names); | |
1524 | ||
1525 | if (write_midx) { | |
1526 | struct string_list include = STRING_LIST_INIT_DUP; | |
1527 | midx_included_packs(&include, &existing, &names, &geometry); | |
1528 | ||
1529 | ret = write_midx_included_packs(&include, &geometry, &names, | |
1530 | refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL, | |
1531 | show_progress, write_bitmaps > 0); | |
1532 | ||
1533 | if (!ret && write_bitmaps) | |
1534 | remove_redundant_bitmaps(&include, packdir); | |
1535 | ||
1536 | string_list_clear(&include, 0); | |
1537 | ||
1538 | if (ret) | |
1539 | goto cleanup; | |
1540 | } | |
1541 | ||
1542 | reprepare_packed_git(the_repository); | |
1543 | ||
1544 | if (delete_redundant) { | |
1545 | int opts = 0; | |
1546 | remove_redundant_existing_packs(&existing); | |
1547 | ||
1548 | if (geometry.split_factor) | |
1549 | geometry_remove_redundant_packs(&geometry, &names, | |
1550 | &existing); | |
1551 | if (show_progress) | |
1552 | opts |= PRUNE_PACKED_VERBOSE; | |
1553 | prune_packed_objects(opts); | |
1554 | ||
1555 | if (!keep_unreachable && | |
1556 | (!(pack_everything & LOOSEN_UNREACHABLE) || | |
1557 | unpack_unreachable) && | |
1558 | is_repository_shallow(the_repository)) | |
1559 | prune_shallow(PRUNE_QUICK); | |
1560 | } | |
1561 | ||
1562 | if (run_update_server_info) | |
1563 | update_server_info(the_repository, 0); | |
1564 | ||
1565 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) { | |
1566 | unsigned flags = 0; | |
1567 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0)) | |
1568 | flags |= MIDX_WRITE_INCREMENTAL; | |
1569 | write_midx_file(the_repository, repo_get_object_directory(the_repository), | |
1570 | NULL, NULL, flags); | |
1571 | } | |
1572 | ||
1573 | cleanup: | |
1574 | string_list_clear(&keep_pack_list, 0); | |
1575 | string_list_clear(&names, 1); | |
1576 | existing_packs_release(&existing); | |
1577 | free_pack_geometry(&geometry); | |
1578 | pack_objects_args_release(&po_args); | |
1579 | pack_objects_args_release(&cruft_po_args); | |
1580 | ||
1581 | return ret; | |
1582 | } |