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