t7700: drop explicit --no-pack-kept-objects from .keep test
[git.git] / builtin / repack.c
blobcd4d4d4273665880fff66029df6419b7493d4e2c
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 int repack_config(const char *var, const char *value, void *cb)
23 if (!strcmp(var, "repack.usedeltabaseoffset")) {
24 delta_base_offset = git_config_bool(var, value);
25 return 0;
27 if (!strcmp(var, "repack.packkeptobjects")) {
28 pack_kept_objects = git_config_bool(var, value);
29 return 0;
31 if (!strcmp(var, "repack.writebitmaps") ||
32 !strcmp(var, "pack.writebitmaps")) {
33 write_bitmaps = git_config_bool(var, value);
34 return 0;
36 return git_default_config(var, value, cb);
40 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
42 static void remove_temporary_files(void)
44 struct strbuf buf = STRBUF_INIT;
45 size_t dirlen, prefixlen;
46 DIR *dir;
47 struct dirent *e;
49 dir = opendir(packdir);
50 if (!dir)
51 return;
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;
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);
66 closedir(dir);
67 strbuf_release(&buf);
70 static void remove_pack_on_signal(int signo)
72 remove_temporary_files();
73 sigchain_pop(signo);
74 raise(signo);
78 * Adds all packs hex strings to the fname list, which do not
79 * have a corresponding .keep file.
81 static void get_non_kept_pack_filenames(struct string_list *fname_list)
83 DIR *dir;
84 struct dirent *e;
85 char *fname;
86 size_t len;
88 if (!(dir = opendir(packdir)))
89 return;
91 while ((e = readdir(dir)) != NULL) {
92 if (suffixcmp(e->d_name, ".pack"))
93 continue;
95 len = strlen(e->d_name) - strlen(".pack");
96 fname = xmemdupz(e->d_name, len);
98 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
99 string_list_append_nodup(fname_list, fname);
100 else
101 free(fname);
103 closedir(dir);
106 static void remove_redundant_pack(const char *dir_name, const char *base_name)
108 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
109 int i;
110 struct strbuf buf = STRBUF_INIT;
111 size_t plen;
113 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
114 plen = buf.len;
116 for (i = 0; i < ARRAY_SIZE(exts); i++) {
117 strbuf_setlen(&buf, plen);
118 strbuf_addstr(&buf, exts[i]);
119 unlink(buf.buf);
121 strbuf_release(&buf);
124 #define ALL_INTO_ONE 1
125 #define LOOSEN_UNREACHABLE 2
127 int cmd_repack(int argc, const char **argv, const char *prefix)
129 struct {
130 const char *name;
131 unsigned optional:1;
132 } exts[] = {
133 {".pack"},
134 {".idx"},
135 {".bitmap", 1},
137 struct child_process cmd;
138 struct string_list_item *item;
139 struct argv_array cmd_args = ARGV_ARRAY_INIT;
140 struct string_list names = STRING_LIST_INIT_DUP;
141 struct string_list rollback = STRING_LIST_INIT_NODUP;
142 struct string_list existing_packs = STRING_LIST_INIT_DUP;
143 struct strbuf line = STRBUF_INIT;
144 int nr_packs, ext, ret, failed;
145 FILE *out;
147 /* variables to be filled by option parsing */
148 int pack_everything = 0;
149 int delete_redundant = 0;
150 char *unpack_unreachable = NULL;
151 int window = 0, window_memory = 0;
152 int depth = 0;
153 int max_pack_size = 0;
154 int no_reuse_delta = 0, no_reuse_object = 0;
155 int no_update_server_info = 0;
156 int quiet = 0;
157 int local = 0;
159 struct option builtin_repack_options[] = {
160 OPT_BIT('a', NULL, &pack_everything,
161 N_("pack everything in a single pack"), ALL_INTO_ONE),
162 OPT_BIT('A', NULL, &pack_everything,
163 N_("same as -a, and turn unreachable objects loose"),
164 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
165 OPT_BOOL('d', NULL, &delete_redundant,
166 N_("remove redundant packs, and run git-prune-packed")),
167 OPT_BOOL('f', NULL, &no_reuse_delta,
168 N_("pass --no-reuse-delta to git-pack-objects")),
169 OPT_BOOL('F', NULL, &no_reuse_object,
170 N_("pass --no-reuse-object to git-pack-objects")),
171 OPT_BOOL('n', NULL, &no_update_server_info,
172 N_("do not run git-update-server-info")),
173 OPT__QUIET(&quiet, N_("be quiet")),
174 OPT_BOOL('l', "local", &local,
175 N_("pass --local to git-pack-objects")),
176 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
177 N_("write bitmap index")),
178 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
179 N_("with -A, do not loosen objects older than this")),
180 OPT_INTEGER(0, "window", &window,
181 N_("size of the window used for delta compression")),
182 OPT_INTEGER(0, "window-memory", &window_memory,
183 N_("same as the above, but limit memory size instead of entries count")),
184 OPT_INTEGER(0, "depth", &depth,
185 N_("limits the maximum delta depth")),
186 OPT_INTEGER(0, "max-pack-size", &max_pack_size,
187 N_("maximum size of each packfile")),
188 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
189 N_("repack objects in packs marked with .keep")),
190 OPT_END()
193 git_config(repack_config, NULL);
195 argc = parse_options(argc, argv, prefix, builtin_repack_options,
196 git_repack_usage, 0);
198 if (pack_kept_objects < 0)
199 pack_kept_objects = write_bitmaps;
201 packdir = mkpathdup("%s/pack", get_object_directory());
202 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
204 sigchain_push_common(remove_pack_on_signal);
206 argv_array_push(&cmd_args, "pack-objects");
207 argv_array_push(&cmd_args, "--keep-true-parents");
208 if (!pack_kept_objects)
209 argv_array_push(&cmd_args, "--honor-pack-keep");
210 argv_array_push(&cmd_args, "--non-empty");
211 argv_array_push(&cmd_args, "--all");
212 argv_array_push(&cmd_args, "--reflog");
213 if (window)
214 argv_array_pushf(&cmd_args, "--window=%u", window);
215 if (window_memory)
216 argv_array_pushf(&cmd_args, "--window-memory=%u", window_memory);
217 if (depth)
218 argv_array_pushf(&cmd_args, "--depth=%u", depth);
219 if (max_pack_size)
220 argv_array_pushf(&cmd_args, "--max_pack_size=%u", max_pack_size);
221 if (no_reuse_delta)
222 argv_array_pushf(&cmd_args, "--no-reuse-delta");
223 if (no_reuse_object)
224 argv_array_pushf(&cmd_args, "--no-reuse-object");
225 if (write_bitmaps)
226 argv_array_push(&cmd_args, "--write-bitmap-index");
228 if (pack_everything & ALL_INTO_ONE) {
229 get_non_kept_pack_filenames(&existing_packs);
231 if (existing_packs.nr && delete_redundant) {
232 if (unpack_unreachable)
233 argv_array_pushf(&cmd_args,
234 "--unpack-unreachable=%s",
235 unpack_unreachable);
236 else if (pack_everything & LOOSEN_UNREACHABLE)
237 argv_array_push(&cmd_args,
238 "--unpack-unreachable");
240 } else {
241 argv_array_push(&cmd_args, "--unpacked");
242 argv_array_push(&cmd_args, "--incremental");
245 if (local)
246 argv_array_push(&cmd_args, "--local");
247 if (quiet)
248 argv_array_push(&cmd_args, "--quiet");
249 if (delta_base_offset)
250 argv_array_push(&cmd_args, "--delta-base-offset");
252 argv_array_push(&cmd_args, packtmp);
254 memset(&cmd, 0, sizeof(cmd));
255 cmd.argv = cmd_args.argv;
256 cmd.git_cmd = 1;
257 cmd.out = -1;
258 cmd.no_stdin = 1;
260 ret = start_command(&cmd);
261 if (ret)
262 return ret;
264 nr_packs = 0;
265 out = xfdopen(cmd.out, "r");
266 while (strbuf_getline(&line, out, '\n') != EOF) {
267 if (line.len != 40)
268 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
269 string_list_append(&names, line.buf);
270 nr_packs++;
272 fclose(out);
273 ret = finish_command(&cmd);
274 if (ret)
275 return ret;
276 argv_array_clear(&cmd_args);
278 if (!nr_packs && !quiet)
279 printf("Nothing new to pack.\n");
282 * Ok we have prepared all new packfiles.
283 * First see if there are packs of the same name and if so
284 * if we can move them out of the way (this can happen if we
285 * repacked immediately after packing fully.
287 failed = 0;
288 for_each_string_list_item(item, &names) {
289 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
290 char *fname, *fname_old;
291 fname = mkpathdup("%s/%s%s", packdir,
292 item->string, exts[ext].name);
293 if (!file_exists(fname)) {
294 free(fname);
295 continue;
298 fname_old = mkpath("%s/old-%s%s", packdir,
299 item->string, exts[ext].name);
300 if (file_exists(fname_old))
301 if (unlink(fname_old))
302 failed = 1;
304 if (!failed && rename(fname, fname_old)) {
305 free(fname);
306 failed = 1;
307 break;
308 } else {
309 string_list_append(&rollback, fname);
312 if (failed)
313 break;
315 if (failed) {
316 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
317 for_each_string_list_item(item, &rollback) {
318 char *fname, *fname_old;
319 fname = mkpathdup("%s/%s", packdir, item->string);
320 fname_old = mkpath("%s/old-%s", packdir, item->string);
321 if (rename(fname_old, fname))
322 string_list_append(&rollback_failure, fname);
323 free(fname);
326 if (rollback_failure.nr) {
327 int i;
328 fprintf(stderr,
329 "WARNING: Some packs in use have been renamed by\n"
330 "WARNING: prefixing old- to their name, in order to\n"
331 "WARNING: replace them with the new version of the\n"
332 "WARNING: file. But the operation failed, and the\n"
333 "WARNING: attempt to rename them back to their\n"
334 "WARNING: original names also failed.\n"
335 "WARNING: Please rename them in %s manually:\n", packdir);
336 for (i = 0; i < rollback_failure.nr; i++)
337 fprintf(stderr, "WARNING: old-%s -> %s\n",
338 rollback_failure.items[i].string,
339 rollback_failure.items[i].string);
341 exit(1);
344 /* Now the ones with the same name are out of the way... */
345 for_each_string_list_item(item, &names) {
346 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
347 char *fname, *fname_old;
348 struct stat statbuffer;
349 int exists = 0;
350 fname = mkpathdup("%s/pack-%s%s",
351 packdir, item->string, exts[ext].name);
352 fname_old = mkpathdup("%s-%s%s",
353 packtmp, item->string, exts[ext].name);
354 if (!stat(fname_old, &statbuffer)) {
355 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
356 chmod(fname_old, statbuffer.st_mode);
357 exists = 1;
359 if (exists || !exts[ext].optional) {
360 if (rename(fname_old, fname))
361 die_errno(_("renaming '%s' failed"), fname_old);
363 free(fname);
364 free(fname_old);
368 /* Remove the "old-" files */
369 for_each_string_list_item(item, &names) {
370 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
371 char *fname;
372 fname = mkpath("%s/old-pack-%s%s",
373 packdir,
374 item->string,
375 exts[ext].name);
376 if (remove_path(fname))
377 warning(_("removing '%s' failed"), fname);
381 /* End of pack replacement. */
383 if (delete_redundant) {
384 sort_string_list(&names);
385 for_each_string_list_item(item, &existing_packs) {
386 char *sha1;
387 size_t len = strlen(item->string);
388 if (len < 40)
389 continue;
390 sha1 = item->string + len - 40;
391 if (!string_list_has_string(&names, sha1))
392 remove_redundant_pack(packdir, item->string);
394 argv_array_push(&cmd_args, "prune-packed");
395 if (quiet)
396 argv_array_push(&cmd_args, "--quiet");
398 memset(&cmd, 0, sizeof(cmd));
399 cmd.argv = cmd_args.argv;
400 cmd.git_cmd = 1;
401 run_command(&cmd);
402 argv_array_clear(&cmd_args);
405 if (!no_update_server_info) {
406 argv_array_push(&cmd_args, "update-server-info");
407 memset(&cmd, 0, sizeof(cmd));
408 cmd.argv = cmd_args.argv;
409 cmd.git_cmd = 1;
410 run_command(&cmd);
411 argv_array_clear(&cmd_args);
413 remove_temporary_files();
414 string_list_clear(&names, 0);
415 string_list_clear(&rollback, 0);
416 string_list_clear(&existing_packs, 0);
417 strbuf_release(&line);
419 return 0;