blob: 120c8190b187bc8c6c73630f06f5dd4dc994df30 [file] [log] [blame]
Patrick Steinhardte7da9382024-06-14 08:50:23 +02001#define USE_THE_REPOSITORY_VARIABLE
2
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00003#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00004#include "gettext.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00005#include "hash.h"
Elijah Newren5bc07222023-04-11 00:41:47 -07006#include "mem-pool.h"
Elijah Newren08c46a42023-05-16 06:33:56 +00007#include "read-cache-ll.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07008#include "split-index.h"
Elijah Newren69a63fe2023-04-22 20:17:08 +00009#include "strbuf.h"
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +070010#include "ewah/ewok.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070011
12struct split_index *init_split_index(struct index_state *istate)
13{
14 if (!istate->split_index) {
Johannes Schindelin451b66c2022-01-19 17:29:39 +000015 if (istate->sparse_index)
16 die(_("cannot use split index with a sparse index"));
17
René Scharfeca56dad2021-03-13 17:17:22 +010018 CALLOC_ARRAY(istate->split_index, 1);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070019 istate->split_index->refcount = 1;
20 }
21 return istate->split_index;
22}
23
24int 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 Duy76b07b32014-06-13 19:19:41 +070029 int ret;
30
brian m. carlson2182abd2018-05-02 00:25:43 +000031 if (sz < the_hash_algo->rawsz)
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070032 return error("corrupt link extension (too short)");
33 si = init_split_index(istate);
Patrick Steinhardt9da95bd2024-06-14 08:49:54 +020034 oidread(&si->base_oid, data, the_repository->hash_algo);
brian m. carlson2182abd2018-05-02 00:25:43 +000035 data += the_hash_algo->rawsz;
36 sz -= the_hash_algo->rawsz;
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +070037 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 Duy5fc2fc82014-06-13 19:19:36 +070050 return error("garbage at the end of link extension");
51 return 0;
52}
53
54int write_link_extension(struct strbuf *sb,
55 struct index_state *istate)
56{
57 struct split_index *si = istate->split_index;
brian m. carlson2182abd2018-05-02 00:25:43 +000058 strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +070059 if (!si->delete_bitmap && !si->replace_bitmap)
60 return 0;
Nguyễn Thái Ngọc Duybe0d9d52015-03-08 17:12:32 +070061 ewah_serialize_strbuf(si->delete_bitmap, sb);
62 ewah_serialize_strbuf(si->replace_bitmap, sb);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070063 return 0;
64}
65
66static 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 Peng832c0e52016-05-06 20:36:46 +080073 * from 1 instead of 0, which is reserved to say "this is a new
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070074 * 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 Duyc18b80a2014-06-13 19:19:44 +070080void move_cache_to_base_index(struct index_state *istate)
81{
82 struct split_index *si = istate->split_index;
83 int i;
84
85 /*
Jameson Miller8e72d672018-07-02 19:49:37 +000086 * If there was a previous base index, then transfer ownership of allocated
87 * entries to the parent index.
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +070088 */
Jameson Miller8e72d672018-07-02 19:49:37 +000089 if (si->base &&
90 si->base->ce_mem_pool) {
91
Elijah Newren44c7e1a2020-08-15 17:37:56 +000092 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 Miller8e72d672018-07-02 19:49:37 +000096
97 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
98 }
99
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +0100100 ALLOC_ARRAY(si->base, 1);
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +0100101 index_state_init(si->base, istate->repo);
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +0700102 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 Miller8e72d672018-07-02 19:49:37 +0000107
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é Scharfe45ccef82016-09-25 09:24:03 +0200114 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +0700115 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 Duy76b07b32014-06-13 19:19:41 +0700120static 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ábor2034e842018-10-11 11:43:07 +0200127 istate->split_index->nr_deletions++;
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700128}
129
130static 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 Duyb3c96fb2014-06-13 19:19:43 +0700135
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700136 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 Duyb3c96fb2014-06-13 19:19:43 +0700147 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 Duy76b07b32014-06-13 19:19:41 +0700150 src->index = pos + 1;
151 src->ce_flags |= CE_UPDATE_IN_BASE;
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +0700152 src->ce_namelen = dst->ce_namelen;
153 copy_cache_entry(dst, src);
Jameson Millera8497352018-07-02 19:49:31 +0000154 discard_cache_entry(src);
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700155 si->nr_replacements++;
156}
157
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +0700158void merge_base_index(struct index_state *istate)
159{
160 struct split_index *si = istate->split_index;
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700161 unsigned int i;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +0700162
163 mark_base_index_entries(si->base);
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700164
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 Duy5fc2fc82014-06-13 19:19:36 +0700170 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
René Scharfe45ccef82016-09-25 09:24:03 +0200171 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700172
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 Gummerer6fdc2052018-12-20 13:48:16 +0000178 remove_marked_cache_entries(istate, 0);
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700179
180 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +0700181 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 Duy76b07b32014-06-13 19:19:41 +0700184 add_index_entry(istate, si->saved_cache[i],
185 ADD_CACHE_OK_TO_ADD |
Nguyễn Thái Ngọc Duyce7c6142014-06-13 19:19:42 +0700186 ADD_CACHE_KEEP_CACHE_TREE |
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700187 /*
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ð Bjarmason88ce3ef2017-06-15 23:15:49 +0000198 FREE_AND_NULL(si->saved_cache);
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700199 si->delete_bitmap = NULL;
200 si->replace_bitmap = NULL;
Nguyễn Thái Ngọc Duy76b07b32014-06-13 19:19:41 +0700201 si->saved_cache_nr = 0;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +0700202}
203
SZEDER Gábore3d83792018-10-11 11:43:08 +0200204/*
205 * Compare most of the fields in two cache entries, i.e. all except the
206 * hashmap_entry and the name.
207 */
208static 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 Hunt09751bf2021-06-14 15:51:15 +0000221 offsetof(struct cache_entry, oid)) ||
222 !oideq(&a->oid, &b->oid);
SZEDER Gábore3d83792018-10-11 11:43:08 +0200223 a->ce_flags = ce_flags;
224 b->ce_flags = base_flags;
225
226 return ret;
227}
228
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +0700229void 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 Duy96a1d8d2014-06-13 19:19:40 +0700232 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 Couder753c4512016-10-23 11:26:30 +0200242 * that are not marked with either CE_MATCHED or
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700243 * 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 Duy96a1d8d2014-06-13 19:19:40 +0700248 ce = istate->cache[i];
SZEDER Gábore3d83792018-10-11 11:43:08 +0200249 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 Duy96a1d8d2014-06-13 19:19:40 +0700268 continue;
SZEDER Gábore3d83792018-10-11 11:43:08 +0200269 }
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700270 if (ce->index > si->base->cache_nr) {
SZEDER Gábor4c490f32018-10-11 11:53:57 +0200271 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 Duy96a1d8d2014-06-13 19:19:40 +0700273 }
274 ce->ce_flags |= CE_MATCHED; /* or "shared" */
275 base = si->base->cache[ce->index - 1];
SZEDER Gábor5581a012018-10-11 11:43:09 +0200276 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 Duy96a1d8d2014-06-13 19:19:40 +0700307 continue;
SZEDER Gábor5581a012018-10-11 11:43:09 +0200308 }
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700309 if (ce->ce_namelen != base->ce_namelen ||
310 strcmp(ce->name, base->name)) {
311 ce->index = 0;
312 continue;
313 }
SZEDER Gábore3d83792018-10-11 11:43:08 +0200314 /*
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ábor5581a012018-10-11 11:43:09 +0200329 } 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ábore3d83792018-10-11 11:43:08 +0200338 } 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 Millera8497352018-07-02 19:49:31 +0000351 discard_cache_entry(base);
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700352 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 Duyb3c96fb2014-06-13 19:19:43 +0700361 ce->ce_flags |= CE_STRIP_NAME;
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700362 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
363 entries[nr_entries++] = ce;
364 }
Thomas Gummerer4bddd982018-01-07 22:30:14 +0000365 if (is_null_oid(&ce->oid))
366 istate->drop_cache_tree = 1;
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700367 }
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 Duyb3c96fb2014-06-13 19:19:43 +0700373 assert(!(ce->ce_flags & CE_STRIP_NAME));
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700374 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 Duy5fc2fc82014-06-13 19:19:36 +0700385 si->saved_cache_nr = istate->cache_nr;
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700386 istate->cache = entries;
387 istate->cache_nr = nr_entries;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +0700388}
389
390void finish_writing_split_index(struct index_state *istate)
391{
392 struct split_index *si = init_split_index(istate);
Nguyễn Thái Ngọc Duy96a1d8d2014-06-13 19:19:40 +0700393
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 Duy5fc2fc82014-06-13 19:19:36 +0700400 istate->cache_nr = si->saved_cache_nr;
401}
402
403void 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 Duy045113a2014-06-13 19:19:38 +0700418
419void 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 Millera8497352018-07-02 19:49:31 +0000428 discard_cache_entry(ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700429}
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700430
431void replace_index_entry_in_base(struct index_state *istate,
Brandon Williams75b7b972018-02-14 10:59:48 -0800432 struct cache_entry *old_entry,
433 struct cache_entry *new_entry)
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700434{
Brandon Williams75b7b972018-02-14 10:59:48 -0800435 if (old_entry->index &&
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700436 istate->split_index &&
437 istate->split_index->base &&
Brandon Williams75b7b972018-02-14 10:59:48 -0800438 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 Millera8497352018-07-02 19:49:31 +0000441 discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
Brandon Williams75b7b972018-02-14 10:59:48 -0800442 istate->split_index->base->cache[new_entry->index - 1] = new_entry;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700443 }
444}
Christian Coudercef4fc72017-02-27 19:00:01 +0100445
446void 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
454void remove_split_index(struct index_state *istate)
455{
Junio C Hamano64719b12017-06-24 12:02:39 -0700456 if (istate->split_index) {
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +0700457 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 Miller8e72d672018-07-02 19:49:37 +0000467
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +0700468 /*
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 Miller8e72d672018-07-02 19:49:37 +0000475 istate->split_index->base->cache_nr = 0;
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +0700476 }
Jameson Miller8e72d672018-07-02 19:49:37 +0000477
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 Hamano64719b12017-06-24 12:02:39 -0700485 istate->cache_changed |= SOMETHING_CHANGED;
486 }
Christian Coudercef4fc72017-02-27 19:00:01 +0100487}