[PATCH] Make the test more shell generic and fix missing Solaris find option
[git.git] / update-index.c
index d10dfd9..1ea36ea 100644 (file)
@@ -31,7 +31,7 @@ static inline long IS_ERR(const void *ptr)
        return (unsigned long)ptr > (unsigned long)-1000L;
 }
 
-static int add_file_to_cache(char *path)
+static int add_file_to_cache(const char *path)
 {
        int size, namelen, option, status;
        struct cache_entry *ce;
@@ -50,11 +50,20 @@ static int add_file_to_cache(char *path)
                 * case just fine without --force-remove.
                 */
                if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
-                       if (allow_remove)
-                               return remove_file_from_cache(path);
+                       if (allow_remove) {
+                               if (remove_file_from_cache(path))
+                                       return error("%s: cannot remove from the index",
+                                                    path);
+                               else
+                                       return 0;
+                       } else if (status < 0) {
+                               return error("%s: does not exist and --remove not passed",
+                                            path);
+                       }
                }
                if (0 == status)
-                       return error("%s: is a directory", path);
+                       return error("%s: is a directory - add files inside instead",
+                                    path);
                else
                        return error("lstat(\"%s\"): %s", path,
                                     strerror(errno));
@@ -71,15 +80,17 @@ static int add_file_to_cache(char *path)
        case S_IFREG:
                fd = open(path, O_RDONLY);
                if (fd < 0)
-                       return -1;
+                       return error("open(\"%s\"): %s", path, strerror(errno));
                if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
-                       return -1;
+                       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 -1;
+                       return error("readlink(\"%s\"): %s", path,
+                                    errstr);
                }
                if (info_only) {
                        unsigned char hdr[50];
@@ -87,56 +98,18 @@ static int add_file_to_cache(char *path)
                        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 -1;
+                       return error("%s: failed to insert into database", path);
                free(target);
                break;
        default:
-               return -1;
+               return error("%s: unsupported file type", path);
        }
        option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
        option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
-       return add_cache_entry(ce, option);
-}
-
-static int compare_data(struct cache_entry *ce, struct stat *st)
-{
-       int match = -1;
-       int fd = open(ce->name, O_RDONLY);
-
-       if (fd >= 0) {
-               unsigned char sha1[20];
-               if (!index_fd(sha1, fd, st, 0, NULL))
-                       match = memcmp(sha1, ce->sha1, 20);
-               close(fd);
-       }
-       return match;
-}
-
-static int compare_link(struct cache_entry *ce, unsigned long expected_size)
-{
-       int match = -1;
-       char *target;
-       void *buffer;
-       unsigned long size;
-       char type[10];
-       int len;
-
-       target = xmalloc(expected_size);
-       len = readlink(ce->name, target, expected_size);
-       if (len != expected_size) {
-               free(target);
-               return -1;
-       }
-       buffer = read_sha1_file(ce->sha1, type, &size);
-       if (!buffer) {
-               free(target);
-               return -1;
-       }
-       if (size == expected_size)
-               match = memcmp(buffer, target, size);
-       free(buffer);
-       free(target);
-       return match;
+       if (add_cache_entry(ce, option))
+               return error("%s: cannot add to the index - missing --add option?",
+                            path);
+       return 0;
 }
 
 /*
@@ -163,26 +136,9 @@ static struct cache_entry *refresh_entry(struct cache_entry *ce)
        if (!changed)
                return ce;
 
-       /*
-        * If the mode or type has changed, there's no point in trying
-        * to refresh the entry - it's not going to match
-        */
-       if (changed & (MODE_CHANGED | TYPE_CHANGED))
+       if (ce_modified(ce, &st))
                return ERR_PTR(-EINVAL);
 
-       switch (st.st_mode & S_IFMT) {
-       case S_IFREG:
-               if (compare_data(ce, &st))
-                       return ERR_PTR(-EINVAL);
-               break;
-       case S_IFLNK:
-               if (compare_link(ce, st.st_size))
-                       return ERR_PTR(-EINVAL);
-               break;
-       default:
-               return ERR_PTR(-EINVAL);
-       }
-
        size = ce_size(ce);
        updated = xmalloc(size);
        memcpy(updated, ce, size);
@@ -265,7 +221,7 @@ static int verify_dotfile(const char *rest)
        return 1;
 }
 
-static int verify_path(char *path)
+static int verify_path(const char *path)
 {
        char c;
 
@@ -291,7 +247,7 @@ inside:
        }
 }
 
-static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
+static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
 {
        int size, len, option;
        unsigned int mode;
@@ -321,7 +277,7 @@ static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
 
 static struct cache_file cache_file;
 
-int main(int argc, char **argv)
+int main(int argc, const char **argv)
 {
        int i, newfd, entries, has_errors = 0;
        int allow_options = 1;
@@ -336,7 +292,7 @@ int main(int argc, char **argv)
                die("cache corrupted");
 
        for (i = 1 ; i < argc; i++) {
-               char *path = argv[i];
+               const char *path = argv[i];
 
                if (allow_options && *path == '-') {
                        if (!strcmp(path, "--")) {
@@ -393,11 +349,11 @@ int main(int argc, char **argv)
                }
                if (force_remove) {
                        if (remove_file_from_cache(path))
-                               die("git-update-index: --force-remove cannot remove %s", path);
+                               die("git-update-index: unable to remove %s", path);
                        continue;
                }
                if (add_file_to_cache(path))
-                       die("Unable to add %s to database; maybe you want to use --add option?", path);
+                       die("Unable to process file %s", path);
        }
        if (write_cache(newfd, active_cache, active_nr) ||
            commit_index_file(&cache_file))