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