5 #include "parse-options.h"
6 #include "run-command.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>]"),
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);
34 if (!strcmp(var, "repack.packkeptobjects")) {
35 pack_kept_objects = git_config_bool(var, value);
38 if (!strcmp(var, "repack.writebitmaps") ||
39 !strcmp(var, "pack.writebitmaps")) {
40 write_bitmaps = git_config_bool(var, value);
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;
56 dir = opendir(packdir);
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))
69 strbuf_setlen(&buf, dirlen);
70 strbuf_addstr(&buf, e->d_name);
77 static void remove_pack_on_signal(int signo)
79 remove_temporary_files();
85 * Adds all packs hex strings to the fname list, which do not
86 * have a corresponding .keep or .promisor file. These packs are not to
87 * be kept if we are going to pack everything into one file.
89 static void get_non_kept_pack_filenames(struct string_list *fname_list)
95 if (!(dir = opendir(packdir)))
98 while ((e = readdir(dir)) != NULL) {
100 if (!strip_suffix(e->d_name, ".pack", &len))
103 fname = xmemdupz(e->d_name, len);
105 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)) &&
106 !file_exists(mkpath("%s/%s.promisor", packdir, fname)))
107 string_list_append_nodup(fname_list, fname);
114 static void remove_redundant_pack(const char *dir_name, const char *base_name)
116 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
118 struct strbuf buf = STRBUF_INIT;
121 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
124 for (i = 0; i < ARRAY_SIZE(exts); i++) {
125 strbuf_setlen(&buf, plen);
126 strbuf_addstr(&buf, exts[i]);
129 strbuf_release(&buf);
132 #define ALL_INTO_ONE 1
133 #define LOOSEN_UNREACHABLE 2
135 int cmd_repack(int argc, const char **argv, const char *prefix)
145 struct child_process cmd = CHILD_PROCESS_INIT;
146 struct string_list_item *item;
147 struct string_list names = STRING_LIST_INIT_DUP;
148 struct string_list rollback = STRING_LIST_INIT_NODUP;
149 struct string_list existing_packs = STRING_LIST_INIT_DUP;
150 struct strbuf line = STRBUF_INIT;
151 int ext, ret, failed;
154 /* variables to be filled by option parsing */
155 int pack_everything = 0;
156 int delete_redundant = 0;
157 const char *unpack_unreachable = NULL;
158 int keep_unreachable = 0;
159 const char *window = NULL, *window_memory = NULL;
160 const char *depth = NULL;
161 const char *threads = NULL;
162 const char *max_pack_size = NULL;
163 int no_reuse_delta = 0, no_reuse_object = 0;
164 int no_update_server_info = 0;
168 struct option builtin_repack_options[] = {
169 OPT_BIT('a', NULL, &pack_everything,
170 N_("pack everything in a single pack"), ALL_INTO_ONE),
171 OPT_BIT('A', NULL, &pack_everything,
172 N_("same as -a, and turn unreachable objects loose"),
173 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
174 OPT_BOOL('d', NULL, &delete_redundant,
175 N_("remove redundant packs, and run git-prune-packed")),
176 OPT_BOOL('f', NULL, &no_reuse_delta,
177 N_("pass --no-reuse-delta to git-pack-objects")),
178 OPT_BOOL('F', NULL, &no_reuse_object,
179 N_("pass --no-reuse-object to git-pack-objects")),
180 OPT_BOOL('n', NULL, &no_update_server_info,
181 N_("do not run git-update-server-info")),
182 OPT__QUIET(&quiet, N_("be quiet")),
183 OPT_BOOL('l', "local", &local,
184 N_("pass --local to git-pack-objects")),
185 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
186 N_("write bitmap index")),
187 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
188 N_("with -A, do not loosen objects older than this")),
189 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
190 N_("with -a, repack unreachable objects")),
191 OPT_STRING(0, "window", &window, N_("n"),
192 N_("size of the window used for delta compression")),
193 OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),
194 N_("same as the above, but limit memory size instead of entries count")),
195 OPT_STRING(0, "depth", &depth, N_("n"),
196 N_("limits the maximum delta depth")),
197 OPT_STRING(0, "threads", &threads, N_("n"),
198 N_("limits the maximum number of threads")),
199 OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),
200 N_("maximum size of each packfile")),
201 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
202 N_("repack objects in packs marked with .keep")),
206 git_config(repack_config, NULL);
208 argc = parse_options(argc, argv, prefix, builtin_repack_options,
209 git_repack_usage, 0);
211 if (delete_redundant && repository_format_precious_objects)
212 die(_("cannot delete packs in a precious-objects repo"));
214 if (keep_unreachable &&
215 (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
216 die(_("--keep-unreachable and -A are incompatible"));
218 if (pack_kept_objects < 0)
219 pack_kept_objects = write_bitmaps;
221 if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))
222 die(_(incremental_bitmap_conflict_error));
224 packdir = mkpathdup("%s/pack", get_object_directory());
225 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
227 sigchain_push_common(remove_pack_on_signal);
229 argv_array_push(&cmd.args, "pack-objects");
230 argv_array_push(&cmd.args, "--keep-true-parents");
231 if (!pack_kept_objects)
232 argv_array_push(&cmd.args, "--honor-pack-keep");
233 argv_array_push(&cmd.args, "--non-empty");
234 argv_array_push(&cmd.args, "--all");
235 argv_array_push(&cmd.args, "--reflog");
236 argv_array_push(&cmd.args, "--indexed-objects");
237 if (repository_format_partial_clone)
238 argv_array_push(&cmd.args, "--exclude-promisor-objects");
240 argv_array_pushf(&cmd.args, "--window=%s", window);
242 argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory);
244 argv_array_pushf(&cmd.args, "--depth=%s", depth);
246 argv_array_pushf(&cmd.args, "--threads=%s", threads);
248 argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size);
250 argv_array_pushf(&cmd.args, "--no-reuse-delta");
252 argv_array_pushf(&cmd.args, "--no-reuse-object");
254 argv_array_push(&cmd.args, "--write-bitmap-index");
256 if (pack_everything & ALL_INTO_ONE) {
257 get_non_kept_pack_filenames(&existing_packs);
259 if (existing_packs.nr && delete_redundant) {
260 if (unpack_unreachable) {
261 argv_array_pushf(&cmd.args,
262 "--unpack-unreachable=%s",
264 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
265 } else if (pack_everything & LOOSEN_UNREACHABLE) {
266 argv_array_push(&cmd.args,
267 "--unpack-unreachable");
268 } else if (keep_unreachable) {
269 argv_array_push(&cmd.args, "--keep-unreachable");
270 argv_array_push(&cmd.args, "--pack-loose-unreachable");
272 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
276 argv_array_push(&cmd.args, "--unpacked");
277 argv_array_push(&cmd.args, "--incremental");
281 argv_array_push(&cmd.args, "--local");
283 argv_array_push(&cmd.args, "--quiet");
284 if (delta_base_offset)
285 argv_array_push(&cmd.args, "--delta-base-offset");
287 argv_array_push(&cmd.args, packtmp);
293 ret = start_command(&cmd);
297 out = xfdopen(cmd.out, "r");
298 while (strbuf_getline_lf(&line, out) != EOF) {
300 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
301 string_list_append(&names, line.buf);
304 ret = finish_command(&cmd);
308 if (!names.nr && !quiet)
309 printf("Nothing new to pack.\n");
312 * Ok we have prepared all new packfiles.
313 * First see if there are packs of the same name and if so
314 * if we can move them out of the way (this can happen if we
315 * repacked immediately after packing fully.
318 for_each_string_list_item(item, &names) {
319 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
320 char *fname, *fname_old;
321 fname = mkpathdup("%s/pack-%s%s", packdir,
322 item->string, exts[ext].name);
323 if (!file_exists(fname)) {
328 fname_old = mkpathdup("%s/old-%s%s", packdir,
329 item->string, exts[ext].name);
330 if (file_exists(fname_old))
331 if (unlink(fname_old))
334 if (!failed && rename(fname, fname_old)) {
340 string_list_append(&rollback, fname);
348 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
349 for_each_string_list_item(item, &rollback) {
350 char *fname, *fname_old;
351 fname = mkpathdup("%s/%s", packdir, item->string);
352 fname_old = mkpathdup("%s/old-%s", packdir, item->string);
353 if (rename(fname_old, fname))
354 string_list_append(&rollback_failure, fname);
359 if (rollback_failure.nr) {
362 "WARNING: Some packs in use have been renamed by\n"
363 "WARNING: prefixing old- to their name, in order to\n"
364 "WARNING: replace them with the new version of the\n"
365 "WARNING: file. But the operation failed, and the\n"
366 "WARNING: attempt to rename them back to their\n"
367 "WARNING: original names also failed.\n"
368 "WARNING: Please rename them in %s manually:\n", packdir);
369 for (i = 0; i < rollback_failure.nr; i++)
370 fprintf(stderr, "WARNING: old-%s -> %s\n",
371 rollback_failure.items[i].string,
372 rollback_failure.items[i].string);
377 /* Now the ones with the same name are out of the way... */
378 for_each_string_list_item(item, &names) {
379 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
380 char *fname, *fname_old;
381 struct stat statbuffer;
383 fname = mkpathdup("%s/pack-%s%s",
384 packdir, item->string, exts[ext].name);
385 fname_old = mkpathdup("%s-%s%s",
386 packtmp, item->string, exts[ext].name);
387 if (!stat(fname_old, &statbuffer)) {
388 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
389 chmod(fname_old, statbuffer.st_mode);
392 if (exists || !exts[ext].optional) {
393 if (rename(fname_old, fname))
394 die_errno(_("renaming '%s' failed"), fname_old);
401 /* Remove the "old-" files */
402 for_each_string_list_item(item, &names) {
403 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
405 fname = mkpathdup("%s/old-%s%s",
409 if (remove_path(fname))
410 warning(_("failed to remove '%s'"), fname);
415 /* End of pack replacement. */
417 if (delete_redundant) {
419 string_list_sort(&names);
420 for_each_string_list_item(item, &existing_packs) {
422 size_t len = strlen(item->string);
425 sha1 = item->string + len - 40;
426 if (!string_list_has_string(&names, sha1))
427 remove_redundant_pack(packdir, item->string);
429 if (!quiet && isatty(2))
430 opts |= PRUNE_PACKED_VERBOSE;
431 prune_packed_objects(opts);
434 if (!no_update_server_info)
435 update_server_info(0);
436 remove_temporary_files();
437 string_list_clear(&names, 0);
438 string_list_clear(&rollback, 0);
439 string_list_clear(&existing_packs, 0);
440 strbuf_release(&line);