5e97d0949716e4d25d4a9c42813db8a02db53eae
   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
  13/*
  14 * Default to not allowing changes to the list of files. The
  15 * tool doesn't actually care, but this makes it harder to add
  16 * files to the revision control by mistake by doing something
  17 * like "git update-index *" and suddenly having all the object
  18 * files be revision controlled.
  19 */
  20static int allow_add;
  21static int allow_remove;
  22static int allow_replace;
  23static int info_only;
  24static int force_remove;
  25static int verbose;
  26static int mark_valid_only;
  27static int mark_skip_worktree_only;
  28#define MARK_FLAG 1
  29#define UNMARK_FLAG 2
  30
  31static void report(const char *fmt, ...)
  32{
  33        va_list vp;
  34
  35        if (!verbose)
  36                return;
  37
  38        va_start(vp, fmt);
  39        vprintf(fmt, vp);
  40        putchar('\n');
  41        va_end(vp);
  42}
  43
  44static int mark_ce_flags(const char *path, int flag, int mark)
  45{
  46        int namelen = strlen(path);
  47        int pos = cache_name_pos(path, namelen);
  48        if (0 <= pos) {
  49                if (mark)
  50                        active_cache[pos]->ce_flags |= flag;
  51                else
  52                        active_cache[pos]->ce_flags &= ~flag;
  53                cache_tree_invalidate_path(active_cache_tree, path);
  54                active_cache_changed = 1;
  55                return 0;
  56        }
  57        return -1;
  58}
  59
  60static int remove_one_path(const char *path)
  61{
  62        if (!allow_remove)
  63                return error("%s: does not exist and --remove not passed", path);
  64        if (remove_file_from_cache(path))
  65                return error("%s: cannot remove from the index", path);
  66        return 0;
  67}
  68
  69/*
  70 * Handle a path that couldn't be lstat'ed. It's either:
  71 *  - missing file (ENOENT or ENOTDIR). That's ok if we're
  72 *    supposed to be removing it and the removal actually
  73 *    succeeds.
  74 *  - permission error. That's never ok.
  75 */
  76static int process_lstat_error(const char *path, int err)
  77{
  78        if (err == ENOENT || err == ENOTDIR)
  79                return remove_one_path(path);
  80        return error("lstat(\"%s\"): %s", path, strerror(errno));
  81}
  82
  83static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
  84{
  85        int option, size;
  86        struct cache_entry *ce;
  87
  88        /* Was the old index entry already up-to-date? */
  89        if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
  90                return 0;
  91
  92        size = cache_entry_size(len);
  93        ce = xcalloc(1, size);
  94        memcpy(ce->name, path, len);
  95        ce->ce_flags = len;
  96        fill_stat_cache_info(ce, st);
  97        ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
  98
  99        if (index_path(ce->sha1, path, st, !info_only))
 100                return -1;
 101        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 102        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 103        if (add_cache_entry(ce, option))
 104                return error("%s: cannot add to the index - missing --add option?", path);
 105        return 0;
 106}
 107
 108/*
 109 * Handle a path that was a directory. Four cases:
 110 *
 111 *  - it's already a gitlink in the index, and we keep it that
 112 *    way, and update it if we can (if we cannot find the HEAD,
 113 *    we're going to keep it unchanged in the index!)
 114 *
 115 *  - it's a *file* in the index, in which case it should be
 116 *    removed as a file if removal is allowed, since it doesn't
 117 *    exist as such any more. If removal isn't allowed, it's
 118 *    an error.
 119 *
 120 *    (NOTE! This is old and arguably fairly strange behaviour.
 121 *    We might want to make this an error unconditionally, and
 122 *    use "--force-remove" if you actually want to force removal).
 123 *
 124 *  - it used to exist as a subdirectory (ie multiple files with
 125 *    this particular prefix) in the index, in which case it's wrong
 126 *    to try to update it as a directory.
 127 *
 128 *  - it doesn't exist at all in the index, but it is a valid
 129 *    git directory, and it should be *added* as a gitlink.
 130 */
 131static int process_directory(const char *path, int len, struct stat *st)
 132{
 133        unsigned char sha1[20];
 134        int pos = cache_name_pos(path, len);
 135
 136        /* Exact match: file or existing gitlink */
 137        if (pos >= 0) {
 138                struct cache_entry *ce = active_cache[pos];
 139                if (S_ISGITLINK(ce->ce_mode)) {
 140
 141                        /* Do nothing to the index if there is no HEAD! */
 142                        if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
 143                                return 0;
 144
 145                        return add_one_path(ce, path, len, st);
 146                }
 147                /* Should this be an unconditional error? */
 148                return remove_one_path(path);
 149        }
 150
 151        /* Inexact match: is there perhaps a subdirectory match? */
 152        pos = -pos-1;
 153        while (pos < active_nr) {
 154                struct cache_entry *ce = active_cache[pos++];
 155
 156                if (strncmp(ce->name, path, len))
 157                        break;
 158                if (ce->name[len] > '/')
 159                        break;
 160                if (ce->name[len] < '/')
 161                        continue;
 162
 163                /* Subdirectory match - error out */
 164                return error("%s: is a directory - add individual files instead", path);
 165        }
 166
 167        /* No match - should we add it as a gitlink? */
 168        if (!resolve_gitlink_ref(path, "HEAD", sha1))
 169                return add_one_path(NULL, path, len, st);
 170
 171        /* Error out. */
 172        return error("%s: is a directory - add files inside instead", path);
 173}
 174
 175/*
 176 * Process a regular file
 177 */
 178static int process_file(const char *path, int len, struct stat *st)
 179{
 180        int pos = cache_name_pos(path, len);
 181        struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
 182
 183        if (ce && S_ISGITLINK(ce->ce_mode))
 184                return error("%s is already a gitlink, not replacing", path);
 185
 186        return add_one_path(ce, path, len, st);
 187}
 188
 189static int process_path(const char *path)
 190{
 191        int len;
 192        struct stat st;
 193
 194        len = strlen(path);
 195        if (has_symlink_leading_path(path, len))
 196                return error("'%s' is beyond a symbolic link", path);
 197
 198        /*
 199         * First things first: get the stat information, to decide
 200         * what to do about the pathname!
 201         */
 202        if (lstat(path, &st) < 0)
 203                return process_lstat_error(path, errno);
 204
 205        if (S_ISDIR(st.st_mode))
 206                return process_directory(path, len, &st);
 207
 208        return process_file(path, len, &st);
 209}
 210
 211static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
 212                         const char *path, int stage)
 213{
 214        int size, len, option;
 215        struct cache_entry *ce;
 216
 217        if (!verify_path(path))
 218                return error("Invalid path '%s'", path);
 219
 220        len = strlen(path);
 221        size = cache_entry_size(len);
 222        ce = xcalloc(1, size);
 223
 224        hashcpy(ce->sha1, sha1);
 225        memcpy(ce->name, path, len);
 226        ce->ce_flags = create_ce_flags(len, stage);
 227        ce->ce_mode = create_ce_mode(mode);
 228        if (assume_unchanged)
 229                ce->ce_flags |= CE_VALID;
 230        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 231        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 232        if (add_cache_entry(ce, option))
 233                return error("%s: cannot add to the index - missing --add option?",
 234                             path);
 235        report("add '%s'", path);
 236        return 0;
 237}
 238
 239static void chmod_path(int flip, const char *path)
 240{
 241        int pos;
 242        struct cache_entry *ce;
 243        unsigned int mode;
 244
 245        pos = cache_name_pos(path, strlen(path));
 246        if (pos < 0)
 247                goto fail;
 248        ce = active_cache[pos];
 249        mode = ce->ce_mode;
 250        if (!S_ISREG(mode))
 251                goto fail;
 252        switch (flip) {
 253        case '+':
 254                ce->ce_mode |= 0111; break;
 255        case '-':
 256                ce->ce_mode &= ~0111; break;
 257        default:
 258                goto fail;
 259        }
 260        cache_tree_invalidate_path(active_cache_tree, path);
 261        active_cache_changed = 1;
 262        report("chmod %cx '%s'", flip, path);
 263        return;
 264 fail:
 265        die("git update-index: cannot chmod %cx '%s'", flip, path);
 266}
 267
 268static void update_one(const char *path, const char *prefix, int prefix_length)
 269{
 270        const char *p = prefix_path(prefix, prefix_length, path);
 271        if (!verify_path(p)) {
 272                fprintf(stderr, "Ignoring path %s\n", path);
 273                goto free_return;
 274        }
 275        if (mark_valid_only) {
 276                if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG))
 277                        die("Unable to mark file %s", path);
 278                goto free_return;
 279        }
 280        if (mark_skip_worktree_only) {
 281                if (mark_ce_flags(p, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
 282                        die("Unable to mark file %s", path);
 283                goto free_return;
 284        }
 285
 286        if (force_remove) {
 287                if (remove_file_from_cache(p))
 288                        die("git update-index: unable to remove %s", path);
 289                report("remove '%s'", path);
 290                goto free_return;
 291        }
 292        if (process_path(p))
 293                die("Unable to process path %s", path);
 294        report("add '%s'", path);
 295 free_return:
 296        if (p < path || p > path + strlen(path))
 297                free((char *)p);
 298}
 299
 300static void read_index_info(int line_termination)
 301{
 302        struct strbuf buf = STRBUF_INIT;
 303        struct strbuf uq = STRBUF_INIT;
 304
 305        while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
 306                char *ptr, *tab;
 307                char *path_name;
 308                unsigned char sha1[20];
 309                unsigned int mode;
 310                unsigned long ul;
 311                int stage;
 312
 313                /* This reads lines formatted in one of three formats:
 314                 *
 315                 * (1) mode         SP sha1          TAB path
 316                 * The first format is what "git apply --index-info"
 317                 * reports, and used to reconstruct a partial tree
 318                 * that is used for phony merge base tree when falling
 319                 * back on 3-way merge.
 320                 *
 321                 * (2) mode SP type SP sha1          TAB path
 322                 * The second format is to stuff "git ls-tree" output
 323                 * into the index file.
 324                 *
 325                 * (3) mode         SP sha1 SP stage TAB path
 326                 * This format is to put higher order stages into the
 327                 * index file and matches "git ls-files --stage" output.
 328                 */
 329                errno = 0;
 330                ul = strtoul(buf.buf, &ptr, 8);
 331                if (ptr == buf.buf || *ptr != ' '
 332                    || errno || (unsigned int) ul != ul)
 333                        goto bad_line;
 334                mode = ul;
 335
 336                tab = strchr(ptr, '\t');
 337                if (!tab || tab - ptr < 41)
 338                        goto bad_line;
 339
 340                if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
 341                        stage = tab[-1] - '0';
 342                        ptr = tab + 1; /* point at the head of path */
 343                        tab = tab - 2; /* point at tail of sha1 */
 344                }
 345                else {
 346                        stage = 0;
 347                        ptr = tab + 1; /* point at the head of path */
 348                }
 349
 350                if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
 351                        goto bad_line;
 352
 353                path_name = ptr;
 354                if (line_termination && path_name[0] == '"') {
 355                        strbuf_reset(&uq);
 356                        if (unquote_c_style(&uq, path_name, NULL)) {
 357                                die("git update-index: bad quoting of path name");
 358                        }
 359                        path_name = uq.buf;
 360                }
 361
 362                if (!verify_path(path_name)) {
 363                        fprintf(stderr, "Ignoring path %s\n", path_name);
 364                        continue;
 365                }
 366
 367                if (!mode) {
 368                        /* mode == 0 means there is no such path -- remove */
 369                        if (remove_file_from_cache(path_name))
 370                                die("git update-index: unable to remove %s",
 371                                    ptr);
 372                }
 373                else {
 374                        /* mode ' ' sha1 '\t' name
 375                         * ptr[-1] points at tab,
 376                         * ptr[-41] is at the beginning of sha1
 377                         */
 378                        ptr[-42] = ptr[-1] = 0;
 379                        if (add_cacheinfo(mode, sha1, path_name, stage))
 380                                die("git update-index: unable to update %s",
 381                                    path_name);
 382                }
 383                continue;
 384
 385        bad_line:
 386                die("malformed index info %s", buf.buf);
 387        }
 388        strbuf_release(&buf);
 389        strbuf_release(&uq);
 390}
 391
 392static const char update_index_usage[] =
 393"git update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--skip-worktree|--no-skip-worktree] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
 394
 395static unsigned char head_sha1[20];
 396static unsigned char merge_head_sha1[20];
 397
 398static struct cache_entry *read_one_ent(const char *which,
 399                                        unsigned char *ent, const char *path,
 400                                        int namelen, int stage)
 401{
 402        unsigned mode;
 403        unsigned char sha1[20];
 404        int size;
 405        struct cache_entry *ce;
 406
 407        if (get_tree_entry(ent, path, sha1, &mode)) {
 408                if (which)
 409                        error("%s: not in %s branch.", path, which);
 410                return NULL;
 411        }
 412        if (mode == S_IFDIR) {
 413                if (which)
 414                        error("%s: not a blob in %s branch.", path, which);
 415                return NULL;
 416        }
 417        size = cache_entry_size(namelen);
 418        ce = xcalloc(1, size);
 419
 420        hashcpy(ce->sha1, sha1);
 421        memcpy(ce->name, path, namelen);
 422        ce->ce_flags = create_ce_flags(namelen, stage);
 423        ce->ce_mode = create_ce_mode(mode);
 424        return ce;
 425}
 426
 427static int unresolve_one(const char *path)
 428{
 429        int namelen = strlen(path);
 430        int pos;
 431        int ret = 0;
 432        struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
 433
 434        /* See if there is such entry in the index. */
 435        pos = cache_name_pos(path, namelen);
 436        if (pos < 0) {
 437                /* If there isn't, either it is unmerged, or
 438                 * resolved as "removed" by mistake.  We do not
 439                 * want to do anything in the former case.
 440                 */
 441                pos = -pos-1;
 442                if (pos < active_nr) {
 443                        struct cache_entry *ce = active_cache[pos];
 444                        if (ce_namelen(ce) == namelen &&
 445                            !memcmp(ce->name, path, namelen)) {
 446                                fprintf(stderr,
 447                                        "%s: skipping still unmerged path.\n",
 448                                        path);
 449                                goto free_return;
 450                        }
 451                }
 452        }
 453
 454        /* Grab blobs from given path from HEAD and MERGE_HEAD,
 455         * stuff HEAD version in stage #2,
 456         * stuff MERGE_HEAD version in stage #3.
 457         */
 458        ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
 459        ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
 460
 461        if (!ce_2 || !ce_3) {
 462                ret = -1;
 463                goto free_return;
 464        }
 465        if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
 466            ce_2->ce_mode == ce_3->ce_mode) {
 467                fprintf(stderr, "%s: identical in both, skipping.\n",
 468                        path);
 469                goto free_return;
 470        }
 471
 472        remove_file_from_cache(path);
 473        if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
 474                error("%s: cannot add our version to the index.", path);
 475                ret = -1;
 476                goto free_return;
 477        }
 478        if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
 479                return 0;
 480        error("%s: cannot add their version to the index.", path);
 481        ret = -1;
 482 free_return:
 483        free(ce_2);
 484        free(ce_3);
 485        return ret;
 486}
 487
 488static void read_head_pointers(void)
 489{
 490        if (read_ref("HEAD", head_sha1))
 491                die("No HEAD -- no initial commit yet?");
 492        if (read_ref("MERGE_HEAD", merge_head_sha1)) {
 493                fprintf(stderr, "Not in the middle of a merge.\n");
 494                exit(0);
 495        }
 496}
 497
 498static int do_unresolve(int ac, const char **av,
 499                        const char *prefix, int prefix_length)
 500{
 501        int i;
 502        int err = 0;
 503
 504        /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
 505         * are not doing a merge, so exit with success status.
 506         */
 507        read_head_pointers();
 508
 509        for (i = 1; i < ac; i++) {
 510                const char *arg = av[i];
 511                const char *p = prefix_path(prefix, prefix_length, arg);
 512                err |= unresolve_one(p);
 513                if (p < arg || p > arg + strlen(arg))
 514                        free((char *)p);
 515        }
 516        return err;
 517}
 518
 519static int do_reupdate(int ac, const char **av,
 520                       const char *prefix, int prefix_length)
 521{
 522        /* Read HEAD and run update-index on paths that are
 523         * merged and already different between index and HEAD.
 524         */
 525        int pos;
 526        int has_head = 1;
 527        const char **pathspec = get_pathspec(prefix, av + 1);
 528
 529        if (read_ref("HEAD", head_sha1))
 530                /* If there is no HEAD, that means it is an initial
 531                 * commit.  Update everything in the index.
 532                 */
 533                has_head = 0;
 534 redo:
 535        for (pos = 0; pos < active_nr; pos++) {
 536                struct cache_entry *ce = active_cache[pos];
 537                struct cache_entry *old = NULL;
 538                int save_nr;
 539
 540                if (ce_stage(ce) || !ce_path_match(ce, pathspec))
 541                        continue;
 542                if (has_head)
 543                        old = read_one_ent(NULL, head_sha1,
 544                                           ce->name, ce_namelen(ce), 0);
 545                if (old && ce->ce_mode == old->ce_mode &&
 546                    !hashcmp(ce->sha1, old->sha1)) {
 547                        free(old);
 548                        continue; /* unchanged */
 549                }
 550                /* Be careful.  The working tree may not have the
 551                 * path anymore, in which case, under 'allow_remove',
 552                 * or worse yet 'allow_replace', active_nr may decrease.
 553                 */
 554                save_nr = active_nr;
 555                update_one(ce->name + prefix_length, prefix, prefix_length);
 556                if (save_nr != active_nr)
 557                        goto redo;
 558        }
 559        return 0;
 560}
 561
 562int cmd_update_index(int argc, const char **argv, const char *prefix)
 563{
 564        int i, newfd, entries, has_errors = 0, line_termination = '\n';
 565        int allow_options = 1;
 566        int read_from_stdin = 0;
 567        int prefix_length = prefix ? strlen(prefix) : 0;
 568        char set_executable_bit = 0;
 569        unsigned int refresh_flags = 0;
 570        int lock_error = 0;
 571        struct lock_file *lock_file;
 572
 573        git_config(git_default_config, NULL);
 574
 575        /* We can't free this memory, it becomes part of a linked list parsed atexit() */
 576        lock_file = xcalloc(1, sizeof(struct lock_file));
 577
 578        newfd = hold_locked_index(lock_file, 0);
 579        if (newfd < 0)
 580                lock_error = errno;
 581
 582        entries = read_cache();
 583        if (entries < 0)
 584                die("cache corrupted");
 585
 586        for (i = 1 ; i < argc; i++) {
 587                const char *path = argv[i];
 588                const char *p;
 589
 590                if (allow_options && *path == '-') {
 591                        if (!strcmp(path, "--")) {
 592                                allow_options = 0;
 593                                continue;
 594                        }
 595                        if (!strcmp(path, "-q")) {
 596                                refresh_flags |= REFRESH_QUIET;
 597                                continue;
 598                        }
 599                        if (!strcmp(path, "--ignore-submodules")) {
 600                                refresh_flags |= REFRESH_IGNORE_SUBMODULES;
 601                                continue;
 602                        }
 603                        if (!strcmp(path, "--add")) {
 604                                allow_add = 1;
 605                                continue;
 606                        }
 607                        if (!strcmp(path, "--replace")) {
 608                                allow_replace = 1;
 609                                continue;
 610                        }
 611                        if (!strcmp(path, "--remove")) {
 612                                allow_remove = 1;
 613                                continue;
 614                        }
 615                        if (!strcmp(path, "--unmerged")) {
 616                                refresh_flags |= REFRESH_UNMERGED;
 617                                continue;
 618                        }
 619                        if (!strcmp(path, "--refresh")) {
 620                                setup_work_tree();
 621                                has_errors |= refresh_cache(refresh_flags);
 622                                continue;
 623                        }
 624                        if (!strcmp(path, "--really-refresh")) {
 625                                setup_work_tree();
 626                                has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
 627                                continue;
 628                        }
 629                        if (!strcmp(path, "--cacheinfo")) {
 630                                unsigned char sha1[20];
 631                                unsigned int mode;
 632
 633                                if (i+3 >= argc)
 634                                        die("git update-index: --cacheinfo <mode> <sha1> <path>");
 635
 636                                if (strtoul_ui(argv[i+1], 8, &mode) ||
 637                                    get_sha1_hex(argv[i+2], sha1) ||
 638                                    add_cacheinfo(mode, sha1, argv[i+3], 0))
 639                                        die("git update-index: --cacheinfo"
 640                                            " cannot add %s", argv[i+3]);
 641                                i += 3;
 642                                continue;
 643                        }
 644                        if (!strcmp(path, "--chmod=-x") ||
 645                            !strcmp(path, "--chmod=+x")) {
 646                                if (argc <= i+1)
 647                                        die("git update-index: %s <path>", path);
 648                                set_executable_bit = path[8];
 649                                continue;
 650                        }
 651                        if (!strcmp(path, "--assume-unchanged")) {
 652                                mark_valid_only = MARK_FLAG;
 653                                continue;
 654                        }
 655                        if (!strcmp(path, "--no-assume-unchanged")) {
 656                                mark_valid_only = UNMARK_FLAG;
 657                                continue;
 658                        }
 659                        if (!strcmp(path, "--no-skip-worktree")) {
 660                                mark_skip_worktree_only = UNMARK_FLAG;
 661                                continue;
 662                        }
 663                        if (!strcmp(path, "--skip-worktree")) {
 664                                mark_skip_worktree_only = MARK_FLAG;
 665                                continue;
 666                        }
 667                        if (!strcmp(path, "--info-only")) {
 668                                info_only = 1;
 669                                continue;
 670                        }
 671                        if (!strcmp(path, "--force-remove")) {
 672                                force_remove = 1;
 673                                continue;
 674                        }
 675                        if (!strcmp(path, "-z")) {
 676                                line_termination = 0;
 677                                continue;
 678                        }
 679                        if (!strcmp(path, "--stdin")) {
 680                                if (i != argc - 1)
 681                                        die("--stdin must be at the end");
 682                                read_from_stdin = 1;
 683                                break;
 684                        }
 685                        if (!strcmp(path, "--index-info")) {
 686                                if (i != argc - 1)
 687                                        die("--index-info must be at the end");
 688                                allow_add = allow_replace = allow_remove = 1;
 689                                read_index_info(line_termination);
 690                                break;
 691                        }
 692                        if (!strcmp(path, "--unresolve")) {
 693                                has_errors = do_unresolve(argc - i, argv + i,
 694                                                          prefix, prefix_length);
 695                                if (has_errors)
 696                                        active_cache_changed = 0;
 697                                goto finish;
 698                        }
 699                        if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
 700                                setup_work_tree();
 701                                has_errors = do_reupdate(argc - i, argv + i,
 702                                                         prefix, prefix_length);
 703                                if (has_errors)
 704                                        active_cache_changed = 0;
 705                                goto finish;
 706                        }
 707                        if (!strcmp(path, "--ignore-missing")) {
 708                                refresh_flags |= REFRESH_IGNORE_MISSING;
 709                                continue;
 710                        }
 711                        if (!strcmp(path, "--verbose")) {
 712                                verbose = 1;
 713                                continue;
 714                        }
 715                        if (!strcmp(path, "-h") || !strcmp(path, "--help"))
 716                                usage(update_index_usage);
 717                        die("unknown option %s", path);
 718                }
 719                setup_work_tree();
 720                p = prefix_path(prefix, prefix_length, path);
 721                update_one(p, NULL, 0);
 722                if (set_executable_bit)
 723                        chmod_path(set_executable_bit, p);
 724                if (p < path || p > path + strlen(path))
 725                        free((char *)p);
 726        }
 727        if (read_from_stdin) {
 728                struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
 729
 730                setup_work_tree();
 731                while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
 732                        const char *p;
 733                        if (line_termination && buf.buf[0] == '"') {
 734                                strbuf_reset(&nbuf);
 735                                if (unquote_c_style(&nbuf, buf.buf, NULL))
 736                                        die("line is badly quoted");
 737                                strbuf_swap(&buf, &nbuf);
 738                        }
 739                        p = prefix_path(prefix, prefix_length, buf.buf);
 740                        update_one(p, NULL, 0);
 741                        if (set_executable_bit)
 742                                chmod_path(set_executable_bit, p);
 743                        if (p < buf.buf || p > buf.buf + buf.len)
 744                                free((char *)p);
 745                }
 746                strbuf_release(&nbuf);
 747                strbuf_release(&buf);
 748        }
 749
 750 finish:
 751        if (active_cache_changed) {
 752                if (newfd < 0) {
 753                        if (refresh_flags & REFRESH_QUIET)
 754                                exit(128);
 755                        unable_to_lock_index_die(get_index_file(), lock_error);
 756                }
 757                if (write_cache(newfd, active_cache, active_nr) ||
 758                    commit_locked_index(lock_file))
 759                        die("Unable to write new index file");
 760        }
 761
 762        rollback_lock_file(lock_file);
 763
 764        return has_errors ? 1 : 0;
 765}