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