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