read_index_from(): catch out of order entries when reading an index file
authorJaime Soriano Pastor <jsorianopastor@gmail.com>
Fri, 29 Aug 2014 08:54:41 +0000 (10:54 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 29 Aug 2014 17:05:14 +0000 (10:05 -0700)
Signed-off-by: Jaime Soriano Pastor <jsorianopastor@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
read-cache.c
index 7f5645e74546e459efdb584dbf63e1fd75857317..22b0add52f28cbe3c8dab727a1959d8ba72c4688 100644 (file)
@@ -1438,6 +1438,21 @@ 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)
+{
+       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);
+       }
+}
+
 /* remember to discard_cache() before reading a different cache! */
 int read_index_from(struct index_state *istate, const char *path)
 {
@@ -1499,6 +1514,9 @@ int read_index_from(struct index_state *istate, const char *path)
                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);