builtin / repack.con commit config: don't implicitly use gitdir or commondir (dc8441f)
   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
  12static int delta_base_offset = 1;
  13static int pack_kept_objects = -1;
  14static int write_bitmaps;
  15static char *packdir, *packtmp;
  16
  17static const char *const git_repack_usage[] = {
  18        N_("git repack [<options>]"),
  19        NULL
  20};
  21
  22static const char incremental_bitmap_conflict_error[] = N_(
  23"Incremental repacks are incompatible with bitmap indexes.  Use\n"
  24"--no-write-bitmap-index or disable the pack.writebitmaps configuration."
  25);
  26
  27
  28static int repack_config(const char *var, const char *value, void *cb)
  29{
  30        if (!strcmp(var, "repack.usedeltabaseoffset")) {
  31                delta_base_offset = git_config_bool(var, value);
  32                return 0;
  33        }
  34        if (!strcmp(var, "repack.packkeptobjects")) {
  35                pack_kept_objects = git_config_bool(var, value);
  36                return 0;
  37        }
  38        if (!strcmp(var, "repack.writebitmaps") ||
  39            !strcmp(var, "pack.writebitmaps")) {
  40                write_bitmaps = git_config_bool(var, value);
  41                return 0;
  42        }
  43        return git_default_config(var, value, cb);
  44}
  45
  46/*
  47 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
  48 */
  49static void remove_temporary_files(void)
  50{
  51        struct strbuf buf = STRBUF_INIT;
  52        size_t dirlen, prefixlen;
  53        DIR *dir;
  54        struct dirent *e;
  55
  56        dir = opendir(packdir);
  57        if (!dir)
  58                return;
  59
  60        /* Point at the slash at the end of ".../objects/pack/" */
  61        dirlen = strlen(packdir) + 1;
  62        strbuf_addstr(&buf, packtmp);
  63        /* Hold the length of  ".tmp-%d-pack-" */
  64        prefixlen = buf.len - dirlen;
  65
  66        while ((e = readdir(dir))) {
  67                if (strncmp(e->d_name, buf.buf + dirlen, prefixlen))
  68                        continue;
  69                strbuf_setlen(&buf, dirlen);
  70                strbuf_addstr(&buf, e->d_name);
  71                unlink(buf.buf);
  72        }
  73        closedir(dir);
  74        strbuf_release(&buf);
  75}
  76
  77static void remove_pack_on_signal(int signo)
  78{
  79        remove_temporary_files();
  80        sigchain_pop(signo);
  81        raise(signo);
  82}
  83
  84/*
  85 * Adds all packs hex strings to the fname list, which do not
  86 * have a corresponding .keep file.
  87 */
  88static void get_non_kept_pack_filenames(struct string_list *fname_list)
  89{
  90        DIR *dir;
  91        struct dirent *e;
  92        char *fname;
  93
  94        if (!(dir = opendir(packdir)))
  95                return;
  96
  97        while ((e = readdir(dir)) != NULL) {
  98                size_t len;
  99                if (!strip_suffix(e->d_name, ".pack", &len))
 100                        continue;
 101
 102                fname = xmemdupz(e->d_name, len);
 103
 104                if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
 105                        string_list_append_nodup(fname_list, fname);
 106                else
 107                        free(fname);
 108        }
 109        closedir(dir);
 110}
 111
 112static void remove_redundant_pack(const char *dir_name, const char *base_name)
 113{
 114        const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
 115        int i;
 116        struct strbuf buf = STRBUF_INIT;
 117        size_t plen;
 118
 119        strbuf_addf(&buf, "%s/%s", dir_name, base_name);
 120        plen = buf.len;
 121
 122        for (i = 0; i < ARRAY_SIZE(exts); i++) {
 123                strbuf_setlen(&buf, plen);
 124                strbuf_addstr(&buf, exts[i]);
 125                unlink(buf.buf);
 126        }
 127        strbuf_release(&buf);
 128}
 129
 130#define ALL_INTO_ONE 1
 131#define LOOSEN_UNREACHABLE 2
 132
 133int cmd_repack(int argc, const char **argv, const char *prefix)
 134{
 135        struct {
 136                const char *name;
 137                unsigned optional:1;
 138        } exts[] = {
 139                {".pack"},
 140                {".idx"},
 141                {".bitmap", 1},
 142        };
 143        struct child_process cmd = CHILD_PROCESS_INIT;
 144        struct string_list_item *item;
 145        struct string_list names = STRING_LIST_INIT_DUP;
 146        struct string_list rollback = STRING_LIST_INIT_NODUP;
 147        struct string_list existing_packs = STRING_LIST_INIT_DUP;
 148        struct strbuf line = STRBUF_INIT;
 149        int ext, ret, failed;
 150        FILE *out;
 151
 152        /* variables to be filled by option parsing */
 153        int pack_everything = 0;
 154        int delete_redundant = 0;
 155        const char *unpack_unreachable = NULL;
 156        int keep_unreachable = 0;
 157        const char *window = NULL, *window_memory = NULL;
 158        const char *depth = NULL;
 159        const char *max_pack_size = NULL;
 160        int no_reuse_delta = 0, no_reuse_object = 0;
 161        int no_update_server_info = 0;
 162        int quiet = 0;
 163        int local = 0;
 164
 165        struct option builtin_repack_options[] = {
 166                OPT_BIT('a', NULL, &pack_everything,
 167                                N_("pack everything in a single pack"), ALL_INTO_ONE),
 168                OPT_BIT('A', NULL, &pack_everything,
 169                                N_("same as -a, and turn unreachable objects loose"),
 170                                   LOOSEN_UNREACHABLE | ALL_INTO_ONE),
 171                OPT_BOOL('d', NULL, &delete_redundant,
 172                                N_("remove redundant packs, and run git-prune-packed")),
 173                OPT_BOOL('f', NULL, &no_reuse_delta,
 174                                N_("pass --no-reuse-delta to git-pack-objects")),
 175                OPT_BOOL('F', NULL, &no_reuse_object,
 176                                N_("pass --no-reuse-object to git-pack-objects")),
 177                OPT_BOOL('n', NULL, &no_update_server_info,
 178                                N_("do not run git-update-server-info")),
 179                OPT__QUIET(&quiet, N_("be quiet")),
 180                OPT_BOOL('l', "local", &local,
 181                                N_("pass --local to git-pack-objects")),
 182                OPT_BOOL('b', "write-bitmap-index", &write_bitmaps,
 183                                N_("write bitmap index")),
 184                OPT_STRING(0, "unpack-unreachable", &unpack_unreachable, N_("approxidate"),
 185                                N_("with -A, do not loosen objects older than this")),
 186                OPT_BOOL('k', "keep-unreachable", &keep_unreachable,
 187                                N_("with -a, repack unreachable objects")),
 188                OPT_STRING(0, "window", &window, N_("n"),
 189                                N_("size of the window used for delta compression")),
 190                OPT_STRING(0, "window-memory", &window_memory, N_("bytes"),
 191                                N_("same as the above, but limit memory size instead of entries count")),
 192                OPT_STRING(0, "depth", &depth, N_("n"),
 193                                N_("limits the maximum delta depth")),
 194                OPT_STRING(0, "max-pack-size", &max_pack_size, N_("bytes"),
 195                                N_("maximum size of each packfile")),
 196                OPT_BOOL(0, "pack-kept-objects", &pack_kept_objects,
 197                                N_("repack objects in packs marked with .keep")),
 198                OPT_END()
 199        };
 200
 201        git_config(repack_config, NULL);
 202
 203        argc = parse_options(argc, argv, prefix, builtin_repack_options,
 204                                git_repack_usage, 0);
 205
 206        if (delete_redundant && repository_format_precious_objects)
 207                die(_("cannot delete packs in a precious-objects repo"));
 208
 209        if (keep_unreachable &&
 210            (unpack_unreachable || (pack_everything & LOOSEN_UNREACHABLE)))
 211                die(_("--keep-unreachable and -A are incompatible"));
 212
 213        if (pack_kept_objects < 0)
 214                pack_kept_objects = write_bitmaps;
 215
 216        if (write_bitmaps && !(pack_everything & ALL_INTO_ONE))
 217                die(_(incremental_bitmap_conflict_error));
 218
 219        packdir = mkpathdup("%s/pack", get_object_directory());
 220        packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
 221
 222        sigchain_push_common(remove_pack_on_signal);
 223
 224        argv_array_push(&cmd.args, "pack-objects");
 225        argv_array_push(&cmd.args, "--keep-true-parents");
 226        if (!pack_kept_objects)
 227                argv_array_push(&cmd.args, "--honor-pack-keep");
 228        argv_array_push(&cmd.args, "--non-empty");
 229        argv_array_push(&cmd.args, "--all");
 230        argv_array_push(&cmd.args, "--reflog");
 231        argv_array_push(&cmd.args, "--indexed-objects");
 232        if (window)
 233                argv_array_pushf(&cmd.args, "--window=%s", window);
 234        if (window_memory)
 235                argv_array_pushf(&cmd.args, "--window-memory=%s", window_memory);
 236        if (depth)
 237                argv_array_pushf(&cmd.args, "--depth=%s", depth);
 238        if (max_pack_size)
 239                argv_array_pushf(&cmd.args, "--max-pack-size=%s", max_pack_size);
 240        if (no_reuse_delta)
 241                argv_array_pushf(&cmd.args, "--no-reuse-delta");
 242        if (no_reuse_object)
 243                argv_array_pushf(&cmd.args, "--no-reuse-object");
 244        if (write_bitmaps)
 245                argv_array_push(&cmd.args, "--write-bitmap-index");
 246
 247        if (pack_everything & ALL_INTO_ONE) {
 248                get_non_kept_pack_filenames(&existing_packs);
 249
 250                if (existing_packs.nr && delete_redundant) {
 251                        if (unpack_unreachable) {
 252                                argv_array_pushf(&cmd.args,
 253                                                "--unpack-unreachable=%s",
 254                                                unpack_unreachable);
 255                                argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
 256                        } else if (pack_everything & LOOSEN_UNREACHABLE) {
 257                                argv_array_push(&cmd.args,
 258                                                "--unpack-unreachable");
 259                        } else if (keep_unreachable) {
 260                                argv_array_push(&cmd.args, "--keep-unreachable");
 261                                argv_array_push(&cmd.args, "--pack-loose-unreachable");
 262                        } else {
 263                                argv_array_push(&cmd.env_array, "GIT_REF_PARANOIA=1");
 264                        }
 265                }
 266        } else {
 267                argv_array_push(&cmd.args, "--unpacked");
 268                argv_array_push(&cmd.args, "--incremental");
 269        }
 270
 271        if (local)
 272                argv_array_push(&cmd.args,  "--local");
 273        if (quiet)
 274                argv_array_push(&cmd.args,  "--quiet");
 275        if (delta_base_offset)
 276                argv_array_push(&cmd.args,  "--delta-base-offset");
 277
 278        argv_array_push(&cmd.args, packtmp);
 279
 280        cmd.git_cmd = 1;
 281        cmd.out = -1;
 282        cmd.no_stdin = 1;
 283
 284        ret = start_command(&cmd);
 285        if (ret)
 286                return ret;
 287
 288        out = xfdopen(cmd.out, "r");
 289        while (strbuf_getline_lf(&line, out) != EOF) {
 290                if (line.len != 40)
 291                        die("repack: Expecting 40 character sha1 lines only from pack-objects.");
 292                string_list_append(&names, line.buf);
 293        }
 294        fclose(out);
 295        ret = finish_command(&cmd);
 296        if (ret)
 297                return ret;
 298
 299        if (!names.nr && !quiet)
 300                printf("Nothing new to pack.\n");
 301
 302        /*
 303         * Ok we have prepared all new packfiles.
 304         * First see if there are packs of the same name and if so
 305         * if we can move them out of the way (this can happen if we
 306         * repacked immediately after packing fully.
 307         */
 308        failed = 0;
 309        for_each_string_list_item(item, &names) {
 310                for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 311                        char *fname, *fname_old;
 312                        fname = mkpathdup("%s/pack-%s%s", packdir,
 313                                                item->string, exts[ext].name);
 314                        if (!file_exists(fname)) {
 315                                free(fname);
 316                                continue;
 317                        }
 318
 319                        fname_old = mkpathdup("%s/old-%s%s", packdir,
 320                                                item->string, exts[ext].name);
 321                        if (file_exists(fname_old))
 322                                if (unlink(fname_old))
 323                                        failed = 1;
 324
 325                        if (!failed && rename(fname, fname_old)) {
 326                                free(fname);
 327                                free(fname_old);
 328                                failed = 1;
 329                                break;
 330                        } else {
 331                                string_list_append(&rollback, fname);
 332                                free(fname_old);
 333                        }
 334                }
 335                if (failed)
 336                        break;
 337        }
 338        if (failed) {
 339                struct string_list rollback_failure = STRING_LIST_INIT_DUP;
 340                for_each_string_list_item(item, &rollback) {
 341                        char *fname, *fname_old;
 342                        fname = mkpathdup("%s/%s", packdir, item->string);
 343                        fname_old = mkpathdup("%s/old-%s", packdir, item->string);
 344                        if (rename(fname_old, fname))
 345                                string_list_append(&rollback_failure, fname);
 346                        free(fname);
 347                        free(fname_old);
 348                }
 349
 350                if (rollback_failure.nr) {
 351                        int i;
 352                        fprintf(stderr,
 353                                "WARNING: Some packs in use have been renamed by\n"
 354                                "WARNING: prefixing old- to their name, in order to\n"
 355                                "WARNING: replace them with the new version of the\n"
 356                                "WARNING: file.  But the operation failed, and the\n"
 357                                "WARNING: attempt to rename them back to their\n"
 358                                "WARNING: original names also failed.\n"
 359                                "WARNING: Please rename them in %s manually:\n", packdir);
 360                        for (i = 0; i < rollback_failure.nr; i++)
 361                                fprintf(stderr, "WARNING:   old-%s -> %s\n",
 362                                        rollback_failure.items[i].string,
 363                                        rollback_failure.items[i].string);
 364                }
 365                exit(1);
 366        }
 367
 368        /* Now the ones with the same name are out of the way... */
 369        for_each_string_list_item(item, &names) {
 370                for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 371                        char *fname, *fname_old;
 372                        struct stat statbuffer;
 373                        int exists = 0;
 374                        fname = mkpathdup("%s/pack-%s%s",
 375                                        packdir, item->string, exts[ext].name);
 376                        fname_old = mkpathdup("%s-%s%s",
 377                                        packtmp, item->string, exts[ext].name);
 378                        if (!stat(fname_old, &statbuffer)) {
 379                                statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
 380                                chmod(fname_old, statbuffer.st_mode);
 381                                exists = 1;
 382                        }
 383                        if (exists || !exts[ext].optional) {
 384                                if (rename(fname_old, fname))
 385                                        die_errno(_("renaming '%s' failed"), fname_old);
 386                        }
 387                        free(fname);
 388                        free(fname_old);
 389                }
 390        }
 391
 392        /* Remove the "old-" files */
 393        for_each_string_list_item(item, &names) {
 394                for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 395                        char *fname;
 396                        fname = mkpathdup("%s/old-%s%s",
 397                                          packdir,
 398                                          item->string,
 399                                          exts[ext].name);
 400                        if (remove_path(fname))
 401                                warning(_("failed to remove '%s'"), fname);
 402                        free(fname);
 403                }
 404        }
 405
 406        /* End of pack replacement. */
 407
 408        if (delete_redundant) {
 409                int opts = 0;
 410                string_list_sort(&names);
 411                for_each_string_list_item(item, &existing_packs) {
 412                        char *sha1;
 413                        size_t len = strlen(item->string);
 414                        if (len < 40)
 415                                continue;
 416                        sha1 = item->string + len - 40;
 417                        if (!string_list_has_string(&names, sha1))
 418                                remove_redundant_pack(packdir, item->string);
 419                }
 420                if (!quiet && isatty(2))
 421                        opts |= PRUNE_PACKED_VERBOSE;
 422                prune_packed_objects(opts);
 423        }
 424
 425        if (!no_update_server_info)
 426                update_server_info(0);
 427        remove_temporary_files();
 428        string_list_clear(&names, 0);
 429        string_list_clear(&rollback, 0);
 430        string_list_clear(&existing_packs, 0);
 431        strbuf_release(&line);
 432
 433        return 0;
 434}