Patrick Steinhardt | e7da938 | 2024-06-14 08:50:23 +0200 | [diff] [blame] | 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 3 | #include "git-compat-util.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 4 | #include "gettext.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 5 | #include "hash.h" |
Elijah Newren | 5bc0722 | 2023-04-11 00:41:47 -0700 | [diff] [blame] | 6 | #include "mem-pool.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 7 | #include "read-cache-ll.h" |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 8 | #include "split-index.h" |
Elijah Newren | 69a63fe | 2023-04-22 20:17:08 +0000 | [diff] [blame] | 9 | #include "strbuf.h" |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 10 | #include "ewah/ewok.h" |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 11 | |
| 12 | struct split_index *init_split_index(struct index_state *istate) |
| 13 | { |
| 14 | if (!istate->split_index) { |
Johannes Schindelin | 451b66c | 2022-01-19 17:29:39 +0000 | [diff] [blame] | 15 | if (istate->sparse_index) |
| 16 | die(_("cannot use split index with a sparse index")); |
| 17 | |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 18 | CALLOC_ARRAY(istate->split_index, 1); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 19 | istate->split_index->refcount = 1; |
| 20 | } |
| 21 | return istate->split_index; |
| 22 | } |
| 23 | |
| 24 | int read_link_extension(struct index_state *istate, |
| 25 | const void *data_, unsigned long sz) |
| 26 | { |
| 27 | const unsigned char *data = data_; |
| 28 | struct split_index *si; |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 29 | int ret; |
| 30 | |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 31 | if (sz < the_hash_algo->rawsz) |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 32 | return error("corrupt link extension (too short)"); |
| 33 | si = init_split_index(istate); |
Patrick Steinhardt | 9da95bd | 2024-06-14 08:49:54 +0200 | [diff] [blame] | 34 | oidread(&si->base_oid, data, the_repository->hash_algo); |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 35 | data += the_hash_algo->rawsz; |
| 36 | sz -= the_hash_algo->rawsz; |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 37 | if (!sz) |
| 38 | return 0; |
| 39 | si->delete_bitmap = ewah_new(); |
| 40 | ret = ewah_read_mmap(si->delete_bitmap, data, sz); |
| 41 | if (ret < 0) |
| 42 | return error("corrupt delete bitmap in link extension"); |
| 43 | data += ret; |
| 44 | sz -= ret; |
| 45 | si->replace_bitmap = ewah_new(); |
| 46 | ret = ewah_read_mmap(si->replace_bitmap, data, sz); |
| 47 | if (ret < 0) |
| 48 | return error("corrupt replace bitmap in link extension"); |
| 49 | if (ret != sz) |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 50 | return error("garbage at the end of link extension"); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | int write_link_extension(struct strbuf *sb, |
| 55 | struct index_state *istate) |
| 56 | { |
| 57 | struct split_index *si = istate->split_index; |
brian m. carlson | 2182abd | 2018-05-02 00:25:43 +0000 | [diff] [blame] | 58 | strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz); |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 59 | if (!si->delete_bitmap && !si->replace_bitmap) |
| 60 | return 0; |
Nguyễn Thái Ngọc Duy | be0d9d5 | 2015-03-08 17:12:32 +0700 | [diff] [blame] | 61 | ewah_serialize_strbuf(si->delete_bitmap, sb); |
| 62 | ewah_serialize_strbuf(si->replace_bitmap, sb); |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static void mark_base_index_entries(struct index_state *base) |
| 67 | { |
| 68 | int i; |
| 69 | /* |
| 70 | * To keep track of the shared entries between |
| 71 | * istate->base->cache[] and istate->cache[], base entry |
| 72 | * position is stored in each base entry. All positions start |
Li Peng | 832c0e5 | 2016-05-06 20:36:46 +0800 | [diff] [blame] | 73 | * from 1 instead of 0, which is reserved to say "this is a new |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 74 | * entry". |
| 75 | */ |
| 76 | for (i = 0; i < base->cache_nr; i++) |
| 77 | base->cache[i]->index = i + 1; |
| 78 | } |
| 79 | |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 80 | void move_cache_to_base_index(struct index_state *istate) |
| 81 | { |
| 82 | struct split_index *si = istate->split_index; |
| 83 | int i; |
| 84 | |
| 85 | /* |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 86 | * If there was a previous base index, then transfer ownership of allocated |
| 87 | * entries to the parent index. |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 88 | */ |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 89 | if (si->base && |
| 90 | si->base->ce_mem_pool) { |
| 91 | |
Elijah Newren | 44c7e1a | 2020-08-15 17:37:56 +0000 | [diff] [blame] | 92 | if (!istate->ce_mem_pool) { |
| 93 | istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool)); |
| 94 | mem_pool_init(istate->ce_mem_pool, 0); |
| 95 | } |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 96 | |
| 97 | mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool); |
| 98 | } |
| 99 | |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 100 | ALLOC_ARRAY(si->base, 1); |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 101 | index_state_init(si->base, istate->repo); |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 102 | si->base->version = istate->version; |
| 103 | /* zero timestamp disables racy test in ce_write_index() */ |
| 104 | si->base->timestamp = istate->timestamp; |
| 105 | ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc); |
| 106 | si->base->cache_nr = istate->cache_nr; |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * The mem_pool needs to move with the allocated entries. |
| 110 | */ |
| 111 | si->base->ce_mem_pool = istate->ce_mem_pool; |
| 112 | istate->ce_mem_pool = NULL; |
| 113 | |
René Scharfe | 45ccef8 | 2016-09-25 09:24:03 +0200 | [diff] [blame] | 114 | COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr); |
Nguyễn Thái Ngọc Duy | c18b80a | 2014-06-13 19:19:44 +0700 | [diff] [blame] | 115 | mark_base_index_entries(si->base); |
| 116 | for (i = 0; i < si->base->cache_nr; i++) |
| 117 | si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE; |
| 118 | } |
| 119 | |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 120 | static void mark_entry_for_delete(size_t pos, void *data) |
| 121 | { |
| 122 | struct index_state *istate = data; |
| 123 | if (pos >= istate->cache_nr) |
| 124 | die("position for delete %d exceeds base index size %d", |
| 125 | (int)pos, istate->cache_nr); |
| 126 | istate->cache[pos]->ce_flags |= CE_REMOVE; |
SZEDER Gábor | 2034e84 | 2018-10-11 11:43:07 +0200 | [diff] [blame] | 127 | istate->split_index->nr_deletions++; |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void replace_entry(size_t pos, void *data) |
| 131 | { |
| 132 | struct index_state *istate = data; |
| 133 | struct split_index *si = istate->split_index; |
| 134 | struct cache_entry *dst, *src; |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 135 | |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 136 | if (pos >= istate->cache_nr) |
| 137 | die("position for replacement %d exceeds base index size %d", |
| 138 | (int)pos, istate->cache_nr); |
| 139 | if (si->nr_replacements >= si->saved_cache_nr) |
| 140 | die("too many replacements (%d vs %d)", |
| 141 | si->nr_replacements, si->saved_cache_nr); |
| 142 | dst = istate->cache[pos]; |
| 143 | if (dst->ce_flags & CE_REMOVE) |
| 144 | die("entry %d is marked as both replaced and deleted", |
| 145 | (int)pos); |
| 146 | src = si->saved_cache[si->nr_replacements]; |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 147 | if (ce_namelen(src)) |
| 148 | die("corrupt link extension, entry %d should have " |
| 149 | "zero length name", (int)pos); |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 150 | src->index = pos + 1; |
| 151 | src->ce_flags |= CE_UPDATE_IN_BASE; |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 152 | src->ce_namelen = dst->ce_namelen; |
| 153 | copy_cache_entry(dst, src); |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 154 | discard_cache_entry(src); |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 155 | si->nr_replacements++; |
| 156 | } |
| 157 | |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 158 | void merge_base_index(struct index_state *istate) |
| 159 | { |
| 160 | struct split_index *si = istate->split_index; |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 161 | unsigned int i; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 162 | |
| 163 | mark_base_index_entries(si->base); |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 164 | |
| 165 | si->saved_cache = istate->cache; |
| 166 | si->saved_cache_nr = istate->cache_nr; |
| 167 | istate->cache_nr = si->base->cache_nr; |
| 168 | istate->cache = NULL; |
| 169 | istate->cache_alloc = 0; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 170 | ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc); |
René Scharfe | 45ccef8 | 2016-09-25 09:24:03 +0200 | [diff] [blame] | 171 | COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr); |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 172 | |
| 173 | si->nr_deletions = 0; |
| 174 | si->nr_replacements = 0; |
| 175 | ewah_each_bit(si->replace_bitmap, replace_entry, istate); |
| 176 | ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate); |
| 177 | if (si->nr_deletions) |
Thomas Gummerer | 6fdc205 | 2018-12-20 13:48:16 +0000 | [diff] [blame] | 178 | remove_marked_cache_entries(istate, 0); |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 179 | |
| 180 | for (i = si->nr_replacements; i < si->saved_cache_nr; i++) { |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 181 | if (!ce_namelen(si->saved_cache[i])) |
| 182 | die("corrupt link extension, entry %d should " |
| 183 | "have non-zero length name", i); |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 184 | add_index_entry(istate, si->saved_cache[i], |
| 185 | ADD_CACHE_OK_TO_ADD | |
Nguyễn Thái Ngọc Duy | ce7c614 | 2014-06-13 19:19:42 +0700 | [diff] [blame] | 186 | ADD_CACHE_KEEP_CACHE_TREE | |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 187 | /* |
| 188 | * we may have to replay what |
| 189 | * merge-recursive.c:update_stages() |
| 190 | * does, which has this flag on |
| 191 | */ |
| 192 | ADD_CACHE_SKIP_DFCHECK); |
| 193 | si->saved_cache[i] = NULL; |
| 194 | } |
| 195 | |
| 196 | ewah_free(si->delete_bitmap); |
| 197 | ewah_free(si->replace_bitmap); |
Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 +0000 | [diff] [blame] | 198 | FREE_AND_NULL(si->saved_cache); |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 199 | si->delete_bitmap = NULL; |
| 200 | si->replace_bitmap = NULL; |
Nguyễn Thái Ngọc Duy | 76b07b3 | 2014-06-13 19:19:41 +0700 | [diff] [blame] | 201 | si->saved_cache_nr = 0; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 202 | } |
| 203 | |
SZEDER Gábor | e3d8379 | 2018-10-11 11:43:08 +0200 | [diff] [blame] | 204 | /* |
| 205 | * Compare most of the fields in two cache entries, i.e. all except the |
| 206 | * hashmap_entry and the name. |
| 207 | */ |
| 208 | static int compare_ce_content(struct cache_entry *a, struct cache_entry *b) |
| 209 | { |
| 210 | const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID | |
| 211 | CE_EXTENDED_FLAGS; |
| 212 | unsigned int ce_flags = a->ce_flags; |
| 213 | unsigned int base_flags = b->ce_flags; |
| 214 | int ret; |
| 215 | |
| 216 | /* only on-disk flags matter */ |
| 217 | a->ce_flags &= ondisk_flags; |
| 218 | b->ce_flags &= ondisk_flags; |
| 219 | ret = memcmp(&a->ce_stat_data, &b->ce_stat_data, |
| 220 | offsetof(struct cache_entry, name) - |
Andrzej Hunt | 09751bf | 2021-06-14 15:51:15 +0000 | [diff] [blame] | 221 | offsetof(struct cache_entry, oid)) || |
| 222 | !oideq(&a->oid, &b->oid); |
SZEDER Gábor | e3d8379 | 2018-10-11 11:43:08 +0200 | [diff] [blame] | 223 | a->ce_flags = ce_flags; |
| 224 | b->ce_flags = base_flags; |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 229 | void prepare_to_write_split_index(struct index_state *istate) |
| 230 | { |
| 231 | struct split_index *si = init_split_index(istate); |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 232 | struct cache_entry **entries = NULL, *ce; |
| 233 | int i, nr_entries = 0, nr_alloc = 0; |
| 234 | |
| 235 | si->delete_bitmap = ewah_new(); |
| 236 | si->replace_bitmap = ewah_new(); |
| 237 | |
| 238 | if (si->base) { |
| 239 | /* Go through istate->cache[] and mark CE_MATCHED to |
| 240 | * entry with positive index. We'll go through |
| 241 | * base->cache[] later to delete all entries in base |
Christian Couder | 753c451 | 2016-10-23 11:26:30 +0200 | [diff] [blame] | 242 | * that are not marked with either CE_MATCHED or |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 243 | * CE_UPDATE_IN_BASE. If istate->cache[i] is a |
| 244 | * duplicate, deduplicate it. |
| 245 | */ |
| 246 | for (i = 0; i < istate->cache_nr; i++) { |
| 247 | struct cache_entry *base; |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 248 | ce = istate->cache[i]; |
SZEDER Gábor | e3d8379 | 2018-10-11 11:43:08 +0200 | [diff] [blame] | 249 | if (!ce->index) { |
| 250 | /* |
| 251 | * During simple update index operations this |
| 252 | * is a cache entry that is not present in |
| 253 | * the shared index. It will be added to the |
| 254 | * split index. |
| 255 | * |
| 256 | * However, it might also represent a file |
| 257 | * that already has a cache entry in the |
| 258 | * shared index, but a new index has just |
| 259 | * been constructed by unpack_trees(), and |
| 260 | * this entry now refers to different content |
| 261 | * than what was recorded in the original |
| 262 | * index, e.g. during 'read-tree -m HEAD^' or |
| 263 | * 'checkout HEAD^'. In this case the |
| 264 | * original entry in the shared index will be |
| 265 | * marked as deleted, and this entry will be |
| 266 | * added to the split index. |
| 267 | */ |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 268 | continue; |
SZEDER Gábor | e3d8379 | 2018-10-11 11:43:08 +0200 | [diff] [blame] | 269 | } |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 270 | if (ce->index > si->base->cache_nr) { |
SZEDER Gábor | 4c490f3 | 2018-10-11 11:53:57 +0200 | [diff] [blame] | 271 | BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d", |
| 272 | ce->index, si->base->cache_nr); |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 273 | } |
| 274 | ce->ce_flags |= CE_MATCHED; /* or "shared" */ |
| 275 | base = si->base->cache[ce->index - 1]; |
SZEDER Gábor | 5581a01 | 2018-10-11 11:43:09 +0200 | [diff] [blame] | 276 | if (ce == base) { |
| 277 | /* The entry is present in the shared index. */ |
| 278 | if (ce->ce_flags & CE_UPDATE_IN_BASE) { |
| 279 | /* |
| 280 | * Already marked for inclusion in |
| 281 | * the split index, either because |
| 282 | * the corresponding file was |
| 283 | * modified and the cached stat data |
| 284 | * was refreshed, or because there |
| 285 | * is already a replacement entry in |
| 286 | * the split index. |
| 287 | * Nothing more to do here. |
| 288 | */ |
| 289 | } else if (!ce_uptodate(ce) && |
| 290 | is_racy_timestamp(istate, ce)) { |
| 291 | /* |
| 292 | * A racily clean cache entry stored |
| 293 | * only in the shared index: it must |
| 294 | * be added to the split index, so |
| 295 | * the subsequent do_write_index() |
| 296 | * can smudge its stat data. |
| 297 | */ |
| 298 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
| 299 | } else { |
| 300 | /* |
| 301 | * The entry is only present in the |
| 302 | * shared index and it was not |
| 303 | * refreshed. |
| 304 | * Just leave it there. |
| 305 | */ |
| 306 | } |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 307 | continue; |
SZEDER Gábor | 5581a01 | 2018-10-11 11:43:09 +0200 | [diff] [blame] | 308 | } |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 309 | if (ce->ce_namelen != base->ce_namelen || |
| 310 | strcmp(ce->name, base->name)) { |
| 311 | ce->index = 0; |
| 312 | continue; |
| 313 | } |
SZEDER Gábor | e3d8379 | 2018-10-11 11:43:08 +0200 | [diff] [blame] | 314 | /* |
| 315 | * This is the copy of a cache entry that is present |
| 316 | * in the shared index, created by unpack_trees() |
| 317 | * while it constructed a new index. |
| 318 | */ |
| 319 | if (ce->ce_flags & CE_UPDATE_IN_BASE) { |
| 320 | /* |
| 321 | * Already marked for inclusion in the split |
| 322 | * index, either because the corresponding |
| 323 | * file was modified and the cached stat data |
| 324 | * was refreshed, or because the original |
| 325 | * entry already had a replacement entry in |
| 326 | * the split index. |
| 327 | * Nothing to do. |
| 328 | */ |
SZEDER Gábor | 5581a01 | 2018-10-11 11:43:09 +0200 | [diff] [blame] | 329 | } else if (!ce_uptodate(ce) && |
| 330 | is_racy_timestamp(istate, ce)) { |
| 331 | /* |
| 332 | * A copy of a racily clean cache entry from |
| 333 | * the shared index. It must be added to |
| 334 | * the split index, so the subsequent |
| 335 | * do_write_index() can smudge its stat data. |
| 336 | */ |
| 337 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
SZEDER Gábor | e3d8379 | 2018-10-11 11:43:08 +0200 | [diff] [blame] | 338 | } else { |
| 339 | /* |
| 340 | * Thoroughly compare the cached data to see |
| 341 | * whether it should be marked for inclusion |
| 342 | * in the split index. |
| 343 | * |
| 344 | * This comparison might be unnecessary, as |
| 345 | * code paths modifying the cached data do |
| 346 | * set CE_UPDATE_IN_BASE as well. |
| 347 | */ |
| 348 | if (compare_ce_content(ce, base)) |
| 349 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
| 350 | } |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 351 | discard_cache_entry(base); |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 352 | si->base->cache[ce->index - 1] = ce; |
| 353 | } |
| 354 | for (i = 0; i < si->base->cache_nr; i++) { |
| 355 | ce = si->base->cache[i]; |
| 356 | if ((ce->ce_flags & CE_REMOVE) || |
| 357 | !(ce->ce_flags & CE_MATCHED)) |
| 358 | ewah_set(si->delete_bitmap, i); |
| 359 | else if (ce->ce_flags & CE_UPDATE_IN_BASE) { |
| 360 | ewah_set(si->replace_bitmap, i); |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 361 | ce->ce_flags |= CE_STRIP_NAME; |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 362 | ALLOC_GROW(entries, nr_entries+1, nr_alloc); |
| 363 | entries[nr_entries++] = ce; |
| 364 | } |
Thomas Gummerer | 4bddd98 | 2018-01-07 22:30:14 +0000 | [diff] [blame] | 365 | if (is_null_oid(&ce->oid)) |
| 366 | istate->drop_cache_tree = 1; |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | for (i = 0; i < istate->cache_nr; i++) { |
| 371 | ce = istate->cache[i]; |
| 372 | if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) { |
Nguyễn Thái Ngọc Duy | b3c96fb | 2014-06-13 19:19:43 +0700 | [diff] [blame] | 373 | assert(!(ce->ce_flags & CE_STRIP_NAME)); |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 374 | ALLOC_GROW(entries, nr_entries+1, nr_alloc); |
| 375 | entries[nr_entries++] = ce; |
| 376 | } |
| 377 | ce->ce_flags &= ~CE_MATCHED; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * take cache[] out temporarily, put entries[] in its place |
| 382 | * for writing |
| 383 | */ |
| 384 | si->saved_cache = istate->cache; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 385 | si->saved_cache_nr = istate->cache_nr; |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 386 | istate->cache = entries; |
| 387 | istate->cache_nr = nr_entries; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void finish_writing_split_index(struct index_state *istate) |
| 391 | { |
| 392 | struct split_index *si = init_split_index(istate); |
Nguyễn Thái Ngọc Duy | 96a1d8d | 2014-06-13 19:19:40 +0700 | [diff] [blame] | 393 | |
| 394 | ewah_free(si->delete_bitmap); |
| 395 | ewah_free(si->replace_bitmap); |
| 396 | si->delete_bitmap = NULL; |
| 397 | si->replace_bitmap = NULL; |
| 398 | free(istate->cache); |
| 399 | istate->cache = si->saved_cache; |
Nguyễn Thái Ngọc Duy | 5fc2fc8 | 2014-06-13 19:19:36 +0700 | [diff] [blame] | 400 | istate->cache_nr = si->saved_cache_nr; |
| 401 | } |
| 402 | |
| 403 | void discard_split_index(struct index_state *istate) |
| 404 | { |
| 405 | struct split_index *si = istate->split_index; |
| 406 | if (!si) |
| 407 | return; |
| 408 | istate->split_index = NULL; |
| 409 | si->refcount--; |
| 410 | if (si->refcount) |
| 411 | return; |
| 412 | if (si->base) { |
| 413 | discard_index(si->base); |
| 414 | free(si->base); |
| 415 | } |
| 416 | free(si); |
| 417 | } |
Nguyễn Thái Ngọc Duy | 045113a | 2014-06-13 19:19:38 +0700 | [diff] [blame] | 418 | |
| 419 | void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce) |
| 420 | { |
| 421 | if (ce->index && |
| 422 | istate->split_index && |
| 423 | istate->split_index->base && |
| 424 | ce->index <= istate->split_index->base->cache_nr && |
| 425 | ce == istate->split_index->base->cache[ce->index - 1]) |
| 426 | ce->ce_flags |= CE_REMOVE; |
| 427 | else |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 428 | discard_cache_entry(ce); |
Nguyễn Thái Ngọc Duy | 045113a | 2014-06-13 19:19:38 +0700 | [diff] [blame] | 429 | } |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 430 | |
| 431 | void replace_index_entry_in_base(struct index_state *istate, |
Brandon Williams | 75b7b97 | 2018-02-14 10:59:48 -0800 | [diff] [blame] | 432 | struct cache_entry *old_entry, |
| 433 | struct cache_entry *new_entry) |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 434 | { |
Brandon Williams | 75b7b97 | 2018-02-14 10:59:48 -0800 | [diff] [blame] | 435 | if (old_entry->index && |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 436 | istate->split_index && |
| 437 | istate->split_index->base && |
Brandon Williams | 75b7b97 | 2018-02-14 10:59:48 -0800 | [diff] [blame] | 438 | old_entry->index <= istate->split_index->base->cache_nr) { |
| 439 | new_entry->index = old_entry->index; |
| 440 | if (old_entry != istate->split_index->base->cache[new_entry->index - 1]) |
Jameson Miller | a849735 | 2018-07-02 19:49:31 +0000 | [diff] [blame] | 441 | discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]); |
Brandon Williams | 75b7b97 | 2018-02-14 10:59:48 -0800 | [diff] [blame] | 442 | istate->split_index->base->cache[new_entry->index - 1] = new_entry; |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 443 | } |
| 444 | } |
Christian Couder | cef4fc7 | 2017-02-27 19:00:01 +0100 | [diff] [blame] | 445 | |
| 446 | void add_split_index(struct index_state *istate) |
| 447 | { |
| 448 | if (!istate->split_index) { |
| 449 | init_split_index(istate); |
| 450 | istate->cache_changed |= SPLIT_INDEX_ORDERED; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | void remove_split_index(struct index_state *istate) |
| 455 | { |
Junio C Hamano | 64719b1 | 2017-06-24 12:02:39 -0700 | [diff] [blame] | 456 | if (istate->split_index) { |
Nguyễn Thái Ngọc Duy | 6e37c8e | 2019-02-13 16:51:29 +0700 | [diff] [blame] | 457 | if (istate->split_index->base) { |
| 458 | /* |
| 459 | * When removing the split index, we need to move |
| 460 | * ownership of the mem_pool associated with the |
| 461 | * base index to the main index. There may be cache entries |
| 462 | * allocated from the base's memory pool that are shared with |
| 463 | * the_index.cache[]. |
| 464 | */ |
| 465 | mem_pool_combine(istate->ce_mem_pool, |
| 466 | istate->split_index->base->ce_mem_pool); |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 467 | |
Nguyễn Thái Ngọc Duy | 6e37c8e | 2019-02-13 16:51:29 +0700 | [diff] [blame] | 468 | /* |
| 469 | * The split index no longer owns the mem_pool backing |
| 470 | * its cache array. As we are discarding this index, |
| 471 | * mark the index as having no cache entries, so it |
| 472 | * will not attempt to clean up the cache entries or |
| 473 | * validate them. |
| 474 | */ |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 475 | istate->split_index->base->cache_nr = 0; |
Nguyễn Thái Ngọc Duy | 6e37c8e | 2019-02-13 16:51:29 +0700 | [diff] [blame] | 476 | } |
Jameson Miller | 8e72d67 | 2018-07-02 19:49:37 +0000 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * We can discard the split index because its |
| 480 | * memory pool has been incorporated into the |
| 481 | * memory pool associated with the the_index. |
| 482 | */ |
| 483 | discard_split_index(istate); |
| 484 | |
Junio C Hamano | 64719b1 | 2017-06-24 12:02:39 -0700 | [diff] [blame] | 485 | istate->cache_changed |= SOMETHING_CHANGED; |
| 486 | } |
Christian Couder | cef4fc7 | 2017-02-27 19:00:01 +0100 | [diff] [blame] | 487 | } |