t: convert fgrep usage to "grep -F"
[alt-git.git] / split-index.c
blob9d0ccc30d00e35965f47d55253166f3d8ce142e2
1 #include "cache.h"
2 #include "split-index.h"
3 #include "ewah/ewok.h"
5 struct split_index *init_split_index(struct index_state *istate)
7 if (!istate->split_index) {
8 if (istate->sparse_index)
9 die(_("cannot use split index with a sparse index"));
11 CALLOC_ARRAY(istate->split_index, 1);
12 istate->split_index->refcount = 1;
14 return istate->split_index;
17 int read_link_extension(struct index_state *istate,
18 const void *data_, unsigned long sz)
20 const unsigned char *data = data_;
21 struct split_index *si;
22 int ret;
24 if (sz < the_hash_algo->rawsz)
25 return error("corrupt link extension (too short)");
26 si = init_split_index(istate);
27 oidread(&si->base_oid, data);
28 data += the_hash_algo->rawsz;
29 sz -= the_hash_algo->rawsz;
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)
43 return error("garbage at the end of link extension");
44 return 0;
47 int write_link_extension(struct strbuf *sb,
48 struct index_state *istate)
50 struct split_index *si = istate->split_index;
51 strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
52 if (!si->delete_bitmap && !si->replace_bitmap)
53 return 0;
54 ewah_serialize_strbuf(si->delete_bitmap, sb);
55 ewah_serialize_strbuf(si->replace_bitmap, sb);
56 return 0;
59 static void mark_base_index_entries(struct index_state *base)
61 int i;
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
66 * from 1 instead of 0, which is reserved to say "this is a new
67 * entry".
69 for (i = 0; i < base->cache_nr; i++)
70 base->cache[i]->index = i + 1;
73 void move_cache_to_base_index(struct index_state *istate)
75 struct split_index *si = istate->split_index;
76 int i;
79 * If there was a previous base index, then transfer ownership of allocated
80 * entries to the parent index.
82 if (si->base &&
83 si->base->ce_mem_pool) {
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);
90 mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
93 CALLOC_ARRAY(si->base, 1);
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;
101 * The mem_pool needs to move with the allocated entries.
103 si->base->ce_mem_pool = istate->ce_mem_pool;
104 istate->ce_mem_pool = NULL;
106 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
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;
112 static void mark_entry_for_delete(size_t pos, void *data)
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;
119 istate->split_index->nr_deletions++;
122 static void replace_entry(size_t pos, void *data)
124 struct index_state *istate = data;
125 struct split_index *si = istate->split_index;
126 struct cache_entry *dst, *src;
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];
139 if (ce_namelen(src))
140 die("corrupt link extension, entry %d should have "
141 "zero length name", (int)pos);
142 src->index = pos + 1;
143 src->ce_flags |= CE_UPDATE_IN_BASE;
144 src->ce_namelen = dst->ce_namelen;
145 copy_cache_entry(dst, src);
146 discard_cache_entry(src);
147 si->nr_replacements++;
150 void merge_base_index(struct index_state *istate)
152 struct split_index *si = istate->split_index;
153 unsigned int i;
155 mark_base_index_entries(si->base);
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;
162 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
163 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
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)
170 remove_marked_cache_entries(istate, 0);
172 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
173 if (!ce_namelen(si->saved_cache[i]))
174 die("corrupt link extension, entry %d should "
175 "have non-zero length name", i);
176 add_index_entry(istate, si->saved_cache[i],
177 ADD_CACHE_OK_TO_ADD |
178 ADD_CACHE_KEEP_CACHE_TREE |
180 * we may have to replay what
181 * merge-recursive.c:update_stages()
182 * does, which has this flag on
184 ADD_CACHE_SKIP_DFCHECK);
185 si->saved_cache[i] = NULL;
188 ewah_free(si->delete_bitmap);
189 ewah_free(si->replace_bitmap);
190 FREE_AND_NULL(si->saved_cache);
191 si->delete_bitmap = NULL;
192 si->replace_bitmap = NULL;
193 si->saved_cache_nr = 0;
197 * Compare most of the fields in two cache entries, i.e. all except the
198 * hashmap_entry and the name.
200 static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
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;
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) -
213 offsetof(struct cache_entry, oid)) ||
214 !oideq(&a->oid, &b->oid);
215 a->ce_flags = ce_flags;
216 b->ce_flags = base_flags;
218 return ret;
221 void prepare_to_write_split_index(struct index_state *istate)
223 struct split_index *si = init_split_index(istate);
224 struct cache_entry **entries = NULL, *ce;
225 int i, nr_entries = 0, nr_alloc = 0;
227 si->delete_bitmap = ewah_new();
228 si->replace_bitmap = ewah_new();
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
234 * that are not marked with either CE_MATCHED or
235 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
236 * duplicate, deduplicate it.
238 for (i = 0; i < istate->cache_nr; i++) {
239 struct cache_entry *base;
240 ce = istate->cache[i];
241 if (!ce->index) {
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.
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.
260 continue;
262 if (ce->index > si->base->cache_nr) {
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);
266 ce->ce_flags |= CE_MATCHED; /* or "shared" */
267 base = si->base->cache[ce->index - 1];
268 if (ce == base) {
269 /* The entry is present in the shared index. */
270 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
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.
281 } else if (!ce_uptodate(ce) &&
282 is_racy_timestamp(istate, ce)) {
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.
290 ce->ce_flags |= CE_UPDATE_IN_BASE;
291 } else {
293 * The entry is only present in the
294 * shared index and it was not
295 * refreshed.
296 * Just leave it there.
299 continue;
301 if (ce->ce_namelen != base->ce_namelen ||
302 strcmp(ce->name, base->name)) {
303 ce->index = 0;
304 continue;
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.
311 if (ce->ce_flags & CE_UPDATE_IN_BASE) {
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.
321 } else if (!ce_uptodate(ce) &&
322 is_racy_timestamp(istate, ce)) {
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.
329 ce->ce_flags |= CE_UPDATE_IN_BASE;
330 } else {
332 * Thoroughly compare the cached data to see
333 * whether it should be marked for inclusion
334 * in the split index.
336 * This comparison might be unnecessary, as
337 * code paths modifying the cached data do
338 * set CE_UPDATE_IN_BASE as well.
340 if (compare_ce_content(ce, base))
341 ce->ce_flags |= CE_UPDATE_IN_BASE;
343 discard_cache_entry(base);
344 si->base->cache[ce->index - 1] = ce;
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);
353 ce->ce_flags |= CE_STRIP_NAME;
354 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
355 entries[nr_entries++] = ce;
357 if (is_null_oid(&ce->oid))
358 istate->drop_cache_tree = 1;
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)) {
365 assert(!(ce->ce_flags & CE_STRIP_NAME));
366 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
367 entries[nr_entries++] = ce;
369 ce->ce_flags &= ~CE_MATCHED;
373 * take cache[] out temporarily, put entries[] in its place
374 * for writing
376 si->saved_cache = istate->cache;
377 si->saved_cache_nr = istate->cache_nr;
378 istate->cache = entries;
379 istate->cache_nr = nr_entries;
382 void finish_writing_split_index(struct index_state *istate)
384 struct split_index *si = init_split_index(istate);
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;
392 istate->cache_nr = si->saved_cache_nr;
395 void discard_split_index(struct index_state *istate)
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);
408 free(si);
411 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
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
420 discard_cache_entry(ce);
423 void replace_index_entry_in_base(struct index_state *istate,
424 struct cache_entry *old_entry,
425 struct cache_entry *new_entry)
427 if (old_entry->index &&
428 istate->split_index &&
429 istate->split_index->base &&
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])
433 discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
434 istate->split_index->base->cache[new_entry->index - 1] = new_entry;
438 void add_split_index(struct index_state *istate)
440 if (!istate->split_index) {
441 init_split_index(istate);
442 istate->cache_changed |= SPLIT_INDEX_ORDERED;
446 void remove_split_index(struct index_state *istate)
448 if (istate->split_index) {
449 if (istate->split_index->base) {
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[].
457 mem_pool_combine(istate->ce_mem_pool,
458 istate->split_index->base->ce_mem_pool);
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.
467 istate->split_index->base->cache_nr = 0;
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.
475 discard_split_index(istate);
477 istate->cache_changed |= SOMETHING_CHANGED;