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