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