builtin / repack.con commit Merge branch 'ef/mingw-write' (7b4e2b7)
   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 (!ends_with(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 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        out = xfdopen(cmd.out, "r");
 237        while (strbuf_getline(&line, out, '\n') != EOF) {
 238                if (line.len != 40)
 239                        die("repack: Expecting 40 character sha1 lines only from pack-objects.");
 240                string_list_append(&names, line.buf);
 241        }
 242        fclose(out);
 243        ret = finish_command(&cmd);
 244        if (ret)
 245                return ret;
 246        argv_array_clear(&cmd_args);
 247
 248        if (!names.nr && !quiet)
 249                printf("Nothing new to pack.\n");
 250
 251        /*
 252         * Ok we have prepared all new packfiles.
 253         * First see if there are packs of the same name and if so
 254         * if we can move them out of the way (this can happen if we
 255         * repacked immediately after packing fully.
 256         */
 257        failed = 0;
 258        for_each_string_list_item(item, &names) {
 259                for (ext = 0; ext < 2; ext++) {
 260                        char *fname, *fname_old;
 261                        fname = mkpathdup("%s/%s%s", packdir,
 262                                                item->string, exts[ext]);
 263                        if (!file_exists(fname)) {
 264                                free(fname);
 265                                continue;
 266                        }
 267
 268                        fname_old = mkpath("%s/old-%s%s", packdir,
 269                                                item->string, exts[ext]);
 270                        if (file_exists(fname_old))
 271                                if (unlink(fname_old))
 272                                        failed = 1;
 273
 274                        if (!failed && rename(fname, fname_old)) {
 275                                free(fname);
 276                                failed = 1;
 277                                break;
 278                        } else {
 279                                string_list_append(&rollback, fname);
 280                        }
 281                }
 282                if (failed)
 283                        break;
 284        }
 285        if (failed) {
 286                struct string_list rollback_failure = STRING_LIST_INIT_DUP;
 287                for_each_string_list_item(item, &rollback) {
 288                        char *fname, *fname_old;
 289                        fname = mkpathdup("%s/%s", packdir, item->string);
 290                        fname_old = mkpath("%s/old-%s", packdir, item->string);
 291                        if (rename(fname_old, fname))
 292                                string_list_append(&rollback_failure, fname);
 293                        free(fname);
 294                }
 295
 296                if (rollback_failure.nr) {
 297                        int i;
 298                        fprintf(stderr,
 299                                "WARNING: Some packs in use have been renamed by\n"
 300                                "WARNING: prefixing old- to their name, in order to\n"
 301                                "WARNING: replace them with the new version of the\n"
 302                                "WARNING: file.  But the operation failed, and the\n"
 303                                "WARNING: attempt to rename them back to their\n"
 304                                "WARNING: original names also failed.\n"
 305                                "WARNING: Please rename them in %s manually:\n", packdir);
 306                        for (i = 0; i < rollback_failure.nr; i++)
 307                                fprintf(stderr, "WARNING:   old-%s -> %s\n",
 308                                        rollback_failure.items[i].string,
 309                                        rollback_failure.items[i].string);
 310                }
 311                exit(1);
 312        }
 313
 314        /* Now the ones with the same name are out of the way... */
 315        for_each_string_list_item(item, &names) {
 316                for (ext = 0; ext < 2; ext++) {
 317                        char *fname, *fname_old;
 318                        struct stat statbuffer;
 319                        fname = mkpathdup("%s/pack-%s%s",
 320                                        packdir, item->string, exts[ext]);
 321                        fname_old = mkpathdup("%s-%s%s",
 322                                        packtmp, item->string, exts[ext]);
 323                        if (!stat(fname_old, &statbuffer)) {
 324                                statbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
 325                                chmod(fname_old, statbuffer.st_mode);
 326                        }
 327                        if (rename(fname_old, fname))
 328                                die_errno(_("renaming '%s' failed"), fname_old);
 329                        free(fname);
 330                        free(fname_old);
 331                }
 332        }
 333
 334        /* Remove the "old-" files */
 335        for_each_string_list_item(item, &names) {
 336                for (ext = 0; ext < 2; ext++) {
 337                        char *fname;
 338                        fname = mkpath("%s/old-pack-%s%s",
 339                                        packdir,
 340                                        item->string,
 341                                        exts[ext]);
 342                        if (remove_path(fname))
 343                                warning(_("removing '%s' failed"), fname);
 344                }
 345        }
 346
 347        /* End of pack replacement. */
 348
 349        if (delete_redundant) {
 350                sort_string_list(&names);
 351                for_each_string_list_item(item, &existing_packs) {
 352                        char *sha1;
 353                        size_t len = strlen(item->string);
 354                        if (len < 40)
 355                                continue;
 356                        sha1 = item->string + len - 40;
 357                        if (!string_list_has_string(&names, sha1))
 358                                remove_redundant_pack(packdir, item->string);
 359                }
 360                argv_array_push(&cmd_args, "prune-packed");
 361                if (quiet)
 362                        argv_array_push(&cmd_args, "--quiet");
 363
 364                memset(&cmd, 0, sizeof(cmd));
 365                cmd.argv = cmd_args.argv;
 366                cmd.git_cmd = 1;
 367                run_command(&cmd);
 368                argv_array_clear(&cmd_args);
 369        }
 370
 371        if (!no_update_server_info) {
 372                argv_array_push(&cmd_args, "update-server-info");
 373                memset(&cmd, 0, sizeof(cmd));
 374                cmd.argv = cmd_args.argv;
 375                cmd.git_cmd = 1;
 376                run_command(&cmd);
 377                argv_array_clear(&cmd_args);
 378        }
 379        remove_temporary_files();
 380        string_list_clear(&names, 0);
 381        string_list_clear(&rollback, 0);
 382        string_list_clear(&existing_packs, 0);
 383        strbuf_release(&line);
 384
 385        return 0;
 386}