builtin / update-index.con commit read-cache: mark updated entries for split index (078a58e)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "quote.h"
   8#include "cache-tree.h"
   9#include "tree-walk.h"
  10#include "builtin.h"
  11#include "refs.h"
  12#include "resolve-undo.h"
  13#include "parse-options.h"
  14#include "pathspec.h"
  15#include "dir.h"
  16
  17/*
  18 * Default to not allowing changes to the list of files. The
  19 * tool doesn't actually care, but this makes it harder to add
  20 * files to the revision control by mistake by doing something
  21 * like "git update-index *" and suddenly having all the object
  22 * files be revision controlled.
  23 */
  24static int allow_add;
  25static int allow_remove;
  26static int allow_replace;
  27static int info_only;
  28static int force_remove;
  29static int verbose;
  30static int mark_valid_only;
  31static int mark_skip_worktree_only;
  32#define MARK_FLAG 1
  33#define UNMARK_FLAG 2
  34
  35__attribute__((format (printf, 1, 2)))
  36static void report(const char *fmt, ...)
  37{
  38        va_list vp;
  39
  40        if (!verbose)
  41                return;
  42
  43        va_start(vp, fmt);
  44        vprintf(fmt, vp);
  45        putchar('\n');
  46        va_end(vp);
  47}
  48
  49static int mark_ce_flags(const char *path, int flag, int mark)
  50{
  51        int namelen = strlen(path);
  52        int pos = cache_name_pos(path, namelen);
  53        if (0 <= pos) {
  54                if (mark)
  55                        active_cache[pos]->ce_flags |= flag;
  56                else
  57                        active_cache[pos]->ce_flags &= ~flag;
  58                active_cache[pos]->ce_flags |= CE_UPDATE_IN_BASE;
  59                cache_tree_invalidate_path(&the_index, path);
  60                active_cache_changed |= CE_ENTRY_CHANGED;
  61                return 0;
  62        }
  63        return -1;
  64}
  65
  66static int remove_one_path(const char *path)
  67{
  68        if (!allow_remove)
  69                return error("%s: does not exist and --remove not passed", path);
  70        if (remove_file_from_cache(path))
  71                return error("%s: cannot remove from the index", path);
  72        return 0;
  73}
  74
  75/*
  76 * Handle a path that couldn't be lstat'ed. It's either:
  77 *  - missing file (ENOENT or ENOTDIR). That's ok if we're
  78 *    supposed to be removing it and the removal actually
  79 *    succeeds.
  80 *  - permission error. That's never ok.
  81 */
  82static int process_lstat_error(const char *path, int err)
  83{
  84        if (err == ENOENT || err == ENOTDIR)
  85                return remove_one_path(path);
  86        return error("lstat(\"%s\"): %s", path, strerror(errno));
  87}
  88
  89static int add_one_path(const struct cache_entry *old, const char *path, int len, struct stat *st)
  90{
  91        int option, size;
  92        struct cache_entry *ce;
  93
  94        /* Was the old index entry already up-to-date? */
  95        if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
  96                return 0;
  97
  98        size = cache_entry_size(len);
  99        ce = xcalloc(1, size);
 100        memcpy(ce->name, path, len);
 101        ce->ce_flags = create_ce_flags(0);
 102        ce->ce_namelen = len;
 103        fill_stat_cache_info(ce, st);
 104        ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
 105
 106        if (index_path(ce->sha1, path, st,
 107                       info_only ? 0 : HASH_WRITE_OBJECT)) {
 108                free(ce);
 109                return -1;
 110        }
 111        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 112        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 113        if (add_cache_entry(ce, option))
 114                return error("%s: cannot add to the index - missing --add option?", path);
 115        return 0;
 116}
 117
 118/*
 119 * Handle a path that was a directory. Four cases:
 120 *
 121 *  - it's already a gitlink in the index, and we keep it that
 122 *    way, and update it if we can (if we cannot find the HEAD,
 123 *    we're going to keep it unchanged in the index!)
 124 *
 125 *  - it's a *file* in the index, in which case it should be
 126 *    removed as a file if removal is allowed, since it doesn't
 127 *    exist as such any more. If removal isn't allowed, it's
 128 *    an error.
 129 *
 130 *    (NOTE! This is old and arguably fairly strange behaviour.
 131 *    We might want to make this an error unconditionally, and
 132 *    use "--force-remove" if you actually want to force removal).
 133 *
 134 *  - it used to exist as a subdirectory (ie multiple files with
 135 *    this particular prefix) in the index, in which case it's wrong
 136 *    to try to update it as a directory.
 137 *
 138 *  - it doesn't exist at all in the index, but it is a valid
 139 *    git directory, and it should be *added* as a gitlink.
 140 */
 141static int process_directory(const char *path, int len, struct stat *st)
 142{
 143        unsigned char sha1[20];
 144        int pos = cache_name_pos(path, len);
 145
 146        /* Exact match: file or existing gitlink */
 147        if (pos >= 0) {
 148                const struct cache_entry *ce = active_cache[pos];
 149                if (S_ISGITLINK(ce->ce_mode)) {
 150
 151                        /* Do nothing to the index if there is no HEAD! */
 152                        if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
 153                                return 0;
 154
 155                        return add_one_path(ce, path, len, st);
 156                }
 157                /* Should this be an unconditional error? */
 158                return remove_one_path(path);
 159        }
 160
 161        /* Inexact match: is there perhaps a subdirectory match? */
 162        pos = -pos-1;
 163        while (pos < active_nr) {
 164                const struct cache_entry *ce = active_cache[pos++];
 165
 166                if (strncmp(ce->name, path, len))
 167                        break;
 168                if (ce->name[len] > '/')
 169                        break;
 170                if (ce->name[len] < '/')
 171                        continue;
 172
 173                /* Subdirectory match - error out */
 174                return error("%s: is a directory - add individual files instead", path);
 175        }
 176
 177        /* No match - should we add it as a gitlink? */
 178        if (!resolve_gitlink_ref(path, "HEAD", sha1))
 179                return add_one_path(NULL, path, len, st);
 180
 181        /* Error out. */
 182        return error("%s: is a directory - add files inside instead", path);
 183}
 184
 185static int process_path(const char *path)
 186{
 187        int pos, len;
 188        struct stat st;
 189        const struct cache_entry *ce;
 190
 191        len = strlen(path);
 192        if (has_symlink_leading_path(path, len))
 193                return error("'%s' is beyond a symbolic link", path);
 194
 195        pos = cache_name_pos(path, len);
 196        ce = pos < 0 ? NULL : active_cache[pos];
 197        if (ce && ce_skip_worktree(ce)) {
 198                /*
 199                 * working directory version is assumed "good"
 200                 * so updating it does not make sense.
 201                 * On the other hand, removing it from index should work
 202                 */
 203                if (allow_remove && remove_file_from_cache(path))
 204                        return error("%s: cannot remove from the index", path);
 205                return 0;
 206        }
 207
 208        /*
 209         * First things first: get the stat information, to decide
 210         * what to do about the pathname!
 211         */
 212        if (lstat(path, &st) < 0)
 213                return process_lstat_error(path, errno);
 214
 215        if (S_ISDIR(st.st_mode))
 216                return process_directory(path, len, &st);
 217
 218        return add_one_path(ce, path, len, &st);
 219}
 220
 221static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
 222                         const char *path, int stage)
 223{
 224        int size, len, option;
 225        struct cache_entry *ce;
 226
 227        if (!verify_path(path))
 228                return error("Invalid path '%s'", path);
 229
 230        len = strlen(path);
 231        size = cache_entry_size(len);
 232        ce = xcalloc(1, size);
 233
 234        hashcpy(ce->sha1, sha1);
 235        memcpy(ce->name, path, len);
 236        ce->ce_flags = create_ce_flags(stage);
 237        ce->ce_namelen = len;
 238        ce->ce_mode = create_ce_mode(mode);
 239        if (assume_unchanged)
 240                ce->ce_flags |= CE_VALID;
 241        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 242        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 243        if (add_cache_entry(ce, option))
 244                return error("%s: cannot add to the index - missing --add option?",
 245                             path);
 246        report("add '%s'", path);
 247        return 0;
 248}
 249
 250static void chmod_path(int flip, const char *path)
 251{
 252        int pos;
 253        struct cache_entry *ce;
 254        unsigned int mode;
 255
 256        pos = cache_name_pos(path, strlen(path));
 257        if (pos < 0)
 258                goto fail;
 259        ce = active_cache[pos];
 260        mode = ce->ce_mode;
 261        if (!S_ISREG(mode))
 262                goto fail;
 263        switch (flip) {
 264        case '+':
 265                ce->ce_mode |= 0111; break;
 266        case '-':
 267                ce->ce_mode &= ~0111; break;
 268        default:
 269                goto fail;
 270        }
 271        cache_tree_invalidate_path(&the_index, path);
 272        ce->ce_flags |= CE_UPDATE_IN_BASE;
 273        active_cache_changed |= CE_ENTRY_CHANGED;
 274        report("chmod %cx '%s'", flip, path);
 275        return;
 276 fail:
 277        die("git update-index: cannot chmod %cx '%s'", flip, path);
 278}
 279
 280static void update_one(const char *path)
 281{
 282        if (!verify_path(path)) {
 283                fprintf(stderr, "Ignoring path %s\n", path);
 284                return;
 285        }
 286        if (mark_valid_only) {
 287                if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG))
 288                        die("Unable to mark file %s", path);
 289                return;
 290        }
 291        if (mark_skip_worktree_only) {
 292                if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
 293                        die("Unable to mark file %s", path);
 294                return;
 295        }
 296
 297        if (force_remove) {
 298                if (remove_file_from_cache(path))
 299                        die("git update-index: unable to remove %s", path);
 300                report("remove '%s'", path);
 301                return;
 302        }
 303        if (process_path(path))
 304                die("Unable to process path %s", path);
 305        report("add '%s'", path);
 306}
 307
 308static void read_index_info(int line_termination)
 309{
 310        struct strbuf buf = STRBUF_INIT;
 311        struct strbuf uq = STRBUF_INIT;
 312
 313        while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
 314                char *ptr, *tab;
 315                char *path_name;
 316                unsigned char sha1[20];
 317                unsigned int mode;
 318                unsigned long ul;
 319                int stage;
 320
 321                /* This reads lines formatted in one of three formats:
 322                 *
 323                 * (1) mode         SP sha1          TAB path
 324                 * The first format is what "git apply --index-info"
 325                 * reports, and used to reconstruct a partial tree
 326                 * that is used for phony merge base tree when falling
 327                 * back on 3-way merge.
 328                 *
 329                 * (2) mode SP type SP sha1          TAB path
 330                 * The second format is to stuff "git ls-tree" output
 331                 * into the index file.
 332                 *
 333                 * (3) mode         SP sha1 SP stage TAB path
 334                 * This format is to put higher order stages into the
 335                 * index file and matches "git ls-files --stage" output.
 336                 */
 337                errno = 0;
 338                ul = strtoul(buf.buf, &ptr, 8);
 339                if (ptr == buf.buf || *ptr != ' '
 340                    || errno || (unsigned int) ul != ul)
 341                        goto bad_line;
 342                mode = ul;
 343
 344                tab = strchr(ptr, '\t');
 345                if (!tab || tab - ptr < 41)
 346                        goto bad_line;
 347
 348                if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
 349                        stage = tab[-1] - '0';
 350                        ptr = tab + 1; /* point at the head of path */
 351                        tab = tab - 2; /* point at tail of sha1 */
 352                }
 353                else {
 354                        stage = 0;
 355                        ptr = tab + 1; /* point at the head of path */
 356                }
 357
 358                if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
 359                        goto bad_line;
 360
 361                path_name = ptr;
 362                if (line_termination && path_name[0] == '"') {
 363                        strbuf_reset(&uq);
 364                        if (unquote_c_style(&uq, path_name, NULL)) {
 365                                die("git update-index: bad quoting of path name");
 366                        }
 367                        path_name = uq.buf;
 368                }
 369
 370                if (!verify_path(path_name)) {
 371                        fprintf(stderr, "Ignoring path %s\n", path_name);
 372                        continue;
 373                }
 374
 375                if (!mode) {
 376                        /* mode == 0 means there is no such path -- remove */
 377                        if (remove_file_from_cache(path_name))
 378                                die("git update-index: unable to remove %s",
 379                                    ptr);
 380                }
 381                else {
 382                        /* mode ' ' sha1 '\t' name
 383                         * ptr[-1] points at tab,
 384                         * ptr[-41] is at the beginning of sha1
 385                         */
 386                        ptr[-42] = ptr[-1] = 0;
 387                        if (add_cacheinfo(mode, sha1, path_name, stage))
 388                                die("git update-index: unable to update %s",
 389                                    path_name);
 390                }
 391                continue;
 392
 393        bad_line:
 394                die("malformed index info %s", buf.buf);
 395        }
 396        strbuf_release(&buf);
 397        strbuf_release(&uq);
 398}
 399
 400static const char * const update_index_usage[] = {
 401        N_("git update-index [options] [--] [<file>...]"),
 402        NULL
 403};
 404
 405static unsigned char head_sha1[20];
 406static unsigned char merge_head_sha1[20];
 407
 408static struct cache_entry *read_one_ent(const char *which,
 409                                        unsigned char *ent, const char *path,
 410                                        int namelen, int stage)
 411{
 412        unsigned mode;
 413        unsigned char sha1[20];
 414        int size;
 415        struct cache_entry *ce;
 416
 417        if (get_tree_entry(ent, path, sha1, &mode)) {
 418                if (which)
 419                        error("%s: not in %s branch.", path, which);
 420                return NULL;
 421        }
 422        if (mode == S_IFDIR) {
 423                if (which)
 424                        error("%s: not a blob in %s branch.", path, which);
 425                return NULL;
 426        }
 427        size = cache_entry_size(namelen);
 428        ce = xcalloc(1, size);
 429
 430        hashcpy(ce->sha1, sha1);
 431        memcpy(ce->name, path, namelen);
 432        ce->ce_flags = create_ce_flags(stage);
 433        ce->ce_namelen = namelen;
 434        ce->ce_mode = create_ce_mode(mode);
 435        return ce;
 436}
 437
 438static int unresolve_one(const char *path)
 439{
 440        int namelen = strlen(path);
 441        int pos;
 442        int ret = 0;
 443        struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
 444
 445        /* See if there is such entry in the index. */
 446        pos = cache_name_pos(path, namelen);
 447        if (0 <= pos) {
 448                /* already merged */
 449                pos = unmerge_cache_entry_at(pos);
 450                if (pos < active_nr) {
 451                        const struct cache_entry *ce = active_cache[pos];
 452                        if (ce_stage(ce) &&
 453                            ce_namelen(ce) == namelen &&
 454                            !memcmp(ce->name, path, namelen))
 455                                return 0;
 456                }
 457                /* no resolve-undo information; fall back */
 458        } else {
 459                /* If there isn't, either it is unmerged, or
 460                 * resolved as "removed" by mistake.  We do not
 461                 * want to do anything in the former case.
 462                 */
 463                pos = -pos-1;
 464                if (pos < active_nr) {
 465                        const struct cache_entry *ce = active_cache[pos];
 466                        if (ce_namelen(ce) == namelen &&
 467                            !memcmp(ce->name, path, namelen)) {
 468                                fprintf(stderr,
 469                                        "%s: skipping still unmerged path.\n",
 470                                        path);
 471                                goto free_return;
 472                        }
 473                }
 474        }
 475
 476        /* Grab blobs from given path from HEAD and MERGE_HEAD,
 477         * stuff HEAD version in stage #2,
 478         * stuff MERGE_HEAD version in stage #3.
 479         */
 480        ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
 481        ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
 482
 483        if (!ce_2 || !ce_3) {
 484                ret = -1;
 485                goto free_return;
 486        }
 487        if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
 488            ce_2->ce_mode == ce_3->ce_mode) {
 489                fprintf(stderr, "%s: identical in both, skipping.\n",
 490                        path);
 491                goto free_return;
 492        }
 493
 494        remove_file_from_cache(path);
 495        if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
 496                error("%s: cannot add our version to the index.", path);
 497                ret = -1;
 498                goto free_return;
 499        }
 500        if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
 501                return 0;
 502        error("%s: cannot add their version to the index.", path);
 503        ret = -1;
 504 free_return:
 505        free(ce_2);
 506        free(ce_3);
 507        return ret;
 508}
 509
 510static void read_head_pointers(void)
 511{
 512        if (read_ref("HEAD", head_sha1))
 513                die("No HEAD -- no initial commit yet?");
 514        if (read_ref("MERGE_HEAD", merge_head_sha1)) {
 515                fprintf(stderr, "Not in the middle of a merge.\n");
 516                exit(0);
 517        }
 518}
 519
 520static int do_unresolve(int ac, const char **av,
 521                        const char *prefix, int prefix_length)
 522{
 523        int i;
 524        int err = 0;
 525
 526        /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
 527         * are not doing a merge, so exit with success status.
 528         */
 529        read_head_pointers();
 530
 531        for (i = 1; i < ac; i++) {
 532                const char *arg = av[i];
 533                const char *p = prefix_path(prefix, prefix_length, arg);
 534                err |= unresolve_one(p);
 535                if (p < arg || p > arg + strlen(arg))
 536                        free((char *)p);
 537        }
 538        return err;
 539}
 540
 541static int do_reupdate(int ac, const char **av,
 542                       const char *prefix, int prefix_length)
 543{
 544        /* Read HEAD and run update-index on paths that are
 545         * merged and already different between index and HEAD.
 546         */
 547        int pos;
 548        int has_head = 1;
 549        struct pathspec pathspec;
 550
 551        parse_pathspec(&pathspec, 0,
 552                       PATHSPEC_PREFER_CWD,
 553                       prefix, av + 1);
 554
 555        if (read_ref("HEAD", head_sha1))
 556                /* If there is no HEAD, that means it is an initial
 557                 * commit.  Update everything in the index.
 558                 */
 559                has_head = 0;
 560 redo:
 561        for (pos = 0; pos < active_nr; pos++) {
 562                const struct cache_entry *ce = active_cache[pos];
 563                struct cache_entry *old = NULL;
 564                int save_nr;
 565                char *path;
 566
 567                if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
 568                        continue;
 569                if (has_head)
 570                        old = read_one_ent(NULL, head_sha1,
 571                                           ce->name, ce_namelen(ce), 0);
 572                if (old && ce->ce_mode == old->ce_mode &&
 573                    !hashcmp(ce->sha1, old->sha1)) {
 574                        free(old);
 575                        continue; /* unchanged */
 576                }
 577                /* Be careful.  The working tree may not have the
 578                 * path anymore, in which case, under 'allow_remove',
 579                 * or worse yet 'allow_replace', active_nr may decrease.
 580                 */
 581                save_nr = active_nr;
 582                path = xstrdup(ce->name);
 583                update_one(path);
 584                free(path);
 585                if (save_nr != active_nr)
 586                        goto redo;
 587        }
 588        free_pathspec(&pathspec);
 589        return 0;
 590}
 591
 592struct refresh_params {
 593        unsigned int flags;
 594        int *has_errors;
 595};
 596
 597static int refresh(struct refresh_params *o, unsigned int flag)
 598{
 599        setup_work_tree();
 600        read_cache_preload(NULL);
 601        *o->has_errors |= refresh_cache(o->flags | flag);
 602        return 0;
 603}
 604
 605static int refresh_callback(const struct option *opt,
 606                                const char *arg, int unset)
 607{
 608        return refresh(opt->value, 0);
 609}
 610
 611static int really_refresh_callback(const struct option *opt,
 612                                const char *arg, int unset)
 613{
 614        return refresh(opt->value, REFRESH_REALLY);
 615}
 616
 617static int chmod_callback(const struct option *opt,
 618                                const char *arg, int unset)
 619{
 620        char *flip = opt->value;
 621        if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
 622                return error("option 'chmod' expects \"+x\" or \"-x\"");
 623        *flip = arg[0];
 624        return 0;
 625}
 626
 627static int resolve_undo_clear_callback(const struct option *opt,
 628                                const char *arg, int unset)
 629{
 630        resolve_undo_clear();
 631        return 0;
 632}
 633
 634static int parse_new_style_cacheinfo(const char *arg,
 635                                     unsigned int *mode,
 636                                     unsigned char sha1[],
 637                                     const char **path)
 638{
 639        unsigned long ul;
 640        char *endp;
 641
 642        errno = 0;
 643        ul = strtoul(arg, &endp, 8);
 644        if (errno || endp == arg || *endp != ',' || (unsigned int) ul != ul)
 645                return -1; /* not a new-style cacheinfo */
 646        *mode = ul;
 647        endp++;
 648        if (get_sha1_hex(endp, sha1) || endp[40] != ',')
 649                return -1;
 650        *path = endp + 41;
 651        return 0;
 652}
 653
 654static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
 655                                const struct option *opt, int unset)
 656{
 657        unsigned char sha1[20];
 658        unsigned int mode;
 659        const char *path;
 660
 661        if (!parse_new_style_cacheinfo(ctx->argv[1], &mode, sha1, &path)) {
 662                if (add_cacheinfo(mode, sha1, path, 0))
 663                        die("git update-index: --cacheinfo cannot add %s", path);
 664                ctx->argv++;
 665                ctx->argc--;
 666                return 0;
 667        }
 668        if (ctx->argc <= 3)
 669                return error("option 'cacheinfo' expects <mode>,<sha1>,<path>");
 670        if (strtoul_ui(*++ctx->argv, 8, &mode) ||
 671            get_sha1_hex(*++ctx->argv, sha1) ||
 672            add_cacheinfo(mode, sha1, *++ctx->argv, 0))
 673                die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
 674        ctx->argc -= 3;
 675        return 0;
 676}
 677
 678static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
 679                              const struct option *opt, int unset)
 680{
 681        int *line_termination = opt->value;
 682
 683        if (ctx->argc != 1)
 684                return error("option '%s' must be the last argument", opt->long_name);
 685        allow_add = allow_replace = allow_remove = 1;
 686        read_index_info(*line_termination);
 687        return 0;
 688}
 689
 690static int stdin_callback(struct parse_opt_ctx_t *ctx,
 691                                const struct option *opt, int unset)
 692{
 693        int *read_from_stdin = opt->value;
 694
 695        if (ctx->argc != 1)
 696                return error("option '%s' must be the last argument", opt->long_name);
 697        *read_from_stdin = 1;
 698        return 0;
 699}
 700
 701static int unresolve_callback(struct parse_opt_ctx_t *ctx,
 702                                const struct option *opt, int flags)
 703{
 704        int *has_errors = opt->value;
 705        const char *prefix = startup_info->prefix;
 706
 707        /* consume remaining arguments. */
 708        *has_errors = do_unresolve(ctx->argc, ctx->argv,
 709                                prefix, prefix ? strlen(prefix) : 0);
 710        if (*has_errors)
 711                active_cache_changed = 0;
 712
 713        ctx->argv += ctx->argc - 1;
 714        ctx->argc = 1;
 715        return 0;
 716}
 717
 718static int reupdate_callback(struct parse_opt_ctx_t *ctx,
 719                                const struct option *opt, int flags)
 720{
 721        int *has_errors = opt->value;
 722        const char *prefix = startup_info->prefix;
 723
 724        /* consume remaining arguments. */
 725        setup_work_tree();
 726        *has_errors = do_reupdate(ctx->argc, ctx->argv,
 727                                prefix, prefix ? strlen(prefix) : 0);
 728        if (*has_errors)
 729                active_cache_changed = 0;
 730
 731        ctx->argv += ctx->argc - 1;
 732        ctx->argc = 1;
 733        return 0;
 734}
 735
 736int cmd_update_index(int argc, const char **argv, const char *prefix)
 737{
 738        int newfd, entries, has_errors = 0, line_termination = '\n';
 739        int read_from_stdin = 0;
 740        int prefix_length = prefix ? strlen(prefix) : 0;
 741        int preferred_index_format = 0;
 742        char set_executable_bit = 0;
 743        struct refresh_params refresh_args = {0, &has_errors};
 744        int lock_error = 0;
 745        struct lock_file *lock_file;
 746        struct parse_opt_ctx_t ctx;
 747        int parseopt_state = PARSE_OPT_UNKNOWN;
 748        struct option options[] = {
 749                OPT_BIT('q', NULL, &refresh_args.flags,
 750                        N_("continue refresh even when index needs update"),
 751                        REFRESH_QUIET),
 752                OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
 753                        N_("refresh: ignore submodules"),
 754                        REFRESH_IGNORE_SUBMODULES),
 755                OPT_SET_INT(0, "add", &allow_add,
 756                        N_("do not ignore new files"), 1),
 757                OPT_SET_INT(0, "replace", &allow_replace,
 758                        N_("let files replace directories and vice-versa"), 1),
 759                OPT_SET_INT(0, "remove", &allow_remove,
 760                        N_("notice files missing from worktree"), 1),
 761                OPT_BIT(0, "unmerged", &refresh_args.flags,
 762                        N_("refresh even if index contains unmerged entries"),
 763                        REFRESH_UNMERGED),
 764                {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,
 765                        N_("refresh stat information"),
 766                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 767                        refresh_callback},
 768                {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL,
 769                        N_("like --refresh, but ignore assume-unchanged setting"),
 770                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 771                        really_refresh_callback},
 772                {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
 773                        N_("<mode>,<object>,<path>"),
 774                        N_("add the specified entry to the index"),
 775                        PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */
 776                        PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
 777                        (parse_opt_cb *) cacheinfo_callback},
 778                {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, N_("(+/-)x"),
 779                        N_("override the executable bit of the listed files"),
 780                        PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
 781                        chmod_callback},
 782                {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
 783                        N_("mark files as \"not changing\""),
 784                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
 785                {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
 786                        N_("clear assumed-unchanged bit"),
 787                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
 788                {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
 789                        N_("mark files as \"index-only\""),
 790                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
 791                {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
 792                        N_("clear skip-worktree bit"),
 793                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
 794                OPT_SET_INT(0, "info-only", &info_only,
 795                        N_("add to index only; do not add content to object database"), 1),
 796                OPT_SET_INT(0, "force-remove", &force_remove,
 797                        N_("remove named paths even if present in worktree"), 1),
 798                OPT_SET_INT('z', NULL, &line_termination,
 799                        N_("with --stdin: input lines are terminated by null bytes"), '\0'),
 800                {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
 801                        N_("read list of paths to be updated from standard input"),
 802                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 803                        (parse_opt_cb *) stdin_callback},
 804                {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
 805                        N_("add entries from standard input to the index"),
 806                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 807                        (parse_opt_cb *) stdin_cacheinfo_callback},
 808                {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
 809                        N_("repopulate stages #2 and #3 for the listed paths"),
 810                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 811                        (parse_opt_cb *) unresolve_callback},
 812                {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
 813                        N_("only update entries that differ from HEAD"),
 814                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 815                        (parse_opt_cb *) reupdate_callback},
 816                OPT_BIT(0, "ignore-missing", &refresh_args.flags,
 817                        N_("ignore files missing from worktree"),
 818                        REFRESH_IGNORE_MISSING),
 819                OPT_SET_INT(0, "verbose", &verbose,
 820                        N_("report actions to standard output"), 1),
 821                {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL,
 822                        N_("(for porcelains) forget saved unresolved conflicts"),
 823                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 824                        resolve_undo_clear_callback},
 825                OPT_INTEGER(0, "index-version", &preferred_index_format,
 826                        N_("write index in this format")),
 827                OPT_END()
 828        };
 829
 830        if (argc == 2 && !strcmp(argv[1], "-h"))
 831                usage_with_options(update_index_usage, options);
 832
 833        git_config(git_default_config, NULL);
 834
 835        /* We can't free this memory, it becomes part of a linked list parsed atexit() */
 836        lock_file = xcalloc(1, sizeof(struct lock_file));
 837
 838        newfd = hold_locked_index(lock_file, 0);
 839        if (newfd < 0)
 840                lock_error = errno;
 841
 842        entries = read_cache();
 843        if (entries < 0)
 844                die("cache corrupted");
 845
 846        /*
 847         * Custom copy of parse_options() because we want to handle
 848         * filename arguments as they come.
 849         */
 850        parse_options_start(&ctx, argc, argv, prefix,
 851                            options, PARSE_OPT_STOP_AT_NON_OPTION);
 852        while (ctx.argc) {
 853                if (parseopt_state != PARSE_OPT_DONE)
 854                        parseopt_state = parse_options_step(&ctx, options,
 855                                                            update_index_usage);
 856                if (!ctx.argc)
 857                        break;
 858                switch (parseopt_state) {
 859                case PARSE_OPT_HELP:
 860                        exit(129);
 861                case PARSE_OPT_NON_OPTION:
 862                case PARSE_OPT_DONE:
 863                {
 864                        const char *path = ctx.argv[0];
 865                        const char *p;
 866
 867                        setup_work_tree();
 868                        p = prefix_path(prefix, prefix_length, path);
 869                        update_one(p);
 870                        if (set_executable_bit)
 871                                chmod_path(set_executable_bit, p);
 872                        free((char *)p);
 873                        ctx.argc--;
 874                        ctx.argv++;
 875                        break;
 876                }
 877                case PARSE_OPT_UNKNOWN:
 878                        if (ctx.argv[0][1] == '-')
 879                                error("unknown option '%s'", ctx.argv[0] + 2);
 880                        else
 881                                error("unknown switch '%c'", *ctx.opt);
 882                        usage_with_options(update_index_usage, options);
 883                }
 884        }
 885        argc = parse_options_end(&ctx);
 886        if (preferred_index_format) {
 887                if (preferred_index_format < INDEX_FORMAT_LB ||
 888                    INDEX_FORMAT_UB < preferred_index_format)
 889                        die("index-version %d not in range: %d..%d",
 890                            preferred_index_format,
 891                            INDEX_FORMAT_LB, INDEX_FORMAT_UB);
 892
 893                if (the_index.version != preferred_index_format)
 894                        active_cache_changed |= SOMETHING_CHANGED;
 895                the_index.version = preferred_index_format;
 896        }
 897
 898        if (read_from_stdin) {
 899                struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
 900
 901                setup_work_tree();
 902                while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
 903                        const char *p;
 904                        if (line_termination && buf.buf[0] == '"') {
 905                                strbuf_reset(&nbuf);
 906                                if (unquote_c_style(&nbuf, buf.buf, NULL))
 907                                        die("line is badly quoted");
 908                                strbuf_swap(&buf, &nbuf);
 909                        }
 910                        p = prefix_path(prefix, prefix_length, buf.buf);
 911                        update_one(p);
 912                        if (set_executable_bit)
 913                                chmod_path(set_executable_bit, p);
 914                        free((char *)p);
 915                }
 916                strbuf_release(&nbuf);
 917                strbuf_release(&buf);
 918        }
 919
 920        if (active_cache_changed) {
 921                if (newfd < 0) {
 922                        if (refresh_args.flags & REFRESH_QUIET)
 923                                exit(128);
 924                        unable_to_lock_index_die(get_index_file(), lock_error);
 925                }
 926                if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
 927                        die("Unable to write new index file");
 928        }
 929
 930        rollback_lock_file(lock_file);
 931
 932        return has_errors ? 1 : 0;
 933}