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