refs.c: add refs_ref_exists()
[gitweb.git] / split-index.c
index 187b910f5b5b83ae5fb236c58559157c8c195bf7..e6154e4ea9552cfc2b868453f017ba786cd24a2f 100644 (file)
@@ -162,7 +162,7 @@ void merge_base_index(struct index_state *istate)
        ewah_each_bit(si->replace_bitmap, replace_entry, istate);
        ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
        if (si->nr_deletions)
-               remove_marked_cache_entries(istate);
+               remove_marked_cache_entries(istate, 0);
 
        for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
                if (!ce_namelen(si->saved_cache[i]))
@@ -254,13 +254,44 @@ void prepare_to_write_split_index(struct index_state *istate)
                                continue;
                        }
                        if (ce->index > si->base->cache_nr) {
-                               ce->index = 0;
-                               continue;
+                               BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
+                                   ce->index, si->base->cache_nr);
                        }
                        ce->ce_flags |= CE_MATCHED; /* or "shared" */
                        base = si->base->cache[ce->index - 1];
-                       if (ce == base)
+                       if (ce == base) {
+                               /* The entry is present in the shared index. */
+                               if (ce->ce_flags & CE_UPDATE_IN_BASE) {
+                                       /*
+                                        * Already marked for inclusion in
+                                        * the split index, either because
+                                        * the corresponding file was
+                                        * modified and the cached stat data
+                                        * was refreshed, or because there
+                                        * is already a replacement entry in
+                                        * the split index.
+                                        * Nothing more to do here.
+                                        */
+                               } else if (!ce_uptodate(ce) &&
+                                          is_racy_timestamp(istate, ce)) {
+                                       /*
+                                        * A racily clean cache entry stored
+                                        * only in the shared index: it must
+                                        * be added to the split index, so
+                                        * the subsequent do_write_index()
+                                        * can smudge its stat data.
+                                        */
+                                       ce->ce_flags |= CE_UPDATE_IN_BASE;
+                               } else {
+                                       /*
+                                        * The entry is only present in the
+                                        * shared index and it was not
+                                        * refreshed.
+                                        * Just leave it there.
+                                        */
+                               }
                                continue;
+                       }
                        if (ce->ce_namelen != base->ce_namelen ||
                            strcmp(ce->name, base->name)) {
                                ce->index = 0;
@@ -281,6 +312,15 @@ void prepare_to_write_split_index(struct index_state *istate)
                                 * the split index.
                                 * Nothing to do.
                                 */
+                       } else if (!ce_uptodate(ce) &&
+                                  is_racy_timestamp(istate, ce)) {
+                               /*
+                                * A copy of a racily clean cache entry from
+                                * the shared index.  It must be added to
+                                * the split index, so the subsequent
+                                * do_write_index() can smudge its stat data.
+                                */
+                               ce->ce_flags |= CE_UPDATE_IN_BASE;
                        } else {
                                /*
                                 * Thoroughly compare the cached data to see
@@ -400,24 +440,26 @@ void add_split_index(struct index_state *istate)
 void remove_split_index(struct index_state *istate)
 {
        if (istate->split_index) {
-               /*
-                * When removing the split index, we need to move
-                * ownership of the mem_pool associated with the
-                * base index to the main index. There may be cache entries
-                * allocated from the base's memory pool that are shared with
-                * the_index.cache[].
-                */
-               mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
+               if (istate->split_index->base) {
+                       /*
+                        * When removing the split index, we need to move
+                        * ownership of the mem_pool associated with the
+                        * base index to the main index. There may be cache entries
+                        * allocated from the base's memory pool that are shared with
+                        * the_index.cache[].
+                        */
+                       mem_pool_combine(istate->ce_mem_pool,
+                                        istate->split_index->base->ce_mem_pool);
 
-               /*
-                * The split index no longer owns the mem_pool backing
-                * its cache array. As we are discarding this index,
-                * mark the index as having no cache entries, so it
-                * will not attempt to clean up the cache entries or
-                * validate them.
-                */
-               if (istate->split_index->base)
+                       /*
+                        * The split index no longer owns the mem_pool backing
+                        * its cache array. As we are discarding this index,
+                        * mark the index as having no cache entries, so it
+                        * will not attempt to clean up the cache entries or
+                        * validate them.
+                        */
                        istate->split_index->base->cache_nr = 0;
+               }
 
                /*
                 * We can discard the split index because its