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