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