5 #include "parse-options.h"
 
   6 #include "run-command.h"
 
   9 #include "string-list.h"
 
  10 #include "argv-array.h"
 
  13 #include "object-store.h"
 
  14 #include "promisor-remote.h"
 
  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;
 
  22 static const char *const git_repack_usage[] = {
 
  23         N_("git repack [<options>]"),
 
  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."
 
  33 static int repack_config(const char *var, const char *value, void *cb)
 
  35         if (!strcmp(var, "repack.usedeltabaseoffset")) {
 
  36                 delta_base_offset = git_config_bool(var, value);
 
  39         if (!strcmp(var, "repack.packkeptobjects")) {
 
  40                 pack_kept_objects = git_config_bool(var, value);
 
  43         if (!strcmp(var, "repack.writebitmaps") ||
 
  44             !strcmp(var, "pack.writebitmaps")) {
 
  45                 write_bitmaps = git_config_bool(var, value);
 
  48         if (!strcmp(var, "repack.usedeltaislands")) {
 
  49                 use_delta_islands = git_config_bool(var, value);
 
  52         return git_default_config(var, value, cb);
 
  56  * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
 
  58 static void remove_temporary_files(void)
 
  60         struct strbuf buf = STRBUF_INIT;
 
  61         size_t dirlen, prefixlen;
 
  65         dir = opendir(packdir);
 
  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;
 
  75         while ((e = readdir(dir))) {
 
  76                 if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
 
  78                 strbuf_setlen(&buf, dirlen);
 
  79                 strbuf_addstr(&buf, e->d_name);
 
  86 static void remove_pack_on_signal(int signo)
 
  88         remove_temporary_files();
 
  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.
 
  98 static void get_non_kept_pack_filenames(struct string_list *fname_list,
 
  99                                         const struct string_list *extra_keep)
 
 105         if (!(dir = opendir(packdir)))
 
 108         while ((e = readdir(dir)) != NULL) {
 
 112                 for (i = 0; i < extra_keep->nr; i++)
 
 113                         if (!fspathcmp(e->d_name, extra_keep->items[i].string))
 
 115                 if (extra_keep->nr > 0 && i < extra_keep->nr)
 
 118                 if (!strip_suffix(e->d_name, ".pack", &len))
 
 121                 fname = xmemdupz(e->d_name, len);
 
 123                 if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
 
 124                         string_list_append_nodup(fname_list, fname);
 
 131 static void remove_redundant_pack(const char *dir_name, const char *base_name)
 
 133         struct strbuf buf = STRBUF_INIT;
 
 134         strbuf_addf(&buf, "%s/%s.pack", dir_name, base_name);
 
 135         unlink_pack_path(buf.buf, 1);
 
 136         strbuf_release(&buf);
 
 139 struct pack_objects_args {
 
 141         const char *window_memory;
 
 144         const char *max_pack_size;
 
 151 static void prepare_pack_objects(struct child_process *cmd,
 
 152                                  const struct pack_objects_args *args)
 
 154         argv_array_push(&cmd->args, "pack-objects");
 
 156                 argv_array_pushf(&cmd->args, "--window=%s", args->window);
 
 157         if (args->window_memory)
 
 158                 argv_array_pushf(&cmd->args, "--window-memory=%s", args->window_memory);
 
 160                 argv_array_pushf(&cmd->args, "--depth=%s", args->depth);
 
 162                 argv_array_pushf(&cmd->args, "--threads=%s", args->threads);
 
 163         if (args->max_pack_size)
 
 164                 argv_array_pushf(&cmd->args, "--max-pack-size=%s", args->max_pack_size);
 
 165         if (args->no_reuse_delta)
 
 166                 argv_array_pushf(&cmd->args, "--no-reuse-delta");
 
 167         if (args->no_reuse_object)
 
 168                 argv_array_pushf(&cmd->args, "--no-reuse-object");
 
 170                 argv_array_push(&cmd->args,  "--local");
 
 172                 argv_array_push(&cmd->args,  "--quiet");
 
 173         if (delta_base_offset)
 
 174                 argv_array_push(&cmd->args,  "--delta-base-offset");
 
 175         argv_array_push(&cmd->args, packtmp);
 
 181  * Write oid to the given struct child_process's stdin, starting it first if
 
 184 static int write_oid(const struct object_id *oid, struct packed_git *pack,
 
 185                      uint32_t pos, void *data)
 
 187         struct child_process *cmd = data;
 
 190                 if (start_command(cmd))
 
 191                         die(_("could not start pack-objects to repack promisor objects"));
 
 194         xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
 
 195         xwrite(cmd->in, "\n", 1);
 
 199 static void repack_promisor_objects(const struct pack_objects_args *args,
 
 200                                     struct string_list *names)
 
 202         struct child_process cmd = CHILD_PROCESS_INIT;
 
 204         struct strbuf line = STRBUF_INIT;
 
 206         prepare_pack_objects(&cmd, args);
 
 210          * NEEDSWORK: Giving pack-objects only the OIDs without any ordering
 
 211          * hints may result in suboptimal deltas in the resulting pack. See if
 
 212          * the OIDs can be sent with fake paths such that pack-objects can use a
 
 213          * {type -> existing pack order} ordering when computing deltas instead
 
 214          * of a {type -> size} ordering, which may produce better deltas.
 
 216         for_each_packed_object(write_oid, &cmd,
 
 217                                FOR_EACH_OBJECT_PROMISOR_ONLY);
 
 220                 /* No packed objects; cmd was never started */
 
 225         out = xfdopen(cmd.out, "r");
 
 226         while (strbuf_getline_lf(&line, out) != EOF) {
 
 229                 if (line.len != the_hash_algo->hexsz)
 
 230                         die(_("repack: Expecting full hex object ID lines only from pack-objects."));
 
 231                 string_list_append(names, line.buf);
 
 234                  * pack-objects creates the .pack and .idx files, but not the
 
 235                  * .promisor file. Create the .promisor file, which is empty.
 
 237                  * NEEDSWORK: fetch-pack sometimes generates non-empty
 
 238                  * .promisor files containing the ref names and associated
 
 239                  * hashes at the point of generation of the corresponding
 
 240                  * packfile, but this would not preserve their contents. Maybe
 
 241                  * concatenate the contents of all .promisor files instead of
 
 242                  * just creating a new empty file.
 
 244                 promisor_name = mkpathdup("%s-%s.promisor", packtmp,
 
 246                 fd = open(promisor_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
 
 248                         die_errno(_("unable to create '%s'"), promisor_name);
 
 253         if (finish_command(&cmd))
 
 254                 die(_("could not finish pack-objects to repack promisor objects"));
 
 257 #define ALL_INTO_ONE 1
 
 258 #define LOOSEN_UNREACHABLE 2
 
 260 int cmd_repack(int argc, const char **argv, const char *prefix)
 
 271         struct child_process cmd = CHILD_PROCESS_INIT;
 
 272         struct string_list_item *item;
 
 273         struct string_list names = STRING_LIST_INIT_DUP;
 
 274         struct string_list rollback = STRING_LIST_INIT_NODUP;
 
 275         struct string_list existing_packs = STRING_LIST_INIT_DUP;
 
 276         struct strbuf line = STRBUF_INIT;
 
 277         int i, ext, ret, failed;
 
 280         /* variables to be filled by option parsing */
 
 281         int pack_everything = 0;
 
 282         int delete_redundant = 0;
 
 283         const char *unpack_unreachable = NULL;
 
 284         int keep_unreachable = 0;
 
 285         struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
 
 286         int no_update_server_info = 0;
 
 287         int midx_cleared = 0;
 
 288         struct pack_objects_args po_args = {NULL};
 
 290         struct option builtin_repack_options[] = {
 
 291                 OPT_BIT('a', NULL, &pack_everything,
 
 292                                 N_("pack everything in a single pack"), ALL_INTO_ONE),
 
 293                 OPT_BIT('A', NULL, &pack_everything,
 
 294                                 N_("same as -a, and turn unreachable objects loose"),
 
 295                                    LOOSEN_UNREACHABLE | ALL_INTO_ONE),
 
 296                 OPT_BOOL('d', NULL, &delete_redundant,
 
 297                                 N_("remove redundant packs, and run git-prune-packed")),
 
 298                 OPT_BOOL('f', NULL, &po_args.no_reuse_delta,
 
 299                                 N_("pass --no-reuse-delta to git-pack-objects")),
 
 300                 OPT_BOOL('F', NULL, &po_args.no_reuse_object,
 
 301                                 N_("pass --no-reuse-object to git-pack-objects")),
 
 302                 OPT_BOOL('n', NULL, &no_update_server_info,
 
 303                                 N_("do not run git-update-server-info")),
 
 304                 OPT__QUIET(&po_args.quiet, N_("be quiet")),
 
 305                 OPT_BOOL('l', "local", &po_args.local,
 
 306                                 N_("pass --local to git-pack-objects")),
 
 307                 OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
 
 308                                 N_("write bitmap index")),
 
 309                 OPT_BOOL('i', "delta-islands", &use_delta_islands,
 
 310                                 N_("pass --delta-islands to git-pack-objects")),
 
 311                 OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
 
 312                                 N_("with -A, do not loosen objects older than this")),
 
 313                 OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
 
 314                                 N_("with -a, repack unreachable objects")),
 
 315                 OPT_STRING(0, "window", &po_args.window, N_("n"),
 
 316                                 N_("size of the window used for delta compression")),
 
 317                 OPT_STRING(0, "window-memory", &po_args.window_memory, N_("bytes"),
 
 318                                 N_("same as the above, but limit memory size instead of entries count")),
 
 319                 OPT_STRING(0, "depth", &po_args.depth, N_("n"),
 
 320                                 N_("limits the maximum delta depth")),
 
 321                 OPT_STRING(0, "threads", &po_args.threads, N_("n"),
 
 322                                 N_("limits the maximum number of threads")),
 
 323                 OPT_STRING(0, "max-pack-size", &po_args.max_pack_size, N_("bytes"),
 
 324                                 N_("maximum size of each packfile")),
 
 325                 OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
 
 326                                 N_("repack objects in packs marked with .keep")),
 
 327                 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
 
 328                                 N_("do not repack this pack")),
 
 332         git_config(repack_config, NULL);
 
 334         argc = parse_options(argc, argv, prefix, builtin_repack_options,
 
 335                                 git_repack_usage, 0);
 
 337         if (delete_redundant && repository_format_precious_objects)
 
 338                 die(_("cannot delete packs in a precious-objects repo"));
 
 340         if (keep_unreachable &&
 
 341             (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
 
 342                 die(_("--keep-unreachable and -A are incompatible"));
 
 344         if (write_bitmaps < 0) {
 
 345                 if (!(pack_everything & ALL_INTO_ONE) ||
 
 346                     !is_bare_repository())
 
 349         if (pack_kept_objects < 0)
 
 350                 pack_kept_objects = write_bitmaps > 0;
 
 352         if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))
 
 353                 die(_(incremental_bitmap_conflict_error));
 
 355         packdir = mkpathdup("%s/pack", get_object_directory());
 
 356         packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
 
 358         sigchain_push_common(remove_pack_on_signal);
 
 360         prepare_pack_objects(&cmd, &po_args);
 
 362         argv_array_push(&cmd.args, "--keep-true-parents");
 
 363         if (!pack_kept_objects)
 
 364                 argv_array_push(&cmd.args, "--honor-pack-keep");
 
 365         for (i = 0; i < keep_pack_list.nr; i++)
 
 366                 argv_array_pushf(&cmd.args, "--keep-pack=%s",
 
 367                                  keep_pack_list.items[i].string);
 
 368         argv_array_push(&cmd.args, "--non-empty");
 
 369         argv_array_push(&cmd.args, "--all");
 
 370         argv_array_push(&cmd.args, "--reflog");
 
 371         argv_array_push(&cmd.args, "--indexed-objects");
 
 372         if (has_promisor_remote())
 
 373                 argv_array_push(&cmd.args, "--exclude-promisor-objects");
 
 374         if (write_bitmaps > 0)
 
 375                 argv_array_push(&cmd.args, "--write-bitmap-index");
 
 376         else if (write_bitmaps < 0)
 
 377                 argv_array_push(&cmd.args, "--write-bitmap-index-quiet");
 
 378         if (use_delta_islands)
 
 379                 argv_array_push(&cmd.args, "--delta-islands");
 
 381         if (pack_everything & ALL_INTO_ONE) {
 
 382                 get_non_kept_pack_filenames(&existing_packs, &keep_pack_list);
 
 384                 repack_promisor_objects(&po_args, &names);
 
 386                 if (existing_packs.nr && delete_redundant) {
 
 387                         if (unpack_unreachable) {
 
 388                                 argv_array_pushf(&cmd.args,
 
 389                                                 "--unpack-unreachable=%s",
 
 391                                 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
 
 392                         } else if (pack_everything & LOOSEN_UNREACHABLE) {
 
 393                                 argv_array_push(&cmd.args,
 
 394                                                 "--unpack-unreachable");
 
 395                         } else if (keep_unreachable) {
 
 396                                 argv_array_push(&cmd.args, "--keep-unreachable");
 
 397                                 argv_array_push(&cmd.args, "--pack-loose-unreachable");
 
 399                                 argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
 
 403                 argv_array_push(&cmd.args, "--unpacked");
 
 404                 argv_array_push(&cmd.args, "--incremental");
 
 409         ret = start_command(&cmd);
 
 413         out = xfdopen(cmd.out, "r");
 
 414         while (strbuf_getline_lf(&line, out) != EOF) {
 
 415                 if (line.len != the_hash_algo->hexsz)
 
 416                         die(_("repack: Expecting full hex object ID lines only from pack-objects."));
 
 417                 string_list_append(&names, line.buf);
 
 420         ret = finish_command(&cmd);
 
 424         if (!names.nr && !po_args.quiet)
 
 425                 printf_ln(_("Nothing new to pack."));
 
 427         close_object_store(the_repository->objects);
 
 430          * Ok we have prepared all new packfiles.
 
 431          * First see if there are packs of the same name and if so
 
 432          * if we can move them out of the way (this can happen if we
 
 433          * repacked immediately after packing fully.
 
 436         for_each_string_list_item(item, &names) {
 
 437                 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 
 438                         char *fname, *fname_old;
 
 441                                 clear_midx_file(the_repository);
 
 445                         fname = mkpathdup("%s/pack-%s%s", packdir,
 
 446                                                 item->string, exts[ext].name);
 
 447                         if (!file_exists(fname)) {
 
 452                         fname_old = mkpathdup("%s/old-%s%s", packdir,
 
 453                                                 item->string, exts[ext].name);
 
 454                         if (file_exists(fname_old))
 
 455                                 if (unlink(fname_old))
 
 458                         if (!failed && rename(fname, fname_old)) {
 
 464                                 string_list_append(&rollback, fname);
 
 472                 struct string_list rollback_failure = STRING_LIST_INIT_DUP;
 
 473                 for_each_string_list_item(item, &rollback) {
 
 474                         char *fname, *fname_old;
 
 475                         fname = mkpathdup("%s/%s", packdir, item->string);
 
 476                         fname_old = mkpathdup("%s/old-%s", packdir, item->string);
 
 477                         if (rename(fname_old, fname))
 
 478                                 string_list_append(&rollback_failure, fname);
 
 483                 if (rollback_failure.nr) {
 
 486                                 _("WARNING: Some packs in use have been renamed by\n"
 
 487                                   "WARNING: prefixing old- to their name, in order to\n"
 
 488                                   "WARNING: replace them with the new version of the\n"
 
 489                                   "WARNING: file.  But the operation failed, and the\n"
 
 490                                   "WARNING: attempt to rename them back to their\n"
 
 491                                   "WARNING: original names also failed.\n"
 
 492                                   "WARNING: Please rename them in %s manually:\n"), packdir);
 
 493                         for (i = 0; i < rollback_failure.nr; i++)
 
 494                                 fprintf(stderr, "WARNING:   old-%s -> %s\n",
 
 495                                         rollback_failure.items[i].string,
 
 496                                         rollback_failure.items[i].string);
 
 501         /* Now the ones with the same name are out of the way... */
 
 502         for_each_string_list_item(item, &names) {
 
 503                 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 
 504                         char *fname, *fname_old;
 
 505                         struct stat statbuffer;
 
 507                         fname = mkpathdup("%s/pack-%s%s",
 
 508                                         packdir, item->string, exts[ext].name);
 
 509                         fname_old = mkpathdup("%s-%s%s",
 
 510                                         packtmp, item->string, exts[ext].name);
 
 511                         if (!stat(fname_old, &statbuffer)) {
 
 512                                 statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
 
 513                                 chmod(fname_old, statbuffer.st_mode);
 
 516                         if (exists || !exts[ext].optional) {
 
 517                                 if (rename(fname_old, fname))
 
 518                                         die_errno(_("renaming '%s' failed"), fname_old);
 
 525         /* Remove the "old-" files */
 
 526         for_each_string_list_item(item, &names) {
 
 527                 for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 
 529                         fname = mkpathdup("%s/old-%s%s",
 
 533                         if (remove_path(fname))
 
 534                                 warning(_("failed to remove '%s'"), fname);
 
 539         /* End of pack replacement. */
 
 541         reprepare_packed_git(the_repository);
 
 543         if (delete_redundant) {
 
 544                 const int hexsz = the_hash_algo->hexsz;
 
 546                 string_list_sort(&names);
 
 547                 for_each_string_list_item(item, &existing_packs) {
 
 549                         size_t len = strlen(item->string);
 
 552                         sha1 = item->string + len - hexsz;
 
 553                         if (!string_list_has_string(&names, sha1))
 
 554                                 remove_redundant_pack(packdir, item->string);
 
 556                 if (!po_args.quiet && isatty(2))
 
 557                         opts |= PRUNE_PACKED_VERBOSE;
 
 558                 prune_packed_objects(opts);
 
 560                 if (!keep_unreachable &&
 
 561                     (!(pack_everything & LOOSEN_UNREACHABLE) ||
 
 562                      unpack_unreachable) &&
 
 563                     is_repository_shallow(the_repository))
 
 564                         prune_shallow(PRUNE_QUICK);
 
 567         if (!no_update_server_info)
 
 568                 update_server_info(0);
 
 569         remove_temporary_files();
 
 571         if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX, 0))
 
 572                 write_midx_file(get_object_directory(), 0);
 
 574         string_list_clear(&names, 0);
 
 575         string_list_clear(&rollback, 0);
 
 576         string_list_clear(&existing_packs, 0);
 
 577         strbuf_release(&line);