]>
Commit | Line | Data |
---|---|---|
a1bbc6c0 | 1 | #include "builtin.h" |
36bf1958 | 2 | #include "alloc.h" |
b2141fc1 | 3 | #include "config.h" |
a1bbc6c0 | 4 | #include "dir.h" |
32a8f510 | 5 | #include "environment.h" |
f394e093 | 6 | #include "gettext.h" |
41771fa4 | 7 | #include "hex.h" |
a1bbc6c0 SB |
8 | #include "parse-options.h" |
9 | #include "run-command.h" | |
623b80be | 10 | #include "server-info.h" |
a1bbc6c0 SB |
11 | #include "sigchain.h" |
12 | #include "strbuf.h" | |
13 | #include "string-list.h" | |
dbbcd44f | 14 | #include "strvec.h" |
525e18c0 | 15 | #include "midx.h" |
5d19e813 | 16 | #include "packfile.h" |
9460fd48 | 17 | #include "prune-packed.h" |
36f0f344 | 18 | #include "object-store.h" |
b14ed5ad | 19 | #include "promisor-remote.h" |
120ad2b0 | 20 | #include "shallow.h" |
33add2ad | 21 | #include "pack.h" |
324efc90 TB |
22 | #include "pack-bitmap.h" |
23 | #include "refs.h" | |
a1bbc6c0 | 24 | |
f9825d1c TB |
25 | #define ALL_INTO_ONE 1 |
26 | #define LOOSEN_UNREACHABLE 2 | |
27 | #define PACK_CRUFT 4 | |
28 | ||
72263ffc | 29 | #define DELETE_PACK 1 |
ddee3703 | 30 | #define CRUFT_PACK 2 |
72263ffc | 31 | |
f9825d1c | 32 | static int pack_everything; |
a1bbc6c0 | 33 | static int delta_base_offset = 1; |
ee34a2be | 34 | static int pack_kept_objects = -1; |
36eba032 | 35 | static int write_bitmaps = -1; |
16d75fa4 | 36 | static int use_delta_islands; |
a2565c48 | 37 | static int run_update_server_info = 1; |
a643157d | 38 | static char *packdir, *packtmp_name, *packtmp; |
a1bbc6c0 SB |
39 | |
40 | static const char *const git_repack_usage[] = { | |
9c9b4f2f | 41 | N_("git repack [<options>]"), |
a1bbc6c0 SB |
42 | NULL |
43 | }; | |
44 | ||
1c409a70 DT |
45 | static const char incremental_bitmap_conflict_error[] = N_( |
46 | "Incremental repacks are incompatible with bitmap indexes. Use\n" | |
b4eda05d | 47 | "--no-write-bitmap-index or disable the pack.writeBitmaps configuration." |
1c409a70 DT |
48 | ); |
49 | ||
4571324b TB |
50 | struct pack_objects_args { |
51 | const char *window; | |
52 | const char *window_memory; | |
53 | const char *depth; | |
54 | const char *threads; | |
55 | const char *max_pack_size; | |
56 | int no_reuse_delta; | |
57 | int no_reuse_object; | |
58 | int quiet; | |
59 | int local; | |
60 | }; | |
1c409a70 | 61 | |
a4e7e317 GC |
62 | static int repack_config(const char *var, const char *value, |
63 | const struct config_context *ctx, void *cb) | |
a1bbc6c0 | 64 | { |
4571324b | 65 | struct pack_objects_args *cruft_po_args = cb; |
a1bbc6c0 SB |
66 | if (!strcmp(var, "repack.usedeltabaseoffset")) { |
67 | delta_base_offset = git_config_bool(var, value); | |
68 | return 0; | |
69 | } | |
ee34a2be JK |
70 | if (!strcmp(var, "repack.packkeptobjects")) { |
71 | pack_kept_objects = git_config_bool(var, value); | |
72 | return 0; | |
73 | } | |
71d76cb4 JK |
74 | if (!strcmp(var, "repack.writebitmaps") || |
75 | !strcmp(var, "pack.writebitmaps")) { | |
d078d85b | 76 | write_bitmaps = git_config_bool(var, value); |
3198b89f JK |
77 | return 0; |
78 | } | |
16d75fa4 JK |
79 | if (!strcmp(var, "repack.usedeltaislands")) { |
80 | use_delta_islands = git_config_bool(var, value); | |
81 | return 0; | |
82 | } | |
a2565c48 PS |
83 | if (strcmp(var, "repack.updateserverinfo") == 0) { |
84 | run_update_server_info = git_config_bool(var, value); | |
85 | return 0; | |
86 | } | |
4571324b TB |
87 | if (!strcmp(var, "repack.cruftwindow")) |
88 | return git_config_string(&cruft_po_args->window, var, value); | |
89 | if (!strcmp(var, "repack.cruftwindowmemory")) | |
90 | return git_config_string(&cruft_po_args->window_memory, var, value); | |
91 | if (!strcmp(var, "repack.cruftdepth")) | |
92 | return git_config_string(&cruft_po_args->depth, var, value); | |
93 | if (!strcmp(var, "repack.cruftthreads")) | |
94 | return git_config_string(&cruft_po_args->threads, var, value); | |
a4e7e317 | 95 | return git_default_config(var, value, ctx, cb); |
a1bbc6c0 SB |
96 | } |
97 | ||
a1bbc6c0 | 98 | /* |
a169166d TB |
99 | * Adds all packs hex strings to either fname_nonkept_list or |
100 | * fname_kept_list based on whether each pack has a corresponding | |
101 | * .keep file or not. Packs without a .keep file are not to be kept | |
102 | * if we are going to pack everything into one file. | |
a1bbc6c0 | 103 | */ |
a169166d | 104 | static void collect_pack_filenames(struct string_list *fname_nonkept_list, |
90f838bc TB |
105 | struct string_list *fname_kept_list, |
106 | const struct string_list *extra_keep) | |
a1bbc6c0 SB |
107 | { |
108 | DIR *dir; | |
109 | struct dirent *e; | |
110 | char *fname; | |
a1bbc6c0 SB |
111 | |
112 | if (!(dir = opendir(packdir))) | |
113 | return; | |
114 | ||
115 | while ((e = readdir(dir)) != NULL) { | |
26936bfd | 116 | size_t len; |
ed7e5fc3 NTND |
117 | int i; |
118 | ||
90f838bc TB |
119 | if (!strip_suffix(e->d_name, ".pack", &len)) |
120 | continue; | |
121 | ||
ed7e5fc3 NTND |
122 | for (i = 0; i < extra_keep->nr; i++) |
123 | if (!fspathcmp(e->d_name, extra_keep->items[i].string)) | |
124 | break; | |
a1bbc6c0 | 125 | |
a1bbc6c0 SB |
126 | fname = xmemdupz(e->d_name, len); |
127 | ||
90f838bc | 128 | if ((extra_keep->nr > 0 && i < extra_keep->nr) || |
ddee3703 | 129 | (file_exists(mkpath("%s/%s.keep", packdir, fname)))) { |
90f838bc | 130 | string_list_append_nodup(fname_kept_list, fname); |
ddee3703 TB |
131 | } else { |
132 | struct string_list_item *item; | |
133 | item = string_list_append_nodup(fname_nonkept_list, | |
134 | fname); | |
135 | if (file_exists(mkpath("%s/%s.mtimes", packdir, fname))) | |
136 | item->util = (void*)(uintptr_t)CRUFT_PACK; | |
137 | } | |
a1bbc6c0 SB |
138 | } |
139 | closedir(dir); | |
4b5a808b VD |
140 | |
141 | string_list_sort(fname_kept_list); | |
a1bbc6c0 SB |
142 | } |
143 | ||
144 | static void remove_redundant_pack(const char *dir_name, const char *base_name) | |
145 | { | |
a1bbc6c0 | 146 | struct strbuf buf = STRBUF_INIT; |
59552fb3 | 147 | struct multi_pack_index *m = get_local_multi_pack_index(the_repository); |
e08f7bb0 TB |
148 | strbuf_addf(&buf, "%s.pack", base_name); |
149 | if (m && midx_contains_pack(m, buf.buf)) | |
150 | clear_midx_file(the_repository); | |
151 | strbuf_insertf(&buf, 0, "%s/", dir_name); | |
8434e85d | 152 | unlink_pack_path(buf.buf, 1); |
a1bbc6c0 SB |
153 | strbuf_release(&buf); |
154 | } | |
155 | ||
2b958e79 | 156 | static void prepare_pack_objects(struct child_process *cmd, |
4e7b65ba TB |
157 | const struct pack_objects_args *args, |
158 | const char *out) | |
2b958e79 | 159 | { |
22f9b7f3 | 160 | strvec_push(&cmd->args, "pack-objects"); |
2b958e79 | 161 | if (args->window) |
22f9b7f3 | 162 | strvec_pushf(&cmd->args, "--window=%s", args->window); |
2b958e79 | 163 | if (args->window_memory) |
22f9b7f3 | 164 | strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory); |
2b958e79 | 165 | if (args->depth) |
22f9b7f3 | 166 | strvec_pushf(&cmd->args, "--depth=%s", args->depth); |
2b958e79 | 167 | if (args->threads) |
22f9b7f3 | 168 | strvec_pushf(&cmd->args, "--threads=%s", args->threads); |
2b958e79 | 169 | if (args->max_pack_size) |
22f9b7f3 | 170 | strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size); |
2b958e79 | 171 | if (args->no_reuse_delta) |
22f9b7f3 | 172 | strvec_pushf(&cmd->args, "--no-reuse-delta"); |
2b958e79 | 173 | if (args->no_reuse_object) |
22f9b7f3 | 174 | strvec_pushf(&cmd->args, "--no-reuse-object"); |
2b958e79 | 175 | if (args->local) |
22f9b7f3 | 176 | strvec_push(&cmd->args, "--local"); |
2b958e79 | 177 | if (args->quiet) |
22f9b7f3 | 178 | strvec_push(&cmd->args, "--quiet"); |
2b958e79 | 179 | if (delta_base_offset) |
22f9b7f3 | 180 | strvec_push(&cmd->args, "--delta-base-offset"); |
4e7b65ba | 181 | strvec_push(&cmd->args, out); |
2b958e79 JT |
182 | cmd->git_cmd = 1; |
183 | cmd->out = -1; | |
184 | } | |
185 | ||
5d19e813 JT |
186 | /* |
187 | * Write oid to the given struct child_process's stdin, starting it first if | |
188 | * necessary. | |
189 | */ | |
be252d33 JK |
190 | static int write_oid(const struct object_id *oid, |
191 | struct packed_git *pack UNUSED, | |
192 | uint32_t pos UNUSED, void *data) | |
5d19e813 JT |
193 | { |
194 | struct child_process *cmd = data; | |
195 | ||
196 | if (cmd->in == -1) { | |
197 | if (start_command(cmd)) | |
c83d950e | 198 | die(_("could not start pack-objects to repack promisor objects")); |
5d19e813 JT |
199 | } |
200 | ||
dd336a55 | 201 | xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz); |
5d19e813 JT |
202 | xwrite(cmd->in, "\n", 1); |
203 | return 0; | |
204 | } | |
205 | ||
63f4d5cf JK |
206 | static struct { |
207 | const char *name; | |
208 | unsigned optional:1; | |
209 | } exts[] = { | |
210 | {".pack"}, | |
2f4ba2a8 | 211 | {".rev", 1}, |
94cd775a | 212 | {".mtimes", 1}, |
63f4d5cf JK |
213 | {".bitmap", 1}, |
214 | {".promisor", 1}, | |
4e58cedd | 215 | {".idx"}, |
63f4d5cf JK |
216 | }; |
217 | ||
d3d9c519 | 218 | struct generated_pack_data { |
9cf10d87 | 219 | struct tempfile *tempfiles[ARRAY_SIZE(exts)]; |
d3d9c519 JK |
220 | }; |
221 | ||
222 | static struct generated_pack_data *populate_pack_exts(const char *name) | |
704c4a5c TB |
223 | { |
224 | struct stat statbuf; | |
225 | struct strbuf path = STRBUF_INIT; | |
d3d9c519 | 226 | struct generated_pack_data *data = xcalloc(1, sizeof(*data)); |
704c4a5c TB |
227 | int i; |
228 | ||
229 | for (i = 0; i < ARRAY_SIZE(exts); i++) { | |
230 | strbuf_reset(&path); | |
231 | strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name); | |
232 | ||
233 | if (stat(path.buf, &statbuf)) | |
234 | continue; | |
235 | ||
9cf10d87 | 236 | data->tempfiles[i] = register_tempfile(path.buf); |
704c4a5c TB |
237 | } |
238 | ||
239 | strbuf_release(&path); | |
d3d9c519 | 240 | return data; |
704c4a5c TB |
241 | } |
242 | ||
5d19e813 JT |
243 | static void repack_promisor_objects(const struct pack_objects_args *args, |
244 | struct string_list *names) | |
245 | { | |
246 | struct child_process cmd = CHILD_PROCESS_INIT; | |
247 | FILE *out; | |
248 | struct strbuf line = STRBUF_INIT; | |
249 | ||
4e7b65ba | 250 | prepare_pack_objects(&cmd, args, packtmp); |
5d19e813 JT |
251 | cmd.in = -1; |
252 | ||
253 | /* | |
254 | * NEEDSWORK: Giving pack-objects only the OIDs without any ordering | |
255 | * hints may result in suboptimal deltas in the resulting pack. See if | |
256 | * the OIDs can be sent with fake paths such that pack-objects can use a | |
257 | * {type -> existing pack order} ordering when computing deltas instead | |
258 | * of a {type -> size} ordering, which may produce better deltas. | |
259 | */ | |
260 | for_each_packed_object(write_oid, &cmd, | |
261 | FOR_EACH_OBJECT_PROMISOR_ONLY); | |
262 | ||
e6432e0f | 263 | if (cmd.in == -1) { |
5d19e813 | 264 | /* No packed objects; cmd was never started */ |
e6432e0f | 265 | child_process_clear(&cmd); |
5d19e813 | 266 | return; |
e6432e0f | 267 | } |
5d19e813 JT |
268 | |
269 | close(cmd.in); | |
270 | ||
271 | out = xfdopen(cmd.out, "r"); | |
272 | while (strbuf_getline_lf(&line, out) != EOF) { | |
704c4a5c | 273 | struct string_list_item *item; |
5d19e813 | 274 | char *promisor_name; |
33add2ad | 275 | |
2f0c9e9a | 276 | if (line.len != the_hash_algo->hexsz) |
3813a89f | 277 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); |
704c4a5c | 278 | item = string_list_append(names, line.buf); |
5d19e813 JT |
279 | |
280 | /* | |
281 | * pack-objects creates the .pack and .idx files, but not the | |
282 | * .promisor file. Create the .promisor file, which is empty. | |
5374a290 JT |
283 | * |
284 | * NEEDSWORK: fetch-pack sometimes generates non-empty | |
285 | * .promisor files containing the ref names and associated | |
286 | * hashes at the point of generation of the corresponding | |
287 | * packfile, but this would not preserve their contents. Maybe | |
288 | * concatenate the contents of all .promisor files instead of | |
289 | * just creating a new empty file. | |
5d19e813 JT |
290 | */ |
291 | promisor_name = mkpathdup("%s-%s.promisor", packtmp, | |
292 | line.buf); | |
33add2ad | 293 | write_promisor_file(promisor_name, NULL, 0); |
704c4a5c | 294 | |
d3d9c519 | 295 | item->util = populate_pack_exts(item->string); |
704c4a5c | 296 | |
5d19e813 JT |
297 | free(promisor_name); |
298 | } | |
299 | fclose(out); | |
300 | if (finish_command(&cmd)) | |
c83d950e | 301 | die(_("could not finish pack-objects to repack promisor objects")); |
5d19e813 JT |
302 | } |
303 | ||
0fabafd0 TB |
304 | struct pack_geometry { |
305 | struct packed_git **pack; | |
306 | uint32_t pack_nr, pack_alloc; | |
307 | uint32_t split; | |
308 | }; | |
309 | ||
310 | static uint32_t geometry_pack_weight(struct packed_git *p) | |
311 | { | |
312 | if (open_pack_index(p)) | |
313 | die(_("cannot open index for %s"), p->pack_name); | |
314 | return p->num_objects; | |
315 | } | |
316 | ||
317 | static int geometry_cmp(const void *va, const void *vb) | |
318 | { | |
319 | uint32_t aw = geometry_pack_weight(*(struct packed_git **)va), | |
320 | bw = geometry_pack_weight(*(struct packed_git **)vb); | |
321 | ||
322 | if (aw < bw) | |
323 | return -1; | |
324 | if (aw > bw) | |
325 | return 1; | |
326 | return 0; | |
327 | } | |
328 | ||
4b5a808b | 329 | static void init_pack_geometry(struct pack_geometry **geometry_p, |
932c16c0 PS |
330 | struct string_list *existing_kept_packs, |
331 | const struct pack_objects_args *args) | |
0fabafd0 TB |
332 | { |
333 | struct packed_git *p; | |
334 | struct pack_geometry *geometry; | |
4b5a808b | 335 | struct strbuf buf = STRBUF_INIT; |
0fabafd0 TB |
336 | |
337 | *geometry_p = xcalloc(1, sizeof(struct pack_geometry)); | |
338 | geometry = *geometry_p; | |
339 | ||
340 | for (p = get_all_packs(the_repository); p; p = p->next) { | |
932c16c0 PS |
341 | if (args->local && !p->pack_local) |
342 | /* | |
343 | * When asked to only repack local packfiles we skip | |
344 | * over any packfiles that are borrowed from alternate | |
345 | * object directories. | |
346 | */ | |
347 | continue; | |
348 | ||
4b5a808b VD |
349 | if (!pack_kept_objects) { |
350 | /* | |
351 | * Any pack that has its pack_keep bit set will appear | |
352 | * in existing_kept_packs below, but this saves us from | |
353 | * doing a more expensive check. | |
354 | */ | |
355 | if (p->pack_keep) | |
356 | continue; | |
357 | ||
358 | /* | |
359 | * The pack may be kept via the --keep-pack option; | |
360 | * check 'existing_kept_packs' to determine whether to | |
361 | * ignore it. | |
362 | */ | |
363 | strbuf_reset(&buf); | |
364 | strbuf_addstr(&buf, pack_basename(p)); | |
365 | strbuf_strip_suffix(&buf, ".pack"); | |
366 | ||
367 | if (string_list_has_string(existing_kept_packs, buf.buf)) | |
368 | continue; | |
369 | } | |
f9825d1c TB |
370 | if (p->is_cruft) |
371 | continue; | |
0fabafd0 TB |
372 | |
373 | ALLOC_GROW(geometry->pack, | |
374 | geometry->pack_nr + 1, | |
375 | geometry->pack_alloc); | |
376 | ||
377 | geometry->pack[geometry->pack_nr] = p; | |
378 | geometry->pack_nr++; | |
379 | } | |
380 | ||
381 | QSORT(geometry->pack, geometry->pack_nr, geometry_cmp); | |
4b5a808b | 382 | strbuf_release(&buf); |
0fabafd0 TB |
383 | } |
384 | ||
385 | static void split_pack_geometry(struct pack_geometry *geometry, int factor) | |
386 | { | |
387 | uint32_t i; | |
388 | uint32_t split; | |
389 | off_t total_size = 0; | |
390 | ||
f25e33c1 | 391 | if (!geometry->pack_nr) { |
0fabafd0 TB |
392 | geometry->split = geometry->pack_nr; |
393 | return; | |
394 | } | |
395 | ||
0fabafd0 TB |
396 | /* |
397 | * First, count the number of packs (in descending order of size) which | |
398 | * already form a geometric progression. | |
399 | */ | |
400 | for (i = geometry->pack_nr - 1; i > 0; i--) { | |
401 | struct packed_git *ours = geometry->pack[i]; | |
402 | struct packed_git *prev = geometry->pack[i - 1]; | |
2a159641 TB |
403 | |
404 | if (unsigned_mult_overflows(factor, geometry_pack_weight(prev))) | |
405 | die(_("pack %s too large to consider in geometric " | |
406 | "progression"), | |
407 | prev->pack_name); | |
408 | ||
13d746a3 | 409 | if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev)) |
0fabafd0 TB |
410 | break; |
411 | } | |
412 | ||
13d746a3 TB |
413 | split = i; |
414 | ||
0fabafd0 TB |
415 | if (split) { |
416 | /* | |
417 | * Move the split one to the right, since the top element in the | |
418 | * last-compared pair can't be in the progression. Only do this | |
419 | * when we split in the middle of the array (otherwise if we got | |
420 | * to the end, then the split is in the right place). | |
421 | */ | |
422 | split++; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Then, anything to the left of 'split' must be in a new pack. But, | |
427 | * creating that new pack may cause packs in the heavy half to no longer | |
428 | * form a geometric progression. | |
429 | * | |
430 | * Compute an expected size of the new pack, and then determine how many | |
431 | * packs in the heavy half need to be joined into it (if any) to restore | |
432 | * the geometric progression. | |
433 | */ | |
2a159641 TB |
434 | for (i = 0; i < split; i++) { |
435 | struct packed_git *p = geometry->pack[i]; | |
436 | ||
437 | if (unsigned_add_overflows(total_size, geometry_pack_weight(p))) | |
438 | die(_("pack %s too large to roll up"), p->pack_name); | |
439 | total_size += geometry_pack_weight(p); | |
440 | } | |
0fabafd0 TB |
441 | for (i = split; i < geometry->pack_nr; i++) { |
442 | struct packed_git *ours = geometry->pack[i]; | |
2a159641 TB |
443 | |
444 | if (unsigned_mult_overflows(factor, total_size)) | |
445 | die(_("pack %s too large to roll up"), ours->pack_name); | |
446 | ||
0fabafd0 | 447 | if (geometry_pack_weight(ours) < factor * total_size) { |
2a159641 TB |
448 | if (unsigned_add_overflows(total_size, |
449 | geometry_pack_weight(ours))) | |
450 | die(_("pack %s too large to roll up"), | |
451 | ours->pack_name); | |
452 | ||
0fabafd0 TB |
453 | split++; |
454 | total_size += geometry_pack_weight(ours); | |
455 | } else | |
456 | break; | |
457 | } | |
458 | ||
459 | geometry->split = split; | |
460 | } | |
461 | ||
3d74a233 | 462 | static struct packed_git *get_preferred_pack(struct pack_geometry *geometry) |
6d08b9d4 | 463 | { |
3d74a233 PS |
464 | uint32_t i; |
465 | ||
6d08b9d4 TB |
466 | if (!geometry) { |
467 | /* | |
468 | * No geometry means either an all-into-one repack (in which | |
469 | * case there is only one pack left and it is the largest) or an | |
470 | * incremental one. | |
471 | * | |
472 | * If repacking incrementally, then we could check the size of | |
473 | * all packs to determine which should be preferred, but leave | |
474 | * this for later. | |
475 | */ | |
476 | return NULL; | |
477 | } | |
478 | if (geometry->split == geometry->pack_nr) | |
479 | return NULL; | |
3d74a233 PS |
480 | |
481 | /* | |
482 | * The preferred pack is the largest pack above the split line. In | |
483 | * other words, it is the largest pack that does not get rolled up in | |
484 | * the geometric repack. | |
485 | */ | |
486 | for (i = geometry->pack_nr; i > geometry->split; i--) | |
487 | /* | |
488 | * A pack that is not local would never be included in a | |
489 | * multi-pack index. We thus skip over any non-local packs. | |
490 | */ | |
491 | if (geometry->pack[i - 1]->pack_local) | |
492 | return geometry->pack[i - 1]; | |
493 | ||
494 | return NULL; | |
6d08b9d4 TB |
495 | } |
496 | ||
0fabafd0 TB |
497 | static void clear_pack_geometry(struct pack_geometry *geometry) |
498 | { | |
499 | if (!geometry) | |
500 | return; | |
501 | ||
502 | free(geometry->pack); | |
503 | geometry->pack_nr = 0; | |
504 | geometry->pack_alloc = 0; | |
505 | geometry->split = 0; | |
506 | } | |
507 | ||
324efc90 TB |
508 | struct midx_snapshot_ref_data { |
509 | struct tempfile *f; | |
510 | struct oidset seen; | |
511 | int preferred; | |
512 | }; | |
513 | ||
5cf88fd8 | 514 | static int midx_snapshot_ref_one(const char *refname UNUSED, |
324efc90 | 515 | const struct object_id *oid, |
5cf88fd8 | 516 | int flag UNUSED, void *_data) |
324efc90 TB |
517 | { |
518 | struct midx_snapshot_ref_data *data = _data; | |
519 | struct object_id peeled; | |
520 | ||
521 | if (!peel_iterated_oid(oid, &peeled)) | |
522 | oid = &peeled; | |
523 | ||
524 | if (oidset_insert(&data->seen, oid)) | |
525 | return 0; /* already seen */ | |
526 | ||
527 | if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT) | |
528 | return 0; | |
529 | ||
530 | fprintf(data->f->fp, "%s%s\n", data->preferred ? "+" : "", | |
531 | oid_to_hex(oid)); | |
532 | ||
533 | return 0; | |
534 | } | |
535 | ||
536 | static void midx_snapshot_refs(struct tempfile *f) | |
537 | { | |
538 | struct midx_snapshot_ref_data data; | |
539 | const struct string_list *preferred = bitmap_preferred_tips(the_repository); | |
540 | ||
541 | data.f = f; | |
542 | data.preferred = 0; | |
543 | oidset_init(&data.seen, 0); | |
544 | ||
545 | if (!fdopen_tempfile(f, "w")) | |
546 | die(_("could not open tempfile %s for writing"), | |
547 | get_tempfile_path(f)); | |
548 | ||
549 | if (preferred) { | |
550 | struct string_list_item *item; | |
551 | ||
552 | data.preferred = 1; | |
553 | for_each_string_list_item(item, preferred) | |
554 | for_each_ref_in(item->string, midx_snapshot_ref_one, &data); | |
555 | data.preferred = 0; | |
556 | } | |
557 | ||
558 | for_each_ref(midx_snapshot_ref_one, &data); | |
559 | ||
560 | if (close_tempfile_gently(f)) { | |
561 | int save_errno = errno; | |
562 | delete_tempfile(&f); | |
563 | errno = save_errno; | |
564 | die_errno(_("could not close refs snapshot tempfile")); | |
565 | } | |
566 | ||
567 | oidset_clear(&data.seen); | |
568 | } | |
569 | ||
1d89d88d TB |
570 | static void midx_included_packs(struct string_list *include, |
571 | struct string_list *existing_nonkept_packs, | |
572 | struct string_list *existing_kept_packs, | |
573 | struct string_list *names, | |
574 | struct pack_geometry *geometry) | |
575 | { | |
576 | struct string_list_item *item; | |
577 | ||
578 | for_each_string_list_item(item, existing_kept_packs) | |
579 | string_list_insert(include, xstrfmt("%s.idx", item->string)); | |
580 | for_each_string_list_item(item, names) | |
581 | string_list_insert(include, xstrfmt("pack-%s.idx", item->string)); | |
582 | if (geometry) { | |
583 | struct strbuf buf = STRBUF_INIT; | |
584 | uint32_t i; | |
585 | for (i = geometry->split; i < geometry->pack_nr; i++) { | |
586 | struct packed_git *p = geometry->pack[i]; | |
587 | ||
51861340 PS |
588 | /* |
589 | * The multi-pack index never refers to packfiles part | |
590 | * of an alternate object database, so we skip these. | |
591 | * While git-multi-pack-index(1) would silently ignore | |
592 | * them anyway, this allows us to skip executing the | |
593 | * command completely when we have only non-local | |
594 | * packfiles. | |
595 | */ | |
596 | if (!p->pack_local) | |
597 | continue; | |
598 | ||
1d89d88d TB |
599 | strbuf_addstr(&buf, pack_basename(p)); |
600 | strbuf_strip_suffix(&buf, ".pack"); | |
601 | strbuf_addstr(&buf, ".idx"); | |
602 | ||
603 | string_list_insert(include, strbuf_detach(&buf, NULL)); | |
604 | } | |
ddee3703 TB |
605 | |
606 | for_each_string_list_item(item, existing_nonkept_packs) { | |
607 | if (!((uintptr_t)item->util & CRUFT_PACK)) { | |
608 | /* | |
609 | * no need to check DELETE_PACK, since we're not | |
610 | * doing an ALL_INTO_ONE repack | |
611 | */ | |
612 | continue; | |
613 | } | |
614 | string_list_insert(include, xstrfmt("%s.idx", item->string)); | |
615 | } | |
1d89d88d TB |
616 | } else { |
617 | for_each_string_list_item(item, existing_nonkept_packs) { | |
72263ffc | 618 | if ((uintptr_t)item->util & DELETE_PACK) |
1d89d88d TB |
619 | continue; |
620 | string_list_insert(include, xstrfmt("%s.idx", item->string)); | |
621 | } | |
622 | } | |
623 | } | |
624 | ||
625 | static int write_midx_included_packs(struct string_list *include, | |
6d08b9d4 | 626 | struct pack_geometry *geometry, |
324efc90 | 627 | const char *refs_snapshot, |
1d89d88d TB |
628 | int show_progress, int write_bitmaps) |
629 | { | |
630 | struct child_process cmd = CHILD_PROCESS_INIT; | |
631 | struct string_list_item *item; | |
3d74a233 | 632 | struct packed_git *preferred = get_preferred_pack(geometry); |
1d89d88d TB |
633 | FILE *in; |
634 | int ret; | |
635 | ||
636 | if (!include->nr) | |
637 | return 0; | |
638 | ||
639 | cmd.in = -1; | |
640 | cmd.git_cmd = 1; | |
641 | ||
642 | strvec_push(&cmd.args, "multi-pack-index"); | |
643 | strvec_pushl(&cmd.args, "write", "--stdin-packs", NULL); | |
644 | ||
645 | if (show_progress) | |
646 | strvec_push(&cmd.args, "--progress"); | |
647 | else | |
648 | strvec_push(&cmd.args, "--no-progress"); | |
649 | ||
650 | if (write_bitmaps) | |
651 | strvec_push(&cmd.args, "--bitmap"); | |
652 | ||
3d74a233 | 653 | if (preferred) |
6d08b9d4 | 654 | strvec_pushf(&cmd.args, "--preferred-pack=%s", |
3d74a233 | 655 | pack_basename(preferred)); |
6d08b9d4 | 656 | |
324efc90 TB |
657 | if (refs_snapshot) |
658 | strvec_pushf(&cmd.args, "--refs-snapshot=%s", refs_snapshot); | |
659 | ||
1d89d88d TB |
660 | ret = start_command(&cmd); |
661 | if (ret) | |
662 | return ret; | |
663 | ||
664 | in = xfdopen(cmd.in, "w"); | |
665 | for_each_string_list_item(item, include) | |
666 | fprintf(in, "%s\n", item->string); | |
667 | fclose(in); | |
668 | ||
669 | return finish_command(&cmd); | |
670 | } | |
671 | ||
55d902cd TB |
672 | static void remove_redundant_bitmaps(struct string_list *include, |
673 | const char *packdir) | |
674 | { | |
675 | struct strbuf path = STRBUF_INIT; | |
676 | struct string_list_item *item; | |
677 | size_t packdir_len; | |
678 | ||
679 | strbuf_addstr(&path, packdir); | |
680 | strbuf_addch(&path, '/'); | |
681 | packdir_len = path.len; | |
682 | ||
683 | /* | |
684 | * Remove any pack bitmaps corresponding to packs which are now | |
685 | * included in the MIDX. | |
686 | */ | |
687 | for_each_string_list_item(item, include) { | |
688 | strbuf_addstr(&path, item->string); | |
689 | strbuf_strip_suffix(&path, ".idx"); | |
690 | strbuf_addstr(&path, ".bitmap"); | |
691 | ||
692 | if (unlink(path.buf) && errno != ENOENT) | |
693 | warning_errno(_("could not remove stale bitmap: %s"), | |
694 | path.buf); | |
695 | ||
696 | strbuf_setlen(&path, packdir_len); | |
697 | } | |
698 | strbuf_release(&path); | |
699 | } | |
700 | ||
f9825d1c | 701 | static int write_cruft_pack(const struct pack_objects_args *args, |
c12cda47 | 702 | const char *destination, |
f9825d1c | 703 | const char *pack_prefix, |
eddad368 | 704 | const char *cruft_expiration, |
f9825d1c TB |
705 | struct string_list *names, |
706 | struct string_list *existing_packs, | |
707 | struct string_list *existing_kept_packs) | |
708 | { | |
709 | struct child_process cmd = CHILD_PROCESS_INIT; | |
710 | struct strbuf line = STRBUF_INIT; | |
711 | struct string_list_item *item; | |
712 | FILE *in, *out; | |
713 | int ret; | |
c12cda47 TB |
714 | const char *scratch; |
715 | int local = skip_prefix(destination, packdir, &scratch); | |
f9825d1c | 716 | |
c12cda47 | 717 | prepare_pack_objects(&cmd, args, destination); |
f9825d1c TB |
718 | |
719 | strvec_push(&cmd.args, "--cruft"); | |
720 | if (cruft_expiration) | |
721 | strvec_pushf(&cmd.args, "--cruft-expiration=%s", | |
722 | cruft_expiration); | |
723 | ||
724 | strvec_push(&cmd.args, "--honor-pack-keep"); | |
725 | strvec_push(&cmd.args, "--non-empty"); | |
726 | strvec_push(&cmd.args, "--max-pack-size=0"); | |
727 | ||
728 | cmd.in = -1; | |
729 | ||
730 | ret = start_command(&cmd); | |
731 | if (ret) | |
732 | return ret; | |
733 | ||
734 | /* | |
735 | * names has a confusing double use: it both provides the list | |
736 | * of just-written new packs, and accepts the name of the cruft | |
737 | * pack we are writing. | |
738 | * | |
739 | * By the time it is read here, it contains only the pack(s) | |
740 | * that were just written, which is exactly the set of packs we | |
741 | * want to consider kept. | |
91badeba TB |
742 | * |
743 | * If `--expire-to` is given, the double-use served by `names` | |
744 | * ensures that the pack written to `--expire-to` excludes any | |
745 | * objects contained in the cruft pack. | |
f9825d1c TB |
746 | */ |
747 | in = xfdopen(cmd.in, "w"); | |
748 | for_each_string_list_item(item, names) | |
749 | fprintf(in, "%s-%s.pack\n", pack_prefix, item->string); | |
750 | for_each_string_list_item(item, existing_packs) | |
751 | fprintf(in, "-%s.pack\n", item->string); | |
752 | for_each_string_list_item(item, existing_kept_packs) | |
753 | fprintf(in, "%s.pack\n", item->string); | |
754 | fclose(in); | |
755 | ||
756 | out = xfdopen(cmd.out, "r"); | |
757 | while (strbuf_getline_lf(&line, out) != EOF) { | |
b639606f JK |
758 | struct string_list_item *item; |
759 | ||
f9825d1c TB |
760 | if (line.len != the_hash_algo->hexsz) |
761 | die(_("repack: Expecting full hex object ID lines only " | |
762 | "from pack-objects.")); | |
c12cda47 TB |
763 | /* |
764 | * avoid putting packs written outside of the repository in the | |
765 | * list of names | |
766 | */ | |
ad909688 TB |
767 | if (local) { |
768 | item = string_list_append(names, line.buf); | |
769 | item->util = populate_pack_exts(line.buf); | |
770 | } | |
f9825d1c TB |
771 | } |
772 | fclose(out); | |
773 | ||
774 | strbuf_release(&line); | |
775 | ||
776 | return finish_command(&cmd); | |
777 | } | |
778 | ||
a1bbc6c0 SB |
779 | int cmd_repack(int argc, const char **argv, const char *prefix) |
780 | { | |
d3180279 | 781 | struct child_process cmd = CHILD_PROCESS_INIT; |
a1bbc6c0 | 782 | struct string_list_item *item; |
a1bbc6c0 | 783 | struct string_list names = STRING_LIST_INIT_DUP; |
a169166d | 784 | struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP; |
90f838bc | 785 | struct string_list existing_kept_packs = STRING_LIST_INIT_DUP; |
0fabafd0 | 786 | struct pack_geometry *geometry = NULL; |
a1bbc6c0 | 787 | struct strbuf line = STRBUF_INIT; |
324efc90 | 788 | struct tempfile *refs_snapshot = NULL; |
2fcb03b5 | 789 | int i, ext, ret; |
a1bbc6c0 | 790 | FILE *out; |
47ca93d0 | 791 | int show_progress; |
a1bbc6c0 SB |
792 | |
793 | /* variables to be filled by option parsing */ | |
a1bbc6c0 | 794 | int delete_redundant = 0; |
aa8bd519 | 795 | const char *unpack_unreachable = NULL; |
905f27b8 | 796 | int keep_unreachable = 0; |
ed7e5fc3 | 797 | struct string_list keep_pack_list = STRING_LIST_INIT_NODUP; |
2b958e79 | 798 | struct pack_objects_args po_args = {NULL}; |
4571324b | 799 | struct pack_objects_args cruft_po_args = {NULL}; |
0fabafd0 | 800 | int geometric_factor = 0; |
1d89d88d | 801 | int write_midx = 0; |
eddad368 | 802 | const char *cruft_expiration = NULL; |
91badeba | 803 | const char *expire_to = NULL; |
a1bbc6c0 SB |
804 | |
805 | struct option builtin_repack_options[] = { | |
806 | OPT_BIT('a', NULL, &pack_everything, | |
807 | N_("pack everything in a single pack"), ALL_INTO_ONE), | |
808 | OPT_BIT('A', NULL, &pack_everything, | |
809 | N_("same as -a, and turn unreachable objects loose"), | |
810 | LOOSEN_UNREACHABLE | ALL_INTO_ONE), | |
f9825d1c TB |
811 | OPT_BIT(0, "cruft", &pack_everything, |
812 | N_("same as -a, pack unreachable cruft objects separately"), | |
813 | PACK_CRUFT), | |
814 | OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"), | |
c512f311 | 815 | N_("with --cruft, expire objects older than this")), |
a1bbc6c0 SB |
816 | OPT_BOOL('d', NULL, &delete_redundant, |
817 | N_("remove redundant packs, and run git-prune-packed")), | |
2b958e79 | 818 | OPT_BOOL('f', NULL, &po_args.no_reuse_delta, |
a1bbc6c0 | 819 | N_("pass --no-reuse-delta to git-pack-objects")), |
2b958e79 | 820 | OPT_BOOL('F', NULL, &po_args.no_reuse_object, |
a1bbc6c0 | 821 | N_("pass --no-reuse-object to git-pack-objects")), |
64a6151d PS |
822 | OPT_NEGBIT('n', NULL, &run_update_server_info, |
823 | N_("do not run git-update-server-info"), 1), | |
2b958e79 JT |
824 | OPT__QUIET(&po_args.quiet, N_("be quiet")), |
825 | OPT_BOOL('l', "local", &po_args.local, | |
a1bbc6c0 | 826 | N_("pass --local to git-pack-objects")), |
d078d85b | 827 | OPT_BOOL('b', "write-bitmap-index", &write_bitmaps, |
5cf2741c | 828 | N_("write bitmap index")), |
16d75fa4 JK |
829 | OPT_BOOL('i', "delta-islands", &use_delta_islands, |
830 | N_("pass --delta-islands to git-pack-objects")), | |
a1bbc6c0 SB |
831 | OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"), |
832 | N_("with -A, do not loosen objects older than this")), | |
905f27b8 JK |
833 | OPT_BOOL('k', "keep-unreachable", &keep_unreachable, |
834 | N_("with -a, repack unreachable objects")), | |
2b958e79 | 835 | OPT_STRING(0, "window", &po_args.window, N_("n"), |
a1bbc6c0 | 836 | N_("size of the window used for delta compression")), |
2b958e79 | 837 | OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"), |
a1bbc6c0 | 838 | N_("same as the above, but limit memory size instead of entries count")), |
2b958e79 | 839 | OPT_STRING(0, "depth", &po_args.depth, N_("n"), |
a1bbc6c0 | 840 | N_("limits the maximum delta depth")), |
2b958e79 | 841 | OPT_STRING(0, "threads", &po_args.threads, N_("n"), |
40bcf318 | 842 | N_("limits the maximum number of threads")), |
2b958e79 | 843 | OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"), |
a1bbc6c0 | 844 | N_("maximum size of each packfile")), |
ee34a2be JK |
845 | OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects, |
846 | N_("repack objects in packs marked with .keep")), | |
ed7e5fc3 NTND |
847 | OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"), |
848 | N_("do not repack this pack")), | |
0fabafd0 TB |
849 | OPT_INTEGER('g', "geometric", &geometric_factor, |
850 | N_("find a geometric progression with factor <N>")), | |
1d89d88d TB |
851 | OPT_BOOL('m', "write-midx", &write_midx, |
852 | N_("write a multi-pack index of the resulting packs")), | |
91badeba TB |
853 | OPT_STRING(0, "expire-to", &expire_to, N_("dir"), |
854 | N_("pack prefix to store a pack containing pruned objects")), | |
a1bbc6c0 SB |
855 | OPT_END() |
856 | }; | |
857 | ||
4571324b | 858 | git_config(repack_config, &cruft_po_args); |
a1bbc6c0 SB |
859 | |
860 | argc = parse_options(argc, argv, prefix, builtin_repack_options, | |
861 | git_repack_usage, 0); | |
862 | ||
067fbd41 JK |
863 | if (delete_redundant && repository_format_precious_objects) |
864 | die(_("cannot delete packs in a precious-objects repo")); | |
865 | ||
905f27b8 JK |
866 | if (keep_unreachable && |
867 | (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))) | |
12909b6b | 868 | die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A"); |
905f27b8 | 869 | |
f9825d1c TB |
870 | if (pack_everything & PACK_CRUFT) { |
871 | pack_everything |= ALL_INTO_ONE; | |
872 | ||
873 | if (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)) | |
874 | die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A"); | |
875 | if (keep_unreachable) | |
876 | die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k"); | |
877 | } | |
878 | ||
73284822 | 879 | if (write_bitmaps < 0) { |
1d89d88d TB |
880 | if (!write_midx && |
881 | (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository())) | |
25575015 | 882 | write_bitmaps = 0; |
ff1e653c TB |
883 | } else if (write_bitmaps && |
884 | git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) && | |
885 | git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) { | |
886 | write_bitmaps = 0; | |
73284822 | 887 | } |
ee34a2be | 888 | if (pack_kept_objects < 0) |
e4d0c11c | 889 | pack_kept_objects = write_bitmaps > 0 && !write_midx; |
ee34a2be | 890 | |
1d89d88d | 891 | if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx) |
1c409a70 DT |
892 | die(_(incremental_bitmap_conflict_error)); |
893 | ||
d85cd187 PS |
894 | if (write_bitmaps && po_args.local && has_alt_odb(the_repository)) { |
895 | /* | |
896 | * When asked to do a local repack, but we have | |
897 | * packfiles that are inherited from an alternate, then | |
898 | * we cannot guarantee that the multi-pack-index would | |
899 | * have full coverage of all objects. We thus disable | |
900 | * writing bitmaps in that case. | |
901 | */ | |
902 | warning(_("disabling bitmap writing, as some objects are not being packed")); | |
903 | write_bitmaps = 0; | |
904 | } | |
905 | ||
324efc90 TB |
906 | if (write_midx && write_bitmaps) { |
907 | struct strbuf path = STRBUF_INIT; | |
908 | ||
909 | strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(), | |
910 | "bitmap-ref-tips"); | |
911 | ||
912 | refs_snapshot = xmks_tempfile(path.buf); | |
913 | midx_snapshot_refs(refs_snapshot); | |
914 | ||
915 | strbuf_release(&path); | |
916 | } | |
917 | ||
4b5a808b VD |
918 | packdir = mkpathdup("%s/pack", get_object_directory()); |
919 | packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid()); | |
920 | packtmp = mkpathdup("%s/%s", packdir, packtmp_name); | |
921 | ||
922 | collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs, | |
923 | &keep_pack_list); | |
924 | ||
0fabafd0 TB |
925 | if (geometric_factor) { |
926 | if (pack_everything) | |
12909b6b | 927 | die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a"); |
932c16c0 | 928 | init_pack_geometry(&geometry, &existing_kept_packs, &po_args); |
0fabafd0 TB |
929 | split_pack_geometry(geometry, geometric_factor); |
930 | } | |
931 | ||
4e7b65ba | 932 | prepare_pack_objects(&cmd, &po_args, packtmp); |
2b958e79 | 933 | |
47ca93d0 DS |
934 | show_progress = !po_args.quiet && isatty(2); |
935 | ||
22f9b7f3 | 936 | strvec_push(&cmd.args, "--keep-true-parents"); |
ee34a2be | 937 | if (!pack_kept_objects) |
22f9b7f3 | 938 | strvec_push(&cmd.args, "--honor-pack-keep"); |
ed7e5fc3 | 939 | for (i = 0; i < keep_pack_list.nr; i++) |
22f9b7f3 | 940 | strvec_pushf(&cmd.args, "--keep-pack=%s", |
f6d8942b | 941 | keep_pack_list.items[i].string); |
22f9b7f3 | 942 | strvec_push(&cmd.args, "--non-empty"); |
0fabafd0 TB |
943 | if (!geometry) { |
944 | /* | |
ccae01ca JH |
945 | * We need to grab all reachable objects, including those that |
946 | * are reachable from reflogs and the index. | |
0fabafd0 | 947 | * |
ccae01ca JH |
948 | * When repacking into a geometric progression of packs, |
949 | * however, we ask 'git pack-objects --stdin-packs', and it is | |
950 | * not about packing objects based on reachability but about | |
951 | * repacking all the objects in specified packs and loose ones | |
952 | * (indeed, --stdin-packs is incompatible with these options). | |
0fabafd0 TB |
953 | */ |
954 | strvec_push(&cmd.args, "--all"); | |
955 | strvec_push(&cmd.args, "--reflog"); | |
956 | strvec_push(&cmd.args, "--indexed-objects"); | |
957 | } | |
a5183d76 | 958 | if (repo_has_promisor_remote(the_repository)) |
22f9b7f3 | 959 | strvec_push(&cmd.args, "--exclude-promisor-objects"); |
1d89d88d TB |
960 | if (!write_midx) { |
961 | if (write_bitmaps > 0) | |
962 | strvec_push(&cmd.args, "--write-bitmap-index"); | |
963 | else if (write_bitmaps < 0) | |
964 | strvec_push(&cmd.args, "--write-bitmap-index-quiet"); | |
965 | } | |
16d75fa4 | 966 | if (use_delta_islands) |
22f9b7f3 | 967 | strvec_push(&cmd.args, "--delta-islands"); |
a1bbc6c0 | 968 | |
90f838bc | 969 | if (pack_everything & ALL_INTO_ONE) { |
5d19e813 JT |
970 | repack_promisor_objects(&po_args, &names); |
971 | ||
f9825d1c TB |
972 | if (existing_nonkept_packs.nr && delete_redundant && |
973 | !(pack_everything & PACK_CRUFT)) { | |
a643157d RS |
974 | for_each_string_list_item(item, &names) { |
975 | strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack", | |
976 | packtmp_name, item->string); | |
977 | } | |
8d422993 | 978 | if (unpack_unreachable) { |
22f9b7f3 | 979 | strvec_pushf(&cmd.args, |
f6d8942b JK |
980 | "--unpack-unreachable=%s", |
981 | unpack_unreachable); | |
8d422993 | 982 | } else if (pack_everything & LOOSEN_UNREACHABLE) { |
22f9b7f3 | 983 | strvec_push(&cmd.args, |
f6d8942b | 984 | "--unpack-unreachable"); |
905f27b8 | 985 | } else if (keep_unreachable) { |
22f9b7f3 JK |
986 | strvec_push(&cmd.args, "--keep-unreachable"); |
987 | strvec_push(&cmd.args, "--pack-loose-unreachable"); | |
8d422993 | 988 | } |
a1bbc6c0 | 989 | } |
0fabafd0 TB |
990 | } else if (geometry) { |
991 | strvec_push(&cmd.args, "--stdin-packs"); | |
992 | strvec_push(&cmd.args, "--unpacked"); | |
a1bbc6c0 | 993 | } else { |
22f9b7f3 JK |
994 | strvec_push(&cmd.args, "--unpacked"); |
995 | strvec_push(&cmd.args, "--incremental"); | |
a1bbc6c0 SB |
996 | } |
997 | ||
0fabafd0 TB |
998 | if (geometry) |
999 | cmd.in = -1; | |
1000 | else | |
1001 | cmd.no_stdin = 1; | |
a1bbc6c0 SB |
1002 | |
1003 | ret = start_command(&cmd); | |
1004 | if (ret) | |
90428ddc | 1005 | goto cleanup; |
a1bbc6c0 | 1006 | |
0fabafd0 TB |
1007 | if (geometry) { |
1008 | FILE *in = xfdopen(cmd.in, "w"); | |
1009 | /* | |
1010 | * The resulting pack should contain all objects in packs that | |
1011 | * are going to be rolled up, but exclude objects in packs which | |
1012 | * are being left alone. | |
1013 | */ | |
1014 | for (i = 0; i < geometry->split; i++) | |
1015 | fprintf(in, "%s\n", pack_basename(geometry->pack[i])); | |
1016 | for (i = geometry->split; i < geometry->pack_nr; i++) | |
1017 | fprintf(in, "^%s\n", pack_basename(geometry->pack[i])); | |
1018 | fclose(in); | |
1019 | } | |
1020 | ||
a1bbc6c0 | 1021 | out = xfdopen(cmd.out, "r"); |
8f309aeb | 1022 | while (strbuf_getline_lf(&line, out) != EOF) { |
b639606f JK |
1023 | struct string_list_item *item; |
1024 | ||
2f0c9e9a | 1025 | if (line.len != the_hash_algo->hexsz) |
3813a89f | 1026 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); |
b639606f JK |
1027 | item = string_list_append(&names, line.buf); |
1028 | item->util = populate_pack_exts(item->string); | |
a1bbc6c0 | 1029 | } |
b6046abc | 1030 | strbuf_release(&line); |
a1bbc6c0 SB |
1031 | fclose(out); |
1032 | ret = finish_command(&cmd); | |
1033 | if (ret) | |
90428ddc | 1034 | goto cleanup; |
a1bbc6c0 | 1035 | |
2b958e79 | 1036 | if (!names.nr && !po_args.quiet) |
c83d950e | 1037 | printf_ln(_("Nothing new to pack.")); |
a1bbc6c0 | 1038 | |
f9825d1c TB |
1039 | if (pack_everything & PACK_CRUFT) { |
1040 | const char *pack_prefix; | |
1041 | if (!skip_prefix(packtmp, packdir, &pack_prefix)) | |
1042 | die(_("pack prefix %s does not begin with objdir %s"), | |
1043 | packtmp, packdir); | |
1044 | if (*pack_prefix == '/') | |
1045 | pack_prefix++; | |
1046 | ||
4571324b TB |
1047 | if (!cruft_po_args.window) |
1048 | cruft_po_args.window = po_args.window; | |
1049 | if (!cruft_po_args.window_memory) | |
1050 | cruft_po_args.window_memory = po_args.window_memory; | |
1051 | if (!cruft_po_args.depth) | |
1052 | cruft_po_args.depth = po_args.depth; | |
1053 | if (!cruft_po_args.threads) | |
1054 | cruft_po_args.threads = po_args.threads; | |
1055 | ||
1056 | cruft_po_args.local = po_args.local; | |
1057 | cruft_po_args.quiet = po_args.quiet; | |
1058 | ||
c12cda47 | 1059 | ret = write_cruft_pack(&cruft_po_args, packtmp, pack_prefix, |
eddad368 | 1060 | cruft_expiration, &names, |
f9825d1c TB |
1061 | &existing_nonkept_packs, |
1062 | &existing_kept_packs); | |
1063 | if (ret) | |
90428ddc | 1064 | goto cleanup; |
91badeba TB |
1065 | |
1066 | if (delete_redundant && expire_to) { | |
1067 | /* | |
1068 | * If `--expire-to` is given with `-d`, it's possible | |
1069 | * that we're about to prune some objects. With cruft | |
1070 | * packs, pruning is implicit: any objects from existing | |
1071 | * packs that weren't picked up by new packs are removed | |
1072 | * when their packs are deleted. | |
1073 | * | |
1074 | * Generate an additional cruft pack, with one twist: | |
1075 | * `names` now includes the name of the cruft pack | |
1076 | * written in the previous step. So the contents of | |
1077 | * _this_ cruft pack exclude everything contained in the | |
1078 | * existing cruft pack (that is, all of the unreachable | |
1079 | * objects which are no older than | |
1080 | * `--cruft-expiration`). | |
1081 | * | |
1082 | * To make this work, cruft_expiration must become NULL | |
1083 | * so that this cruft pack doesn't actually prune any | |
1084 | * objects. If it were non-NULL, this call would always | |
1085 | * generate an empty pack (since every object not in the | |
1086 | * cruft pack generated above will have an mtime older | |
1087 | * than the expiration). | |
1088 | */ | |
1089 | ret = write_cruft_pack(&cruft_po_args, expire_to, | |
1090 | pack_prefix, | |
1091 | NULL, | |
1092 | &names, | |
1093 | &existing_nonkept_packs, | |
1094 | &existing_kept_packs); | |
1095 | if (ret) | |
90428ddc | 1096 | goto cleanup; |
91badeba | 1097 | } |
f9825d1c TB |
1098 | } |
1099 | ||
66731ff9 TB |
1100 | string_list_sort(&names); |
1101 | ||
2d511cfc | 1102 | close_object_store(the_repository->objects); |
5bdece0d | 1103 | |
a1bbc6c0 SB |
1104 | /* |
1105 | * Ok we have prepared all new packfiles. | |
a1bbc6c0 | 1106 | */ |
a1bbc6c0 | 1107 | for_each_string_list_item(item, &names) { |
d3d9c519 JK |
1108 | struct generated_pack_data *data = item->util; |
1109 | ||
b328c216 | 1110 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
9cf10d87 | 1111 | char *fname; |
525e18c0 | 1112 | |
a1bbc6c0 | 1113 | fname = mkpathdup("%s/pack-%s%s", |
42a02d85 | 1114 | packdir, item->string, exts[ext].name); |
2fcb03b5 | 1115 | |
9cf10d87 JK |
1116 | if (data->tempfiles[ext]) { |
1117 | const char *fname_old = get_tempfile_path(data->tempfiles[ext]); | |
2fcb03b5 | 1118 | struct stat statbuffer; |
9cf10d87 | 1119 | |
2fcb03b5 TB |
1120 | if (!stat(fname_old, &statbuffer)) { |
1121 | statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); | |
1122 | chmod(fname_old, statbuffer.st_mode); | |
1123 | } | |
1124 | ||
9cf10d87 JK |
1125 | if (rename_tempfile(&data->tempfiles[ext], fname)) |
1126 | die_errno(_("renaming pack to '%s' failed"), fname); | |
2fcb03b5 | 1127 | } else if (!exts[ext].optional) |
a4880b20 JK |
1128 | die(_("pack-objects did not write a '%s' file for pack %s-%s"), |
1129 | exts[ext].name, packtmp, item->string); | |
2fcb03b5 TB |
1130 | else if (unlink(fname) < 0 && errno != ENOENT) |
1131 | die_errno(_("could not unlink: %s"), fname); | |
a1bbc6c0 | 1132 | |
e3cf2303 | 1133 | free(fname); |
a1bbc6c0 SB |
1134 | } |
1135 | } | |
a1bbc6c0 SB |
1136 | /* End of pack replacement. */ |
1137 | ||
1d89d88d | 1138 | if (delete_redundant && pack_everything & ALL_INTO_ONE) { |
2f0c9e9a | 1139 | const int hexsz = the_hash_algo->hexsz; |
1d89d88d | 1140 | for_each_string_list_item(item, &existing_nonkept_packs) { |
a1bbc6c0 SB |
1141 | char *sha1; |
1142 | size_t len = strlen(item->string); | |
2f0c9e9a | 1143 | if (len < hexsz) |
a1bbc6c0 | 1144 | continue; |
2f0c9e9a | 1145 | sha1 = item->string + len - hexsz; |
1d89d88d TB |
1146 | /* |
1147 | * Mark this pack for deletion, which ensures that this | |
1148 | * pack won't be included in a MIDX (if `--write-midx` | |
1149 | * was given) and that we will actually delete this pack | |
1150 | * (if `-d` was given). | |
1151 | */ | |
72263ffc TB |
1152 | if (!string_list_has_string(&names, sha1)) |
1153 | item->util = (void*)(uintptr_t)((size_t)item->util | DELETE_PACK); | |
1d89d88d TB |
1154 | } |
1155 | } | |
1156 | ||
1157 | if (write_midx) { | |
1158 | struct string_list include = STRING_LIST_INIT_NODUP; | |
1159 | midx_included_packs(&include, &existing_nonkept_packs, | |
1160 | &existing_kept_packs, &names, geometry); | |
1161 | ||
6d08b9d4 | 1162 | ret = write_midx_included_packs(&include, geometry, |
324efc90 | 1163 | refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL, |
1d89d88d TB |
1164 | show_progress, write_bitmaps > 0); |
1165 | ||
55d902cd TB |
1166 | if (!ret && write_bitmaps) |
1167 | remove_redundant_bitmaps(&include, packdir); | |
1168 | ||
1d89d88d TB |
1169 | string_list_clear(&include, 0); |
1170 | ||
1171 | if (ret) | |
90428ddc | 1172 | goto cleanup; |
1d89d88d TB |
1173 | } |
1174 | ||
5d19e813 JT |
1175 | reprepare_packed_git(the_repository); |
1176 | ||
a1bbc6c0 | 1177 | if (delete_redundant) { |
4489a480 | 1178 | int opts = 0; |
1d89d88d | 1179 | for_each_string_list_item(item, &existing_nonkept_packs) { |
72263ffc | 1180 | if (!((uintptr_t)item->util & DELETE_PACK)) |
1d89d88d TB |
1181 | continue; |
1182 | remove_redundant_pack(packdir, item->string); | |
a1bbc6c0 | 1183 | } |
0fabafd0 TB |
1184 | |
1185 | if (geometry) { | |
1186 | struct strbuf buf = STRBUF_INIT; | |
1187 | ||
1188 | uint32_t i; | |
1189 | for (i = 0; i < geometry->split; i++) { | |
1190 | struct packed_git *p = geometry->pack[i]; | |
1191 | if (string_list_has_string(&names, | |
1192 | hash_to_hex(p->hash))) | |
1193 | continue; | |
1194 | ||
1195 | strbuf_reset(&buf); | |
1196 | strbuf_addstr(&buf, pack_basename(p)); | |
1197 | strbuf_strip_suffix(&buf, ".pack"); | |
1198 | ||
197443e8 TB |
1199 | if ((p->pack_keep) || |
1200 | (string_list_has_string(&existing_kept_packs, | |
1201 | buf.buf))) | |
1202 | continue; | |
1203 | ||
0fabafd0 TB |
1204 | remove_redundant_pack(packdir, buf.buf); |
1205 | } | |
1206 | strbuf_release(&buf); | |
1207 | } | |
47ca93d0 | 1208 | if (show_progress) |
4489a480 RS |
1209 | opts |= PRUNE_PACKED_VERBOSE; |
1210 | prune_packed_objects(opts); | |
5dcfbf56 JS |
1211 | |
1212 | if (!keep_unreachable && | |
1213 | (!(pack_everything & LOOSEN_UNREACHABLE) || | |
1214 | unpack_unreachable) && | |
1215 | is_repository_shallow(the_repository)) | |
1216 | prune_shallow(PRUNE_QUICK); | |
a1bbc6c0 SB |
1217 | } |
1218 | ||
64a6151d | 1219 | if (run_update_server_info) |
4489a480 | 1220 | update_server_info(0); |
0465a505 | 1221 | |
ff1e653c TB |
1222 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) { |
1223 | unsigned flags = 0; | |
1224 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) | |
1225 | flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX; | |
08944d1c | 1226 | write_midx_file(get_object_directory(), NULL, NULL, flags); |
ff1e653c | 1227 | } |
0465a505 | 1228 | |
90428ddc | 1229 | cleanup: |
d3d9c519 | 1230 | string_list_clear(&names, 1); |
a169166d | 1231 | string_list_clear(&existing_nonkept_packs, 0); |
90f838bc | 1232 | string_list_clear(&existing_kept_packs, 0); |
0fabafd0 | 1233 | clear_pack_geometry(geometry); |
a1bbc6c0 | 1234 | |
90428ddc | 1235 | return ret; |
a1bbc6c0 | 1236 | } |