notes-merge: use ssize_t for write_in_full() return value
[git.git] / builtin / repack.c
blobf17a68a17da960813e6e925837638a5d9a26d5ee
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"
12 static int delta_base_offset = 1;
13 static int pack_kept_objects = -1;
14 static int write_bitmaps;
15 static char *packdir, *packtmp;
17 static const char *const git_repack_usage[] = {
18 N_("git repack [<options>]"),
19 NULL
22 static 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."
28 static int repack_config(const char *var, const char *value, void *cb)
30 if (!strcmp(var, "repack.usedeltabaseoffset")) {
31 delta_base_offset = git_config_bool(var, value);
32 return 0;
34 if (!strcmp(var, "repack.packkeptobjects")) {
35 pack_kept_objects = git_config_bool(var, value);
36 return 0;
38 if (!strcmp(var, "repack.writebitmaps") ||
39 !strcmp(var, "pack.writebitmaps")) {
40 write_bitmaps = git_config_bool(var, value);
41 return 0;
43 return git_default_config(var, value, cb);
47 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
49 static void remove_temporary_files(void)
51 struct strbuf buf = STRBUF_INIT;
52 size_t dirlen, prefixlen;
53 DIR *dir;
54 struct dirent *e;
56 dir = opendir(packdir);
57 if (!dir)
58 return;
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;
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);
73 closedir(dir);
74 strbuf_release(&buf);
77 static void remove_pack_on_signal(int signo)
79 remove_temporary_files();
80 sigchain_pop(signo);
81 raise(signo);
85 * Adds all packs hex strings to the fname list, which do not
86 * have a corresponding .keep file.
88 static void get_non_kept_pack_filenames(struct string_list *fname_list)
90 DIR *dir;
91 struct dirent *e;
92 char *fname;
94 if (!(dir = opendir(packdir)))
95 return;
97 while ((e = readdir(dir)) != NULL) {
98 size_t len;
99 if (!strip_suffix(e->d_name, ".pack", &len))
100 continue;
102 fname = xmemdupz(e->d_name, len);
104 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
105 string_list_append_nodup(fname_list, fname);
106 else
107 free(fname);
109 closedir(dir);
112 static void remove_redundant_pack(const char *dir_name, const char *base_name)
114 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
115 int i;
116 struct strbuf buf = STRBUF_INIT;
117 size_t plen;
119 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
120 plen = buf.len;
122 for (i = 0; i < ARRAY_SIZE(exts); i++) {
123 strbuf_setlen(&buf, plen);
124 strbuf_addstr(&buf, exts[i]);
125 unlink(buf.buf);
127 strbuf_release(&buf);
130 #define ALL_INTO_ONE 1
131 #define LOOSEN_UNREACHABLE 2
133 int cmd_repack(int argc, const char **argv, const char *prefix)
135 struct {
136 const char *name;
137 unsigned optional:1;
138 } exts[] = {
139 {".pack"},
140 {".idx"},
141 {".bitmap", 1},
143 struct child_process cmd = CHILD_PROCESS_INIT;
144 struct string_list_item *item;
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;
149 int ext, ret, failed;
150 FILE *out;
152 /* variables to be filled by option parsing */
153 int pack_everything = 0;
154 int delete_redundant = 0;
155 const char *unpack_unreachable = NULL;
156 int keep_unreachable = 0;
157 const char *window = NULL, *window_memory = NULL;
158 const char *depth = NULL;
159 const char *threads = NULL;
160 const char *max_pack_size = NULL;
161 int no_reuse_delta = 0, no_reuse_object = 0;
162 int no_update_server_info = 0;
163 int quiet = 0;
164 int local = 0;
166 struct option builtin_repack_options[] = {
167 OPT_BIT('a', NULL, &pack_everything,
168 N_("pack everything in a single pack"), ALL_INTO_ONE),
169 OPT_BIT('A', NULL, &pack_everything,
170 N_("same as -a, and turn unreachable objects loose"),
171 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
172 OPT_BOOL('d', NULL, &delete_redundant,
173 N_("remove redundant packs, and run git-prune-packed")),
174 OPT_BOOL('f', NULL, &no_reuse_delta,
175 N_("pass --no-reuse-delta to git-pack-objects")),
176 OPT_BOOL('F', NULL, &no_reuse_object,
177 N_("pass --no-reuse-object to git-pack-objects")),
178 OPT_BOOL('n', NULL, &no_update_server_info,
179 N_("do not run git-update-server-info")),
180 OPT__QUIET(&quiet, N_("be quiet")),
181 OPT_BOOL('l', "local", &local,
182 N_("pass --local to git-pack-objects")),
183 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
184 N_("write bitmap index")),
185 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
186 N_("with -A, do not loosen objects older than this")),
187 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
188 N_("with -a, repack unreachable objects")),
189 OPT_STRING(0, "window", &window, N_("n"),
190 N_("size of the window used for delta compression")),
191 OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),
192 N_("same as the above, but limit memory size instead of entries count")),
193 OPT_STRING(0, "depth", &depth, N_("n"),
194 N_("limits the maximum delta depth")),
195 OPT_STRING(0, "threads", &threads, N_("n"),
196 N_("limits the maximum number of threads")),
197 OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),
198 N_("maximum size of each packfile")),
199 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
200 N_("repack objects in packs marked with .keep")),
201 OPT_END()
204 git_config(repack_config, NULL);
206 argc = parse_options(argc, argv, prefix, builtin_repack_options,
207 git_repack_usage, 0);
209 if (delete_redundant && repository_format_precious_objects)
210 die(_("cannot delete packs in a precious-objects repo"));
212 if (keep_unreachable &&
213 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
214 die(_("--keep-unreachable and -A are incompatible"));
216 if (pack_kept_objects < 0)
217 pack_kept_objects = write_bitmaps;
219 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))
220 die(_(incremental_bitmap_conflict_error));
222 packdir = mkpathdup("%s/pack", get_object_directory());
223 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
225 sigchain_push_common(remove_pack_on_signal);
227 argv_array_push(&cmd.args, "pack-objects");
228 argv_array_push(&cmd.args, "--keep-true-parents");
229 if (!pack_kept_objects)
230 argv_array_push(&cmd.args, "--honor-pack-keep");
231 argv_array_push(&cmd.args, "--non-empty");
232 argv_array_push(&cmd.args, "--all");
233 argv_array_push(&cmd.args, "--reflog");
234 argv_array_push(&cmd.args, "--indexed-objects");
235 if (window)
236 argv_array_pushf(&cmd.args, "--window=%s", window);
237 if (window_memory)
238 argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory);
239 if (depth)
240 argv_array_pushf(&cmd.args, "--depth=%s", depth);
241 if (threads)
242 argv_array_pushf(&cmd.args, "--threads=%s", threads);
243 if (max_pack_size)
244 argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size);
245 if (no_reuse_delta)
246 argv_array_pushf(&cmd.args, "--no-reuse-delta");
247 if (no_reuse_object)
248 argv_array_pushf(&cmd.args, "--no-reuse-object");
249 if (write_bitmaps)
250 argv_array_push(&cmd.args, "--write-bitmap-index");
252 if (pack_everything & ALL_INTO_ONE) {
253 get_non_kept_pack_filenames(&existing_packs);
255 if (existing_packs.nr && delete_redundant) {
256 if (unpack_unreachable) {
257 argv_array_pushf(&cmd.args,
258 "--unpack-unreachable=%s",
259 unpack_unreachable);
260 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
261 } else if (pack_everything & LOOSEN_UNREACHABLE) {
262 argv_array_push(&cmd.args,
263 "--unpack-unreachable");
264 } else if (keep_unreachable) {
265 argv_array_push(&cmd.args, "--keep-unreachable");
266 argv_array_push(&cmd.args, "--pack-loose-unreachable");
267 } else {
268 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
271 } else {
272 argv_array_push(&cmd.args, "--unpacked");
273 argv_array_push(&cmd.args, "--incremental");
276 if (local)
277 argv_array_push(&cmd.args, "--local");
278 if (quiet)
279 argv_array_push(&cmd.args, "--quiet");
280 if (delta_base_offset)
281 argv_array_push(&cmd.args, "--delta-base-offset");
283 argv_array_push(&cmd.args, packtmp);
285 cmd.git_cmd = 1;
286 cmd.out = -1;
287 cmd.no_stdin = 1;
289 ret = start_command(&cmd);
290 if (ret)
291 return ret;
293 out = xfdopen(cmd.out, "r");
294 while (strbuf_getline_lf(&line, out) != EOF) {
295 if (line.len != 40)
296 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
297 string_list_append(&names, line.buf);
299 fclose(out);
300 ret = finish_command(&cmd);
301 if (ret)
302 return ret;
304 if (!names.nr && !quiet)
305 printf("Nothing new to pack.\n");
308 * Ok we have prepared all new packfiles.
309 * First see if there are packs of the same name and if so
310 * if we can move them out of the way (this can happen if we
311 * repacked immediately after packing fully.
313 failed = 0;
314 for_each_string_list_item(item, &names) {
315 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
316 char *fname, *fname_old;
317 fname = mkpathdup("%s/pack-%s%s", packdir,
318 item->string, exts[ext].name);
319 if (!file_exists(fname)) {
320 free(fname);
321 continue;
324 fname_old = mkpathdup("%s/old-%s%s", packdir,
325 item->string, exts[ext].name);
326 if (file_exists(fname_old))
327 if (unlink(fname_old))
328 failed = 1;
330 if (!failed && rename(fname, fname_old)) {
331 free(fname);
332 free(fname_old);
333 failed = 1;
334 break;
335 } else {
336 string_list_append(&rollback, fname);
337 free(fname_old);
340 if (failed)
341 break;
343 if (failed) {
344 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
345 for_each_string_list_item(item, &rollback) {
346 char *fname, *fname_old;
347 fname = mkpathdup("%s/%s", packdir, item->string);
348 fname_old = mkpathdup("%s/old-%s", packdir, item->string);
349 if (rename(fname_old, fname))
350 string_list_append(&rollback_failure, fname);
351 free(fname);
352 free(fname_old);
355 if (rollback_failure.nr) {
356 int i;
357 fprintf(stderr,
358 "WARNING: Some packs in use have been renamed by\n"
359 "WARNING: prefixing old- to their name, in order to\n"
360 "WARNING: replace them with the new version of the\n"
361 "WARNING: file. But the operation failed, and the\n"
362 "WARNING: attempt to rename them back to their\n"
363 "WARNING: original names also failed.\n"
364 "WARNING: Please rename them in %s manually:\n", packdir);
365 for (i = 0; i < rollback_failure.nr; i++)
366 fprintf(stderr, "WARNING: old-%s -> %s\n",
367 rollback_failure.items[i].string,
368 rollback_failure.items[i].string);
370 exit(1);
373 /* Now the ones with the same name are out of the way... */
374 for_each_string_list_item(item, &names) {
375 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
376 char *fname, *fname_old;
377 struct stat statbuffer;
378 int exists = 0;
379 fname = mkpathdup("%s/pack-%s%s",
380 packdir, item->string, exts[ext].name);
381 fname_old = mkpathdup("%s-%s%s",
382 packtmp, item->string, exts[ext].name);
383 if (!stat(fname_old, &statbuffer)) {
384 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
385 chmod(fname_old, statbuffer.st_mode);
386 exists = 1;
388 if (exists || !exts[ext].optional) {
389 if (rename(fname_old, fname))
390 die_errno(_("renaming '%s' failed"), fname_old);
392 free(fname);
393 free(fname_old);
397 /* Remove the "old-" files */
398 for_each_string_list_item(item, &names) {
399 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
400 char *fname;
401 fname = mkpathdup("%s/old-%s%s",
402 packdir,
403 item->string,
404 exts[ext].name);
405 if (remove_path(fname))
406 warning(_("failed to remove '%s'"), fname);
407 free(fname);
411 /* End of pack replacement. */
413 if (delete_redundant) {
414 int opts = 0;
415 string_list_sort(&names);
416 for_each_string_list_item(item, &existing_packs) {
417 char *sha1;
418 size_t len = strlen(item->string);
419 if (len < 40)
420 continue;
421 sha1 = item->string + len - 40;
422 if (!string_list_has_string(&names, sha1))
423 remove_redundant_pack(packdir, item->string);
425 if (!quiet && isatty(2))
426 opts |= PRUNE_PACKED_VERBOSE;
427 prune_packed_objects(opts);
430 if (!no_update_server_info)
431 update_server_info(0);
432 remove_temporary_files();
433 string_list_clear(&names, 0);
434 string_list_clear(&rollback, 0);
435 string_list_clear(&existing_packs, 0);
436 strbuf_release(&line);
438 return 0;