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