Merge 'send-email' into HEAD
[git/mingw/4msysgit.git] / builtin / repack.c
blob6b0b62dcb2687e78ec433e7de1103f0c0f74a1fc
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 char *packdir, *packtmp;
15 static const char *const git_repack_usage[] = {
16 N_("git repack [options]"),
17 NULL
20 static int repack_config(const char *var, const char *value, void *cb)
22 if (!strcmp(var, "repack.usedeltabaseoffset")) {
23 delta_base_offset = git_config_bool(var, value);
24 return 0;
26 if (!strcmp(var, "repack.packkeptobjects")) {
27 pack_kept_objects = git_config_bool(var, value);
28 return 0;
30 return git_default_config(var, value, cb);
34 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
36 static void remove_temporary_files(void)
38 struct strbuf buf = STRBUF_INIT;
39 size_t dirlen, prefixlen;
40 DIR *dir;
41 struct dirent *e;
43 dir = opendir(packdir);
44 if (!dir)
45 return;
47 /* Point at the slash at the end of ".../objects/pack/" */
48 dirlen = strlen(packdir) + 1;
49 strbuf_addstr(&buf, packtmp);
50 /* Hold the length of ".tmp-%d-pack-" */
51 prefixlen = buf.len - dirlen;
53 while ((e = readdir(dir))) {
54 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
55 continue;
56 strbuf_setlen(&buf, dirlen);
57 strbuf_addstr(&buf, e->d_name);
58 unlink(buf.buf);
60 closedir(dir);
61 strbuf_release(&buf);
64 static void remove_pack_on_signal(int signo)
66 remove_temporary_files();
67 sigchain_pop(signo);
68 raise(signo);
72 * Adds all packs hex strings to the fname list, which do not
73 * have a corresponding .keep file.
75 static void get_non_kept_pack_filenames(struct string_list *fname_list)
77 DIR *dir;
78 struct dirent *e;
79 char *fname;
80 size_t len;
82 if (!(dir = opendir(packdir)))
83 return;
85 while ((e = readdir(dir)) != NULL) {
86 if (!ends_with(e->d_name, ".pack"))
87 continue;
89 len = strlen(e->d_name) - strlen(".pack");
90 fname = xmemdupz(e->d_name, len);
92 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
93 string_list_append_nodup(fname_list, fname);
94 else
95 free(fname);
97 closedir(dir);
100 static void remove_redundant_pack(const char *dir_name, const char *base_name)
102 const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
103 int i;
104 struct strbuf buf = STRBUF_INIT;
105 size_t plen;
107 strbuf_addf(&buf, "%s/%s", dir_name, base_name);
108 plen = buf.len;
110 for (i = 0; i < ARRAY_SIZE(exts); i++) {
111 strbuf_setlen(&buf, plen);
112 strbuf_addstr(&buf, exts[i]);
113 unlink(buf.buf);
115 strbuf_release(&buf);
118 #define ALL_INTO_ONE 1
119 #define LOOSEN_UNREACHABLE 2
121 int cmd_repack(int argc, const char **argv, const char *prefix)
123 struct {
124 const char *name;
125 unsigned optional:1;
126 } exts[] = {
127 {".pack"},
128 {".idx"},
129 {".bitmap", 1},
131 struct child_process cmd;
132 struct string_list_item *item;
133 struct argv_array cmd_args = ARGV_ARRAY_INIT;
134 struct string_list names = STRING_LIST_INIT_DUP;
135 struct string_list rollback = STRING_LIST_INIT_NODUP;
136 struct string_list existing_packs = STRING_LIST_INIT_DUP;
137 struct strbuf line = STRBUF_INIT;
138 int ext, ret, failed;
139 FILE *out;
141 /* variables to be filled by option parsing */
142 int pack_everything = 0;
143 int delete_redundant = 0;
144 const char *unpack_unreachable = NULL;
145 const char *window = NULL, *window_memory = NULL;
146 const char *depth = NULL;
147 const char *max_pack_size = NULL;
148 int no_reuse_delta = 0, no_reuse_object = 0;
149 int no_update_server_info = 0;
150 int quiet = 0;
151 int local = 0;
152 int write_bitmap = -1;
154 struct option builtin_repack_options[] = {
155 OPT_BIT('a', NULL, &pack_everything,
156 N_("pack everything in a single pack"), ALL_INTO_ONE),
157 OPT_BIT('A', NULL, &pack_everything,
158 N_("same as -a, and turn unreachable objects loose"),
159 LOOSEN_UNREACHABLE | ALL_INTO_ONE),
160 OPT_BOOL('d', NULL, &delete_redundant,
161 N_("remove redundant packs, and run git-prune-packed")),
162 OPT_BOOL('f', NULL, &no_reuse_delta,
163 N_("pass --no-reuse-delta to git-pack-objects")),
164 OPT_BOOL('F', NULL, &no_reuse_object,
165 N_("pass --no-reuse-object to git-pack-objects")),
166 OPT_BOOL('n', NULL, &no_update_server_info,
167 N_("do not run git-update-server-info")),
168 OPT__QUIET(&quiet, N_("be quiet")),
169 OPT_BOOL('l', "local", &local,
170 N_("pass --local to git-pack-objects")),
171 OPT_BOOL('b', "write-bitmap-index", &write_bitmap,
172 N_("write bitmap index")),
173 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
174 N_("with -A, do not loosen objects older than this")),
175 OPT_STRING(0, "window", &window, N_("n"),
176 N_("size of the window used for delta compression")),
177 OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),
178 N_("same as the above, but limit memory size instead of entries count")),
179 OPT_STRING(0, "depth", &depth, N_("n"),
180 N_("limits the maximum delta depth")),
181 OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),
182 N_("maximum size of each packfile")),
183 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
184 N_("repack objects in packs marked with .keep")),
185 OPT_END()
188 git_config(repack_config, NULL);
190 argc = parse_options(argc, argv, prefix, builtin_repack_options,
191 git_repack_usage, 0);
193 if (pack_kept_objects < 0)
194 pack_kept_objects = write_bitmap;
196 packdir = mkpathdup("%s/pack", get_object_directory());
197 packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
199 sigchain_push_common(remove_pack_on_signal);
201 argv_array_push(&cmd_args, "pack-objects");
202 argv_array_push(&cmd_args, "--keep-true-parents");
203 if (!pack_kept_objects)
204 argv_array_push(&cmd_args, "--honor-pack-keep");
205 argv_array_push(&cmd_args, "--non-empty");
206 argv_array_push(&cmd_args, "--all");
207 argv_array_push(&cmd_args, "--reflog");
208 if (window)
209 argv_array_pushf(&cmd_args, "--window=%s", window);
210 if (window_memory)
211 argv_array_pushf(&cmd_args, "--window-memory=%s", window_memory);
212 if (depth)
213 argv_array_pushf(&cmd_args, "--depth=%s", depth);
214 if (max_pack_size)
215 argv_array_pushf(&cmd_args, "--max-pack-size=%s", max_pack_size);
216 if (no_reuse_delta)
217 argv_array_pushf(&cmd_args, "--no-reuse-delta");
218 if (no_reuse_object)
219 argv_array_pushf(&cmd_args, "--no-reuse-object");
220 if (write_bitmap >= 0)
221 argv_array_pushf(&cmd_args, "--%swrite-bitmap-index",
222 write_bitmap ? "" : "no-");
224 if (pack_everything & ALL_INTO_ONE) {
225 get_non_kept_pack_filenames(&existing_packs);
227 if (existing_packs.nr && delete_redundant) {
228 if (unpack_unreachable)
229 argv_array_pushf(&cmd_args,
230 "--unpack-unreachable=%s",
231 unpack_unreachable);
232 else if (pack_everything & LOOSEN_UNREACHABLE)
233 argv_array_push(&cmd_args,
234 "--unpack-unreachable");
236 } else {
237 argv_array_push(&cmd_args, "--unpacked");
238 argv_array_push(&cmd_args, "--incremental");
241 if (local)
242 argv_array_push(&cmd_args, "--local");
243 if (quiet)
244 argv_array_push(&cmd_args, "--quiet");
245 if (delta_base_offset)
246 argv_array_push(&cmd_args, "--delta-base-offset");
248 argv_array_push(&cmd_args, packtmp);
250 memset(&cmd, 0, sizeof(cmd));
251 cmd.argv = cmd_args.argv;
252 cmd.git_cmd = 1;
253 cmd.out = -1;
254 cmd.no_stdin = 1;
256 ret = start_command(&cmd);
257 if (ret)
258 return ret;
260 out = xfdopen(cmd.out, "r");
261 while (strbuf_getline(&line, out, '\n') != EOF) {
262 if (line.len != 40)
263 die("repack: Expecting 40 character sha1 lines only from pack-objects.");
264 string_list_append(&names, line.buf);
266 fclose(out);
267 ret = finish_command(&cmd);
268 if (ret)
269 return ret;
270 argv_array_clear(&cmd_args);
272 if (!names.nr && !quiet)
273 printf("Nothing new to pack.\n");
276 * Ok we have prepared all new packfiles.
277 * First see if there are packs of the same name and if so
278 * if we can move them out of the way (this can happen if we
279 * repacked immediately after packing fully.
281 failed = 0;
282 for_each_string_list_item(item, &names) {
283 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
284 char *fname, *fname_old;
285 fname = mkpathdup("%s/pack-%s%s", packdir,
286 item->string, exts[ext].name);
287 if (!file_exists(fname)) {
288 free(fname);
289 continue;
292 fname_old = mkpath("%s/old-%s%s", packdir,
293 item->string, exts[ext].name);
294 if (file_exists(fname_old))
295 if (unlink(fname_old))
296 failed = 1;
298 if (!failed && rename(fname, fname_old)) {
299 free(fname);
300 failed = 1;
301 break;
302 } else {
303 string_list_append(&rollback, fname);
306 if (failed)
307 break;
309 if (failed) {
310 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
311 for_each_string_list_item(item, &rollback) {
312 char *fname, *fname_old;
313 fname = mkpathdup("%s/%s", packdir, item->string);
314 fname_old = mkpath("%s/old-%s", packdir, item->string);
315 if (rename(fname_old, fname))
316 string_list_append(&rollback_failure, fname);
317 free(fname);
320 if (rollback_failure.nr) {
321 int i;
322 fprintf(stderr,
323 "WARNING: Some packs in use have been renamed by\n"
324 "WARNING: prefixing old- to their name, in order to\n"
325 "WARNING: replace them with the new version of the\n"
326 "WARNING: file. But the operation failed, and the\n"
327 "WARNING: attempt to rename them back to their\n"
328 "WARNING: original names also failed.\n"
329 "WARNING: Please rename them in %s manually:\n", packdir);
330 for (i = 0; i < rollback_failure.nr; i++)
331 fprintf(stderr, "WARNING: old-%s -> %s\n",
332 rollback_failure.items[i].string,
333 rollback_failure.items[i].string);
335 exit(1);
338 /* Now the ones with the same name are out of the way... */
339 for_each_string_list_item(item, &names) {
340 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
341 char *fname, *fname_old;
342 struct stat statbuffer;
343 int exists = 0;
344 fname = mkpathdup("%s/pack-%s%s",
345 packdir, item->string, exts[ext].name);
346 fname_old = mkpathdup("%s-%s%s",
347 packtmp, item->string, exts[ext].name);
348 if (!stat(fname_old, &statbuffer)) {
349 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
350 chmod(fname_old, statbuffer.st_mode);
351 exists = 1;
353 if (exists || !exts[ext].optional) {
354 if (rename(fname_old, fname))
355 die_errno(_("renaming '%s' failed"), fname_old);
357 free(fname);
358 free(fname_old);
362 /* Remove the "old-" files */
363 for_each_string_list_item(item, &names) {
364 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
365 char *fname;
366 fname = mkpath("%s/old-%s%s",
367 packdir,
368 item->string,
369 exts[ext].name);
370 if (remove_path(fname))
371 warning(_("removing '%s' failed"), fname);
375 /* End of pack replacement. */
377 if (delete_redundant) {
378 sort_string_list(&names);
379 for_each_string_list_item(item, &existing_packs) {
380 char *sha1;
381 size_t len = strlen(item->string);
382 if (len < 40)
383 continue;
384 sha1 = item->string + len - 40;
385 if (!string_list_has_string(&names, sha1))
386 remove_redundant_pack(packdir, item->string);
388 argv_array_push(&cmd_args, "prune-packed");
389 if (quiet)
390 argv_array_push(&cmd_args, "--quiet");
392 memset(&cmd, 0, sizeof(cmd));
393 cmd.argv = cmd_args.argv;
394 cmd.git_cmd = 1;
395 run_command(&cmd);
396 argv_array_clear(&cmd_args);
399 if (!no_update_server_info) {
400 argv_array_push(&cmd_args, "update-server-info");
401 memset(&cmd, 0, sizeof(cmd));
402 cmd.argv = cmd_args.argv;
403 cmd.git_cmd = 1;
404 run_command(&cmd);
405 argv_array_clear(&cmd_args);
407 remove_temporary_files();
408 string_list_clear(&names, 0);
409 string_list_clear(&rollback, 0);
410 string_list_clear(&existing_packs, 0);
411 strbuf_release(&line);
413 return 0;