doc: describe git svn init --ignore-refs
[git/git-svn.git] / builtin / repack.c
blob38ba4ef825ebf4791afb50e72e69bdb61db2aa14
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"
11 static int delta_base_offset = 1;
12 static int pack_kept_objects = -1;
13 static int write_bitmaps;
14 static char *packdir, *packtmp;
16 static const char *const git_repack_usage[] = {
17 N_("git repack [<options>]"),
18 NULL
21 static const char incremental_bitmap_conflict_error[] = N_(
22 "Incremental repacks are incompatible with bitmap indexes. Use\n"
23 "--no-write-bitmap-index or disable the pack.writebitmaps configuration."
27 static int repack_config(const char *var, const char *value, void *cb)
29 if (!strcmp(var, "repack.usedeltabaseoffset")) {
30 delta_base_offset = git_config_bool(var, value);
31 return 0;
33 if (!strcmp(var, "repack.packkeptobjects")) {
34 pack_kept_objects = git_config_bool(var, value);
35 return 0;
37 if (!strcmp(var, "repack.writebitmaps") ||
38 !strcmp(var, "pack.writebitmaps")) {
39 write_bitmaps = git_config_bool(var, value);
40 return 0;
42 return git_default_config(var, value, cb);
46 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
48 static void remove_temporary_files(void)
50 struct strbuf buf = STRBUF_INIT;
51 size_t dirlen, prefixlen;
52 DIR *dir;
53 struct dirent *e;
55 dir = opendir(packdir);
56 if (!dir)
57 return;
59 /* Point at the slash at the end of ".../objects/pack/" */
60 dirlen = strlen(packdir) + 1;
61 strbuf_addstr(&buf, packtmp);
62 /* Hold the length of ".tmp-%d-pack-" */
63 prefixlen = buf.len - dirlen;
65 while ((e = readdir(dir))) {
66 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
67 continue;
68 strbuf_setlen(&buf, dirlen);
69 strbuf_addstr(&buf, e->d_name);
70 unlink(buf.buf);
72 closedir(dir);
73 strbuf_release(&buf);
76 static void remove_pack_on_signal(int signo)
78 remove_temporary_files();
79 sigchain_pop(signo);
80 raise(signo);
84 * Adds all packs hex strings to the fname list, which do not
85 * have a corresponding .keep file.
87 static void get_non_kept_pack_filenames(struct string_list *fname_list)
89 DIR *dir;
90 struct dirent *e;
91 char *fname;
93 if (!(dir = opendir(packdir)))
94 return;
96 while ((e = readdir(dir)) != NULL) {
97 size_t len;
98 if (!strip_suffix(e->d_name, ".pack", &len))
99 continue;
101 fname = xmemdupz(e->d_name, len);
103 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
104 string_list_append_nodup(fname_list, fname);
105 else
106 free(fname);
108 closedir(dir);
111 static void remove_redundant_pack(const char *dir_name, const char *base_name)
113 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
114 int i;
115 struct strbuf buf = STRBUF_INIT;
116 size_t plen;
118 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
119 plen = buf.len;
121 for (i = 0; i < ARRAY_SIZE(exts); i++) {
122 strbuf_setlen(&buf, plen);
123 strbuf_addstr(&buf, exts[i]);
124 unlink(buf.buf);
126 strbuf_release(&buf);
129 #define ALL_INTO_ONE 1
130 #define LOOSEN_UNREACHABLE 2
132 int cmd_repack(int argc, const char **argv, const char *prefix)
134 struct {
135 const char *name;
136 unsigned optional:1;
137 } exts[] = {
138 {".pack"},
139 {".idx"},
140 {".bitmap", 1},
142 struct child_process cmd = CHILD_PROCESS_INIT;
143 struct string_list_item *item;
144 struct string_list names = STRING_LIST_INIT_DUP;
145 struct string_list rollback = STRING_LIST_INIT_NODUP;
146 struct string_list existing_packs = STRING_LIST_INIT_DUP;
147 struct strbuf line = STRBUF_INIT;
148 int ext, ret, failed;
149 FILE *out;
151 /* variables to be filled by option parsing */
152 int pack_everything = 0;
153 int delete_redundant = 0;
154 const char *unpack_unreachable = NULL;
155 int keep_unreachable = 0;
156 const char *window = NULL, *window_memory = NULL;
157 const char *depth = NULL;
158 const char *threads = NULL;
159 const char *max_pack_size = NULL;
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;
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")),
182 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
183 N_("write bitmap index")),
184 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
185 N_("with -A, do not loosen objects older than this")),
186 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
187 N_("with -a, repack unreachable objects")),
188 OPT_STRING(0, "window", &window, N_("n"),
189 N_("size of the window used for delta compression")),
190 OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),
191 N_("same as the above, but limit memory size instead of entries count")),
192 OPT_STRING(0, "depth", &depth, N_("n"),
193 N_("limits the maximum delta depth")),
194 OPT_STRING(0, "threads", &threads, N_("n"),
195 N_("limits the maximum number of threads")),
196 OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),
197 N_("maximum size of each packfile")),
198 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
199 N_("repack objects in packs marked with .keep")),
200 OPT_END()
203 git_config(repack_config, NULL);
205 argc = parse_options(argc, argv, prefix, builtin_repack_options,
206 git_repack_usage, 0);
208 if (delete_redundant && repository_format_precious_objects)
209 die(_("cannot delete packs in a precious-objects repo"));
211 if (keep_unreachable &&
212 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
213 die(_("--keep-unreachable and -A are incompatible"));
215 if (pack_kept_objects < 0)
216 pack_kept_objects = write_bitmaps;
218 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))
219 die(_(incremental_bitmap_conflict_error));
221 packdir = mkpathdup("%s/pack", get_object_directory());
222 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
224 sigchain_push_common(remove_pack_on_signal);
226 argv_array_push(&cmd.args, "pack-objects");
227 argv_array_push(&cmd.args, "--keep-true-parents");
228 if (!pack_kept_objects)
229 argv_array_push(&cmd.args, "--honor-pack-keep");
230 argv_array_push(&cmd.args, "--non-empty");
231 argv_array_push(&cmd.args, "--all");
232 argv_array_push(&cmd.args, "--reflog");
233 argv_array_push(&cmd.args, "--indexed-objects");
234 if (window)
235 argv_array_pushf(&cmd.args, "--window=%s", window);
236 if (window_memory)
237 argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory);
238 if (depth)
239 argv_array_pushf(&cmd.args, "--depth=%s", depth);
240 if (threads)
241 argv_array_pushf(&cmd.args, "--threads=%s", threads);
242 if (max_pack_size)
243 argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size);
244 if (no_reuse_delta)
245 argv_array_pushf(&cmd.args, "--no-reuse-delta");
246 if (no_reuse_object)
247 argv_array_pushf(&cmd.args, "--no-reuse-object");
248 if (write_bitmaps)
249 argv_array_push(&cmd.args, "--write-bitmap-index");
251 if (pack_everything & ALL_INTO_ONE) {
252 get_non_kept_pack_filenames(&existing_packs);
254 if (existing_packs.nr && delete_redundant) {
255 if (unpack_unreachable) {
256 argv_array_pushf(&cmd.args,
257 "--unpack-unreachable=%s",
258 unpack_unreachable);
259 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
260 } else if (pack_everything & LOOSEN_UNREACHABLE) {
261 argv_array_push(&cmd.args,
262 "--unpack-unreachable");
263 } else if (keep_unreachable) {
264 argv_array_push(&cmd.args, "--keep-unreachable");
265 argv_array_push(&cmd.args, "--pack-loose-unreachable");
266 } else {
267 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
270 } else {
271 argv_array_push(&cmd.args, "--unpacked");
272 argv_array_push(&cmd.args, "--incremental");
275 if (local)
276 argv_array_push(&cmd.args, "--local");
277 if (quiet)
278 argv_array_push(&cmd.args, "--quiet");
279 if (delta_base_offset)
280 argv_array_push(&cmd.args, "--delta-base-offset");
282 argv_array_push(&cmd.args, packtmp);
284 cmd.git_cmd = 1;
285 cmd.out = -1;
286 cmd.no_stdin = 1;
288 ret = start_command(&cmd);
289 if (ret)
290 return ret;
292 out = xfdopen(cmd.out, "r");
293 while (strbuf_getline_lf(&line, out) != EOF) {
294 if (line.len != 40)
295 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
296 string_list_append(&names, line.buf);
298 fclose(out);
299 ret = finish_command(&cmd);
300 if (ret)
301 return ret;
303 if (!names.nr && !quiet)
304 printf("Nothing new to pack.\n");
307 * Ok we have prepared all new packfiles.
308 * First see if there are packs of the same name and if so
309 * if we can move them out of the way (this can happen if we
310 * repacked immediately after packing fully.
312 failed = 0;
313 for_each_string_list_item(item, &names) {
314 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
315 char *fname, *fname_old;
316 fname = mkpathdup("%s/pack-%s%s", packdir,
317 item->string, exts[ext].name);
318 if (!file_exists(fname)) {
319 free(fname);
320 continue;
323 fname_old = mkpathdup("%s/old-%s%s", packdir,
324 item->string, exts[ext].name);
325 if (file_exists(fname_old))
326 if (unlink(fname_old))
327 failed = 1;
329 if (!failed && rename(fname, fname_old)) {
330 free(fname);
331 free(fname_old);
332 failed = 1;
333 break;
334 } else {
335 string_list_append(&rollback, fname);
336 free(fname_old);
339 if (failed)
340 break;
342 if (failed) {
343 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
344 for_each_string_list_item(item, &rollback) {
345 char *fname, *fname_old;
346 fname = mkpathdup("%s/%s", packdir, item->string);
347 fname_old = mkpathdup("%s/old-%s", packdir, item->string);
348 if (rename(fname_old, fname))
349 string_list_append(&rollback_failure, fname);
350 free(fname);
351 free(fname_old);
354 if (rollback_failure.nr) {
355 int i;
356 fprintf(stderr,
357 "WARNING: Some packs in use have been renamed by\n"
358 "WARNING: prefixing old- to their name, in order to\n"
359 "WARNING: replace them with the new version of the\n"
360 "WARNING: file. But the operation failed, and the\n"
361 "WARNING: attempt to rename them back to their\n"
362 "WARNING: original names also failed.\n"
363 "WARNING: Please rename them in %s manually:\n", packdir);
364 for (i = 0; i < rollback_failure.nr; i++)
365 fprintf(stderr, "WARNING: old-%s -> %s\n",
366 rollback_failure.items[i].string,
367 rollback_failure.items[i].string);
369 exit(1);
372 /* Now the ones with the same name are out of the way... */
373 for_each_string_list_item(item, &names) {
374 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
375 char *fname, *fname_old;
376 struct stat statbuffer;
377 int exists = 0;
378 fname = mkpathdup("%s/pack-%s%s",
379 packdir, item->string, exts[ext].name);
380 fname_old = mkpathdup("%s-%s%s",
381 packtmp, item->string, exts[ext].name);
382 if (!stat(fname_old, &statbuffer)) {
383 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
384 chmod(fname_old, statbuffer.st_mode);
385 exists = 1;
387 if (exists || !exts[ext].optional) {
388 if (rename(fname_old, fname))
389 die_errno(_("renaming '%s' failed"), fname_old);
391 free(fname);
392 free(fname_old);
396 /* Remove the "old-" files */
397 for_each_string_list_item(item, &names) {
398 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
399 char *fname;
400 fname = mkpathdup("%s/old-%s%s",
401 packdir,
402 item->string,
403 exts[ext].name);
404 if (remove_path(fname))
405 warning(_("failed to remove '%s'"), fname);
406 free(fname);
410 /* End of pack replacement. */
412 if (delete_redundant) {
413 int opts = 0;
414 string_list_sort(&names);
415 for_each_string_list_item(item, &existing_packs) {
416 char *sha1;
417 size_t len = strlen(item->string);
418 if (len < 40)
419 continue;
420 sha1 = item->string + len - 40;
421 if (!string_list_has_string(&names, sha1))
422 remove_redundant_pack(packdir, item->string);
424 if (!quiet && isatty(2))
425 opts |= PRUNE_PACKED_VERBOSE;
426 prune_packed_objects(opts);
429 if (!no_update_server_info)
430 update_server_info(0);
431 remove_temporary_files();
432 string_list_clear(&names, 0);
433 string_list_clear(&rollback, 0);
434 string_list_clear(&existing_packs, 0);
435 strbuf_release(&line);
437 return 0;