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