]>
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" | |
10 | #include "argv-array.h" | |
11 | ||
12 | static int delta_base_offset = 1; | |
ee34a2be | 13 | static int pack_kept_objects = -1; |
2bed2d47 | 14 | static int write_bitmaps; |
a1bbc6c0 SB |
15 | static char *packdir, *packtmp; |
16 | ||
17 | static const char *const git_repack_usage[] = { | |
9c9b4f2f | 18 | N_("git repack [<options>]"), |
a1bbc6c0 SB |
19 | NULL |
20 | }; | |
21 | ||
1c409a70 DT |
22 | static const char incremental_bitmap_conflict_error[] = N_( |
23 | "Incremental repacks are incompatible with bitmap indexes. Use\n" | |
24 | "--no-write-bitmap-index or disable the pack.writebitmaps configuration." | |
25 | ); | |
26 | ||
27 | ||
a1bbc6c0 SB |
28 | static int repack_config(const char *var, const char *value, void *cb) |
29 | { | |
30 | if (!strcmp(var, "repack.usedeltabaseoffset")) { | |
31 | delta_base_offset = git_config_bool(var, value); | |
32 | return 0; | |
33 | } | |
ee34a2be JK |
34 | if (!strcmp(var, "repack.packkeptobjects")) { |
35 | pack_kept_objects = git_config_bool(var, value); | |
36 | return 0; | |
37 | } | |
71d76cb4 JK |
38 | if (!strcmp(var, "repack.writebitmaps") || |
39 | !strcmp(var, "pack.writebitmaps")) { | |
d078d85b | 40 | write_bitmaps = git_config_bool(var, value); |
3198b89f JK |
41 | return 0; |
42 | } | |
a1bbc6c0 SB |
43 | return git_default_config(var, value, cb); |
44 | } | |
45 | ||
46 | /* | |
47 | * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files. | |
48 | */ | |
49 | static void remove_temporary_files(void) | |
50 | { | |
51 | struct strbuf buf = STRBUF_INIT; | |
52 | size_t dirlen, prefixlen; | |
53 | DIR *dir; | |
54 | struct dirent *e; | |
55 | ||
56 | dir = opendir(packdir); | |
57 | if (!dir) | |
58 | return; | |
59 | ||
60 | /* Point at the slash at the end of ".../objects/pack/" */ | |
61 | dirlen = strlen(packdir) + 1; | |
62 | strbuf_addstr(&buf, packtmp); | |
63 | /* Hold the length of ".tmp-%d-pack-" */ | |
64 | prefixlen = buf.len - dirlen; | |
65 | ||
66 | while ((e = readdir(dir))) { | |
67 | if (strncmp(e->d_name, buf.buf + dirlen, prefixlen)) | |
68 | continue; | |
69 | strbuf_setlen(&buf, dirlen); | |
70 | strbuf_addstr(&buf, e->d_name); | |
71 | unlink(buf.buf); | |
72 | } | |
73 | closedir(dir); | |
74 | strbuf_release(&buf); | |
75 | } | |
76 | ||
77 | static void remove_pack_on_signal(int signo) | |
78 | { | |
79 | remove_temporary_files(); | |
80 | sigchain_pop(signo); | |
81 | raise(signo); | |
82 | } | |
83 | ||
84 | /* | |
85 | * Adds all packs hex strings to the fname list, which do not | |
0c16cd49 JT |
86 | * have a corresponding .keep or .promisor file. These packs are not to |
87 | * be kept if we are going to pack everything into one file. | |
a1bbc6c0 | 88 | */ |
ed7e5fc3 NTND |
89 | static void get_non_kept_pack_filenames(struct string_list *fname_list, |
90 | const struct string_list *extra_keep) | |
a1bbc6c0 SB |
91 | { |
92 | DIR *dir; | |
93 | struct dirent *e; | |
94 | char *fname; | |
a1bbc6c0 SB |
95 | |
96 | if (!(dir = opendir(packdir))) | |
97 | return; | |
98 | ||
99 | while ((e = readdir(dir)) != NULL) { | |
26936bfd | 100 | size_t len; |
ed7e5fc3 NTND |
101 | int i; |
102 | ||
103 | for (i = 0; i < extra_keep->nr; i++) | |
104 | if (!fspathcmp(e->d_name, extra_keep->items[i].string)) | |
105 | break; | |
106 | if (extra_keep->nr > 0 && i < extra_keep->nr) | |
107 | continue; | |
108 | ||
26936bfd | 109 | if (!strip_suffix(e->d_name, ".pack", &len)) |
a1bbc6c0 SB |
110 | continue; |
111 | ||
a1bbc6c0 SB |
112 | fname = xmemdupz(e->d_name, len); |
113 | ||
0c16cd49 JT |
114 | if (!file_exists(mkpath("%s/%s.keep", packdir, fname)) && |
115 | !file_exists(mkpath("%s/%s.promisor", packdir, fname))) | |
a1bbc6c0 SB |
116 | string_list_append_nodup(fname_list, fname); |
117 | else | |
118 | free(fname); | |
119 | } | |
120 | closedir(dir); | |
121 | } | |
122 | ||
123 | static void remove_redundant_pack(const char *dir_name, const char *base_name) | |
124 | { | |
5cf2741c | 125 | const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"}; |
a1bbc6c0 SB |
126 | int i; |
127 | struct strbuf buf = STRBUF_INIT; | |
128 | size_t plen; | |
129 | ||
130 | strbuf_addf(&buf, "%s/%s", dir_name, base_name); | |
131 | plen = buf.len; | |
132 | ||
133 | for (i = 0; i < ARRAY_SIZE(exts); i++) { | |
134 | strbuf_setlen(&buf, plen); | |
135 | strbuf_addstr(&buf, exts[i]); | |
136 | unlink(buf.buf); | |
137 | } | |
138 | strbuf_release(&buf); | |
139 | } | |
140 | ||
141 | #define ALL_INTO_ONE 1 | |
142 | #define LOOSEN_UNREACHABLE 2 | |
143 | ||
144 | int cmd_repack(int argc, const char **argv, const char *prefix) | |
145 | { | |
42a02d85 JK |
146 | struct { |
147 | const char *name; | |
b77fcd1e | 148 | unsigned optional:1; |
42a02d85 JK |
149 | } exts[] = { |
150 | {".pack"}, | |
151 | {".idx"}, | |
5cf2741c | 152 | {".bitmap", 1}, |
42a02d85 | 153 | }; |
d3180279 | 154 | struct child_process cmd = CHILD_PROCESS_INIT; |
a1bbc6c0 | 155 | struct string_list_item *item; |
a1bbc6c0 SB |
156 | struct string_list names = STRING_LIST_INIT_DUP; |
157 | struct string_list rollback = STRING_LIST_INIT_NODUP; | |
158 | struct string_list existing_packs = STRING_LIST_INIT_DUP; | |
159 | struct strbuf line = STRBUF_INIT; | |
ed7e5fc3 | 160 | int i, ext, ret, failed; |
a1bbc6c0 SB |
161 | FILE *out; |
162 | ||
163 | /* variables to be filled by option parsing */ | |
164 | int pack_everything = 0; | |
165 | int delete_redundant = 0; | |
aa8bd519 | 166 | const char *unpack_unreachable = NULL; |
905f27b8 | 167 | int keep_unreachable = 0; |
b861e235 JK |
168 | const char *window = NULL, *window_memory = NULL; |
169 | const char *depth = NULL; | |
40bcf318 | 170 | const char *threads = NULL; |
b861e235 | 171 | const char *max_pack_size = NULL; |
ed7e5fc3 | 172 | struct string_list keep_pack_list = STRING_LIST_INIT_NODUP; |
a1bbc6c0 SB |
173 | int no_reuse_delta = 0, no_reuse_object = 0; |
174 | int no_update_server_info = 0; | |
175 | int quiet = 0; | |
176 | int local = 0; | |
177 | ||
178 | struct option builtin_repack_options[] = { | |
179 | OPT_BIT('a', NULL, &pack_everything, | |
180 | N_("pack everything in a single pack"), ALL_INTO_ONE), | |
181 | OPT_BIT('A', NULL, &pack_everything, | |
182 | N_("same as -a, and turn unreachable objects loose"), | |
183 | LOOSEN_UNREACHABLE | ALL_INTO_ONE), | |
184 | OPT_BOOL('d', NULL, &delete_redundant, | |
185 | N_("remove redundant packs, and run git-prune-packed")), | |
186 | OPT_BOOL('f', NULL, &no_reuse_delta, | |
187 | N_("pass --no-reuse-delta to git-pack-objects")), | |
188 | OPT_BOOL('F', NULL, &no_reuse_object, | |
189 | N_("pass --no-reuse-object to git-pack-objects")), | |
190 | OPT_BOOL('n', NULL, &no_update_server_info, | |
191 | N_("do not run git-update-server-info")), | |
192 | OPT__QUIET(&quiet, N_("be quiet")), | |
193 | OPT_BOOL('l', "local", &local, | |
194 | N_("pass --local to git-pack-objects")), | |
d078d85b | 195 | OPT_BOOL('b', "write-bitmap-index", &write_bitmaps, |
5cf2741c | 196 | N_("write bitmap index")), |
a1bbc6c0 SB |
197 | OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"), |
198 | N_("with -A, do not loosen objects older than this")), | |
905f27b8 JK |
199 | OPT_BOOL('k', "keep-unreachable", &keep_unreachable, |
200 | N_("with -a, repack unreachable objects")), | |
b861e235 | 201 | OPT_STRING(0, "window", &window, N_("n"), |
a1bbc6c0 | 202 | N_("size of the window used for delta compression")), |
b861e235 | 203 | OPT_STRING(0, "window-memory", &window_memory, N_("bytes"), |
a1bbc6c0 | 204 | N_("same as the above, but limit memory size instead of entries count")), |
b861e235 | 205 | OPT_STRING(0, "depth", &depth, N_("n"), |
a1bbc6c0 | 206 | N_("limits the maximum delta depth")), |
40bcf318 JH |
207 | OPT_STRING(0, "threads", &threads, N_("n"), |
208 | N_("limits the maximum number of threads")), | |
b861e235 | 209 | OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"), |
a1bbc6c0 | 210 | N_("maximum size of each packfile")), |
ee34a2be JK |
211 | OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects, |
212 | N_("repack objects in packs marked with .keep")), | |
ed7e5fc3 NTND |
213 | OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"), |
214 | N_("do not repack this pack")), | |
a1bbc6c0 SB |
215 | OPT_END() |
216 | }; | |
217 | ||
218 | git_config(repack_config, NULL); | |
219 | ||
220 | argc = parse_options(argc, argv, prefix, builtin_repack_options, | |
221 | git_repack_usage, 0); | |
222 | ||
067fbd41 JK |
223 | if (delete_redundant && repository_format_precious_objects) |
224 | die(_("cannot delete packs in a precious-objects repo")); | |
225 | ||
905f27b8 JK |
226 | if (keep_unreachable && |
227 | (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE))) | |
228 | die(_("--keep-unreachable and -A are incompatible")); | |
229 | ||
ee34a2be | 230 | if (pack_kept_objects < 0) |
2bed2d47 | 231 | pack_kept_objects = write_bitmaps; |
ee34a2be | 232 | |
1c409a70 DT |
233 | if (write_bitmaps && !(pack_everything & ALL_INTO_ONE)) |
234 | die(_(incremental_bitmap_conflict_error)); | |
235 | ||
a1bbc6c0 SB |
236 | packdir = mkpathdup("%s/pack", get_object_directory()); |
237 | packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid()); | |
238 | ||
239 | sigchain_push_common(remove_pack_on_signal); | |
240 | ||
a2bae2dc RS |
241 | argv_array_push(&cmd.args, "pack-objects"); |
242 | argv_array_push(&cmd.args, "--keep-true-parents"); | |
ee34a2be | 243 | if (!pack_kept_objects) |
a2bae2dc | 244 | argv_array_push(&cmd.args, "--honor-pack-keep"); |
ed7e5fc3 NTND |
245 | for (i = 0; i < keep_pack_list.nr; i++) |
246 | argv_array_pushf(&cmd.args, "--keep-pack=%s", | |
247 | keep_pack_list.items[i].string); | |
a2bae2dc RS |
248 | argv_array_push(&cmd.args, "--non-empty"); |
249 | argv_array_push(&cmd.args, "--all"); | |
250 | argv_array_push(&cmd.args, "--reflog"); | |
251 | argv_array_push(&cmd.args, "--indexed-objects"); | |
0c16cd49 JT |
252 | if (repository_format_partial_clone) |
253 | argv_array_push(&cmd.args, "--exclude-promisor-objects"); | |
a1bbc6c0 | 254 | if (window) |
a2bae2dc | 255 | argv_array_pushf(&cmd.args, "--window=%s", window); |
a1bbc6c0 | 256 | if (window_memory) |
a2bae2dc | 257 | argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory); |
a1bbc6c0 | 258 | if (depth) |
a2bae2dc | 259 | argv_array_pushf(&cmd.args, "--depth=%s", depth); |
40bcf318 JH |
260 | if (threads) |
261 | argv_array_pushf(&cmd.args, "--threads=%s", threads); | |
a1bbc6c0 | 262 | if (max_pack_size) |
a2bae2dc | 263 | argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size); |
a1bbc6c0 | 264 | if (no_reuse_delta) |
a2bae2dc | 265 | argv_array_pushf(&cmd.args, "--no-reuse-delta"); |
a1bbc6c0 | 266 | if (no_reuse_object) |
a2bae2dc | 267 | argv_array_pushf(&cmd.args, "--no-reuse-object"); |
2bed2d47 | 268 | if (write_bitmaps) |
a2bae2dc | 269 | argv_array_push(&cmd.args, "--write-bitmap-index"); |
a1bbc6c0 SB |
270 | |
271 | if (pack_everything & ALL_INTO_ONE) { | |
ed7e5fc3 | 272 | get_non_kept_pack_filenames(&existing_packs, &keep_pack_list); |
a1bbc6c0 SB |
273 | |
274 | if (existing_packs.nr && delete_redundant) { | |
8d422993 | 275 | if (unpack_unreachable) { |
a2bae2dc | 276 | argv_array_pushf(&cmd.args, |
a1bbc6c0 SB |
277 | "--unpack-unreachable=%s", |
278 | unpack_unreachable); | |
8d422993 JK |
279 | argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1"); |
280 | } else if (pack_everything & LOOSEN_UNREACHABLE) { | |
a2bae2dc | 281 | argv_array_push(&cmd.args, |
a1bbc6c0 | 282 | "--unpack-unreachable"); |
905f27b8 JK |
283 | } else if (keep_unreachable) { |
284 | argv_array_push(&cmd.args, "--keep-unreachable"); | |
e26a8c47 | 285 | argv_array_push(&cmd.args, "--pack-loose-unreachable"); |
8d422993 JK |
286 | } else { |
287 | argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1"); | |
288 | } | |
a1bbc6c0 SB |
289 | } |
290 | } else { | |
a2bae2dc RS |
291 | argv_array_push(&cmd.args, "--unpacked"); |
292 | argv_array_push(&cmd.args, "--incremental"); | |
a1bbc6c0 SB |
293 | } |
294 | ||
295 | if (local) | |
a2bae2dc | 296 | argv_array_push(&cmd.args, "--local"); |
a1bbc6c0 | 297 | if (quiet) |
a2bae2dc | 298 | argv_array_push(&cmd.args, "--quiet"); |
a1bbc6c0 | 299 | if (delta_base_offset) |
a2bae2dc | 300 | argv_array_push(&cmd.args, "--delta-base-offset"); |
a1bbc6c0 | 301 | |
a2bae2dc | 302 | argv_array_push(&cmd.args, packtmp); |
a1bbc6c0 | 303 | |
a1bbc6c0 SB |
304 | cmd.git_cmd = 1; |
305 | cmd.out = -1; | |
306 | cmd.no_stdin = 1; | |
307 | ||
308 | ret = start_command(&cmd); | |
309 | if (ret) | |
ffc9329f | 310 | return ret; |
a1bbc6c0 | 311 | |
a1bbc6c0 | 312 | out = xfdopen(cmd.out, "r"); |
8f309aeb | 313 | while (strbuf_getline_lf(&line, out) != EOF) { |
a1bbc6c0 SB |
314 | if (line.len != 40) |
315 | die("repack: Expecting 40 character sha1 lines only from pack-objects."); | |
316 | string_list_append(&names, line.buf); | |
a1bbc6c0 SB |
317 | } |
318 | fclose(out); | |
319 | ret = finish_command(&cmd); | |
320 | if (ret) | |
ffc9329f | 321 | return ret; |
a1bbc6c0 | 322 | |
3e7b066e | 323 | if (!names.nr && !quiet) |
a1bbc6c0 SB |
324 | printf("Nothing new to pack.\n"); |
325 | ||
326 | /* | |
327 | * Ok we have prepared all new packfiles. | |
328 | * First see if there are packs of the same name and if so | |
329 | * if we can move them out of the way (this can happen if we | |
330 | * repacked immediately after packing fully. | |
331 | */ | |
332 | failed = 0; | |
333 | for_each_string_list_item(item, &names) { | |
b328c216 | 334 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
e3cf2303 | 335 | char *fname, *fname_old; |
9d7fbfd2 | 336 | fname = mkpathdup("%s/pack-%s%s", packdir, |
42a02d85 | 337 | item->string, exts[ext].name); |
a1bbc6c0 SB |
338 | if (!file_exists(fname)) { |
339 | free(fname); | |
340 | continue; | |
341 | } | |
342 | ||
e3cf2303 | 343 | fname_old = mkpathdup("%s/old-%s%s", packdir, |
42a02d85 | 344 | item->string, exts[ext].name); |
a1bbc6c0 SB |
345 | if (file_exists(fname_old)) |
346 | if (unlink(fname_old)) | |
347 | failed = 1; | |
348 | ||
349 | if (!failed && rename(fname, fname_old)) { | |
350 | free(fname); | |
e3cf2303 | 351 | free(fname_old); |
a1bbc6c0 SB |
352 | failed = 1; |
353 | break; | |
354 | } else { | |
355 | string_list_append(&rollback, fname); | |
e3cf2303 | 356 | free(fname_old); |
a1bbc6c0 SB |
357 | } |
358 | } | |
359 | if (failed) | |
360 | break; | |
361 | } | |
362 | if (failed) { | |
363 | struct string_list rollback_failure = STRING_LIST_INIT_DUP; | |
364 | for_each_string_list_item(item, &rollback) { | |
e3cf2303 | 365 | char *fname, *fname_old; |
a1bbc6c0 | 366 | fname = mkpathdup("%s/%s", packdir, item->string); |
e3cf2303 | 367 | fname_old = mkpathdup("%s/old-%s", packdir, item->string); |
a1bbc6c0 SB |
368 | if (rename(fname_old, fname)) |
369 | string_list_append(&rollback_failure, fname); | |
370 | free(fname); | |
e3cf2303 | 371 | free(fname_old); |
a1bbc6c0 SB |
372 | } |
373 | ||
374 | if (rollback_failure.nr) { | |
375 | int i; | |
376 | fprintf(stderr, | |
377 | "WARNING: Some packs in use have been renamed by\n" | |
378 | "WARNING: prefixing old- to their name, in order to\n" | |
379 | "WARNING: replace them with the new version of the\n" | |
380 | "WARNING: file. But the operation failed, and the\n" | |
381 | "WARNING: attempt to rename them back to their\n" | |
382 | "WARNING: original names also failed.\n" | |
383 | "WARNING: Please rename them in %s manually:\n", packdir); | |
384 | for (i = 0; i < rollback_failure.nr; i++) | |
385 | fprintf(stderr, "WARNING: old-%s -> %s\n", | |
386 | rollback_failure.items[i].string, | |
387 | rollback_failure.items[i].string); | |
388 | } | |
389 | exit(1); | |
390 | } | |
391 | ||
392 | /* Now the ones with the same name are out of the way... */ | |
393 | for_each_string_list_item(item, &names) { | |
b328c216 | 394 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
a1bbc6c0 SB |
395 | char *fname, *fname_old; |
396 | struct stat statbuffer; | |
b77fcd1e | 397 | int exists = 0; |
a1bbc6c0 | 398 | fname = mkpathdup("%s/pack-%s%s", |
42a02d85 | 399 | packdir, item->string, exts[ext].name); |
a1bbc6c0 | 400 | fname_old = mkpathdup("%s-%s%s", |
42a02d85 | 401 | packtmp, item->string, exts[ext].name); |
a1bbc6c0 SB |
402 | if (!stat(fname_old, &statbuffer)) { |
403 | statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); | |
404 | chmod(fname_old, statbuffer.st_mode); | |
b77fcd1e JK |
405 | exists = 1; |
406 | } | |
407 | if (exists || !exts[ext].optional) { | |
408 | if (rename(fname_old, fname)) | |
409 | die_errno(_("renaming '%s' failed"), fname_old); | |
a1bbc6c0 | 410 | } |
a1bbc6c0 SB |
411 | free(fname); |
412 | free(fname_old); | |
413 | } | |
414 | } | |
415 | ||
416 | /* Remove the "old-" files */ | |
417 | for_each_string_list_item(item, &names) { | |
b328c216 | 418 | for (ext = 0; ext < ARRAY_SIZE(exts); ext++) { |
e3cf2303 JK |
419 | char *fname; |
420 | fname = mkpathdup("%s/old-%s%s", | |
421 | packdir, | |
422 | item->string, | |
423 | exts[ext].name); | |
0b63c6a5 | 424 | if (remove_path(fname)) |
e923a8ab | 425 | warning(_("failed to remove '%s'"), fname); |
e3cf2303 | 426 | free(fname); |
a1bbc6c0 SB |
427 | } |
428 | } | |
429 | ||
430 | /* End of pack replacement. */ | |
431 | ||
432 | if (delete_redundant) { | |
4489a480 | 433 | int opts = 0; |
3383e199 | 434 | string_list_sort(&names); |
a1bbc6c0 SB |
435 | for_each_string_list_item(item, &existing_packs) { |
436 | char *sha1; | |
437 | size_t len = strlen(item->string); | |
438 | if (len < 40) | |
439 | continue; | |
440 | sha1 = item->string + len - 40; | |
441 | if (!string_list_has_string(&names, sha1)) | |
442 | remove_redundant_pack(packdir, item->string); | |
443 | } | |
4489a480 RS |
444 | if (!quiet && isatty(2)) |
445 | opts |= PRUNE_PACKED_VERBOSE; | |
446 | prune_packed_objects(opts); | |
a1bbc6c0 SB |
447 | } |
448 | ||
4489a480 RS |
449 | if (!no_update_server_info) |
450 | update_server_info(0); | |
a1bbc6c0 SB |
451 | remove_temporary_files(); |
452 | string_list_clear(&names, 0); | |
453 | string_list_clear(&rollback, 0); | |
454 | string_list_clear(&existing_packs, 0); | |
455 | strbuf_release(&line); | |
456 | ||
457 | return 0; | |
458 | } |