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