50cc2815279e17b255ddc74d23f619e1259b8a7c
   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 char *packdir, *packtmp;
  14
  15static const char *const git_repack_usage[] = {
  16        N_("git repack [options]"),
  17        NULL
  18};
  19
  20static int repack_config(const char *var, const char *value, void *cb)
  21{
  22        if (!strcmp(var, "repack.usedeltabaseoffset")) {
  23                delta_base_offset = git_config_bool(var, value);
  24                return 0;
  25        }
  26        if (!strcmp(var, "repack.packkeptobjects")) {
  27                pack_kept_objects = git_config_bool(var, value);
  28                return 0;
  29        }
  30        return git_default_config(var, value, cb);
  31}
  32
  33/*
  34 * Remove temporary $GIT_OBJECT_DIRECTORY/pack/.tmp-$$-pack-* files.
  35 */
  36static void remove_temporary_files(void)
  37{
  38        struct strbuf buf = STRBUF_INIT;
  39        size_t dirlen, prefixlen;
  40        DIR *dir;
  41        struct dirent *e;
  42
  43        dir = opendir(packdir);
  44        if (!dir)
  45                return;
  46
  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;
  52
  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);
  59        }
  60        closedir(dir);
  61        strbuf_release(&buf);
  62}
  63
  64static void remove_pack_on_signal(int signo)
  65{
  66        remove_temporary_files();
  67        sigchain_pop(signo);
  68        raise(signo);
  69}
  70
  71/*
  72 * Adds all packs hex strings to the fname list, which do not
  73 * have a corresponding .keep file.
  74 */
  75static void get_non_kept_pack_filenames(struct string_list *fname_list)
  76{
  77        DIR *dir;
  78        struct dirent *e;
  79        char *fname;
  80        size_t len;
  81
  82        if (!(dir = opendir(packdir)))
  83                return;
  84
  85        while ((e = readdir(dir)) != NULL) {
  86                if (suffixcmp(e->d_name, ".pack"))
  87                        continue;
  88
  89                len = strlen(e->d_name) - strlen(".pack");
  90                fname = xmemdupz(e->d_name, len);
  91
  92                if (!file_exists(mkpath("%s/%s.keep", packdir, fname)))
  93                        string_list_append_nodup(fname_list, fname);
  94                else
  95                        free(fname);
  96        }
  97        closedir(dir);
  98}
  99
 100static void remove_redundant_pack(const char *dir_name, const char *base_name)
 101{
 102        const char *exts[] = {".pack", ".idx", ".keep", ".bitmap"};
 103        int i;
 104        struct strbuf buf = STRBUF_INIT;
 105        size_t plen;
 106
 107        strbuf_addf(&buf, "%s/%s", dir_name, base_name);
 108        plen = buf.len;
 109
 110        for (i = 0; i < ARRAY_SIZE(exts); i++) {
 111                strbuf_setlen(&buf, plen);
 112                strbuf_addstr(&buf, exts[i]);
 113                unlink(buf.buf);
 114        }
 115        strbuf_release(&buf);
 116}
 117
 118#define ALL_INTO_ONE 1
 119#define LOOSEN_UNREACHABLE 2
 120
 121int cmd_repack(int argc, const char **argv, const char *prefix)
 122{
 123        struct {
 124                const char *name;
 125                unsigned optional:1;
 126        } exts[] = {
 127                {".pack"},
 128                {".idx"},
 129                {".bitmap", 1},
 130        };
 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 nr_packs, ext, ret, failed;
 139        FILE *out;
 140
 141        /* variables to be filled by option parsing */
 142        int pack_everything = 0;
 143        int delete_redundant = 0;
 144        char *unpack_unreachable = NULL;
 145        int window = 0, window_memory = 0;
 146        int depth = 0;
 147        int max_pack_size = 0;
 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;
 153
 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_INTEGER(0, "window", &window,
 176                                N_("size of the window used for delta compression")),
 177                OPT_INTEGER(0, "window-memory", &window_memory,
 178                                N_("same as the above, but limit memory size instead of entries count")),
 179                OPT_INTEGER(0, "depth", &depth,
 180                                N_("limits the maximum delta depth")),
 181                OPT_INTEGER(0, "max-pack-size", &max_pack_size,
 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()
 186        };
 187
 188        git_config(repack_config, NULL);
 189
 190        argc = parse_options(argc, argv, prefix, builtin_repack_options,
 191                                git_repack_usage, 0);
 192
 193        if (pack_kept_objects < 0)
 194                pack_kept_objects = write_bitmap;
 195
 196        packdir = mkpathdup("%s/pack", get_object_directory());
 197        packtmp = mkpathdup("%s/.tmp-%d-pack", packdir, (int)getpid());
 198
 199        sigchain_push_common(remove_pack_on_signal);
 200
 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=%u", window);
 210        if (window_memory)
 211                argv_array_pushf(&cmd_args, "--window-memory=%u", window_memory);
 212        if (depth)
 213                argv_array_pushf(&cmd_args, "--depth=%u", depth);
 214        if (max_pack_size)
 215                argv_array_pushf(&cmd_args, "--max_pack_size=%u", 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-");
 223
 224        if (pack_everything & ALL_INTO_ONE) {
 225                get_non_kept_pack_filenames(&existing_packs);
 226
 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");
 235                }
 236        } else {
 237                argv_array_push(&cmd_args, "--unpacked");
 238                argv_array_push(&cmd_args, "--incremental");
 239        }
 240
 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");
 247
 248        argv_array_push(&cmd_args, packtmp);
 249
 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;
 255
 256        ret = start_command(&cmd);
 257        if (ret)
 258                return ret;
 259
 260        nr_packs = 0;
 261        out = xfdopen(cmd.out, "r");
 262        while (strbuf_getline(&line, out, '\n') != EOF) {
 263                if (line.len != 40)
 264                        die("repack: Expecting 40 character sha1 lines only from pack-objects.");
 265                string_list_append(&names, line.buf);
 266                nr_packs++;
 267        }
 268        fclose(out);
 269        ret = finish_command(&cmd);
 270        if (ret)
 271                return ret;
 272        argv_array_clear(&cmd_args);
 273
 274        if (!nr_packs && !quiet)
 275                printf("Nothing new to pack.\n");
 276
 277        /*
 278         * Ok we have prepared all new packfiles.
 279         * First see if there are packs of the same name and if so
 280         * if we can move them out of the way (this can happen if we
 281         * repacked immediately after packing fully.
 282         */
 283        failed = 0;
 284        for_each_string_list_item(item, &names) {
 285                for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 286                        char *fname, *fname_old;
 287                        fname = mkpathdup("%s/%s%s", packdir,
 288                                                item->string, exts[ext].name);
 289                        if (!file_exists(fname)) {
 290                                free(fname);
 291                                continue;
 292                        }
 293
 294                        fname_old = mkpath("%s/old-%s%s", packdir,
 295                                                item->string, exts[ext].name);
 296                        if (file_exists(fname_old))
 297                                if (unlink(fname_old))
 298                                        failed = 1;
 299
 300                        if (!failed && rename(fname, fname_old)) {
 301                                free(fname);
 302                                failed = 1;
 303                                break;
 304                        } else {
 305                                string_list_append(&rollback, fname);
 306                        }
 307                }
 308                if (failed)
 309                        break;
 310        }
 311        if (failed) {
 312                struct string_list rollback_failure = STRING_LIST_INIT_DUP;
 313                for_each_string_list_item(item, &rollback) {
 314                        char *fname, *fname_old;
 315                        fname = mkpathdup("%s/%s", packdir, item->string);
 316                        fname_old = mkpath("%s/old-%s", packdir, item->string);
 317                        if (rename(fname_old, fname))
 318                                string_list_append(&rollback_failure, fname);
 319                        free(fname);
 320                }
 321
 322                if (rollback_failure.nr) {
 323                        int i;
 324                        fprintf(stderr,
 325                                "WARNING: Some packs in use have been renamed by\n"
 326                                "WARNING: prefixing old- to their name, in order to\n"
 327                                "WARNING: replace them with the new version of the\n"
 328                                "WARNING: file.  But the operation failed, and the\n"
 329                                "WARNING: attempt to rename them back to their\n"
 330                                "WARNING: original names also failed.\n"
 331                                "WARNING: Please rename them in %s manually:\n", packdir);
 332                        for (i = 0; i < rollback_failure.nr; i++)
 333                                fprintf(stderr, "WARNING:   old-%s -> %s\n",
 334                                        rollback_failure.items[i].string,
 335                                        rollback_failure.items[i].string);
 336                }
 337                exit(1);
 338        }
 339
 340        /* Now the ones with the same name are out of the way... */
 341        for_each_string_list_item(item, &names) {
 342                for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 343                        char *fname, *fname_old;
 344                        struct stat statbuffer;
 345                        int exists = 0;
 346                        fname = mkpathdup("%s/pack-%s%s",
 347                                        packdir, item->string, exts[ext].name);
 348                        fname_old = mkpathdup("%s-%s%s",
 349                                        packtmp, item->string, exts[ext].name);
 350                        if (!stat(fname_old, &statbuffer)) {
 351                                statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
 352                                chmod(fname_old, statbuffer.st_mode);
 353                                exists = 1;
 354                        }
 355                        if (exists || !exts[ext].optional) {
 356                                if (rename(fname_old, fname))
 357                                        die_errno(_("renaming '%s' failed"), fname_old);
 358                        }
 359                        free(fname);
 360                        free(fname_old);
 361                }
 362        }
 363
 364        /* Remove the "old-" files */
 365        for_each_string_list_item(item, &names) {
 366                for (ext = 0; ext < ARRAY_SIZE(exts); ext++) {
 367                        char *fname;
 368                        fname = mkpath("%s/old-pack-%s%s",
 369                                        packdir,
 370                                        item->string,
 371                                        exts[ext].name);
 372                        if (remove_path(fname))
 373                                warning(_("removing '%s' failed"), fname);
 374                }
 375        }
 376
 377        /* End of pack replacement. */
 378
 379        if (delete_redundant) {
 380                sort_string_list(&names);
 381                for_each_string_list_item(item, &existing_packs) {
 382                        char *sha1;
 383                        size_t len = strlen(item->string);
 384                        if (len < 40)
 385                                continue;
 386                        sha1 = item->string + len - 40;
 387                        if (!string_list_has_string(&names, sha1))
 388                                remove_redundant_pack(packdir, item->string);
 389                }
 390                argv_array_push(&cmd_args, "prune-packed");
 391                if (quiet)
 392                        argv_array_push(&cmd_args, "--quiet");
 393
 394                memset(&cmd, 0, sizeof(cmd));
 395                cmd.argv = cmd_args.argv;
 396                cmd.git_cmd = 1;
 397                run_command(&cmd);
 398                argv_array_clear(&cmd_args);
 399        }
 400
 401        if (!no_update_server_info) {
 402                argv_array_push(&cmd_args, "update-server-info");
 403                memset(&cmd, 0, sizeof(cmd));
 404                cmd.argv = cmd_args.argv;
 405                cmd.git_cmd = 1;
 406                run_command(&cmd);
 407                argv_array_clear(&cmd_args);
 408        }
 409        remove_temporary_files();
 410        string_list_clear(&names, 0);
 411        string_list_clear(&rollback, 0);
 412        string_list_clear(&existing_packs, 0);
 413        strbuf_release(&line);
 414
 415        return 0;
 416}