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