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