]>
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" |
a1bbc6c0 SB |
18 | |
19 | static int delta_base_offset = 1; | |
ee34a2be | 20 | static int pack_kept_objects = -1; |
36eba032 | 21 | static int write_bitmaps = -1; |
16d75fa4 | 22 | static int use_delta_islands; |
a1bbc6c0 SB |
23 | static char *packdir, *packtmp; |
24 | ||
25 | static const char *const git_repack_usage[] = { | |
9c9b4f2f | 26 | N_("git repack [<options>]"), |
a1bbc6c0 SB |
27 | NULL |
28 | }; | |
29 | ||
1c409a70 DT |
30 | static const char incremental_bitmap_conflict_error[] = N_( |
31 | "Incremental repacks are incompatible with bitmap indexes. Use\n" | |
32 | "--no-write-bitmap-index or disable the pack.writebitmaps configuration." | |
33 | ); | |
34 | ||
35 | ||
a1bbc6c0 SB |
36 | static int repack_config(const char *var, const char *value, void *cb) |
37 | { | |
38 | if (!strcmp(var, "repack.usedeltabaseoffset")) { | |
39 | delta_base_offset = git_config_bool(var, value); | |
40 | return 0; | |
41 | } | |
ee34a2be JK |
42 | if (!strcmp(var, "repack.packkeptobjects")) { |
43 | pack_kept_objects = git_config_bool(var, value); | |
44 | return 0; | |
45 | } | |
71d76cb4 JK |
46 | if (!strcmp(var, "repack.writebitmaps") || |
47 | !strcmp(var, "pack.writebitmaps")) { | |
d078d85b | 48 | write_bitmaps = git_config_bool(var, value); |
3198b89f JK |
49 | return 0; |
50 | } | |
16d75fa4 JK |
51 | if (!strcmp(var, "repack.usedeltaislands")) { |
52 | use_delta_islands = git_config_bool(var, value); | |
53 | return 0; | |
54 | } | |
a1bbc6c0 SB |
55 | return git_default_config(var, value, cb); |
56 | } | |
57 | ||
58 | /* | |
59 | * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files. | |
60 | */ | |
61 | static void remove_temporary_files(void) | |
62 | { | |
63 | struct strbuf buf = STRBUF_INIT; | |
64 | size_t dirlen, prefixlen; | |
65 | DIR *dir; | |
66 | struct dirent *e; | |
67 | ||
68 | dir = opendir(packdir); | |
69 | if (!dir) | |
70 | return; | |
71 | ||
72 | /* Point at the slash at the end of ".../objects/pack/" */ | |
73 | dirlen = strlen(packdir) + 1; | |
74 | strbuf_addstr(&buf, packtmp); | |
75 | /* Hold the length of ".tmp-%d-pack-" */ | |
76 | prefixlen = buf.len - dirlen; | |
77 | ||
78 | while ((e = readdir(dir))) { | |
79 | if (strncmp(e->d_name, buf.buf + dirlen, prefixlen)) | |
80 | continue; | |
81 | strbuf_setlen(&buf, dirlen); | |
82 | strbuf_addstr(&buf, e->d_name); | |
83 | unlink(buf.buf); | |
84 | } | |
85 | closedir(dir); | |
86 | strbuf_release(&buf); | |
87 | } | |
88 | ||
89 | static void remove_pack_on_signal(int signo) | |
90 | { | |
91 | remove_temporary_files(); | |
92 | sigchain_pop(signo); | |
93 | raise(signo); | |
94 | } | |
95 | ||
96 | /* | |
97 | * Adds all packs hex strings to the fname list, which do not | |
5d19e813 | 98 | * have a corresponding .keep file. These packs are not to |
0c16cd49 | 99 | * be kept if we are going to pack everything into one file. |
a1bbc6c0 | 100 | */ |
ed7e5fc3 NTND |
101 | static void get_non_kept_pack_filenames(struct string_list *fname_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 | ||
115 | for (i = 0; i < extra_keep->nr; i++) | |
116 | if (!fspathcmp(e->d_name, extra_keep->items[i].string)) | |
117 | break; | |
118 | if (extra_keep->nr > 0 && i < extra_keep->nr) | |
119 | continue; | |
120 | ||
26936bfd | 121 | if (!strip_suffix(e->d_name, ".pack", &len)) |
a1bbc6c0 SB |
122 | continue; |
123 | ||
a1bbc6c0 SB |
124 | fname = xmemdupz(e->d_name, len); |
125 | ||
5d19e813 | 126 | if (!file_exists(mkpath("%s/%s.keep", packdir, fname))) |
a1bbc6c0 SB |
127 | string_list_append_nodup(fname_list, fname); |
128 | else | |
129 | free(fname); | |
130 | } | |
131 | closedir(dir); | |
132 | } | |
133 | ||
134 | static void remove_redundant_pack(const char *dir_name, const char *base_name) | |
135 | { | |
a1bbc6c0 | 136 | struct strbuf buf = STRBUF_INIT; |
59552fb3 | 137 | struct multi_pack_index *m = get_local_multi_pack_index(the_repository); |
e08f7bb0 TB |
138 | strbuf_addf(&buf, "%s.pack", base_name); |
139 | if (m && midx_contains_pack(m, buf.buf)) | |
140 | clear_midx_file(the_repository); | |
141 | strbuf_insertf(&buf, 0, "%s/", dir_name); | |
8434e85d | 142 | unlink_pack_path(buf.buf, 1); |
a1bbc6c0 SB |
143 | strbuf_release(&buf); |
144 | } | |
145 | ||
2b958e79 JT |
146 | struct pack_objects_args { |
147 | const char *window; | |
148 | const char *window_memory; | |
149 | const char *depth; | |
150 | const char *threads; | |
151 | const char *max_pack_size; | |
152 | int no_reuse_delta; | |
153 | int no_reuse_object; | |
154 | int quiet; | |
155 | int local; | |
156 | }; | |
157 | ||
158 | static void prepare_pack_objects(struct child_process *cmd, | |
159 | const struct pack_objects_args *args) | |
160 | { | |
22f9b7f3 | 161 | strvec_push(&cmd->args, "pack-objects"); |
2b958e79 | 162 | if (args->window) |
22f9b7f3 | 163 | strvec_pushf(&cmd->args, "--window=%s", args->window); |
2b958e79 | 164 | if (args->window_memory) |
22f9b7f3 | 165 | strvec_pushf(&cmd->args, "--window-memory=%s", args->window_memory); |
2b958e79 | 166 | if (args->depth) |
22f9b7f3 | 167 | strvec_pushf(&cmd->args, "--depth=%s", args->depth); |
2b958e79 | 168 | if (args->threads) |
22f9b7f3 | 169 | strvec_pushf(&cmd->args, "--threads=%s", args->threads); |
2b958e79 | 170 | if (args->max_pack_size) |
22f9b7f3 | 171 | strvec_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size); |
2b958e79 | 172 | if (args->no_reuse_delta) |
22f9b7f3 | 173 | strvec_pushf(&cmd->args, "--no-reuse-delta"); |
2b958e79 | 174 | if (args->no_reuse_object) |
22f9b7f3 | 175 | strvec_pushf(&cmd->args, "--no-reuse-object"); |
2b958e79 | 176 | if (args->local) |
22f9b7f3 | 177 | strvec_push(&cmd->args, "--local"); |
2b958e79 | 178 | if (args->quiet) |
22f9b7f3 | 179 | strvec_push(&cmd->args, "--quiet"); |
2b958e79 | 180 | if (delta_base_offset) |
22f9b7f3 JK |
181 | strvec_push(&cmd->args, "--delta-base-offset"); |
182 | strvec_push(&cmd->args, packtmp); | |
2b958e79 JT |
183 | cmd->git_cmd = 1; |
184 | cmd->out = -1; | |
185 | } | |
186 | ||
5d19e813 JT |
187 | /* |
188 | * Write oid to the given struct child_process's stdin, starting it first if | |
189 | * necessary. | |
190 | */ | |
191 | static int write_oid(const struct object_id *oid, struct packed_git *pack, | |
192 | uint32_t pos, void *data) | |
193 | { | |
194 | struct child_process *cmd = data; | |
195 | ||
196 | if (cmd->in == -1) { | |
197 | if (start_command(cmd)) | |
c83d950e | 198 | die(_("could not start pack-objects to repack promisor objects")); |
5d19e813 JT |
199 | } |
200 | ||
dd336a55 | 201 | xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz); |
5d19e813 JT |
202 | xwrite(cmd->in, "\n", 1); |
203 | return 0; | |
204 | } | |
205 | ||
63f4d5cf JK |
206 | static struct { |
207 | const char *name; | |
208 | unsigned optional:1; | |
209 | } exts[] = { | |
210 | {".pack"}, | |
211 | {".idx"}, | |
2f4ba2a8 | 212 | {".rev", 1}, |
63f4d5cf JK |
213 | {".bitmap", 1}, |
214 | {".promisor", 1}, | |
215 | }; | |
216 | ||
704c4a5c TB |
217 | static unsigned populate_pack_exts(char *name) |
218 | { | |
219 | struct stat statbuf; | |
220 | struct strbuf path = STRBUF_INIT; | |
221 | unsigned ret = 0; | |
222 | int i; | |
223 | ||
224 | for (i = 0; i < ARRAY_SIZE(exts); i++) { | |
225 | strbuf_reset(&path); | |
226 | strbuf_addf(&path, "%s-%s%s", packtmp, name, exts[i].name); | |
227 | ||
228 | if (stat(path.buf, &statbuf)) | |
229 | continue; | |
230 | ||
231 | ret |= (1 << i); | |
232 | } | |
233 | ||
234 | strbuf_release(&path); | |
235 | return ret; | |
236 | } | |
237 | ||
5d19e813 JT |
238 | static void repack_promisor_objects(const struct pack_objects_args *args, |
239 | struct string_list *names) | |
240 | { | |
241 | struct child_process cmd = CHILD_PROCESS_INIT; | |
242 | FILE *out; | |
243 | struct strbuf line = STRBUF_INIT; | |
244 | ||
245 | prepare_pack_objects(&cmd, args); | |
246 | cmd.in = -1; | |
247 | ||
248 | /* | |
249 | * NEEDSWORK: Giving pack-objects only the OIDs without any ordering | |
250 | * hints may result in suboptimal deltas in the resulting pack. See if | |
251 | * the OIDs can be sent with fake paths such that pack-objects can use a | |
252 | * {type -> existing pack order} ordering when computing deltas instead | |
253 | * of a {type -> size} ordering, which may produce better deltas. | |
254 | */ | |
255 | for_each_packed_object(write_oid, &cmd, | |
256 | FOR_EACH_OBJECT_PROMISOR_ONLY); | |
257 | ||
258 | if (cmd.in == -1) | |
259 | /* No packed objects; cmd was never started */ | |
260 | return; | |
261 | ||
262 | close(cmd.in); | |
263 | ||
264 | out = xfdopen(cmd.out, "r"); | |
265 | while (strbuf_getline_lf(&line, out) != EOF) { | |
704c4a5c | 266 | struct string_list_item *item; |
5d19e813 | 267 | char *promisor_name; |
33add2ad | 268 | |
2f0c9e9a | 269 | if (line.len != the_hash_algo->hexsz) |
3813a89f | 270 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); |
704c4a5c | 271 | item = string_list_append(names, line.buf); |
5d19e813 JT |
272 | |
273 | /* | |
274 | * pack-objects creates the .pack and .idx files, but not the | |
275 | * .promisor file. Create the .promisor file, which is empty. | |
5374a290 JT |
276 | * |
277 | * NEEDSWORK: fetch-pack sometimes generates non-empty | |
278 | * .promisor files containing the ref names and associated | |
279 | * hashes at the point of generation of the corresponding | |
280 | * packfile, but this would not preserve their contents. Maybe | |
281 | * concatenate the contents of all .promisor files instead of | |
282 | * just creating a new empty file. | |
5d19e813 JT |
283 | */ |
284 | promisor_name = mkpathdup("%s-%s.promisor", packtmp, | |
285 | line.buf); | |
33add2ad | 286 | write_promisor_file(promisor_name, NULL, 0); |
704c4a5c TB |
287 | |
288 | item->util = (void *)(uintptr_t)populate_pack_exts(item->string); | |
289 | ||
5d19e813 JT |
290 | free(promisor_name); |
291 | } | |
292 | fclose(out); | |
293 | if (finish_command(&cmd)) | |
c83d950e | 294 | die(_("could not finish pack-objects to repack promisor objects")); |
5d19e813 JT |
295 | } |
296 | ||
a1bbc6c0 SB |
297 | #define ALL_INTO_ONE 1 |
298 | #define LOOSEN_UNREACHABLE 2 | |
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 | ||
325 | static void init_pack_geometry(struct pack_geometry **geometry_p) | |
326 | { | |
327 | struct packed_git *p; | |
328 | struct pack_geometry *geometry; | |
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) { | |
334 | if (!pack_kept_objects && p->pack_keep) | |
335 | continue; | |
336 | ||
337 | ALLOC_GROW(geometry->pack, | |
338 | geometry->pack_nr + 1, | |
339 | geometry->pack_alloc); | |
340 | ||
341 | geometry->pack[geometry->pack_nr] = p; | |
342 | geometry->pack_nr++; | |
343 | } | |
344 | ||
345 | QSORT(geometry->pack, geometry->pack_nr, geometry_cmp); | |
346 | } | |
347 | ||
348 | static void split_pack_geometry(struct pack_geometry *geometry, int factor) | |
349 | { | |
350 | uint32_t i; | |
351 | uint32_t split; | |
352 | off_t total_size = 0; | |
353 | ||
f25e33c1 | 354 | if (!geometry->pack_nr) { |
0fabafd0 TB |
355 | geometry->split = geometry->pack_nr; |
356 | return; | |
357 | } | |
358 | ||
0fabafd0 TB |
359 | /* |
360 | * First, count the number of packs (in descending order of size) which | |
361 | * already form a geometric progression. | |
362 | */ | |
363 | for (i = geometry->pack_nr - 1; i > 0; i--) { | |
364 | struct packed_git *ours = geometry->pack[i]; | |
365 | struct packed_git *prev = geometry->pack[i - 1]; | |
2a159641 TB |
366 | |
367 | if (unsigned_mult_overflows(factor, geometry_pack_weight(prev))) | |
368 | die(_("pack %s too large to consider in geometric " | |
369 | "progression"), | |
370 | prev->pack_name); | |
371 | ||
13d746a3 | 372 | if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev)) |
0fabafd0 TB |
373 | break; |
374 | } | |
375 | ||
13d746a3 TB |
376 | split = i; |
377 | ||
0fabafd0 TB |
378 | if (split) { |
379 | /* | |
380 | * Move the split one to the right, since the top element in the | |
381 | * last-compared pair can't be in the progression. Only do this | |
382 | * when we split in the middle of the array (otherwise if we got | |
383 | * to the end, then the split is in the right place). | |
384 | */ | |
385 | split++; | |
386 | } | |
387 | ||
388 | /* | |
389 | * Then, anything to the left of 'split' must be in a new pack. But, | |
390 | * creating that new pack may cause packs in the heavy half to no longer | |
391 | * form a geometric progression. | |
392 | * | |
393 | * Compute an expected size of the new pack, and then determine how many | |
394 | * packs in the heavy half need to be joined into it (if any) to restore | |
395 | * the geometric progression. | |
396 | */ | |
2a159641 TB |
397 | for (i = 0; i < split; i++) { |
398 | struct packed_git *p = geometry->pack[i]; | |
399 | ||
400 | if (unsigned_add_overflows(total_size, geometry_pack_weight(p))) | |
401 | die(_("pack %s too large to roll up"), p->pack_name); | |
402 | total_size += geometry_pack_weight(p); | |
403 | } | |
0fabafd0 TB |
404 | for (i = split; i < geometry->pack_nr; i++) { |
405 | struct packed_git *ours = geometry->pack[i]; | |
2a159641 TB |
406 | |
407 | if (unsigned_mult_overflows(factor, total_size)) | |
408 | die(_("pack %s too large to roll up"), ours->pack_name); | |
409 | ||
0fabafd0 | 410 | if (geometry_pack_weight(ours) < factor * total_size) { |
2a159641 TB |
411 | if (unsigned_add_overflows(total_size, |
412 | geometry_pack_weight(ours))) | |
413 | die(_("pack %s too large to roll up"), | |
414 | ours->pack_name); | |
415 | ||
0fabafd0 TB |
416 | split++; |
417 | total_size += geometry_pack_weight(ours); | |
418 | } else | |
419 | break; | |
420 | } | |
421 | ||
422 | geometry->split = split; | |
423 | } | |
424 | ||
425 | static void clear_pack_geometry(struct pack_geometry *geometry) | |
426 | { | |
427 | if (!geometry) | |
428 | return; | |
429 | ||
430 | free(geometry->pack); | |
431 | geometry->pack_nr = 0; | |
432 | geometry->pack_alloc = 0; | |
433 | geometry->split = 0; | |
434 | } | |
435 | ||
a1bbc6c0 SB |
436 | int cmd_repack(int argc, const char **argv, const char *prefix) |
437 | { | |
d3180279 | 438 | struct child_process cmd = CHILD_PROCESS_INIT; |
a1bbc6c0 | 439 | struct string_list_item *item; |
a1bbc6c0 SB |
440 | struct string_list names = STRING_LIST_INIT_DUP; |
441 | struct string_list rollback = STRING_LIST_INIT_NODUP; | |
442 | struct string_list existing_packs = STRING_LIST_INIT_DUP; | |
0fabafd0 | 443 | struct pack_geometry *geometry = NULL; |
a1bbc6c0 | 444 | struct strbuf line = STRBUF_INIT; |
2fcb03b5 | 445 | int i, ext, ret; |
a1bbc6c0 SB |
446 | FILE *out; |
447 | ||
448 | /* variables to be filled by option parsing */ | |
449 | int pack_everything = 0; | |
450 | int delete_redundant = 0; | |
aa8bd519 | 451 | const char *unpack_unreachable = NULL; |
905f27b8 | 452 | int keep_unreachable = 0; |
ed7e5fc3 | 453 | struct string_list keep_pack_list = STRING_LIST_INIT_NODUP; |
a1bbc6c0 | 454 | int no_update_server_info = 0; |
2b958e79 | 455 | struct pack_objects_args po_args = {NULL}; |
0fabafd0 | 456 | int geometric_factor = 0; |
a1bbc6c0 SB |
457 | |
458 | struct option builtin_repack_options[] = { | |
459 | OPT_BIT('a', NULL, &pack_everything, | |
460 | N_("pack everything in a single pack"), ALL_INTO_ONE), | |
461 | OPT_BIT('A', NULL, &pack_everything, | |
462 | N_("same as -a, and turn unreachable objects loose"), | |
463 | LOOSEN_UNREACHABLE | ALL_INTO_ONE), | |
464 | OPT_BOOL('d', NULL, &delete_redundant, | |
465 | N_("remove redundant packs, and run git-prune-packed")), | |
2b958e79 | 466 | OPT_BOOL('f', NULL, &po_args.no_reuse_delta, |
a1bbc6c0 | 467 | N_("pass --no-reuse-delta to git-pack-objects")), |
2b958e79 | 468 | OPT_BOOL('F', NULL, &po_args.no_reuse_object, |
a1bbc6c0 SB |
469 | N_("pass --no-reuse-object to git-pack-objects")), |
470 | OPT_BOOL('n', NULL, &no_update_server_info, | |
471 | N_("do not run git-update-server-info")), | |
2b958e79 JT |
472 | OPT__QUIET(&po_args.quiet, N_("be quiet")), |
473 | OPT_BOOL('l', "local", &po_args.local, | |
a1bbc6c0 | 474 | N_("pass --local to git-pack-objects")), |
d078d85b | 475 | OPT_BOOL('b', "write-bitmap-index", &write_bitmaps, |
5cf2741c | 476 | N_("write bitmap index")), |
16d75fa4 JK |
477 | OPT_BOOL('i', "delta-islands", &use_delta_islands, |
478 | N_("pass --delta-islands to git-pack-objects")), | |
a1bbc6c0 SB |
479 | OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"), |
480 | N_("with -A, do not loosen objects older than this")), | |
905f27b8 JK |
481 | OPT_BOOL('k', "keep-unreachable", &keep_unreachable, |
482 | N_("with -a, repack unreachable objects")), | |
2b958e79 | 483 | OPT_STRING(0, "window", &po_args.window, N_("n"), |
a1bbc6c0 | 484 | N_("size of the window used for delta compression")), |
2b958e79 | 485 | OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"), |
a1bbc6c0 | 486 | N_("same as the above, but limit memory size instead of entries count")), |
2b958e79 | 487 | OPT_STRING(0, "depth", &po_args.depth, N_("n"), |
a1bbc6c0 | 488 | N_("limits the maximum delta depth")), |
2b958e79 | 489 | OPT_STRING(0, "threads", &po_args.threads, N_("n"), |
40bcf318 | 490 | N_("limits the maximum number of threads")), |
2b958e79 | 491 | OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"), |
a1bbc6c0 | 492 | N_("maximum size of each packfile")), |
ee34a2be JK |
493 | OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects, |
494 | N_("repack objects in packs marked with .keep")), | |
ed7e5fc3 NTND |
495 | OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"), |
496 | N_("do not repack this pack")), | |
0fabafd0 TB |
497 | OPT_INTEGER('g', "geometric", &geometric_factor, |
498 | N_("find a geometric progression with factor <N>")), | |
a1bbc6c0 SB |
499 | OPT_END() |
500 | }; | |
501 | ||
502 | git_config(repack_config, NULL); | |
503 | ||
504 | argc = parse_options(argc, argv, prefix, builtin_repack_options, | |
505 | git_repack_usage, 0); | |
506 | ||
067fbd41 JK |
507 | if (delete_redundant && repository_format_precious_objects) |
508 | die(_("cannot delete packs in a precious-objects repo")); | |
509 | ||
905f27b8 JK |
510 | if (keep_unreachable && |
511 | (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))) | |
512 | die(_("--keep-unreachable and -A are incompatible")); | |
513 | ||
73284822 | 514 | if (write_bitmaps < 0) { |
25575015 | 515 | if (!(pack_everything & ALL_INTO_ONE) || |
7ff024e7 | 516 | !is_bare_repository()) |
25575015 | 517 | write_bitmaps = 0; |
73284822 | 518 | } |
ee34a2be | 519 | if (pack_kept_objects < 0) |
7ff024e7 | 520 | pack_kept_objects = write_bitmaps > 0; |
ee34a2be | 521 | |
1c409a70 DT |
522 | if (write_bitmaps && !(pack_everything & ALL_INTO_ONE)) |
523 | die(_(incremental_bitmap_conflict_error)); | |
524 | ||
0fabafd0 TB |
525 | if (geometric_factor) { |
526 | if (pack_everything) | |
527 | die(_("--geometric is incompatible with -A, -a")); | |
528 | init_pack_geometry(&geometry); | |
529 | split_pack_geometry(geometry, geometric_factor); | |
530 | } | |
531 | ||
a1bbc6c0 SB |
532 | packdir = mkpathdup("%s/pack", get_object_directory()); |
533 | packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid()); | |
534 | ||
535 | sigchain_push_common(remove_pack_on_signal); | |
536 | ||
2b958e79 JT |
537 | prepare_pack_objects(&cmd, &po_args); |
538 | ||
22f9b7f3 | 539 | strvec_push(&cmd.args, "--keep-true-parents"); |
ee34a2be | 540 | if (!pack_kept_objects) |
22f9b7f3 | 541 | strvec_push(&cmd.args, "--honor-pack-keep"); |
ed7e5fc3 | 542 | for (i = 0; i < keep_pack_list.nr; i++) |
22f9b7f3 | 543 | strvec_pushf(&cmd.args, "--keep-pack=%s", |
f6d8942b | 544 | keep_pack_list.items[i].string); |
22f9b7f3 | 545 | strvec_push(&cmd.args, "--non-empty"); |
0fabafd0 TB |
546 | if (!geometry) { |
547 | /* | |
ccae01ca JH |
548 | * We need to grab all reachable objects, including those that |
549 | * are reachable from reflogs and the index. | |
0fabafd0 | 550 | * |
ccae01ca JH |
551 | * When repacking into a geometric progression of packs, |
552 | * however, we ask 'git pack-objects --stdin-packs', and it is | |
553 | * not about packing objects based on reachability but about | |
554 | * repacking all the objects in specified packs and loose ones | |
555 | * (indeed, --stdin-packs is incompatible with these options). | |
0fabafd0 TB |
556 | */ |
557 | strvec_push(&cmd.args, "--all"); | |
558 | strvec_push(&cmd.args, "--reflog"); | |
559 | strvec_push(&cmd.args, "--indexed-objects"); | |
560 | } | |
b14ed5ad | 561 | if (has_promisor_remote()) |
22f9b7f3 | 562 | strvec_push(&cmd.args, "--exclude-promisor-objects"); |
25575015 | 563 | if (write_bitmaps > 0) |
22f9b7f3 | 564 | strvec_push(&cmd.args, "--write-bitmap-index"); |
25575015 | 565 | else if (write_bitmaps < 0) |
22f9b7f3 | 566 | strvec_push(&cmd.args, "--write-bitmap-index-quiet"); |
16d75fa4 | 567 | if (use_delta_islands) |
22f9b7f3 | 568 | strvec_push(&cmd.args, "--delta-islands"); |
a1bbc6c0 SB |
569 | |
570 | if (pack_everything & ALL_INTO_ONE) { | |
ed7e5fc3 | 571 | get_non_kept_pack_filenames(&existing_packs, &keep_pack_list); |
a1bbc6c0 | 572 | |
5d19e813 JT |
573 | repack_promisor_objects(&po_args, &names); |
574 | ||
a1bbc6c0 | 575 | if (existing_packs.nr && delete_redundant) { |
8d422993 | 576 | if (unpack_unreachable) { |
22f9b7f3 | 577 | strvec_pushf(&cmd.args, |
f6d8942b JK |
578 | "--unpack-unreachable=%s", |
579 | unpack_unreachable); | |
22f9b7f3 | 580 | strvec_push(&cmd.env_array, "GIT_REF_PARANOIA=1"); |
8d422993 | 581 | } else if (pack_everything & LOOSEN_UNREACHABLE) { |
22f9b7f3 | 582 | strvec_push(&cmd.args, |
f6d8942b | 583 | "--unpack-unreachable"); |
905f27b8 | 584 | } else if (keep_unreachable) { |
22f9b7f3 JK |
585 | strvec_push(&cmd.args, "--keep-unreachable"); |
586 | strvec_push(&cmd.args, "--pack-loose-unreachable"); | |
8d422993 | 587 | } else { |
22f9b7f3 | 588 | strvec_push(&cmd.env_array, "GIT_REF_PARANOIA=1"); |
8d422993 | 589 | } |
a1bbc6c0 | 590 | } |
0fabafd0 TB |
591 | } else if (geometry) { |
592 | strvec_push(&cmd.args, "--stdin-packs"); | |
593 | strvec_push(&cmd.args, "--unpacked"); | |
a1bbc6c0 | 594 | } else { |
22f9b7f3 JK |
595 | strvec_push(&cmd.args, "--unpacked"); |
596 | strvec_push(&cmd.args, "--incremental"); | |
a1bbc6c0 SB |
597 | } |
598 | ||
0fabafd0 TB |
599 | if (geometry) |
600 | cmd.in = -1; | |
601 | else | |
602 | cmd.no_stdin = 1; | |
a1bbc6c0 SB |
603 | |
604 | ret = start_command(&cmd); | |
605 | if (ret) | |
ffc9329f | 606 | return ret; |
a1bbc6c0 | 607 | |
0fabafd0 TB |
608 | if (geometry) { |
609 | FILE *in = xfdopen(cmd.in, "w"); | |
610 | /* | |
611 | * The resulting pack should contain all objects in packs that | |
612 | * are going to be rolled up, but exclude objects in packs which | |
613 | * are being left alone. | |
614 | */ | |
615 | for (i = 0; i < geometry->split; i++) | |
616 | fprintf(in, "%s\n", pack_basename(geometry->pack[i])); | |
617 | for (i = geometry->split; i < geometry->pack_nr; i++) | |
618 | fprintf(in, "^%s\n", pack_basename(geometry->pack[i])); | |
619 | fclose(in); | |
620 | } | |
621 | ||
a1bbc6c0 | 622 | out = xfdopen(cmd.out, "r"); |
8f309aeb | 623 | while (strbuf_getline_lf(&line, out) != EOF) { |
2f0c9e9a | 624 | if (line.len != the_hash_algo->hexsz) |
3813a89f | 625 | die(_("repack: Expecting full hex object ID lines only from pack-objects.")); |
a1bbc6c0 | 626 | string_list_append(&names, line.buf); |
a1bbc6c0 SB |
627 | } |
628 | fclose(out); | |
629 | ret = finish_command(&cmd); | |
630 | if (ret) | |
ffc9329f | 631 | return ret; |
a1bbc6c0 | 632 | |
2b958e79 | 633 | if (!names.nr && !po_args.quiet) |
c83d950e | 634 | printf_ln(_("Nothing new to pack.")); |
a1bbc6c0 | 635 | |
704c4a5c TB |
636 | for_each_string_list_item(item, &names) { |
637 | item->util = (void *)(uintptr_t)populate_pack_exts(item->string); | |
638 | } | |
639 | ||
2d511cfc | 640 | close_object_store(the_repository->objects); |
5bdece0d | 641 | |
a1bbc6c0 SB |
642 | /* |
643 | * Ok we have prepared all new packfiles. | |
a1bbc6c0 | 644 | */ |
a1bbc6c0 | 645 | for_each_string_list_item(item, &names) { |
b328c216 | 646 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
e3cf2303 | 647 | char *fname, *fname_old; |
525e18c0 | 648 | |
a1bbc6c0 | 649 | fname = mkpathdup("%s/pack-%s%s", |
42a02d85 | 650 | packdir, item->string, exts[ext].name); |
a1bbc6c0 | 651 | fname_old = mkpathdup("%s-%s%s", |
42a02d85 | 652 | packtmp, item->string, exts[ext].name); |
2fcb03b5 TB |
653 | |
654 | if (((uintptr_t)item->util) & (1 << ext)) { | |
655 | struct stat statbuffer; | |
656 | if (!stat(fname_old, &statbuffer)) { | |
657 | statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); | |
658 | chmod(fname_old, statbuffer.st_mode); | |
659 | } | |
660 | ||
b77fcd1e JK |
661 | if (rename(fname_old, fname)) |
662 | die_errno(_("renaming '%s' failed"), fname_old); | |
2fcb03b5 TB |
663 | } else if (!exts[ext].optional) |
664 | die(_("missing required file: %s"), fname_old); | |
665 | else if (unlink(fname) < 0 && errno != ENOENT) | |
666 | die_errno(_("could not unlink: %s"), fname); | |
a1bbc6c0 | 667 | |
e3cf2303 | 668 | free(fname); |
2fcb03b5 | 669 | free(fname_old); |
a1bbc6c0 SB |
670 | } |
671 | } | |
a1bbc6c0 SB |
672 | /* End of pack replacement. */ |
673 | ||
5d19e813 JT |
674 | reprepare_packed_git(the_repository); |
675 | ||
a1bbc6c0 | 676 | if (delete_redundant) { |
2f0c9e9a | 677 | const int hexsz = the_hash_algo->hexsz; |
4489a480 | 678 | int opts = 0; |
3383e199 | 679 | string_list_sort(&names); |
a1bbc6c0 SB |
680 | for_each_string_list_item(item, &existing_packs) { |
681 | char *sha1; | |
682 | size_t len = strlen(item->string); | |
2f0c9e9a | 683 | if (len < hexsz) |
a1bbc6c0 | 684 | continue; |
2f0c9e9a | 685 | sha1 = item->string + len - hexsz; |
a1bbc6c0 SB |
686 | if (!string_list_has_string(&names, sha1)) |
687 | remove_redundant_pack(packdir, item->string); | |
688 | } | |
0fabafd0 TB |
689 | |
690 | if (geometry) { | |
691 | struct strbuf buf = STRBUF_INIT; | |
692 | ||
693 | uint32_t i; | |
694 | for (i = 0; i < geometry->split; i++) { | |
695 | struct packed_git *p = geometry->pack[i]; | |
696 | if (string_list_has_string(&names, | |
697 | hash_to_hex(p->hash))) | |
698 | continue; | |
699 | ||
700 | strbuf_reset(&buf); | |
701 | strbuf_addstr(&buf, pack_basename(p)); | |
702 | strbuf_strip_suffix(&buf, ".pack"); | |
703 | ||
704 | remove_redundant_pack(packdir, buf.buf); | |
705 | } | |
706 | strbuf_release(&buf); | |
707 | } | |
2b958e79 | 708 | if (!po_args.quiet && isatty(2)) |
4489a480 RS |
709 | opts |= PRUNE_PACKED_VERBOSE; |
710 | prune_packed_objects(opts); | |
5dcfbf56 JS |
711 | |
712 | if (!keep_unreachable && | |
713 | (!(pack_everything & LOOSEN_UNREACHABLE) || | |
714 | unpack_unreachable) && | |
715 | is_repository_shallow(the_repository)) | |
716 | prune_shallow(PRUNE_QUICK); | |
a1bbc6c0 SB |
717 | } |
718 | ||
4489a480 RS |
719 | if (!no_update_server_info) |
720 | update_server_info(0); | |
a1bbc6c0 | 721 | remove_temporary_files(); |
0465a505 DS |
722 | |
723 | if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0)) | |
efbc3aee | 724 | write_midx_file(get_object_directory(), 0); |
0465a505 | 725 | |
a1bbc6c0 SB |
726 | string_list_clear(&names, 0); |
727 | string_list_clear(&rollback, 0); | |
728 | string_list_clear(&existing_packs, 0); | |
0fabafd0 | 729 | clear_pack_geometry(geometry); |
a1bbc6c0 SB |
730 | strbuf_release(&line); |
731 | ||
732 | return 0; | |
733 | } |