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