update-cache.con commit Teach "git-pull-script" about alternate HEAD's to pull.. (552e2bd)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include <signal.h>
   7#include "cache.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 "update-cache *" and suddenly having all the object
  14 * files be revision controlled.
  15 */
  16static int allow_add = 0, allow_remove = 0, not_new = 0;
  17
  18/* Three functions to allow overloaded pointer return; see linux/err.h */
  19static inline void *ERR_PTR(long error)
  20{
  21        return (void *) error;
  22}
  23
  24static inline long PTR_ERR(const void *ptr)
  25{
  26        return (long) ptr;
  27}
  28
  29static inline long IS_ERR(const void *ptr)
  30{
  31        return (unsigned long)ptr > (unsigned long)-1000L;
  32}
  33
  34/*
  35 * This only updates the "non-critical" parts of the directory
  36 * cache, ie the parts that aren't tracked by GIT, and only used
  37 * to validate the cache.
  38 */
  39static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
  40{
  41        ce->ce_ctime.sec = htonl(st->st_ctime);
  42        ce->ce_mtime.sec = htonl(st->st_mtime);
  43#ifdef NSEC
  44        ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
  45        ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
  46#endif
  47        ce->ce_dev = htonl(st->st_dev);
  48        ce->ce_ino = htonl(st->st_ino);
  49        ce->ce_uid = htonl(st->st_uid);
  50        ce->ce_gid = htonl(st->st_gid);
  51        ce->ce_size = htonl(st->st_size);
  52}
  53
  54static int add_file_to_cache(char *path)
  55{
  56        int size, namelen;
  57        struct cache_entry *ce;
  58        struct stat st;
  59        int fd;
  60
  61        fd = open(path, O_RDONLY);
  62        if (fd < 0) {
  63                if (errno == ENOENT || errno == ENOTDIR) {
  64                        if (allow_remove)
  65                                return remove_file_from_cache(path);
  66                }
  67                return -1;
  68        }
  69        if (fstat(fd, &st) < 0) {
  70                close(fd);
  71                return -1;
  72        }
  73        namelen = strlen(path);
  74        size = cache_entry_size(namelen);
  75        ce = xmalloc(size);
  76        memset(ce, 0, size);
  77        memcpy(ce->name, path, namelen);
  78        fill_stat_cache_info(ce, &st);
  79        ce->ce_mode = create_ce_mode(st.st_mode);
  80        ce->ce_flags = htons(namelen);
  81
  82        if (index_fd(ce->sha1, fd, &st) < 0)
  83                return -1;
  84
  85        return add_cache_entry(ce, allow_add);
  86}
  87
  88static int match_data(int fd, void *buffer, unsigned long size)
  89{
  90        while (size) {
  91                char compare[1024];
  92                int ret = read(fd, compare, sizeof(compare));
  93
  94                if (ret <= 0 || ret > size || memcmp(buffer, compare, ret))
  95                        return -1;
  96                size -= ret;
  97                buffer += ret;
  98        }
  99        return 0;
 100}
 101
 102static int compare_data(struct cache_entry *ce, unsigned long expected_size)
 103{
 104        int match = -1;
 105        int fd = open(ce->name, O_RDONLY);
 106
 107        if (fd >= 0) {
 108                void *buffer;
 109                unsigned long size;
 110                char type[10];
 111
 112                buffer = read_sha1_file(ce->sha1, type, &size);
 113                if (buffer) {
 114                        if (size == expected_size && !strcmp(type, "blob"))
 115                                match = match_data(fd, buffer, size);
 116                        free(buffer);
 117                }
 118                close(fd);
 119        }
 120        return match;
 121}
 122
 123/*
 124 * "refresh" does not calculate a new sha1 file or bring the
 125 * cache up-to-date for mode/content changes. But what it
 126 * _does_ do is to "re-match" the stat information of a file
 127 * with the cache, so that you can refresh the cache for a
 128 * file that hasn't been changed but where the stat entry is
 129 * out of date.
 130 *
 131 * For example, you'd want to do this after doing a "read-tree",
 132 * to link up the stat cache details with the proper files.
 133 */
 134static struct cache_entry *refresh_entry(struct cache_entry *ce)
 135{
 136        struct stat st;
 137        struct cache_entry *updated;
 138        int changed, size;
 139
 140        if (stat(ce->name, &st) < 0)
 141                return ERR_PTR(-errno);
 142
 143        changed = cache_match_stat(ce, &st);
 144        if (!changed)
 145                return ce;
 146
 147        /*
 148         * If the mode has changed, there's no point in trying
 149         * to refresh the entry - it's not going to match
 150         */
 151        if (changed & MODE_CHANGED)
 152                return ERR_PTR(-EINVAL);
 153
 154        if (compare_data(ce, st.st_size))
 155                return ERR_PTR(-EINVAL);
 156
 157        size = ce_size(ce);
 158        updated = xmalloc(size);
 159        memcpy(updated, ce, size);
 160        fill_stat_cache_info(updated, &st);
 161        return updated;
 162}
 163
 164static int refresh_cache(void)
 165{
 166        int i;
 167        int has_errors = 0;
 168
 169        for (i = 0; i < active_nr; i++) {
 170                struct cache_entry *ce, *new;
 171                ce = active_cache[i];
 172                if (ce_stage(ce)) {
 173                        printf("%s: needs merge\n", ce->name);
 174                        has_errors = 1;
 175                        while ((i < active_nr) &&
 176                               ! strcmp(active_cache[i]->name, ce->name))
 177                                i++;
 178                        i--;
 179                        continue;
 180                }
 181
 182                new = refresh_entry(ce);
 183                if (IS_ERR(new)) {
 184                        if (!(not_new && PTR_ERR(new) == -ENOENT)) {
 185                                printf("%s: needs update\n", ce->name);
 186                                has_errors = 1;
 187                        }
 188                        continue;
 189                }
 190                active_cache[i] = new;
 191        }
 192        return has_errors;
 193}
 194
 195/*
 196 * We fundamentally don't like some paths: we don't want
 197 * dot or dot-dot anywhere, and in fact, we don't even want
 198 * any other dot-files (.git or anything else). They
 199 * are hidden, for chist sake.
 200 *
 201 * Also, we don't want double slashes or slashes at the
 202 * end that can make pathnames ambiguous.
 203 */
 204static int verify_path(char *path)
 205{
 206        char c;
 207
 208        goto inside;
 209        for (;;) {
 210                if (!c)
 211                        return 1;
 212                if (c == '/') {
 213inside:
 214                        c = *path++;
 215                        if (c != '/' && c != '.' && c != '\0')
 216                                continue;
 217                        return 0;
 218                }
 219                c = *path++;
 220        }
 221}
 222
 223static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
 224{
 225        int size, len;
 226        unsigned int mode;
 227        unsigned char sha1[20];
 228        struct cache_entry *ce;
 229
 230        if (sscanf(arg1, "%o", &mode) != 1)
 231                return -1;
 232        if (get_sha1_hex(arg2, sha1))
 233                return -1;
 234        if (!verify_path(arg3))
 235                return -1;
 236
 237        len = strlen(arg3);
 238        size = cache_entry_size(len);
 239        ce = xmalloc(size);
 240        memset(ce, 0, size);
 241
 242        memcpy(ce->sha1, sha1, 20);
 243        memcpy(ce->name, arg3, len);
 244        ce->ce_flags = htons(len);
 245        ce->ce_mode = create_ce_mode(mode);
 246        return add_cache_entry(ce, allow_add);
 247}
 248
 249static const char *lockfile_name = NULL;
 250
 251static void remove_lock_file(void)
 252{
 253        if (lockfile_name)
 254                unlink(lockfile_name);
 255}
 256
 257static void remove_lock_file_on_signal(int signo)
 258{
 259        remove_lock_file();
 260}
 261
 262int main(int argc, char **argv)
 263{
 264        int i, newfd, entries, has_errors = 0;
 265        int allow_options = 1;
 266        static char lockfile[MAXPATHLEN+1];
 267        const char *indexfile = get_index_file();
 268
 269        snprintf(lockfile, sizeof(lockfile), "%s.lock", indexfile);
 270
 271        newfd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0600);
 272        if (newfd < 0)
 273                die("unable to create new cachefile");
 274
 275        signal(SIGINT, remove_lock_file_on_signal);
 276        atexit(remove_lock_file);
 277        lockfile_name = lockfile;
 278
 279        entries = read_cache();
 280        if (entries < 0)
 281                die("cache corrupted");
 282
 283        for (i = 1 ; i < argc; i++) {
 284                char *path = argv[i];
 285
 286                if (allow_options && *path == '-') {
 287                        if (!strcmp(path, "--")) {
 288                                allow_options = 0;
 289                                continue;
 290                        }
 291                        if (!strcmp(path, "--add")) {
 292                                allow_add = 1;
 293                                continue;
 294                        }
 295                        if (!strcmp(path, "--remove")) {
 296                                allow_remove = 1;
 297                                continue;
 298                        }
 299                        if (!strcmp(path, "--refresh")) {
 300                                has_errors |= refresh_cache();
 301                                continue;
 302                        }
 303                        if (!strcmp(path, "--cacheinfo")) {
 304                                if (i+3 >= argc || add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
 305                                        die("update-cache: --cacheinfo <mode> <sha1> <path>");
 306                                i += 3;
 307                                continue;
 308                        }
 309                        if (!strcmp(path, "--force-remove")) {
 310                                if (argc <= i + 1)
 311                                        die("update-cache: --force-remove <path>");
 312                                if (remove_file_from_cache(argv[i+1]))
 313                                        die("update-cache: --force-remove cannot remove %s", argv[i+1]);
 314                                i++;
 315                                continue;
 316                        }
 317
 318                        if (!strcmp(path, "--ignore-missing")) {
 319                                not_new = 1;
 320                                continue;
 321                        }
 322                        die("unknown option %s", path);
 323                }
 324                if (!verify_path(path)) {
 325                        fprintf(stderr, "Ignoring path %s\n", argv[i]);
 326                        continue;
 327                }
 328                if (add_file_to_cache(path))
 329                        die("Unable to add %s to database", path);
 330        }
 331        if (write_cache(newfd, active_cache, active_nr) || rename(lockfile, indexfile))
 332                die("Unable to write new cachefile");
 333
 334        lockfile_name = NULL;
 335        return has_errors;
 336}