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