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