repack: refactor setup of pack-objects cmd
[git/git.git] / builtin / repack.c
CommitLineData
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
12static int delta_base_offset = 1;
ee34a2be 13static int pack_kept_objects = -1;
2bed2d47 14static int write_bitmaps;
a1bbc6c0
SB
15static char *packdir, *packtmp;
16
17static const char *const git_repack_usage[] = {
9c9b4f2f 18 N_("git repack [<options>]"),
a1bbc6c0
SB
19 NULL
20};
21
1c409a70
DT
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
a1bbc6c0
SB
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 }
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 */
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
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
89static 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
123static 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
2b958e79
JT
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
a1bbc6c0
SB
182#define ALL_INTO_ONE 1
183#define LOOSEN_UNREACHABLE 2
184
185int cmd_repack(int argc, const char **argv, const char *prefix)
186{
42a02d85
JK
187 struct {
188 const char *name;
b77fcd1e 189 unsigned optional:1;
42a02d85
JK
190 } exts[] = {
191 {".pack"},
192 {".idx"},
5cf2741c 193 {".bitmap", 1},
42a02d85 194 };
d3180279 195 struct child_process cmd = CHILD_PROCESS_INIT;
a1bbc6c0 196 struct string_list_item *item;
a1bbc6c0
SB
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;
ed7e5fc3 201 int i, ext, ret, failed;
a1bbc6c0
SB
202 FILE *out;
203
204 /* variables to be filled by option parsing */
205 int pack_everything = 0;
206 int delete_redundant = 0;
aa8bd519 207 const char *unpack_unreachable = NULL;
905f27b8 208 int keep_unreachable = 0;
ed7e5fc3 209 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
a1bbc6c0 210 int no_update_server_info = 0;
2b958e79 211 struct pack_objects_args po_args = {NULL};
a1bbc6c0
SB
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")),
2b958e79 221 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
a1bbc6c0 222 N_("pass --no-reuse-delta to git-pack-objects")),
2b958e79 223 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
a1bbc6c0
SB
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")),
2b958e79
JT
227 OPT__QUIET(&po_args.quiet, N_("be quiet")),
228 OPT_BOOL('l', "local", &po_args.local,
a1bbc6c0 229 N_("pass --local to git-pack-objects")),
d078d85b 230 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
5cf2741c 231 N_("write bitmap index")),
a1bbc6c0
SB
232 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
233 N_("with -A, do not loosen objects older than this")),
905f27b8
JK
234 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
235 N_("with -a, repack unreachable objects")),
2b958e79 236 OPT_STRING(0, "window", &po_args.window, N_("n"),
a1bbc6c0 237 N_("size of the window used for delta compression")),
2b958e79 238 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
a1bbc6c0 239 N_("same as the above, but limit memory size instead of entries count")),
2b958e79 240 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
a1bbc6c0 241 N_("limits the maximum delta depth")),
2b958e79 242 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
40bcf318 243 N_("limits the maximum number of threads")),
2b958e79 244 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
a1bbc6c0 245 N_("maximum size of each packfile")),
ee34a2be
JK
246 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
247 N_("repack objects in packs marked with .keep")),
ed7e5fc3
NTND
248 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
249 N_("do not repack this pack")),
a1bbc6c0
SB
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
067fbd41
JK
258 if (delete_redundant && repository_format_precious_objects)
259 die(_("cannot delete packs in a precious-objects repo"));
260
905f27b8
JK
261 if (keep_unreachable &&
262 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
263 die(_("--keep-unreachable and -A are incompatible"));
264
ee34a2be 265 if (pack_kept_objects < 0)
2bed2d47 266 pack_kept_objects = write_bitmaps;
ee34a2be 267
1c409a70
DT
268 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))
269 die(_(incremental_bitmap_conflict_error));
270
a1bbc6c0
SB
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
2b958e79
JT
276 prepare_pack_objects(&cmd, &po_args);
277
a2bae2dc 278 argv_array_push(&cmd.args, "--keep-true-parents");
ee34a2be 279 if (!pack_kept_objects)
a2bae2dc 280 argv_array_push(&cmd.args, "--honor-pack-keep");
ed7e5fc3
NTND
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);
a2bae2dc
RS
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");
0c16cd49
JT
288 if (repository_format_partial_clone)
289 argv_array_push(&cmd.args, "--exclude-promisor-objects");
2bed2d47 290 if (write_bitmaps)
a2bae2dc 291 argv_array_push(&cmd.args, "--write-bitmap-index");
a1bbc6c0
SB
292
293 if (pack_everything & ALL_INTO_ONE) {
ed7e5fc3 294 get_non_kept_pack_filenames(&existing_packs, &keep_pack_list);
a1bbc6c0
SB
295
296 if (existing_packs.nr && delete_redundant) {
8d422993 297 if (unpack_unreachable) {
a2bae2dc 298 argv_array_pushf(&cmd.args,
a1bbc6c0
SB
299 "--unpack-unreachable=%s",
300 unpack_unreachable);
8d422993
JK
301 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
302 } else if (pack_everything & LOOSEN_UNREACHABLE) {
a2bae2dc 303 argv_array_push(&cmd.args,
a1bbc6c0 304 "--unpack-unreachable");
905f27b8
JK
305 } else if (keep_unreachable) {
306 argv_array_push(&cmd.args, "--keep-unreachable");
e26a8c47 307 argv_array_push(&cmd.args, "--pack-loose-unreachable");
8d422993
JK
308 } else {
309 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
310 }
a1bbc6c0
SB
311 }
312 } else {
a2bae2dc
RS
313 argv_array_push(&cmd.args, "--unpacked");
314 argv_array_push(&cmd.args, "--incremental");
a1bbc6c0
SB
315 }
316
a1bbc6c0
SB
317 cmd.no_stdin = 1;
318
319 ret = start_command(&cmd);
320 if (ret)
ffc9329f 321 return ret;
a1bbc6c0 322
a1bbc6c0 323 out = xfdopen(cmd.out, "r");
8f309aeb 324 while (strbuf_getline_lf(&line, out) != EOF) {
a1bbc6c0
SB
325 if (line.len != 40)
326 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
327 string_list_append(&names, line.buf);
a1bbc6c0
SB
328 }
329 fclose(out);
330 ret = finish_command(&cmd);
331 if (ret)
ffc9329f 332 return ret;
a1bbc6c0 333
2b958e79 334 if (!names.nr && !po_args.quiet)
a1bbc6c0
SB
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) {
b328c216 345 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
e3cf2303 346 char *fname, *fname_old;
9d7fbfd2 347 fname = mkpathdup("%s/pack-%s%s", packdir,
42a02d85 348 item->string, exts[ext].name);
a1bbc6c0
SB
349 if (!file_exists(fname)) {
350 free(fname);
351 continue;
352 }
353
e3cf2303 354 fname_old = mkpathdup("%s/old-%s%s", packdir,
42a02d85 355 item->string, exts[ext].name);
a1bbc6c0
SB
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);
e3cf2303 362 free(fname_old);
a1bbc6c0
SB
363 failed = 1;
364 break;
365 } else {
366 string_list_append(&rollback, fname);
e3cf2303 367 free(fname_old);
a1bbc6c0
SB
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) {
e3cf2303 376 char *fname, *fname_old;
a1bbc6c0 377 fname = mkpathdup("%s/%s", packdir, item->string);
e3cf2303 378 fname_old = mkpathdup("%s/old-%s", packdir, item->string);
a1bbc6c0
SB
379 if (rename(fname_old, fname))
380 string_list_append(&rollback_failure, fname);
381 free(fname);
e3cf2303 382 free(fname_old);
a1bbc6c0
SB
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) {
b328c216 405 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
a1bbc6c0
SB
406 char *fname, *fname_old;
407 struct stat statbuffer;
b77fcd1e 408 int exists = 0;
a1bbc6c0 409 fname = mkpathdup("%s/pack-%s%s",
42a02d85 410 packdir, item->string, exts[ext].name);
a1bbc6c0 411 fname_old = mkpathdup("%s-%s%s",
42a02d85 412 packtmp, item->string, exts[ext].name);
a1bbc6c0
SB
413 if (!stat(fname_old, &statbuffer)) {
414 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
415 chmod(fname_old, statbuffer.st_mode);
b77fcd1e
JK
416 exists = 1;
417 }
418 if (exists || !exts[ext].optional) {
419 if (rename(fname_old, fname))
420 die_errno(_("renaming '%s' failed"), fname_old);
a1bbc6c0 421 }
a1bbc6c0
SB
422 free(fname);
423 free(fname_old);
424 }
425 }
426
427 /* Remove the "old-" files */
428 for_each_string_list_item(item, &names) {
b328c216 429 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
e3cf2303
JK
430 char *fname;
431 fname = mkpathdup("%s/old-%s%s",
432 packdir,
433 item->string,
434 exts[ext].name);
0b63c6a5 435 if (remove_path(fname))
e923a8ab 436 warning(_("failed to remove '%s'"), fname);
e3cf2303 437 free(fname);
a1bbc6c0
SB
438 }
439 }
440
441 /* End of pack replacement. */
442
443 if (delete_redundant) {
4489a480 444 int opts = 0;
3383e199 445 string_list_sort(&names);
a1bbc6c0
SB
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 }
2b958e79 455 if (!po_args.quiet && isatty(2))
4489a480
RS
456 opts |= PRUNE_PACKED_VERBOSE;
457 prune_packed_objects(opts);
a1bbc6c0
SB
458 }
459
4489a480
RS
460 if (!no_update_server_info)
461 update_server_info(0);
a1bbc6c0
SB
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}