checkout-index: plug memory leak from prefix_path()
[gitweb.git] / update-index.c
index 64f4c4912ea41944f892d0e1be2bc386dc582c0d..1870ac79669142d2c63e30ae035637eb55cf2850 100644 (file)
@@ -329,7 +329,7 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
        return 0;
 }
 
-static int chmod_path(int flip, const char *path)
+static void chmod_path(int flip, const char *path)
 {
        int pos;
        struct cache_entry *ce;
@@ -337,21 +337,24 @@ static int chmod_path(int flip, const char *path)
 
        pos = cache_name_pos(path, strlen(path));
        if (pos < 0)
-               return -1;
+               goto fail;
        ce = active_cache[pos];
        mode = ntohl(ce->ce_mode);
        if (!S_ISREG(mode))
-               return -1;
+               goto fail;
        switch (flip) {
        case '+':
                ce->ce_mode |= htonl(0111); break;
        case '-':
                ce->ce_mode &= htonl(~0111); break;
        default:
-               return -1;
+               goto fail;
        }
        active_cache_changed = 1;
-       return 0;
+       report("chmod %cx '%s'", flip, path);
+       return;
+ fail:
+       die("git-update-index: cannot chmod %cx '%s'", flip, path);
 }
 
 static struct cache_file cache_file;
@@ -470,7 +473,7 @@ static void read_index_info(int line_termination)
 }
 
 static const char update_index_usage[] =
-"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--cacheinfo] [--chmod=(+|-)x] [--info-only] [--force-remove] [--stdin] [--index-info] [--ignore-missing] [-z] [--verbose] [--] <file>...";
+"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--ignore-missing] [-z] [--verbose] [--] <file>...";
 
 static unsigned char head_sha1[20];
 static unsigned char merge_head_sha1[20];
@@ -573,7 +576,8 @@ static void read_head_pointers(void)
        }
 }
 
-static int do_unresolve(int ac, const char **av)
+static int do_unresolve(int ac, const char **av,
+                       const char *prefix, int prefix_length)
 {
        int i;
        int err = 0;
@@ -585,7 +589,10 @@ static int do_unresolve(int ac, const char **av)
 
        for (i = 1; i < ac; i++) {
                const char *arg = av[i];
-               err |= unresolve_one(arg);
+               const char *p = prefix_path(prefix, prefix_length, arg);
+               err |= unresolve_one(p);
+               if (p != arg)
+                       free((char*)p);
        }
        return err;
 }
@@ -597,6 +604,7 @@ int main(int argc, const char **argv)
        int read_from_stdin = 0;
        const char *prefix = setup_git_directory();
        int prefix_length = prefix ? strlen(prefix) : 0;
+       char set_executable_bit = 0;
 
        git_config(git_default_config);
 
@@ -663,8 +671,7 @@ int main(int argc, const char **argv)
                            !strcmp(path, "--chmod=+x")) {
                                if (argc <= i+1)
                                        die("git-update-index: %s <path>", path);
-                               if (chmod_path(path[8], argv[++i]))
-                                       die("git-update-index: %s cannot chmod %s", path, argv[i]);
+                               set_executable_bit = path[8];
                                continue;
                        }
                        if (!strcmp(path, "--assume-unchanged")) {
@@ -701,7 +708,8 @@ int main(int argc, const char **argv)
                                break;
                        }
                        if (!strcmp(path, "--unresolve")) {
-                               has_errors = do_unresolve(argc - i, argv + i);
+                               has_errors = do_unresolve(argc - i, argv + i,
+                                                         prefix, prefix_length);
                                if (has_errors)
                                        active_cache_changed = 0;
                                goto finish;
@@ -719,6 +727,8 @@ int main(int argc, const char **argv)
                        die("unknown option %s", path);
                }
                update_one(path, prefix, prefix_length);
+               if (set_executable_bit)
+                       chmod_path(set_executable_bit, path);
        }
        if (read_from_stdin) {
                struct strbuf buf;
@@ -733,6 +743,10 @@ int main(int argc, const char **argv)
                        else
                                path_name = buf.buf;
                        update_one(path_name, prefix, prefix_length);
+                       if (set_executable_bit) {
+                               const char *p = prefix_path(prefix, prefix_length, path_name);
+                               chmod_path(set_executable_bit, p);
+                       }
                        if (path_name != buf.buf)
                                free(path_name);
                }