update-index.con commit [PATCH] HTTP partial transfer support fix. (4fa2197)
   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
   9/*
  10 * Default to not allowing changes to the list of files. The
  11 * tool doesn't actually care, but this makes it harder to add
  12 * files to the revision control by mistake by doing something
  13 * like "git-update-index *" and suddenly having all the object
  14 * files be revision controlled.
  15 */
  16static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0, info_only = 0;
  17static int force_remove;
  18
  19/* Three functions to allow overloaded pointer return; see linux/err.h */
  20static inline void *ERR_PTR(long error)
  21{
  22        return (void *) error;
  23}
  24
  25static inline long PTR_ERR(const void *ptr)
  26{
  27        return (long) ptr;
  28}
  29
  30static inline long IS_ERR(const void *ptr)
  31{
  32        return (unsigned long)ptr > (unsigned long)-1000L;
  33}
  34
  35static int add_file_to_cache(const char *path)
  36{
  37        int size, namelen, option, status;
  38        struct cache_entry *ce;
  39        struct stat st;
  40        int fd;
  41        char *target;
  42
  43        status = lstat(path, &st);
  44        if (status < 0 || S_ISDIR(st.st_mode)) {
  45                /* When we used to have "path" and now we want to add
  46                 * "path/file", we need a way to remove "path" before
  47                 * being able to add "path/file".  However,
  48                 * "git-update-index --remove path" would not work.
  49                 * --force-remove can be used but this is more user
  50                 * friendly, especially since we can do the opposite
  51                 * case just fine without --force-remove.
  52                 */
  53                if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
  54                        if (allow_remove) {
  55                                if (remove_file_from_cache(path))
  56                                        return error("%s: cannot remove from the index",
  57                                                     path);
  58                                else
  59                                        return 0;
  60                        } else if (status < 0) {
  61                                return error("%s: does not exist and --remove not passed",
  62                                             path);
  63                        }
  64                }
  65                if (0 == status)
  66                        return error("%s: is a directory - add files inside instead",
  67                                     path);
  68                else
  69                        return error("lstat(\"%s\"): %s", path,
  70                                     strerror(errno));
  71        }
  72        namelen = strlen(path);
  73        size = cache_entry_size(namelen);
  74        ce = xmalloc(size);
  75        memset(ce, 0, size);
  76        memcpy(ce->name, path, namelen);
  77        fill_stat_cache_info(ce, &st);
  78        ce->ce_mode = create_ce_mode(st.st_mode);
  79        ce->ce_flags = htons(namelen);
  80        switch (st.st_mode & S_IFMT) {
  81        case S_IFREG:
  82                fd = open(path, O_RDONLY);
  83                if (fd < 0)
  84                        return error("open(\"%s\"): %s", path, strerror(errno));
  85                if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
  86                        return error("%s: failed to insert into database", path);
  87                break;
  88        case S_IFLNK:
  89                target = xmalloc(st.st_size+1);
  90                if (readlink(path, target, st.st_size+1) != st.st_size) {
  91                        char *errstr = strerror(errno);
  92                        free(target);
  93                        return error("readlink(\"%s\"): %s", path,
  94                                     errstr);
  95                }
  96                if (info_only) {
  97                        unsigned char hdr[50];
  98                        int hdrlen;
  99                        write_sha1_file_prepare(target, st.st_size, "blob",
 100                                                ce->sha1, hdr, &hdrlen);
 101                } else if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
 102                        return error("%s: failed to insert into database", path);
 103                free(target);
 104                break;
 105        default:
 106                return error("%s: unsupported file type", path);
 107        }
 108        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 109        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 110        if (add_cache_entry(ce, option))
 111                return error("%s: cannot add to the index - missing --add option?",
 112                             path);
 113        return 0;
 114}
 115
 116/*
 117 * "refresh" does not calculate a new sha1 file or bring the
 118 * cache up-to-date for mode/content changes. But what it
 119 * _does_ do is to "re-match" the stat information of a file
 120 * with the cache, so that you can refresh the cache for a
 121 * file that hasn't been changed but where the stat entry is
 122 * out of date.
 123 *
 124 * For example, you'd want to do this after doing a "git-read-tree",
 125 * to link up the stat cache details with the proper files.
 126 */
 127static struct cache_entry *refresh_entry(struct cache_entry *ce)
 128{
 129        struct stat st;
 130        struct cache_entry *updated;
 131        int changed, size;
 132
 133        if (lstat(ce->name, &st) < 0)
 134                return ERR_PTR(-errno);
 135
 136        changed = ce_match_stat(ce, &st);
 137        if (!changed)
 138                return ce;
 139
 140        if (ce_modified(ce, &st))
 141                return ERR_PTR(-EINVAL);
 142
 143        size = ce_size(ce);
 144        updated = xmalloc(size);
 145        memcpy(updated, ce, size);
 146        fill_stat_cache_info(updated, &st);
 147        return updated;
 148}
 149
 150static int refresh_cache(void)
 151{
 152        int i;
 153        int has_errors = 0;
 154
 155        for (i = 0; i < active_nr; i++) {
 156                struct cache_entry *ce, *new;
 157                ce = active_cache[i];
 158                if (ce_stage(ce)) {
 159                        printf("%s: needs merge\n", ce->name);
 160                        has_errors = 1;
 161                        while ((i < active_nr) &&
 162                               ! strcmp(active_cache[i]->name, ce->name))
 163                                i++;
 164                        i--;
 165                        continue;
 166                }
 167
 168                new = refresh_entry(ce);
 169                if (IS_ERR(new)) {
 170                        if (not_new && PTR_ERR(new) == -ENOENT)
 171                                continue;
 172                        if (quiet)
 173                                continue;
 174                        printf("%s: needs update\n", ce->name);
 175                        has_errors = 1;
 176                        continue;
 177                }
 178                active_cache_changed = 1;
 179                /* You can NOT just free active_cache[i] here, since it
 180                 * might not be necessarily malloc()ed but can also come
 181                 * from mmap(). */
 182                active_cache[i] = new;
 183        }
 184        return has_errors;
 185}
 186
 187/*
 188 * We fundamentally don't like some paths: we don't want
 189 * dot or dot-dot anywhere, and for obvious reasons don't
 190 * want to recurse into ".git" either.
 191 *
 192 * Also, we don't want double slashes or slashes at the
 193 * end that can make pathnames ambiguous.
 194 */
 195static int verify_dotfile(const char *rest)
 196{
 197        /*
 198         * The first character was '.', but that
 199         * has already been discarded, we now test
 200         * the rest.
 201         */
 202        switch (*rest) {
 203        /* "." is not allowed */
 204        case '\0': case '/':
 205                return 0;
 206
 207        /*
 208         * ".git" followed by  NUL or slash is bad. This
 209         * shares the path end test with the ".." case.
 210         */
 211        case 'g':
 212                if (rest[1] != 'i')
 213                        break;
 214                if (rest[2] != 't')
 215                        break;
 216                rest += 2;
 217        /* fallthrough */
 218        case '.':
 219                if (rest[1] == '\0' || rest[1] == '/')
 220                        return 0;
 221        }
 222        return 1;
 223}
 224
 225static int verify_path(const char *path)
 226{
 227        char c;
 228
 229        goto inside;
 230        for (;;) {
 231                if (!c)
 232                        return 1;
 233                if (c == '/') {
 234inside:
 235                        c = *path++;
 236                        switch (c) {
 237                        default:
 238                                continue;
 239                        case '/': case '\0':
 240                                break;
 241                        case '.':
 242                                if (verify_dotfile(path))
 243                                        continue;
 244                        }
 245                        return 0;
 246                }
 247                c = *path++;
 248        }
 249}
 250
 251static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
 252{
 253        int size, len, option;
 254        unsigned int mode;
 255        unsigned char sha1[20];
 256        struct cache_entry *ce;
 257
 258        if (sscanf(arg1, "%o", &mode) != 1)
 259                return -1;
 260        if (get_sha1_hex(arg2, sha1))
 261                return -1;
 262        if (!verify_path(arg3))
 263                return -1;
 264
 265        len = strlen(arg3);
 266        size = cache_entry_size(len);
 267        ce = xmalloc(size);
 268        memset(ce, 0, size);
 269
 270        memcpy(ce->sha1, sha1, 20);
 271        memcpy(ce->name, arg3, len);
 272        ce->ce_flags = htons(len);
 273        ce->ce_mode = create_ce_mode(mode);
 274        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
 275        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
 276        return add_cache_entry(ce, option);
 277}
 278
 279static struct cache_file cache_file;
 280
 281
 282static void update_one(const char *path, const char *prefix, int prefix_length)
 283{
 284        const char *p = prefix_path(prefix, prefix_length, path);
 285        if (!verify_path(p)) {
 286                fprintf(stderr, "Ignoring path %s\n", path);
 287                return;
 288        }
 289        if (force_remove) {
 290                if (remove_file_from_cache(p))
 291                        die("git-update-index: unable to remove %s", path);
 292                return;
 293        }
 294        if (add_file_to_cache(p))
 295                die("Unable to process file %s", path);
 296}
 297
 298int main(int argc, const char **argv)
 299{
 300        int i, newfd, entries, has_errors = 0, line_termination = '\n';
 301        int allow_options = 1;
 302        int read_from_stdin = 0;
 303        const char *prefix = setup_git_directory();
 304        int prefix_length = prefix ? strlen(prefix) : 0;
 305
 306        newfd = hold_index_file_for_update(&cache_file, get_index_file());
 307        if (newfd < 0)
 308                die("unable to create new cachefile");
 309
 310        entries = read_cache();
 311        if (entries < 0)
 312                die("cache corrupted");
 313
 314        for (i = 1 ; i < argc; i++) {
 315                const char *path = argv[i];
 316
 317                if (allow_options && *path == '-') {
 318                        if (!strcmp(path, "--")) {
 319                                allow_options = 0;
 320                                continue;
 321                        }
 322                        if (!strcmp(path, "-q")) {
 323                                quiet = 1;
 324                                continue;
 325                        }
 326                        if (!strcmp(path, "--add")) {
 327                                allow_add = 1;
 328                                continue;
 329                        }
 330                        if (!strcmp(path, "--replace")) {
 331                                allow_replace = 1;
 332                                continue;
 333                        }
 334                        if (!strcmp(path, "--remove")) {
 335                                allow_remove = 1;
 336                                continue;
 337                        }
 338                        if (!strcmp(path, "--refresh")) {
 339                                has_errors |= refresh_cache();
 340                                continue;
 341                        }
 342                        if (!strcmp(path, "--cacheinfo")) {
 343                                if (i+3 >= argc)
 344                                        die("git-update-index: --cacheinfo <mode> <sha1> <path>");
 345                                if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
 346                                        die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
 347                                i += 3;
 348                                continue;
 349                        }
 350                        if (!strcmp(path, "--info-only")) {
 351                                info_only = 1;
 352                                continue;
 353                        }
 354                        if (!strcmp(path, "--force-remove")) {
 355                                force_remove = 1;
 356                                continue;
 357                        }
 358                        if (!strcmp(path, "-z")) {
 359                                line_termination = 0;
 360                                continue;
 361                        }
 362                        if (!strcmp(path, "--stdin")) {
 363                                if (i != argc - 1)
 364                                        die("--stdin must be at the end");
 365                                read_from_stdin = 1;
 366                                break;
 367                        }
 368                        if (!strcmp(path, "--ignore-missing")) {
 369                                not_new = 1;
 370                                continue;
 371                        }
 372                        die("unknown option %s", path);
 373                }
 374                update_one(path, prefix, prefix_length);
 375        }
 376        if (read_from_stdin) {
 377                struct strbuf buf;
 378                strbuf_init(&buf);
 379                while (1) {
 380                        read_line(&buf, stdin, line_termination);
 381                        if (buf.eof)
 382                                break;
 383                        update_one(buf.buf, prefix, prefix_length);
 384                }
 385        }
 386        if (write_cache(newfd, active_cache, active_nr) ||
 387            commit_index_file(&cache_file))
 388                die("Unable to write new cachefile");
 389
 390        return has_errors ? 1 : 0;
 391}