]> git.ipfire.org Git - thirdparty/git.git/blobdiff - read-cache.c
Optimize "diff --cached" performance.
[thirdparty/git.git] / read-cache.c
index e060392d1da89f4446431fa361cd0e2c95ec3925..865369df0e7210446853c6704f68f12a8872124b 100644 (file)
@@ -665,7 +665,7 @@ static int check_file_directory_conflict(struct index_state *istate,
        return retval + has_dir_name(istate, ce, pos, ok_to_replace);
 }
 
-int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
+static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
 {
        int pos;
        int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
@@ -707,6 +707,22 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
                pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags));
                pos = -pos-1;
        }
+       return pos + 1;
+}
+
+int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
+{
+       int pos;
+
+       if (option & ADD_CACHE_JUST_APPEND)
+               pos = istate->cache_nr;
+       else {
+               int ret;
+               ret = add_index_entry_with_check(istate, ce, option);
+               if (ret <= 0)
+                       return ret;
+               pos = ret - 1;
+       }
 
        /* Make sure the array is big enough .. */
        if (istate->cache_nr == istate->cache_alloc) {
@@ -717,7 +733,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
 
        /* Add it in.. */
        istate->cache_nr++;
-       if (istate->cache_nr > pos)
+       if (istate->cache_nr > pos + 1)
                memmove(istate->cache + pos + 1,
                        istate->cache + pos,
                        (istate->cache_nr - pos - 1) * sizeof(ce));