repack: handle optional files created by pack-objects
[git/gitster.git] / builtin / repack.c
blob8b7dfd043aeb63b95a0fc522f4d5b9c6f905a159
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 char *packdir, *packtmp;
14 static const char *const git_repack_usage[] = {
15 N_("git repack [options]"),
16 NULL
19 static int repack_config(const char *var, const char *value, void *cb)
21 if (!strcmp(var, "repack.usedeltabaseoffset")) {
22 delta_base_offset = git_config_bool(var, value);
23 return 0;
25 return git_default_config(var, value, cb);
29 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
31 static void remove_temporary_files(void)
33 struct strbuf buf = STRBUF_INIT;
34 size_t dirlen, prefixlen;
35 DIR *dir;
36 struct dirent *e;
38 dir = opendir(packdir);
39 if (!dir)
40 return;
42 /* Point at the slash at the end of ".../objects/pack/" */
43 dirlen = strlen(packdir) + 1;
44 strbuf_addstr(&buf, packtmp);
45 /* Hold the length of ".tmp-%d-pack-" */
46 prefixlen = buf.len - dirlen;
48 while ((e = readdir(dir))) {
49 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
50 continue;
51 strbuf_setlen(&buf, dirlen);
52 strbuf_addstr(&buf, e->d_name);
53 unlink(buf.buf);
55 closedir(dir);
56 strbuf_release(&buf);
59 static void remove_pack_on_signal(int signo)
61 remove_temporary_files();
62 sigchain_pop(signo);
63 raise(signo);
67 * Adds all packs hex strings to the fname list, which do not
68 * have a corresponding .keep file.
70 static void get_non_kept_pack_filenames(struct string_list *fname_list)
72 DIR *dir;
73 struct dirent *e;
74 char *fname;
75 size_t len;
77 if (!(dir = opendir(packdir)))
78 return;
80 while ((e = readdir(dir)) != NULL) {
81 if (suffixcmp(e->d_name, ".pack"))
82 continue;
84 len = strlen(e->d_name) - strlen(".pack");
85 fname = xmemdupz(e->d_name, len);
87 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
88 string_list_append_nodup(fname_list, fname);
89 else
90 free(fname);
92 closedir(dir);
95 static void remove_redundant_pack(const char *dir_name, const char *base_name)
97 const char *exts[] = {".pack", ".idx", ".keep"};
98 int i;
99 struct strbuf buf = STRBUF_INIT;
100 size_t plen;
102 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
103 plen = buf.len;
105 for (i = 0; i < ARRAY_SIZE(exts); i++) {
106 strbuf_setlen(&buf, plen);
107 strbuf_addstr(&buf, exts[i]);
108 unlink(buf.buf);
110 strbuf_release(&buf);
113 #define ALL_INTO_ONE 1
114 #define LOOSEN_UNREACHABLE 2
116 int cmd_repack(int argc, const char **argv, const char *prefix)
118 struct {
119 const char *name;
120 unsigned optional:1;
121 } exts[] = {
122 {".pack"},
123 {".idx"},
125 struct child_process cmd;
126 struct string_list_item *item;
127 struct argv_array cmd_args = ARGV_ARRAY_INIT;
128 struct string_list names = STRING_LIST_INIT_DUP;
129 struct string_list rollback = STRING_LIST_INIT_NODUP;
130 struct string_list existing_packs = STRING_LIST_INIT_DUP;
131 struct strbuf line = STRBUF_INIT;
132 int nr_packs, ext, ret, failed;
133 FILE *out;
135 /* variables to be filled by option parsing */
136 int pack_everything = 0;
137 int delete_redundant = 0;
138 char *unpack_unreachable = NULL;
139 int window = 0, window_memory = 0;
140 int depth = 0;
141 int max_pack_size = 0;
142 int no_reuse_delta = 0, no_reuse_object = 0;
143 int no_update_server_info = 0;
144 int quiet = 0;
145 int local = 0;
147 struct option builtin_repack_options[] = {
148 OPT_BIT('a', NULL, &pack_everything,
149 N_("pack everything in a single pack"), ALL_INTO_ONE),
150 OPT_BIT('A', NULL, &pack_everything,
151 N_("same as -a, and turn unreachable objects loose"),
152 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
153 OPT_BOOL('d', NULL, &delete_redundant,
154 N_("remove redundant packs, and run git-prune-packed")),
155 OPT_BOOL('f', NULL, &no_reuse_delta,
156 N_("pass --no-reuse-delta to git-pack-objects")),
157 OPT_BOOL('F', NULL, &no_reuse_object,
158 N_("pass --no-reuse-object to git-pack-objects")),
159 OPT_BOOL('n', NULL, &no_update_server_info,
160 N_("do not run git-update-server-info")),
161 OPT__QUIET(&quiet, N_("be quiet")),
162 OPT_BOOL('l', "local", &local,
163 N_("pass --local to git-pack-objects")),
164 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
165 N_("with -A, do not loosen objects older than this")),
166 OPT_INTEGER(0, "window", &window,
167 N_("size of the window used for delta compression")),
168 OPT_INTEGER(0, "window-memory", &window_memory,
169 N_("same as the above, but limit memory size instead of entries count")),
170 OPT_INTEGER(0, "depth", &depth,
171 N_("limits the maximum delta depth")),
172 OPT_INTEGER(0, "max-pack-size", &max_pack_size,
173 N_("maximum size of each packfile")),
174 OPT_END()
177 git_config(repack_config, NULL);
179 argc = parse_options(argc, argv, prefix, builtin_repack_options,
180 git_repack_usage, 0);
182 packdir = mkpathdup("%s/pack", get_object_directory());
183 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
185 sigchain_push_common(remove_pack_on_signal);
187 argv_array_push(&cmd_args, "pack-objects");
188 argv_array_push(&cmd_args, "--keep-true-parents");
189 argv_array_push(&cmd_args, "--honor-pack-keep");
190 argv_array_push(&cmd_args, "--non-empty");
191 argv_array_push(&cmd_args, "--all");
192 argv_array_push(&cmd_args, "--reflog");
193 if (window)
194 argv_array_pushf(&cmd_args, "--window=%u", window);
195 if (window_memory)
196 argv_array_pushf(&cmd_args, "--window-memory=%u", window_memory);
197 if (depth)
198 argv_array_pushf(&cmd_args, "--depth=%u", depth);
199 if (max_pack_size)
200 argv_array_pushf(&cmd_args, "--max_pack_size=%u", max_pack_size);
201 if (no_reuse_delta)
202 argv_array_pushf(&cmd_args, "--no-reuse-delta");
203 if (no_reuse_object)
204 argv_array_pushf(&cmd_args, "--no-reuse-object");
206 if (pack_everything & ALL_INTO_ONE) {
207 get_non_kept_pack_filenames(&existing_packs);
209 if (existing_packs.nr && delete_redundant) {
210 if (unpack_unreachable)
211 argv_array_pushf(&cmd_args,
212 "--unpack-unreachable=%s",
213 unpack_unreachable);
214 else if (pack_everything & LOOSEN_UNREACHABLE)
215 argv_array_push(&cmd_args,
216 "--unpack-unreachable");
218 } else {
219 argv_array_push(&cmd_args, "--unpacked");
220 argv_array_push(&cmd_args, "--incremental");
223 if (local)
224 argv_array_push(&cmd_args, "--local");
225 if (quiet)
226 argv_array_push(&cmd_args, "--quiet");
227 if (delta_base_offset)
228 argv_array_push(&cmd_args, "--delta-base-offset");
230 argv_array_push(&cmd_args, packtmp);
232 memset(&cmd, 0, sizeof(cmd));
233 cmd.argv = cmd_args.argv;
234 cmd.git_cmd = 1;
235 cmd.out = -1;
236 cmd.no_stdin = 1;
238 ret = start_command(&cmd);
239 if (ret)
240 return ret;
242 nr_packs = 0;
243 out = xfdopen(cmd.out, "r");
244 while (strbuf_getline(&line, out, '\n') != EOF) {
245 if (line.len != 40)
246 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
247 string_list_append(&names, line.buf);
248 nr_packs++;
250 fclose(out);
251 ret = finish_command(&cmd);
252 if (ret)
253 return ret;
254 argv_array_clear(&cmd_args);
256 if (!nr_packs && !quiet)
257 printf("Nothing new to pack.\n");
260 * Ok we have prepared all new packfiles.
261 * First see if there are packs of the same name and if so
262 * if we can move them out of the way (this can happen if we
263 * repacked immediately after packing fully.
265 failed = 0;
266 for_each_string_list_item(item, &names) {
267 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
268 char *fname, *fname_old;
269 fname = mkpathdup("%s/%s%s", packdir,
270 item->string, exts[ext].name);
271 if (!file_exists(fname)) {
272 free(fname);
273 continue;
276 fname_old = mkpath("%s/old-%s%s", packdir,
277 item->string, exts[ext].name);
278 if (file_exists(fname_old))
279 if (unlink(fname_old))
280 failed = 1;
282 if (!failed && rename(fname, fname_old)) {
283 free(fname);
284 failed = 1;
285 break;
286 } else {
287 string_list_append(&rollback, fname);
290 if (failed)
291 break;
293 if (failed) {
294 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
295 for_each_string_list_item(item, &rollback) {
296 char *fname, *fname_old;
297 fname = mkpathdup("%s/%s", packdir, item->string);
298 fname_old = mkpath("%s/old-%s", packdir, item->string);
299 if (rename(fname_old, fname))
300 string_list_append(&rollback_failure, fname);
301 free(fname);
304 if (rollback_failure.nr) {
305 int i;
306 fprintf(stderr,
307 "WARNING: Some packs in use have been renamed by\n"
308 "WARNING: prefixing old- to their name, in order to\n"
309 "WARNING: replace them with the new version of the\n"
310 "WARNING: file. But the operation failed, and the\n"
311 "WARNING: attempt to rename them back to their\n"
312 "WARNING: original names also failed.\n"
313 "WARNING: Please rename them in %s manually:\n", packdir);
314 for (i = 0; i < rollback_failure.nr; i++)
315 fprintf(stderr, "WARNING: old-%s -> %s\n",
316 rollback_failure.items[i].string,
317 rollback_failure.items[i].string);
319 exit(1);
322 /* Now the ones with the same name are out of the way... */
323 for_each_string_list_item(item, &names) {
324 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
325 char *fname, *fname_old;
326 struct stat statbuffer;
327 int exists = 0;
328 fname = mkpathdup("%s/pack-%s%s",
329 packdir, item->string, exts[ext].name);
330 fname_old = mkpathdup("%s-%s%s",
331 packtmp, item->string, exts[ext].name);
332 if (!stat(fname_old, &statbuffer)) {
333 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
334 chmod(fname_old, statbuffer.st_mode);
335 exists = 1;
337 if (exists || !exts[ext].optional) {
338 if (rename(fname_old, fname))
339 die_errno(_("renaming '%s' failed"), fname_old);
341 free(fname);
342 free(fname_old);
346 /* Remove the "old-" files */
347 for_each_string_list_item(item, &names) {
348 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
349 char *fname;
350 fname = mkpath("%s/old-pack-%s%s",
351 packdir,
352 item->string,
353 exts[ext].name);
354 if (remove_path(fname))
355 warning(_("removing '%s' failed"), fname);
359 /* End of pack replacement. */
361 if (delete_redundant) {
362 sort_string_list(&names);
363 for_each_string_list_item(item, &existing_packs) {
364 char *sha1;
365 size_t len = strlen(item->string);
366 if (len < 40)
367 continue;
368 sha1 = item->string + len - 40;
369 if (!string_list_has_string(&names, sha1))
370 remove_redundant_pack(packdir, item->string);
372 argv_array_push(&cmd_args, "prune-packed");
373 if (quiet)
374 argv_array_push(&cmd_args, "--quiet");
376 memset(&cmd, 0, sizeof(cmd));
377 cmd.argv = cmd_args.argv;
378 cmd.git_cmd = 1;
379 run_command(&cmd);
380 argv_array_clear(&cmd_args);
383 if (!no_update_server_info) {
384 argv_array_push(&cmd_args, "update-server-info");
385 memset(&cmd, 0, sizeof(cmd));
386 cmd.argv = cmd_args.argv;
387 cmd.git_cmd = 1;
388 run_command(&cmd);
389 argv_array_clear(&cmd_args);
391 remove_temporary_files();
392 string_list_clear(&names, 0);
393 string_list_clear(&rollback, 0);
394 string_list_clear(&existing_packs, 0);
395 strbuf_release(&line);
397 return 0;