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