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