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