receive-pack: simplify keep_arg computation
[gitweb.git] / unpack-trees.c
index d6cf84904f189a284b4bd1d72c09ee975207700e..8e2032f4e592910d6f3336f95019647992113877 100644 (file)
@@ -1029,10 +1029,12 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
        if (!core_apply_sparse_checkout || !o->update)
                o->skip_sparse_checkout = 1;
        if (!o->skip_sparse_checkout) {
-               if (add_excludes_from_file_to_list(git_path("info/sparse-checkout"), "", 0, &el, 0) < 0)
+               char *sparse = git_pathdup("info/sparse-checkout");
+               if (add_excludes_from_file_to_list(sparse, "", 0, &el, 0) < 0)
                        o->skip_sparse_checkout = 1;
                else
                        o->el = &el;
+               free(sparse);
        }
 
        memset(&o->result, 0, sizeof(o->result));
@@ -1160,6 +1162,14 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
        o->src_index = NULL;
        ret = check_updates(o) ? (-2) : 0;
        if (o->dst_index) {
+               if (!ret) {
+                       if (!o->result.cache_tree)
+                               o->result.cache_tree = cache_tree();
+                       if (!cache_tree_fully_valid(o->result.cache_tree))
+                               cache_tree_update(&o->result,
+                                                 WRITE_TREE_SILENT |
+                                                 WRITE_TREE_REPAIR);
+               }
                discard_index(o->dst_index);
                *o->dst_index = o->result;
        } else {
@@ -1340,9 +1350,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
         * Then we need to make sure that we do not lose a locally
         * present file that is not ignored.
         */
-       pathbuf = xmalloc(namelen + 2);
-       memcpy(pathbuf, ce->name, namelen);
-       strcpy(pathbuf+namelen, "/");
+       pathbuf = xstrfmt("%.*s/", namelen, ce->name);
 
        memset(&d, 0, sizeof(d));
        if (o->dir)
@@ -1441,15 +1449,18 @@ static int verify_absent_1(const struct cache_entry *ce,
        if (!len)
                return 0;
        else if (len > 0) {
-               char path[PATH_MAX + 1];
-               memcpy(path, ce->name, len);
-               path[len] = 0;
+               char *path;
+               int ret;
+
+               path = xmemdupz(ce->name, len);
                if (lstat(path, &st))
-                       return error("cannot stat '%s': %s", path,
+                       ret = error("cannot stat '%s': %s", path,
                                        strerror(errno));
-
-               return check_ok_to_remove(path, len, DT_UNKNOWN, NULL, &st,
-                               error_type, o);
+               else
+                       ret = check_ok_to_remove(path, len, DT_UNKNOWN, NULL,
+                                                &st, error_type, o);
+               free(path);
+               return ret;
        } else if (lstat(ce->name, &st)) {
                if (errno != ENOENT)
                        return error("cannot stat '%s': %s", ce->name,