grep: fix grepping for "intent to add" files
[gitweb.git] / read-cache.c
index 5b922fd583317335b75a428de84420f2f7f4eb3c..fb80c47e3820009a0c40797cf33892177afdf153 100644 (file)
@@ -311,7 +311,7 @@ int ie_match_stat(const struct index_state *istate,
         * by definition never matches what is in the work tree until it
         * actually gets added.
         */
-       if (ce->ce_flags & CE_INTENT_TO_ADD)
+       if (ce_intent_to_add(ce))
                return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
 
        changed = ce_match_stat_basic(ce, st);
@@ -748,12 +748,9 @@ struct cache_entry *make_cache_entry(unsigned int mode,
        ce->ce_mode = create_ce_mode(mode);
 
        ret = refresh_cache_entry(ce, refresh_options);
-       if (!ret) {
+       if (ret != ce)
                free(ce);
-               return NULL;
-       } else {
-               return ret;
-       }
+       return ret;
 }
 
 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
@@ -1234,7 +1231,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,
 
                        if (cache_errno == ENOENT)
                                fmt = deleted_fmt;
-                       else if (ce->ce_flags & CE_INTENT_TO_ADD)
+                       else if (ce_intent_to_add(ce))
                                fmt = added_fmt; /* must be before other checks */
                        else if (changed & TYPE_CHANGED)
                                fmt = typechange_fmt;
@@ -1491,18 +1488,25 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,
        return ce;
 }
 
-static void check_ce_order(struct cache_entry *ce, struct cache_entry *next_ce)
+static void check_ce_order(struct index_state *istate)
 {
-       int name_compare = strcmp(ce->name, next_ce->name);
-       if (0 < name_compare)
-               die("unordered stage entries in index");
-       if (!name_compare) {
-               if (!ce_stage(ce))
-                       die("multiple stage entries for merged file '%s'",
-                               ce->name);
-               if (ce_stage(ce) > ce_stage(next_ce))
-                       die("unordered stage entries for '%s'",
-                               ce->name);
+       unsigned int i;
+
+       for (i = 1; i < istate->cache_nr; i++) {
+               struct cache_entry *ce = istate->cache[i - 1];
+               struct cache_entry *next_ce = istate->cache[i];
+               int name_compare = strcmp(ce->name, next_ce->name);
+
+               if (0 < name_compare)
+                       die("unordered stage entries in index");
+               if (!name_compare) {
+                       if (!ce_stage(ce))
+                               die("multiple stage entries for merged file '%s'",
+                                   ce->name);
+                       if (ce_stage(ce) > ce_stage(next_ce))
+                               die("unordered stage entries for '%s'",
+                                   ce->name);
+               }
        }
 }
 
@@ -1567,9 +1571,6 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)
                ce = create_from_disk(disk_ce, &consumed, previous_name);
                set_index_entry(istate, i, ce);
 
-               if (i > 0)
-                       check_ce_order(istate->cache[i - 1], ce);
-
                src_offset += consumed;
        }
        strbuf_release(&previous_name_buf);
@@ -1613,11 +1614,10 @@ int read_index_from(struct index_state *istate, const char *path)
 
        ret = do_read_index(istate, path, 0);
        split_index = istate->split_index;
-       if (!split_index)
-               return ret;
-
-       if (is_null_sha1(split_index->base_sha1))
+       if (!split_index || is_null_sha1(split_index->base_sha1)) {
+               check_ce_order(istate);
                return ret;
+       }
 
        if (split_index->base)
                discard_index(split_index->base);
@@ -1633,6 +1633,7 @@ int read_index_from(struct index_state *istate, const char *path)
                                     sha1_to_hex(split_index->base_sha1)),
                    sha1_to_hex(split_index->base->sha1));
        merge_base_index(istate);
+       check_ce_order(istate);
        return ret;
 }