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