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