The sixteenth batch
[git/gitster.git] / split-index.c
blob4c74c4adda45d611bfad0a4d9aa5cb2f0eeb2212
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
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"
13 struct split_index *init_split_index(struct index_state *istate)
15 if (!istate->split_index) {
16 if (istate->sparse_index)
17 die(_("cannot use split index with a sparse index"));
19 CALLOC_ARRAY(istate->split_index, 1);
20 istate->split_index->refcount = 1;
22 return istate->split_index;
25 int read_link_extension(struct index_state *istate,
26 const void *data_, unsigned long sz)
28 const unsigned char *data = data_;
29 struct split_index *si;
30 int ret;
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;
55 int write_link_extension(struct strbuf *sb,
56 struct index_state *istate)
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;
67 static void mark_base_index_entries(struct index_state *base)
69 int i;
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".
77 for (i = 0; i < base->cache_nr; i++)
78 base->cache[i]->index = i + 1;
81 void move_cache_to_base_index(struct index_state *istate)
83 struct split_index *si = istate->split_index;
84 int i;
87 * If there was a previous base index, then transfer ownership of allocated
88 * entries to the parent index.
90 if (si->base &&
91 si->base->ce_mem_pool) {
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);
98 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
101 if (si->base)
102 release_index(si->base);
103 else
104 ALLOC_ARRAY(si->base, 1);
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;
114 * The mem_pool needs to move with the allocated entries.
116 si->base->ce_mem_pool = istate->ce_mem_pool;
117 istate->ce_mem_pool = NULL;
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;
125 static void mark_entry_for_delete(size_t pos, void *data)
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++;
135 static void replace_entry(size_t pos, void *data)
137 struct index_state *istate = data;
138 struct split_index *si = istate->split_index;
139 struct cache_entry *dst, *src;
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++;
163 void merge_base_index(struct index_state *istate)
165 struct split_index *si = istate->split_index;
166 unsigned int i;
168 mark_base_index_entries(si->base);
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);
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);
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 |
193 * we may have to replay what
194 * merge-recursive.c:update_stages()
195 * does, which has this flag on
197 ADD_CACHE_SKIP_DFCHECK);
198 si->saved_cache[i] = NULL;
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;
210 * Compare most of the fields in two cache entries, i.e. all except the
211 * hashmap_entry and the name.
213 static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
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;
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;
231 return ret;
234 void prepare_to_write_split_index(struct index_state *istate)
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;
240 si->delete_bitmap = ewah_new();
241 si->replace_bitmap = ewah_new();
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.
251 for (i = 0; i < istate->cache_nr; i++) {
252 struct cache_entry *base;
253 ce = istate->cache[i];
254 if (!ce->index) {
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.
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.
273 continue;
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);
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) {
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.
294 } else if (!ce_uptodate(ce) &&
295 is_racy_timestamp(istate, ce)) {
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.
303 ce->ce_flags |= CE_UPDATE_IN_BASE;
304 } else {
306 * The entry is only present in the
307 * shared index and it was not
308 * refreshed.
309 * Just leave it there.
312 continue;
314 if (ce->ce_namelen != base->ce_namelen ||
315 strcmp(ce->name, base->name)) {
316 ce->index = 0;
317 continue;
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.
324 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
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.
334 } else if (!ce_uptodate(ce) &&
335 is_racy_timestamp(istate, ce)) {
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.
342 ce->ce_flags |= CE_UPDATE_IN_BASE;
343 } else {
345 * Thoroughly compare the cached data to see
346 * whether it should be marked for inclusion
347 * in the split index.
349 * This comparison might be unnecessary, as
350 * code paths modifying the cached data do
351 * set CE_UPDATE_IN_BASE as well.
353 if (compare_ce_content(ce, base))
354 ce->ce_flags |= CE_UPDATE_IN_BASE;
356 discard_cache_entry(base);
357 si->base->cache[ce->index - 1] = ce;
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;
370 if (is_null_oid(&ce->oid))
371 istate->drop_cache_tree = 1;
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;
382 ce->ce_flags &= ~CE_MATCHED;
386 * take cache[] out temporarily, put entries[] in its place
387 * for writing
389 si->saved_cache = istate->cache;
390 si->saved_cache_nr = istate->cache_nr;
391 istate->cache = entries;
392 istate->cache_nr = nr_entries;
395 void finish_writing_split_index(struct index_state *istate)
397 struct split_index *si = init_split_index(istate);
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;
408 void discard_split_index(struct index_state *istate)
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);
421 free(si);
424 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
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);
436 void replace_index_entry_in_base(struct index_state *istate,
437 struct cache_entry *old_entry,
438 struct cache_entry *new_entry)
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;
451 void add_split_index(struct index_state *istate)
453 if (!istate->split_index) {
454 init_split_index(istate);
455 istate->cache_changed |= SPLIT_INDEX_ORDERED;
459 void remove_split_index(struct index_state *istate)
461 if (istate->split_index) {
462 if (istate->split_index->base) {
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[].
470 mem_pool_combine(istate->ce_mem_pool,
471 istate->split_index->base->ce_mem_pool);
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.
480 istate->split_index->base->cache_nr = 0;
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.
488 discard_split_index(istate);
490 istate->cache_changed |= SOMETHING_CHANGED;