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