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