builtin / update-index.con commit t3507: introduce pristine-detach helper (2161da1)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "quote.h"
   8#include "cache-tree.h"
   9#include "tree-walk.h"
  10#include "builtin.h"
  11#include "refs.h"
  12#include "resolve-undo.h"
  13#include "parse-options.h"
  14
  15/*
  16 * Default to not allowing changes to the list of files. The
  17 * tool doesn't actually care, but this makes it harder to add
  18 * files to the revision control by mistake by doing something
  19 * like "git update-index *" and suddenly having all the object
  20 * files be revision controlled.
  21 */
  22static int allow_add;
  23static int allow_remove;
  24static int allow_replace;
  25static int info_only;
  26static int force_remove;
  27static int verbose;
  28static int mark_valid_only;
  29static int mark_skip_worktree_only;
  30#define MARK_FLAG 1
  31#define UNMARK_FLAG 2
  32
  33__attribute__((format (printf, 1, 2)))
  34static void report(const char *fmt, ...)
  35{
  36        va_list vp;
  37
  38        if (!verbose)
  39                return;
  40
  41        va_start(vp, fmt);
  42        vprintf(fmt, vp);
  43        putchar('\n');
  44        va_end(vp);
  45}
  46
  47static int mark_ce_flags(const char *path, int flag, int mark)
  48{
  49        int namelen = strlen(path);
  50        int pos = cache_name_pos(path, namelen);
  51        if (0 <= pos) {
  52                if (mark)
  53                        active_cache[pos]->ce_flags |= flag;
  54                else
  55                        active_cache[pos]->ce_flags &= ~flag;
  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 = 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(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
 178static int process_path(const char *path)
 179{
 180        int pos, len;
 181        struct stat st;
 182        struct cache_entry *ce;
 183
 184        len = strlen(path);
 185        if (has_symlink_leading_path(path, len))
 186                return error("'%s' is beyond a symbolic link", path);
 187
 188        pos = cache_name_pos(path, len);
 189        ce = pos < 0 ? NULL : active_cache[pos];
 190        if (ce && ce_skip_worktree(ce)) {
 191                /*
 192                 * working directory version is assumed "good"
 193                 * so updating it does not make sense.
 194                 * On the other hand, removing it from index should work
 195                 */
 196                if (allow_remove && remove_file_from_cache(path))
 197                        return error("%s: cannot remove from the index", path);
 198                return 0;
 199        }
 200
 201        /*
 202         * First things first: get the stat information, to decide
 203         * what to do about the pathname!
 204         */
 205        if (lstat(path, &st) < 0)
 206                return process_lstat_error(path, errno);
 207
 208        if (S_ISDIR(st.st_mode))
 209                return process_directory(path, len, &st);
 210
 211        /*
 212         * Process a regular file
 213         */
 214        if (ce && S_ISGITLINK(ce->ce_mode))
 215                return error("%s is already a gitlink, not replacing", path);
 216
 217        return add_one_path(ce, path, len, &st);
 218}
 219
 220static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
 221                         const char *path, int stage)
 222{
 223        int size, len, option;
 224        struct cache_entry *ce;
 225
 226        if (!verify_path(path))
 227                return error("Invalid path '%s'", path);
 228
 229        len = strlen(path);
 230        size = cache_entry_size(len);
 231        ce = xcalloc(1, size);
 232
 233        hashcpy(ce->sha1, sha1);
 234        memcpy(ce->name, path, len);
 235        ce->ce_flags = create_ce_flags(len, stage);
 236        ce->ce_mode = create_ce_mode(mode);
 237        if (assume_unchanged)
 238                ce->ce_flags |= CE_VALID;
 239        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 240        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 241        if (add_cache_entry(ce, option))
 242                return error("%s: cannot add to the index - missing --add option?",
 243                             path);
 244        report("add '%s'", path);
 245        return 0;
 246}
 247
 248static void chmod_path(int flip, const char *path)
 249{
 250        int pos;
 251        struct cache_entry *ce;
 252        unsigned int mode;
 253
 254        pos = cache_name_pos(path, strlen(path));
 255        if (pos < 0)
 256                goto fail;
 257        ce = active_cache[pos];
 258        mode = ce->ce_mode;
 259        if (!S_ISREG(mode))
 260                goto fail;
 261        switch (flip) {
 262        case '+':
 263                ce->ce_mode |= 0111; break;
 264        case '-':
 265                ce->ce_mode &= ~0111; break;
 266        default:
 267                goto fail;
 268        }
 269        cache_tree_invalidate_path(active_cache_tree, path);
 270        active_cache_changed = 1;
 271        report("chmod %cx '%s'", flip, path);
 272        return;
 273 fail:
 274        die("git update-index: cannot chmod %cx '%s'", flip, path);
 275}
 276
 277static void update_one(const char *path, const char *prefix, int prefix_length)
 278{
 279        const char *p = prefix_path(prefix, prefix_length, path);
 280        if (!verify_path(p)) {
 281                fprintf(stderr, "Ignoring path %s\n", path);
 282                goto free_return;
 283        }
 284        if (mark_valid_only) {
 285                if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG))
 286                        die("Unable to mark file %s", path);
 287                goto free_return;
 288        }
 289        if (mark_skip_worktree_only) {
 290                if (mark_ce_flags(p, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG))
 291                        die("Unable to mark file %s", path);
 292                goto free_return;
 293        }
 294
 295        if (force_remove) {
 296                if (remove_file_from_cache(p))
 297                        die("git update-index: unable to remove %s", path);
 298                report("remove '%s'", path);
 299                goto free_return;
 300        }
 301        if (process_path(p))
 302                die("Unable to process path %s", path);
 303        report("add '%s'", path);
 304 free_return:
 305        if (p < path || p > path + strlen(path))
 306                free((char *)p);
 307}
 308
 309static void read_index_info(int line_termination)
 310{
 311        struct strbuf buf = STRBUF_INIT;
 312        struct strbuf uq = STRBUF_INIT;
 313
 314        while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
 315                char *ptr, *tab;
 316                char *path_name;
 317                unsigned char sha1[20];
 318                unsigned int mode;
 319                unsigned long ul;
 320                int stage;
 321
 322                /* This reads lines formatted in one of three formats:
 323                 *
 324                 * (1) mode         SP sha1          TAB path
 325                 * The first format is what "git apply --index-info"
 326                 * reports, and used to reconstruct a partial tree
 327                 * that is used for phony merge base tree when falling
 328                 * back on 3-way merge.
 329                 *
 330                 * (2) mode SP type SP sha1          TAB path
 331                 * The second format is to stuff "git ls-tree" output
 332                 * into the index file.
 333                 *
 334                 * (3) mode         SP sha1 SP stage TAB path
 335                 * This format is to put higher order stages into the
 336                 * index file and matches "git ls-files --stage" output.
 337                 */
 338                errno = 0;
 339                ul = strtoul(buf.buf, &ptr, 8);
 340                if (ptr == buf.buf || *ptr != ' '
 341                    || errno || (unsigned int) ul != ul)
 342                        goto bad_line;
 343                mode = ul;
 344
 345                tab = strchr(ptr, '\t');
 346                if (!tab || tab - ptr < 41)
 347                        goto bad_line;
 348
 349                if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
 350                        stage = tab[-1] - '0';
 351                        ptr = tab + 1; /* point at the head of path */
 352                        tab = tab - 2; /* point at tail of sha1 */
 353                }
 354                else {
 355                        stage = 0;
 356                        ptr = tab + 1; /* point at the head of path */
 357                }
 358
 359                if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
 360                        goto bad_line;
 361
 362                path_name = ptr;
 363                if (line_termination && path_name[0] == '"') {
 364                        strbuf_reset(&uq);
 365                        if (unquote_c_style(&uq, path_name, NULL)) {
 366                                die("git update-index: bad quoting of path name");
 367                        }
 368                        path_name = uq.buf;
 369                }
 370
 371                if (!verify_path(path_name)) {
 372                        fprintf(stderr, "Ignoring path %s\n", path_name);
 373                        continue;
 374                }
 375
 376                if (!mode) {
 377                        /* mode == 0 means there is no such path -- remove */
 378                        if (remove_file_from_cache(path_name))
 379                                die("git update-index: unable to remove %s",
 380                                    ptr);
 381                }
 382                else {
 383                        /* mode ' ' sha1 '\t' name
 384                         * ptr[-1] points at tab,
 385                         * ptr[-41] is at the beginning of sha1
 386                         */
 387                        ptr[-42] = ptr[-1] = 0;
 388                        if (add_cacheinfo(mode, sha1, path_name, stage))
 389                                die("git update-index: unable to update %s",
 390                                    path_name);
 391                }
 392                continue;
 393
 394        bad_line:
 395                die("malformed index info %s", buf.buf);
 396        }
 397        strbuf_release(&buf);
 398        strbuf_release(&uq);
 399}
 400
 401static const char * const update_index_usage[] = {
 402        "git update-index [options] [--] [<file>...]",
 403        NULL
 404};
 405
 406static unsigned char head_sha1[20];
 407static unsigned char merge_head_sha1[20];
 408
 409static struct cache_entry *read_one_ent(const char *which,
 410                                        unsigned char *ent, const char *path,
 411                                        int namelen, int stage)
 412{
 413        unsigned mode;
 414        unsigned char sha1[20];
 415        int size;
 416        struct cache_entry *ce;
 417
 418        if (get_tree_entry(ent, path, sha1, &mode)) {
 419                if (which)
 420                        error("%s: not in %s branch.", path, which);
 421                return NULL;
 422        }
 423        if (mode == S_IFDIR) {
 424                if (which)
 425                        error("%s: not a blob in %s branch.", path, which);
 426                return NULL;
 427        }
 428        size = cache_entry_size(namelen);
 429        ce = xcalloc(1, size);
 430
 431        hashcpy(ce->sha1, sha1);
 432        memcpy(ce->name, path, namelen);
 433        ce->ce_flags = create_ce_flags(namelen, stage);
 434        ce->ce_mode = create_ce_mode(mode);
 435        return ce;
 436}
 437
 438static int unresolve_one(const char *path)
 439{
 440        int namelen = strlen(path);
 441        int pos;
 442        int ret = 0;
 443        struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
 444
 445        /* See if there is such entry in the index. */
 446        pos = cache_name_pos(path, namelen);
 447        if (0 <= pos) {
 448                /* already merged */
 449                pos = unmerge_cache_entry_at(pos);
 450                if (pos < active_nr) {
 451                        struct cache_entry *ce = active_cache[pos];
 452                        if (ce_stage(ce) &&
 453                            ce_namelen(ce) == namelen &&
 454                            !memcmp(ce->name, path, namelen))
 455                                return 0;
 456                }
 457                /* no resolve-undo information; fall back */
 458        } else {
 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 (!hashcmp(ce_2->sha1, ce_3->sha1) &&
 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("HEAD", head_sha1))
 513                die("No HEAD -- no initial commit yet?");
 514        if (read_ref("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("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                    !hashcmp(ce->sha1, old->sha1)) {
 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
 584struct refresh_params {
 585        unsigned int flags;
 586        int *has_errors;
 587};
 588
 589static int refresh(struct refresh_params *o, unsigned int flag)
 590{
 591        setup_work_tree();
 592        *o->has_errors |= refresh_cache(o->flags | flag);
 593        return 0;
 594}
 595
 596static int refresh_callback(const struct option *opt,
 597                                const char *arg, int unset)
 598{
 599        return refresh(opt->value, 0);
 600}
 601
 602static int really_refresh_callback(const struct option *opt,
 603                                const char *arg, int unset)
 604{
 605        return refresh(opt->value, REFRESH_REALLY);
 606}
 607
 608static int chmod_callback(const struct option *opt,
 609                                const char *arg, int unset)
 610{
 611        char *flip = opt->value;
 612        if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2])
 613                return error("option 'chmod' expects \"+x\" or \"-x\"");
 614        *flip = arg[0];
 615        return 0;
 616}
 617
 618static int resolve_undo_clear_callback(const struct option *opt,
 619                                const char *arg, int unset)
 620{
 621        resolve_undo_clear();
 622        return 0;
 623}
 624
 625static int cacheinfo_callback(struct parse_opt_ctx_t *ctx,
 626                                const struct option *opt, int unset)
 627{
 628        unsigned char sha1[20];
 629        unsigned int mode;
 630
 631        if (ctx->argc <= 3)
 632                return error("option 'cacheinfo' expects three arguments");
 633        if (strtoul_ui(*++ctx->argv, 8, &mode) ||
 634            get_sha1_hex(*++ctx->argv, sha1) ||
 635            add_cacheinfo(mode, sha1, *++ctx->argv, 0))
 636                die("git update-index: --cacheinfo cannot add %s", *ctx->argv);
 637        ctx->argc -= 3;
 638        return 0;
 639}
 640
 641static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx,
 642                              const struct option *opt, int unset)
 643{
 644        int *line_termination = opt->value;
 645
 646        if (ctx->argc != 1)
 647                return error("option '%s' must be the last argument", opt->long_name);
 648        allow_add = allow_replace = allow_remove = 1;
 649        read_index_info(*line_termination);
 650        return 0;
 651}
 652
 653static int stdin_callback(struct parse_opt_ctx_t *ctx,
 654                                const struct option *opt, int unset)
 655{
 656        int *read_from_stdin = opt->value;
 657
 658        if (ctx->argc != 1)
 659                return error("option '%s' must be the last argument", opt->long_name);
 660        *read_from_stdin = 1;
 661        return 0;
 662}
 663
 664static int unresolve_callback(struct parse_opt_ctx_t *ctx,
 665                                const struct option *opt, int flags)
 666{
 667        int *has_errors = opt->value;
 668        const char *prefix = startup_info->prefix;
 669
 670        /* consume remaining arguments. */
 671        *has_errors = do_unresolve(ctx->argc, ctx->argv,
 672                                prefix, prefix ? strlen(prefix) : 0);
 673        if (*has_errors)
 674                active_cache_changed = 0;
 675
 676        ctx->argv += ctx->argc - 1;
 677        ctx->argc = 1;
 678        return 0;
 679}
 680
 681static int reupdate_callback(struct parse_opt_ctx_t *ctx,
 682                                const struct option *opt, int flags)
 683{
 684        int *has_errors = opt->value;
 685        const char *prefix = startup_info->prefix;
 686
 687        /* consume remaining arguments. */
 688        setup_work_tree();
 689        *has_errors = do_reupdate(ctx->argc, ctx->argv,
 690                                prefix, prefix ? strlen(prefix) : 0);
 691        if (*has_errors)
 692                active_cache_changed = 0;
 693
 694        ctx->argv += ctx->argc - 1;
 695        ctx->argc = 1;
 696        return 0;
 697}
 698
 699int cmd_update_index(int argc, const char **argv, const char *prefix)
 700{
 701        int newfd, entries, has_errors = 0, line_termination = '\n';
 702        int read_from_stdin = 0;
 703        int prefix_length = prefix ? strlen(prefix) : 0;
 704        char set_executable_bit = 0;
 705        struct refresh_params refresh_args = {0, &has_errors};
 706        int lock_error = 0;
 707        struct lock_file *lock_file;
 708        struct parse_opt_ctx_t ctx;
 709        int parseopt_state = PARSE_OPT_UNKNOWN;
 710        struct option options[] = {
 711                OPT_BIT('q', NULL, &refresh_args.flags,
 712                        "continue refresh even when index needs update",
 713                        REFRESH_QUIET),
 714                OPT_BIT(0, "ignore-submodules", &refresh_args.flags,
 715                        "refresh: ignore submodules",
 716                        REFRESH_IGNORE_SUBMODULES),
 717                OPT_SET_INT(0, "add", &allow_add,
 718                        "do not ignore new files", 1),
 719                OPT_SET_INT(0, "replace", &allow_replace,
 720                        "let files replace directories and vice-versa", 1),
 721                OPT_SET_INT(0, "remove", &allow_remove,
 722                        "notice files missing from worktree", 1),
 723                OPT_BIT(0, "unmerged", &refresh_args.flags,
 724                        "refresh even if index contains unmerged entries",
 725                        REFRESH_UNMERGED),
 726                {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL,
 727                        "refresh stat information",
 728                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 729                        refresh_callback},
 730                {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL,
 731                        "like --refresh, but ignore assume-unchanged setting",
 732                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 733                        really_refresh_callback},
 734                {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL,
 735                        "<mode> <object> <path>",
 736                        "add the specified entry to the index",
 737                        PARSE_OPT_NOARG |       /* disallow --cacheinfo=<mode> form */
 738                        PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
 739                        (parse_opt_cb *) cacheinfo_callback},
 740                {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+/-)x",
 741                        "override the executable bit of the listed files",
 742                        PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP,
 743                        chmod_callback},
 744                {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL,
 745                        "mark files as \"not changing\"",
 746                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
 747                {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL,
 748                        "clear assumed-unchanged bit",
 749                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
 750                {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL,
 751                        "mark files as \"index-only\"",
 752                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG},
 753                {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL,
 754                        "clear skip-worktree bit",
 755                        PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG},
 756                OPT_SET_INT(0, "info-only", &info_only,
 757                        "add to index only; do not add content to object database", 1),
 758                OPT_SET_INT(0, "force-remove", &force_remove,
 759                        "remove named paths even if present in worktree", 1),
 760                OPT_SET_INT('z', NULL, &line_termination,
 761                        "with --stdin: input lines are terminated by null bytes", '\0'),
 762                {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL,
 763                        "read list of paths to be updated from standard input",
 764                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 765                        (parse_opt_cb *) stdin_callback},
 766                {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL,
 767                        "add entries from standard input to the index",
 768                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 769                        (parse_opt_cb *) stdin_cacheinfo_callback},
 770                {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL,
 771                        "repopulate stages #2 and #3 for the listed paths",
 772                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 773                        (parse_opt_cb *) unresolve_callback},
 774                {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL,
 775                        "only update entries that differ from HEAD",
 776                        PARSE_OPT_NONEG | PARSE_OPT_NOARG,
 777                        (parse_opt_cb *) reupdate_callback},
 778                OPT_BIT(0, "ignore-missing", &refresh_args.flags,
 779                        "ignore files missing from worktree",
 780                        REFRESH_IGNORE_MISSING),
 781                OPT_SET_INT(0, "verbose", &verbose,
 782                        "report actions to standard output", 1),
 783                {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL,
 784                        "(for porcelains) forget saved unresolved conflicts",
 785                        PARSE_OPT_NOARG | PARSE_OPT_NONEG,
 786                        resolve_undo_clear_callback},
 787                OPT_END()
 788        };
 789
 790        if (argc == 2 && !strcmp(argv[1], "-h"))
 791                usage(update_index_usage[0]);
 792
 793        git_config(git_default_config, NULL);
 794
 795        /* We can't free this memory, it becomes part of a linked list parsed atexit() */
 796        lock_file = xcalloc(1, sizeof(struct lock_file));
 797
 798        newfd = hold_locked_index(lock_file, 0);
 799        if (newfd < 0)
 800                lock_error = errno;
 801
 802        entries = read_cache();
 803        if (entries < 0)
 804                die("cache corrupted");
 805
 806        /*
 807         * Custom copy of parse_options() because we want to handle
 808         * filename arguments as they come.
 809         */
 810        parse_options_start(&ctx, argc, argv, prefix,
 811                            options, PARSE_OPT_STOP_AT_NON_OPTION);
 812        while (ctx.argc) {
 813                if (parseopt_state != PARSE_OPT_DONE)
 814                        parseopt_state = parse_options_step(&ctx, options,
 815                                                            update_index_usage);
 816                if (!ctx.argc)
 817                        break;
 818                switch (parseopt_state) {
 819                case PARSE_OPT_HELP:
 820                        exit(129);
 821                case PARSE_OPT_NON_OPTION:
 822                case PARSE_OPT_DONE:
 823                {
 824                        const char *path = ctx.argv[0];
 825                        const char *p;
 826
 827                        setup_work_tree();
 828                        p = prefix_path(prefix, prefix_length, path);
 829                        update_one(p, NULL, 0);
 830                        if (set_executable_bit)
 831                                chmod_path(set_executable_bit, p);
 832                        if (p < path || p > path + strlen(path))
 833                                free((char *)p);
 834                        ctx.argc--;
 835                        ctx.argv++;
 836                        break;
 837                }
 838                case PARSE_OPT_UNKNOWN:
 839                        if (ctx.argv[0][1] == '-')
 840                                error("unknown option '%s'", ctx.argv[0] + 2);
 841                        else
 842                                error("unknown switch '%c'", *ctx.opt);
 843                        usage_with_options(update_index_usage, options);
 844                }
 845        }
 846        argc = parse_options_end(&ctx);
 847
 848        if (read_from_stdin) {
 849                struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;
 850
 851                setup_work_tree();
 852                while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
 853                        const char *p;
 854                        if (line_termination && buf.buf[0] == '"') {
 855                                strbuf_reset(&nbuf);
 856                                if (unquote_c_style(&nbuf, buf.buf, NULL))
 857                                        die("line is badly quoted");
 858                                strbuf_swap(&buf, &nbuf);
 859                        }
 860                        p = prefix_path(prefix, prefix_length, buf.buf);
 861                        update_one(p, NULL, 0);
 862                        if (set_executable_bit)
 863                                chmod_path(set_executable_bit, p);
 864                        if (p < buf.buf || p > buf.buf + buf.len)
 865                                free((char *)p);
 866                }
 867                strbuf_release(&nbuf);
 868                strbuf_release(&buf);
 869        }
 870
 871        if (active_cache_changed) {
 872                if (newfd < 0) {
 873                        if (refresh_args.flags & REFRESH_QUIET)
 874                                exit(128);
 875                        unable_to_lock_index_die(get_index_file(), lock_error);
 876                }
 877                if (write_cache(newfd, active_cache, active_nr) ||
 878                    commit_locked_index(lock_file))
 879                        die("Unable to write new index file");
 880        }
 881
 882        rollback_lock_file(lock_file);
 883
 884        return has_errors ? 1 : 0;
 885}