Imported Upstream version 2.14.0
[platform/upstream/git.git] / read-cache.c
index aa99f1f..2121b6e 100644 (file)
@@ -5,6 +5,7 @@
  */
 #define NO_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
+#include "config.h"
 #include "tempfile.h"
 #include "lockfile.h"
 #include "cache-tree.h"
@@ -732,7 +733,7 @@ struct cache_entry *make_cache_entry(unsigned int mode,
        int size, len;
        struct cache_entry *ce, *ret;
 
-       if (!verify_path(path, mode)) {
+       if (!verify_path(path)) {
                error("Invalid path '%s'", path);
                return NULL;
        }
@@ -796,7 +797,7 @@ int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
  * Also, we don't want double slashes or slashes at the
  * end that can make pathnames ambiguous.
  */
-static int verify_dotfile(const char *rest, unsigned mode)
+static int verify_dotfile(const char *rest)
 {
        /*
         * The first character was '.', but that
@@ -810,13 +811,8 @@ static int verify_dotfile(const char *rest, unsigned mode)
 
        switch (*rest) {
        /*
-        * ".git" followed by NUL or slash is bad. Note that we match
-        * case-insensitively here, even if ignore_case is not set.
-        * This outlaws ".GIT" everywhere out of an abundance of caution,
-        * since there's really no good reason to allow it.
-        *
-        * Once we've seen ".git", we can also find ".gitmodules", etc (also
-        * case-insensitively).
+        * ".git" followed by  NUL or slash is bad. This
+        * shares the path end test with the ".." case.
         */
        case 'g':
        case 'G':
@@ -824,15 +820,8 @@ static int verify_dotfile(const char *rest, unsigned mode)
                        break;
                if (rest[2] != 't' && rest[2] != 'T')
                        break;
-               if (rest[3] == '\0' || is_dir_sep(rest[3]))
-                       return 0;
-               if (S_ISLNK(mode)) {
-                       rest += 3;
-                       if (skip_iprefix(rest, "modules", &rest) &&
-                           (*rest == '\0' || is_dir_sep(*rest)))
-                               return 0;
-               }
-               break;
+               rest += 2;
+       /* fallthrough */
        case '.':
                if (rest[1] == '\0' || is_dir_sep(rest[1]))
                        return 0;
@@ -840,7 +829,7 @@ static int verify_dotfile(const char *rest, unsigned mode)
        return 1;
 }
 
-int verify_path(const char *path, unsigned mode)
+int verify_path(const char *path)
 {
        char c;
 
@@ -853,25 +842,12 @@ int verify_path(const char *path, unsigned mode)
                        return 1;
                if (is_dir_sep(c)) {
 inside:
-                       if (protect_hfs) {
-                               if (is_hfs_dotgit(path))
-                                       return 0;
-                               if (S_ISLNK(mode)) {
-                                       if (is_hfs_dotgitmodules(path))
-                                               return 0;
-                               }
-                       }
-                       if (protect_ntfs) {
-                               if (is_ntfs_dotgit(path))
-                                       return 0;
-                               if (S_ISLNK(mode)) {
-                                       if (is_ntfs_dotgitmodules(path))
-                                               return 0;
-                               }
-                       }
-
+                       if (protect_hfs && is_hfs_dotgit(path))
+                               return 0;
+                       if (protect_ntfs && is_ntfs_dotgit(path))
+                               return 0;
                        c = *path++;
-                       if ((c == '.' && !verify_dotfile(path, mode)) ||
+                       if ((c == '.' && !verify_dotfile(path)) ||
                            is_dir_sep(c) || c == '\0')
                                return 0;
                }
@@ -1188,7 +1164,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_e
 
        if (!ok_to_add)
                return -1;
-       if (!verify_path(ce->name, ce->ce_mode))
+       if (!verify_path(ce->name))
                return error("Invalid path '%s'", ce->name);
 
        if (!skip_df_check &&
@@ -1919,8 +1895,7 @@ int discard_index(struct index_state *istate)
        free_name_hash(istate);
        cache_tree_free(&(istate->cache_tree));
        istate->initialized = 0;
-       free(istate->cache);
-       istate->cache = NULL;
+       FREE_AND_NULL(istate->cache);
        istate->cache_alloc = 0;
        discard_split_index(istate);
        free_untracked_cache(istate->untracked);
@@ -2223,6 +2198,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
        int entries = istate->cache_nr;
        struct stat st;
        struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
+       int drop_cache_tree = 0;
 
        for (i = removed = extended = 0; i < entries; i++) {
                if (cache[i]->ce_flags & CE_REMOVE)
@@ -2273,6 +2249,8 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
                                warning(msg, ce->name);
                        else
                                return error(msg, ce->name);
+
+                       drop_cache_tree = 1;
                }
                if (ce_write_entry(&c, newfd, ce, previous_name) < 0)
                        return -1;
@@ -2291,7 +2269,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
                if (err)
                        return -1;
        }
-       if (!strip_extensions && istate->cache_tree) {
+       if (!strip_extensions && !drop_cache_tree && istate->cache_tree) {
                struct strbuf sb = STRBUF_INIT;
 
                cache_tree_write(&sb, istate->cache_tree);
@@ -2639,8 +2617,7 @@ void *read_blob_data_from_index(const struct index_state *istate,
 
 void stat_validity_clear(struct stat_validity *sv)
 {
-       free(sv->sd);
-       sv->sd = NULL;
+       FREE_AND_NULL(sv->sd);
 }
 
 int stat_validity_check(struct stat_validity *sv, const char *path)
@@ -2666,3 +2643,9 @@ void stat_validity_update(struct stat_validity *sv, int fd)
                fill_stat_data(sv->sd, &st);
        }
 }
+
+void move_index_extensions(struct index_state *dst, struct index_state *src)
+{
+       dst->untracked = src->untracked;
+       src->untracked = NULL;
+}