]>
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; |
f9825d1c | 35 | static char *cruft_expiration; |
a1bbc6c0 SB |
36 | |
37 | static const char *const git_repack_usage[] = { | |
9c9b4f2f | 38 | N_("git repack [<options>]"), |
a1bbc6c0 SB |
39 | NULL |
40 | }; | |
41 | ||
1c409a70 DT |
42 | static const char incremental_bitmap_conflict_error[] = N_( |
43 | "Incremental repacks are incompatible with bitmap indexes. Use\n" | |
b4eda05d | 44 | "--no-write-bitmap-index or disable the pack.writeBitmaps configuration." |
1c409a70 DT |
45 | ); |
46 | ||
4571324b TB |
47 | struct pack_objects_args { |
48 | const char *window; | |
49 | const char *window_memory; | |
50 | const char *depth; | |
51 | const char *threads; | |
52 | const char *max_pack_size; | |
53 | int no_reuse_delta; | |
54 | int no_reuse_object; | |
55 | int quiet; | |
56 | int local; | |
57 | }; | |
1c409a70 | 58 | |
a1bbc6c0 SB |
59 | static int repack_config(const char *var, const char *value, void *cb) |
60 | { | |
4571324b | 61 | struct pack_objects_args *cruft_po_args = cb; |
a1bbc6c0 SB |
62 | if (!strcmp(var, "repack.usedeltabaseoffset")) { |
63 | delta_base_offset = git_config_bool(var, value); | |
64 | return 0; | |
65 | } | |
ee34a2be JK |
66 | if (!strcmp(var, "repack.packkeptobjects")) { |
67 | pack_kept_objects = git_config_bool(var, value); | |
68 | return 0; | |
69 | } | |
71d76cb4 JK |
70 | if (!strcmp(var, "repack.writebitmaps") || |
71 | !strcmp(var, "pack.writebitmaps")) { | |
d078d85b | 72 | write_bitmaps = git_config_bool(var, value); |
3198b89f JK |
73 | return 0; |
74 | } | |
16d75fa4 JK |
75 | if (!strcmp(var, "repack.usedeltaislands")) { |
76 | use_delta_islands = git_config_bool(var, value); | |
77 | return 0; | |
78 | } | |
a2565c48 PS |
79 | if (strcmp(var, "repack.updateserverinfo") == 0) { |
80 | run_update_server_info = git_config_bool(var, value); | |
81 | return 0; | |
82 | } | |
4571324b TB |
83 | if (!strcmp(var, "repack.cruftwindow")) |
84 | return git_config_string(&cruft_po_args->window, var, value); | |
85 | if (!strcmp(var, "repack.cruftwindowmemory")) | |
86 | return git_config_string(&cruft_po_args->window_memory, var, value); | |
87 | if (!strcmp(var, "repack.cruftdepth")) | |
88 | return git_config_string(&cruft_po_args->depth, var, value); | |
89 | if (!strcmp(var, "repack.cruftthreads")) | |
90 | return git_config_string(&cruft_po_args->threads, var, value); | |
a1bbc6c0 SB |
91 | return git_default_config(var, value, cb); |
92 | } | |
93 | ||
a1bbc6c0 | 94 | /* |
a169166d TB |
95 | * Adds all packs hex strings to either fname_nonkept_list or |
96 | * fname_kept_list based on whether each pack has a corresponding | |
97 | * .keep file or not. Packs without a .keep file are not to be kept | |
98 | * if we are going to pack everything into one file. | |
a1bbc6c0 | 99 | */ |
a169166d | 100 | static void collect_pack_filenames(struct string_list *fname_nonkept_list, |
90f838bc TB |
101 | struct string_list *fname_kept_list, |
102 | const struct string_list *extra_keep) | |
a1bbc6c0 SB |
103 | { |
104 | DIR *dir; | |
105 | struct dirent *e; | |
106 | char *fname; | |
a1bbc6c0 SB |
107 | |
108 | if (!(dir = opendir(packdir))) | |
109 | return; | |
110 | ||
111 | while ((e = readdir(dir)) != NULL) { | |
26936bfd | 112 | size_t len; |
ed7e5fc3 NTND |
113 | int i; |
114 | ||
90f838bc TB |
115 | if (!strip_suffix(e->d_name, ".pack", &len)) |
116 | continue; | |
117 | ||
ed7e5fc3 NTND |
118 | for (i = 0; i < extra_keep->nr; i++) |
119 | if (!fspathcmp(e->d_name, extra_keep->items[i].string)) | |
120 | break; | |
a1bbc6c0 | 121 | |
a1bbc6c0 SB |
122 | fname = xmemdupz(e->d_name, len); |
123 | ||
90f838bc | 124 | if ((extra_keep->nr > 0 && i < extra_keep->nr) || |
ddee3703 | 125 | (file_exists(mkpath("%s/%s.keep", packdir, fname)))) { |
90f838bc | 126 | string_list_append_nodup(fname_kept_list, fname); |
ddee3703 TB |
127 | } else { |
128 | struct string_list_item *item; | |
129 | item = string_list_append_nodup(fname_nonkept_list, | |
130 | fname); | |
131 | if (file_exists(mkpath("%s/%s.mtimes", packdir, fname))) | |
132 | item->util = (void*)(uintptr_t)CRUFT_PACK; | |
133 | } | |
a1bbc6c0 SB |
134 | } |
135 | closedir(dir); | |
4b5a808b VD |
136 | |
137 | string_list_sort(fname_kept_list); | |
a1bbc6c0 SB |
138 | } |
139 | ||
140 | static void remove_redundant_pack(const char *dir_name, const char *base_name) | |
141 | { | |
a1bbc6c0 | 142 | struct strbuf buf = STRBUF_INIT; |
59552fb3 | 143 | struct multi_pack_index *m = get_local_multi_pack_index(the_repository); |
e08f7bb0 TB |
144 | strbuf_addf(&buf, "%s.pack", base_name); |
145 | if (m && midx_contains_pack(m, buf.buf)) | |
146 | clear_midx_file(the_repository); | |
147 | strbuf_insertf(&buf, 0, "%s/", dir_name); | |
8434e85d | 148 | unlink_pack_path(buf.buf, 1); |
a1bbc6c0 SB |
149 | strbuf_release(&buf); |
150 | } | |
151 | ||
2b958e79 JT |
152 | static void prepare_pack_objects(struct child_process *cmd, |
153 | const struct pack_objects_args *args) | |
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 JK |
175 | strvec_push(&cmd->args, "--delta-base-offset"); |
176 | strvec_push(&cmd->args, packtmp); | |
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 | ||
244 | prepare_pack_objects(&cmd, args); | |
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 TB |
659 | static int write_cruft_pack(const struct pack_objects_args *args, |
660 | const char *pack_prefix, | |
661 | struct string_list *names, | |
662 | struct string_list *existing_packs, | |
663 | struct string_list *existing_kept_packs) | |
664 | { | |
665 | struct child_process cmd = CHILD_PROCESS_INIT; | |
666 | struct strbuf line = STRBUF_INIT; | |
667 | struct string_list_item *item; | |
668 | FILE *in, *out; | |
669 | int ret; | |
670 | ||
671 | prepare_pack_objects(&cmd, args); | |
672 | ||
673 | strvec_push(&cmd.args, "--cruft"); | |
674 | if (cruft_expiration) | |
675 | strvec_pushf(&cmd.args, "--cruft-expiration=%s", | |
676 | cruft_expiration); | |
677 | ||
678 | strvec_push(&cmd.args, "--honor-pack-keep"); | |
679 | strvec_push(&cmd.args, "--non-empty"); | |
680 | strvec_push(&cmd.args, "--max-pack-size=0"); | |
681 | ||
682 | cmd.in = -1; | |
683 | ||
684 | ret = start_command(&cmd); | |
685 | if (ret) | |
686 | return ret; | |
687 | ||
688 | /* | |
689 | * names has a confusing double use: it both provides the list | |
690 | * of just-written new packs, and accepts the name of the cruft | |
691 | * pack we are writing. | |
692 | * | |
693 | * By the time it is read here, it contains only the pack(s) | |
694 | * that were just written, which is exactly the set of packs we | |
695 | * want to consider kept. | |
696 | */ | |
697 | in = xfdopen(cmd.in, "w"); | |
698 | for_each_string_list_item(item, names) | |
699 | fprintf(in, "%s-%s.pack\n", pack_prefix, item->string); | |
700 | for_each_string_list_item(item, existing_packs) | |
701 | fprintf(in, "-%s.pack\n", item->string); | |
702 | for_each_string_list_item(item, existing_kept_packs) | |
703 | fprintf(in, "%s.pack\n", item->string); | |
704 | fclose(in); | |
705 | ||
706 | out = xfdopen(cmd.out, "r"); | |
707 | while (strbuf_getline_lf(&line, out) != EOF) { | |
b639606f JK |
708 | struct string_list_item *item; |
709 | ||
f9825d1c TB |
710 | if (line.len != the_hash_algo->hexsz) |
711 | die(_("repack: Expecting full hex object ID lines only " | |
712 | "from pack-objects.")); | |
b639606f JK |
713 | |
714 | item = string_list_append(names, line.buf); | |
715 | item->util = populate_pack_exts(line.buf); | |
f9825d1c TB |
716 | } |
717 | fclose(out); | |
718 | ||
719 | strbuf_release(&line); | |
720 | ||
721 | return finish_command(&cmd); | |
722 | } | |
723 | ||
a1bbc6c0 SB |
724 | int cmd_repack(int argc, const char **argv, const char *prefix) |
725 | { | |
d3180279 | 726 | struct child_process cmd = CHILD_PROCESS_INIT; |
a1bbc6c0 | 727 | struct string_list_item *item; |
a1bbc6c0 | 728 | struct string_list names = STRING_LIST_INIT_DUP; |
a169166d | 729 | struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP; |
90f838bc | 730 | struct string_list existing_kept_packs = STRING_LIST_INIT_DUP; |
0fabafd0 | 731 | struct pack_geometry *geometry = NULL; |
a1bbc6c0 | 732 | struct strbuf line = STRBUF_INIT; |
324efc90 | 733 | struct tempfile *refs_snapshot = NULL; |
2fcb03b5 | 734 | int i, ext, ret; |
a1bbc6c0 | 735 | FILE *out; |
47ca93d0 | 736 | int show_progress; |
a1bbc6c0 SB |
737 | |
738 | /* variables to be filled by option parsing */ | |
a1bbc6c0 | 739 | int delete_redundant = 0; |
aa8bd519 | 740 | const char *unpack_unreachable = NULL; |
905f27b8 | 741 | int keep_unreachable = 0; |
ed7e5fc3 | 742 | struct string_list keep_pack_list = STRING_LIST_INIT_NODUP; |
2b958e79 | 743 | struct pack_objects_args po_args = {NULL}; |
4571324b | 744 | struct pack_objects_args cruft_po_args = {NULL}; |
0fabafd0 | 745 | int geometric_factor = 0; |
1d89d88d | 746 | int write_midx = 0; |
a1bbc6c0 SB |
747 | |
748 | struct option builtin_repack_options[] = { | |
749 | OPT_BIT('a', NULL, &pack_everything, | |
750 | N_("pack everything in a single pack"), ALL_INTO_ONE), | |
751 | OPT_BIT('A', NULL, &pack_everything, | |
752 | N_("same as -a, and turn unreachable objects loose"), | |
753 | LOOSEN_UNREACHABLE | ALL_INTO_ONE), | |
f9825d1c TB |
754 | OPT_BIT(0, "cruft", &pack_everything, |
755 | N_("same as -a, pack unreachable cruft objects separately"), | |
756 | PACK_CRUFT), | |
757 | OPT_STRING(0, "cruft-expiration", &cruft_expiration, N_("approxidate"), | |
758 | N_("with -C, expire objects older than this")), | |
a1bbc6c0 SB |
759 | OPT_BOOL('d', NULL, &delete_redundant, |
760 | N_("remove redundant packs, and run git-prune-packed")), | |
2b958e79 | 761 | OPT_BOOL('f', NULL, &po_args.no_reuse_delta, |
a1bbc6c0 | 762 | N_("pass --no-reuse-delta to git-pack-objects")), |
2b958e79 | 763 | OPT_BOOL('F', NULL, &po_args.no_reuse_object, |
a1bbc6c0 | 764 | N_("pass --no-reuse-object to git-pack-objects")), |
64a6151d PS |
765 | OPT_NEGBIT('n', NULL, &run_update_server_info, |
766 | N_("do not run git-update-server-info"), 1), | |
2b958e79 JT |
767 | OPT__QUIET(&po_args.quiet, N_("be quiet")), |
768 | OPT_BOOL('l', "local", &po_args.local, | |
a1bbc6c0 | 769 | N_("pass --local to git-pack-objects")), |
d078d85b | 770 | OPT_BOOL('b', "write-bitmap-index", &write_bitmaps, |
5cf2741c | 771 | N_("write bitmap index")), |
16d75fa4 JK |
772 | OPT_BOOL('i', "delta-islands", &use_delta_islands, |
773 | N_("pass --delta-islands to git-pack-objects")), | |
a1bbc6c0 SB |
774 | OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"), |
775 | N_("with -A, do not loosen objects older than this")), | |
905f27b8 JK |
776 | OPT_BOOL('k', "keep-unreachable", &keep_unreachable, |
777 | N_("with -a, repack unreachable objects")), | |
2b958e79 | 778 | OPT_STRING(0, "window", &po_args.window, N_("n"), |
a1bbc6c0 | 779 | N_("size of the window used for delta compression")), |
2b958e79 | 780 | OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"), |
a1bbc6c0 | 781 | N_("same as the above, but limit memory size instead of entries count")), |
2b958e79 | 782 | OPT_STRING(0, "depth", &po_args.depth, N_("n"), |
a1bbc6c0 | 783 | N_("limits the maximum delta depth")), |
2b958e79 | 784 | OPT_STRING(0, "threads", &po_args.threads, N_("n"), |
40bcf318 | 785 | N_("limits the maximum number of threads")), |
2b958e79 | 786 | OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"), |
a1bbc6c0 | 787 | N_("maximum size of each packfile")), |
ee34a2be JK |
788 | OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects, |
789 | N_("repack objects in packs marked with .keep")), | |
ed7e5fc3 NTND |
790 | OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"), |
791 | N_("do not repack this pack")), | |
0fabafd0 TB |
792 | OPT_INTEGER('g', "geometric", &geometric_factor, |
793 | N_("find a geometric progression with factor <N>")), | |
1d89d88d TB |
794 | OPT_BOOL('m', "write-midx", &write_midx, |
795 | N_("write a multi-pack index of the resulting packs")), | |
a1bbc6c0 SB |
796 | OPT_END() |
797 | }; | |
798 | ||
4571324b | 799 | git_config(repack_config, &cruft_po_args); |
a1bbc6c0 SB |
800 | |
801 | argc = parse_options(argc, argv, prefix, builtin_repack_options, | |
802 | git_repack_usage, 0); | |
803 | ||
067fbd41 JK |
804 | if (delete_redundant && repository_format_precious_objects) |
805 | die(_("cannot delete packs in a precious-objects repo")); | |
806 | ||
905f27b8 JK |
807 | if (keep_unreachable && |
808 | (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))) | |
12909b6b | 809 | die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "-A"); |
905f27b8 | 810 | |
f9825d1c TB |
811 | if (pack_everything & PACK_CRUFT) { |
812 | pack_everything |= ALL_INTO_ONE; | |
813 | ||
814 | if (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)) | |
815 | die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-A"); | |
816 | if (keep_unreachable) | |
817 | die(_("options '%s' and '%s' cannot be used together"), "--cruft", "-k"); | |
818 | } | |
819 | ||
73284822 | 820 | if (write_bitmaps < 0) { |
1d89d88d TB |
821 | if (!write_midx && |
822 | (!(pack_everything & ALL_INTO_ONE) || !is_bare_repository())) | |
25575015 | 823 | write_bitmaps = 0; |
ff1e653c TB |
824 | } else if (write_bitmaps && |
825 | git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0) && | |
826 | git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) { | |
827 | write_bitmaps = 0; | |
73284822 | 828 | } |
ee34a2be | 829 | if (pack_kept_objects < 0) |
e4d0c11c | 830 | pack_kept_objects = write_bitmaps > 0 && !write_midx; |
ee34a2be | 831 | |
1d89d88d | 832 | if (write_bitmaps && !(pack_everything & ALL_INTO_ONE) && !write_midx) |
1c409a70 DT |
833 | die(_(incremental_bitmap_conflict_error)); |
834 | ||
324efc90 TB |
835 | if (write_midx && write_bitmaps) { |
836 | struct strbuf path = STRBUF_INIT; | |
837 | ||
838 | strbuf_addf(&path, "%s/%s_XXXXXX", get_object_directory(), | |
839 | "bitmap-ref-tips"); | |
840 | ||
841 | refs_snapshot = xmks_tempfile(path.buf); | |
842 | midx_snapshot_refs(refs_snapshot); | |
843 | ||
844 | strbuf_release(&path); | |
845 | } | |
846 | ||
4b5a808b VD |
847 | packdir = mkpathdup("%s/pack", get_object_directory()); |
848 | packtmp_name = xstrfmt(".tmp-%d-pack", (int)getpid()); | |
849 | packtmp = mkpathdup("%s/%s", packdir, packtmp_name); | |
850 | ||
851 | collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs, | |
852 | &keep_pack_list); | |
853 | ||
0fabafd0 TB |
854 | if (geometric_factor) { |
855 | if (pack_everything) | |
12909b6b | 856 | die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a"); |
4b5a808b | 857 | init_pack_geometry(&geometry, &existing_kept_packs); |
0fabafd0 TB |
858 | split_pack_geometry(geometry, geometric_factor); |
859 | } | |
860 | ||
2b958e79 JT |
861 | prepare_pack_objects(&cmd, &po_args); |
862 | ||
47ca93d0 DS |
863 | show_progress = !po_args.quiet && isatty(2); |
864 | ||
22f9b7f3 | 865 | strvec_push(&cmd.args, "--keep-true-parents"); |
ee34a2be | 866 | if (!pack_kept_objects) |
22f9b7f3 | 867 | strvec_push(&cmd.args, "--honor-pack-keep"); |
ed7e5fc3 | 868 | for (i = 0; i < keep_pack_list.nr; i++) |
22f9b7f3 | 869 | strvec_pushf(&cmd.args, "--keep-pack=%s", |
f6d8942b | 870 | keep_pack_list.items[i].string); |
22f9b7f3 | 871 | strvec_push(&cmd.args, "--non-empty"); |
0fabafd0 TB |
872 | if (!geometry) { |
873 | /* | |
ccae01ca JH |
874 | * We need to grab all reachable objects, including those that |
875 | * are reachable from reflogs and the index. | |
0fabafd0 | 876 | * |
ccae01ca JH |
877 | * When repacking into a geometric progression of packs, |
878 | * however, we ask 'git pack-objects --stdin-packs', and it is | |
879 | * not about packing objects based on reachability but about | |
880 | * repacking all the objects in specified packs and loose ones | |
881 | * (indeed, --stdin-packs is incompatible with these options). | |
0fabafd0 TB |
882 | */ |
883 | strvec_push(&cmd.args, "--all"); | |
884 | strvec_push(&cmd.args, "--reflog"); | |
885 | strvec_push(&cmd.args, "--indexed-objects"); | |
886 | } | |
b14ed5ad | 887 | if (has_promisor_remote()) |
22f9b7f3 | 888 | strvec_push(&cmd.args, "--exclude-promisor-objects"); |
1d89d88d TB |
889 | if (!write_midx) { |
890 | if (write_bitmaps > 0) | |
891 | strvec_push(&cmd.args, "--write-bitmap-index"); | |
892 | else if (write_bitmaps < 0) | |
893 | strvec_push(&cmd.args, "--write-bitmap-index-quiet"); | |
894 | } | |
16d75fa4 | 895 | if (use_delta_islands) |
22f9b7f3 | 896 | strvec_push(&cmd.args, "--delta-islands"); |
a1bbc6c0 | 897 | |
90f838bc | 898 | if (pack_everything & ALL_INTO_ONE) { |
5d19e813 JT |
899 | repack_promisor_objects(&po_args, &names); |
900 | ||
f9825d1c TB |
901 | if (existing_nonkept_packs.nr && delete_redundant && |
902 | !(pack_everything & PACK_CRUFT)) { | |
a643157d RS |
903 | for_each_string_list_item(item, &names) { |
904 | strvec_pushf(&cmd.args, "--keep-pack=%s-%s.pack", | |
905 | packtmp_name, item->string); | |
906 | } | |
8d422993 | 907 | if (unpack_unreachable) { |
22f9b7f3 | 908 | strvec_pushf(&cmd.args, |
f6d8942b JK |
909 | "--unpack-unreachable=%s", |
910 | unpack_unreachable); | |
8d422993 | 911 | } else if (pack_everything & LOOSEN_UNREACHABLE) { |
22f9b7f3 | 912 | strvec_push(&cmd.args, |
f6d8942b | 913 | "--unpack-unreachable"); |
905f27b8 | 914 | } else if (keep_unreachable) { |
22f9b7f3 JK |
915 | strvec_push(&cmd.args, "--keep-unreachable"); |
916 | strvec_push(&cmd.args, "--pack-loose-unreachable"); | |
8d422993 | 917 | } |
a1bbc6c0 | 918 | } |
0fabafd0 TB |
919 | } else if (geometry) { |
920 | strvec_push(&cmd.args, "--stdin-packs"); | |
921 | strvec_push(&cmd.args, "--unpacked"); | |
a1bbc6c0 | 922 | } else { |
22f9b7f3 JK |
923 | strvec_push(&cmd.args, "--unpacked"); |
924 | strvec_push(&cmd.args, "--incremental"); | |
a1bbc6c0 SB |
925 | } |
926 | ||
0fabafd0 TB |
927 | if (geometry) |
928 | cmd.in = -1; | |
929 | else | |
930 | cmd.no_stdin = 1; | |
a1bbc6c0 SB |
931 | |
932 | ret = start_command(&cmd); | |
933 | if (ret) | |
ffc9329f | 934 | return ret; |
a1bbc6c0 | 935 | |
0fabafd0 TB |
936 | if (geometry) { |
937 | FILE *in = xfdopen(cmd.in, "w"); | |
938 | /* | |
939 | * The resulting pack should contain all objects in packs that | |
940 | * are going to be rolled up, but exclude objects in packs which | |
941 | * are being left alone. | |
942 | */ | |
943 | for (i = 0; i < geometry->split; i++) | |
944 | fprintf(in, "%s\n", pack_basename(geometry->pack[i])); | |
945 | for (i = geometry->split; i < geometry->pack_nr; i++) | |
946 | fprintf(in, "^%s\n", pack_basename(geometry->pack[i])); | |
947 | fclose(in); | |
948 | } | |
949 | ||
a1bbc6c0 | 950 | out = xfdopen(cmd.out, "r"); |
8f309aeb | 951 | while (strbuf_getline_lf(&line, out) != EOF) { |
b639606f JK |
952 | struct string_list_item *item; |
953 | ||
2f0c9e9a | 954 | if (line.len != the_hash_algo->hexsz) |
3813a89f | 955 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); |
b639606f JK |
956 | item = string_list_append(&names, line.buf); |
957 | item->util = populate_pack_exts(item->string); | |
a1bbc6c0 SB |
958 | } |
959 | fclose(out); | |
960 | ret = finish_command(&cmd); | |
961 | if (ret) | |
ffc9329f | 962 | return ret; |
a1bbc6c0 | 963 | |
2b958e79 | 964 | if (!names.nr && !po_args.quiet) |
c83d950e | 965 | printf_ln(_("Nothing new to pack.")); |
a1bbc6c0 | 966 | |
f9825d1c TB |
967 | if (pack_everything & PACK_CRUFT) { |
968 | const char *pack_prefix; | |
969 | if (!skip_prefix(packtmp, packdir, &pack_prefix)) | |
970 | die(_("pack prefix %s does not begin with objdir %s"), | |
971 | packtmp, packdir); | |
972 | if (*pack_prefix == '/') | |
973 | pack_prefix++; | |
974 | ||
4571324b TB |
975 | if (!cruft_po_args.window) |
976 | cruft_po_args.window = po_args.window; | |
977 | if (!cruft_po_args.window_memory) | |
978 | cruft_po_args.window_memory = po_args.window_memory; | |
979 | if (!cruft_po_args.depth) | |
980 | cruft_po_args.depth = po_args.depth; | |
981 | if (!cruft_po_args.threads) | |
982 | cruft_po_args.threads = po_args.threads; | |
983 | ||
984 | cruft_po_args.local = po_args.local; | |
985 | cruft_po_args.quiet = po_args.quiet; | |
986 | ||
987 | ret = write_cruft_pack(&cruft_po_args, pack_prefix, &names, | |
f9825d1c TB |
988 | &existing_nonkept_packs, |
989 | &existing_kept_packs); | |
990 | if (ret) | |
991 | return ret; | |
992 | } | |
993 | ||
66731ff9 TB |
994 | string_list_sort(&names); |
995 | ||
2d511cfc | 996 | close_object_store(the_repository->objects); |
5bdece0d | 997 | |
a1bbc6c0 SB |
998 | /* |
999 | * Ok we have prepared all new packfiles. | |
a1bbc6c0 | 1000 | */ |
a1bbc6c0 | 1001 | for_each_string_list_item(item, &names) { |
d3d9c519 JK |
1002 | struct generated_pack_data *data = item->util; |
1003 | ||
b328c216 | 1004 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
9cf10d87 | 1005 | char *fname; |
525e18c0 | 1006 | |
a1bbc6c0 | 1007 | fname = mkpathdup("%s/pack-%s%s", |
42a02d85 | 1008 | packdir, item->string, exts[ext].name); |
2fcb03b5 | 1009 | |
9cf10d87 JK |
1010 | if (data->tempfiles[ext]) { |
1011 | const char *fname_old = get_tempfile_path(data->tempfiles[ext]); | |
2fcb03b5 | 1012 | struct stat statbuffer; |
9cf10d87 | 1013 | |
2fcb03b5 TB |
1014 | if (!stat(fname_old, &statbuffer)) { |
1015 | statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); | |
1016 | chmod(fname_old, statbuffer.st_mode); | |
1017 | } | |
1018 | ||
9cf10d87 JK |
1019 | if (rename_tempfile(&data->tempfiles[ext], fname)) |
1020 | die_errno(_("renaming pack to '%s' failed"), fname); | |
2fcb03b5 | 1021 | } else if (!exts[ext].optional) |
a4880b20 JK |
1022 | die(_("pack-objects did not write a '%s' file for pack %s-%s"), |
1023 | exts[ext].name, packtmp, item->string); | |
2fcb03b5 TB |
1024 | else if (unlink(fname) < 0 && errno != ENOENT) |
1025 | die_errno(_("could not unlink: %s"), fname); | |
a1bbc6c0 | 1026 | |
e3cf2303 | 1027 | free(fname); |
a1bbc6c0 SB |
1028 | } |
1029 | } | |
a1bbc6c0 SB |
1030 | /* End of pack replacement. */ |
1031 | ||
1d89d88d | 1032 | if (delete_redundant && pack_everything & ALL_INTO_ONE) { |
2f0c9e9a | 1033 | const int hexsz = the_hash_algo->hexsz; |
1d89d88d | 1034 | for_each_string_list_item(item, &existing_nonkept_packs) { |
a1bbc6c0 SB |
1035 | char *sha1; |
1036 | size_t len = strlen(item->string); | |
2f0c9e9a | 1037 | if (len < hexsz) |
a1bbc6c0 | 1038 | continue; |
2f0c9e9a | 1039 | sha1 = item->string + len - hexsz; |
1d89d88d TB |
1040 | /* |
1041 | * Mark this pack for deletion, which ensures that this | |
1042 | * pack won't be included in a MIDX (if `--write-midx` | |
1043 | * was given) and that we will actually delete this pack | |
1044 | * (if `-d` was given). | |
1045 | */ | |
72263ffc TB |
1046 | if (!string_list_has_string(&names, sha1)) |
1047 | item->util = (void*)(uintptr_t)((size_t)item->util | DELETE_PACK); | |
1d89d88d TB |
1048 | } |
1049 | } | |
1050 | ||
1051 | if (write_midx) { | |
1052 | struct string_list include = STRING_LIST_INIT_NODUP; | |
1053 | midx_included_packs(&include, &existing_nonkept_packs, | |
1054 | &existing_kept_packs, &names, geometry); | |
1055 | ||
6d08b9d4 | 1056 | ret = write_midx_included_packs(&include, geometry, |
324efc90 | 1057 | refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL, |
1d89d88d TB |
1058 | show_progress, write_bitmaps > 0); |
1059 | ||
55d902cd TB |
1060 | if (!ret && write_bitmaps) |
1061 | remove_redundant_bitmaps(&include, packdir); | |
1062 | ||
1d89d88d TB |
1063 | string_list_clear(&include, 0); |
1064 | ||
1065 | if (ret) | |
1066 | return ret; | |
1067 | } | |
1068 | ||
5d19e813 JT |
1069 | reprepare_packed_git(the_repository); |
1070 | ||
a1bbc6c0 | 1071 | if (delete_redundant) { |
4489a480 | 1072 | int opts = 0; |
1d89d88d | 1073 | for_each_string_list_item(item, &existing_nonkept_packs) { |
72263ffc | 1074 | if (!((uintptr_t)item->util & DELETE_PACK)) |
1d89d88d TB |
1075 | continue; |
1076 | remove_redundant_pack(packdir, item->string); | |
a1bbc6c0 | 1077 | } |
0fabafd0 TB |
1078 | |
1079 | if (geometry) { | |
1080 | struct strbuf buf = STRBUF_INIT; | |
1081 | ||
1082 | uint32_t i; | |
1083 | for (i = 0; i < geometry->split; i++) { | |
1084 | struct packed_git *p = geometry->pack[i]; | |
1085 | if (string_list_has_string(&names, | |
1086 | hash_to_hex(p->hash))) | |
1087 | continue; | |
1088 | ||
1089 | strbuf_reset(&buf); | |
1090 | strbuf_addstr(&buf, pack_basename(p)); | |
1091 | strbuf_strip_suffix(&buf, ".pack"); | |
1092 | ||
197443e8 TB |
1093 | if ((p->pack_keep) || |
1094 | (string_list_has_string(&existing_kept_packs, | |
1095 | buf.buf))) | |
1096 | continue; | |
1097 | ||
0fabafd0 TB |
1098 | remove_redundant_pack(packdir, buf.buf); |
1099 | } | |
1100 | strbuf_release(&buf); | |
1101 | } | |
47ca93d0 | 1102 | if (show_progress) |
4489a480 RS |
1103 | opts |= PRUNE_PACKED_VERBOSE; |
1104 | prune_packed_objects(opts); | |
5dcfbf56 JS |
1105 | |
1106 | if (!keep_unreachable && | |
1107 | (!(pack_everything & LOOSEN_UNREACHABLE) || | |
1108 | unpack_unreachable) && | |
1109 | is_repository_shallow(the_repository)) | |
1110 | prune_shallow(PRUNE_QUICK); | |
a1bbc6c0 SB |
1111 | } |
1112 | ||
64a6151d | 1113 | if (run_update_server_info) |
4489a480 | 1114 | update_server_info(0); |
0465a505 | 1115 | |
ff1e653c TB |
1116 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) { |
1117 | unsigned flags = 0; | |
1118 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP, 0)) | |
1119 | flags |= MIDX_WRITE_BITMAP | MIDX_WRITE_REV_INDEX; | |
08944d1c | 1120 | write_midx_file(get_object_directory(), NULL, NULL, flags); |
ff1e653c | 1121 | } |
0465a505 | 1122 | |
d3d9c519 | 1123 | string_list_clear(&names, 1); |
a169166d | 1124 | string_list_clear(&existing_nonkept_packs, 0); |
90f838bc | 1125 | string_list_clear(&existing_kept_packs, 0); |
0fabafd0 | 1126 | clear_pack_geometry(geometry); |
a1bbc6c0 SB |
1127 | strbuf_release(&line); |
1128 | ||
1129 | return 0; | |
1130 | } |