Use git-update-ref and git-symbolic-ref in tests
[git.git] / update-index.c
index b825a11..01b4088 100644 (file)
@@ -37,8 +37,6 @@ static int add_file_to_cache(const char *path)
        int size, namelen, option, status;
        struct cache_entry *ce;
        struct stat st;
-       int fd;
-       char *target;
 
        status = lstat(path, &st);
        if (status < 0 || S_ISDIR(st.st_mode)) {
@@ -77,34 +75,9 @@ static int add_file_to_cache(const char *path)
        fill_stat_cache_info(ce, &st);
        ce->ce_mode = create_ce_mode(st.st_mode);
        ce->ce_flags = htons(namelen);
-       switch (st.st_mode & S_IFMT) {
-       case S_IFREG:
-               fd = open(path, O_RDONLY);
-               if (fd < 0)
-                       return error("open(\"%s\"): %s", path, strerror(errno));
-               if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
-                       return error("%s: failed to insert into database", path);
-               break;
-       case S_IFLNK:
-               target = xmalloc(st.st_size+1);
-               if (readlink(path, target, st.st_size+1) != st.st_size) {
-                       char *errstr = strerror(errno);
-                       free(target);
-                       return error("readlink(\"%s\"): %s", path,
-                                    errstr);
-               }
-               if (info_only) {
-                       unsigned char hdr[50];
-                       int hdrlen;
-                       write_sha1_file_prepare(target, st.st_size, "blob",
-                                               ce->sha1, hdr, &hdrlen);
-               } else if (write_sha1_file(target, st.st_size, "blob", ce->sha1))
-                       return error("%s: failed to insert into database", path);
-               free(target);
-               break;
-       default:
-               return error("%s: unsupported file type", path);
-       }
+
+       if (index_path(ce->sha1, path, &st, !info_only))
+               return -1;
        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
        if (add_cache_entry(ce, option))
@@ -299,6 +272,54 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
                die("Unable to process file %s", path);
 }
 
+static void read_index_info(int line_termination)
+{
+       struct strbuf buf;
+       strbuf_init(&buf);
+       while (1) {
+               char *ptr;
+               unsigned char sha1[20];
+               unsigned int mode;
+
+               read_line(&buf, stdin, line_termination);
+               if (buf.eof)
+                       break;
+
+               mode = strtoul(buf.buf, &ptr, 8);
+               if (ptr == buf.buf || *ptr != ' ' ||
+                   get_sha1_hex(ptr + 1, sha1) ||
+                   ptr[41] != '\t')
+                       goto bad_line;
+
+               ptr += 42;
+               if (!verify_path(ptr)) {
+                       fprintf(stderr, "Ignoring path %s\n", ptr);
+                       continue;
+               }
+
+               if (!mode) {
+                       /* mode == 0 means there is no such path -- remove */
+                       if (remove_file_from_cache(ptr))
+                               die("git-update-index: unable to remove %s",
+                                   ptr);
+               }
+               else {
+                       /* mode ' ' sha1 '\t' name
+                        * ptr[-1] points at tab,
+                        * ptr[-41] is at the beginning of sha1
+                        */
+                       ptr[-42] = ptr[-1] = 0;
+                       if (add_cacheinfo(buf.buf, ptr-41, ptr))
+                               die("git-update-index: unable to update %s",
+                                   ptr);
+               }
+               continue;
+
+       bad_line:
+               die("malformed index info %s", buf.buf);
+       }
+}
+
 int main(int argc, const char **argv)
 {
        int i, newfd, entries, has_errors = 0, line_termination = '\n';
@@ -373,6 +394,11 @@ int main(int argc, const char **argv)
                                read_from_stdin = 1;
                                break;
                        }
+                       if (!strcmp(path, "--index-info")) {
+                               allow_add = allow_replace = allow_remove = 1;
+                               read_index_info(line_termination);
+                               continue;
+                       }
                        if (!strcmp(path, "--ignore-missing")) {
                                not_new = 1;
                                continue;