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