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