Coverage Report

Created: 2025-12-31 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/split-index.c
Line
Count
Source
1
#define USE_THE_REPOSITORY_VARIABLE
2
#define DISABLE_SIGN_COMPARE_WARNINGS
3
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"
12
13
struct split_index *init_split_index(struct index_state *istate)
14
0
{
15
0
  if (!istate->split_index) {
16
0
    if (istate->sparse_index)
17
0
      die(_("cannot use split index with a sparse index"));
18
19
0
    CALLOC_ARRAY(istate->split_index, 1);
20
0
    istate->split_index->refcount = 1;
21
0
  }
22
0
  return istate->split_index;
23
0
}
24
25
int read_link_extension(struct index_state *istate,
26
       const void *data_, unsigned long sz)
27
0
{
28
0
  const unsigned char *data = data_;
29
0
  struct split_index *si;
30
0
  int ret;
31
32
0
  if (sz < the_hash_algo->rawsz)
33
0
    return error("corrupt link extension (too short)");
34
0
  si = init_split_index(istate);
35
0
  oidread(&si->base_oid, data, the_repository->hash_algo);
36
0
  data += the_hash_algo->rawsz;
37
0
  sz -= the_hash_algo->rawsz;
38
0
  if (!sz)
39
0
    return 0;
40
0
  si->delete_bitmap = ewah_new();
41
0
  ret = ewah_read_mmap(si->delete_bitmap, data, sz);
42
0
  if (ret < 0)
43
0
    return error("corrupt delete bitmap in link extension");
44
0
  data += ret;
45
0
  sz -= ret;
46
0
  si->replace_bitmap = ewah_new();
47
0
  ret = ewah_read_mmap(si->replace_bitmap, data, sz);
48
0
  if (ret < 0)
49
0
    return error("corrupt replace bitmap in link extension");
50
0
  if (ret != sz)
51
0
    return error("garbage at the end of link extension");
52
0
  return 0;
53
0
}
54
55
int write_link_extension(struct strbuf *sb,
56
       struct index_state *istate)
57
0
{
58
0
  struct split_index *si = istate->split_index;
59
0
  strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
60
0
  if (!si->delete_bitmap && !si->replace_bitmap)
61
0
    return 0;
62
0
  ewah_serialize_strbuf(si->delete_bitmap, sb);
63
0
  ewah_serialize_strbuf(si->replace_bitmap, sb);
64
0
  return 0;
65
0
}
66
67
static void mark_base_index_entries(struct index_state *base)
68
0
{
69
0
  int i;
70
  /*
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".
76
   */
77
0
  for (i = 0; i < base->cache_nr; i++)
78
0
    base->cache[i]->index = i + 1;
79
0
}
80
81
void move_cache_to_base_index(struct index_state *istate)
82
0
{
83
0
  struct split_index *si = istate->split_index;
84
0
  int i;
85
86
  /*
87
   * If there was a previous base index, then transfer ownership of allocated
88
   * entries to the parent index.
89
   */
90
0
  if (si->base &&
91
0
    si->base->ce_mem_pool) {
92
93
0
    if (!istate->ce_mem_pool) {
94
0
      istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
95
0
      mem_pool_init(istate->ce_mem_pool, 0);
96
0
    }
97
98
0
    mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
99
0
  }
100
101
0
  if (si->base)
102
0
    release_index(si->base);
103
0
  else
104
0
    ALLOC_ARRAY(si->base, 1);
105
106
0
  index_state_init(si->base, istate->repo);
107
0
  si->base->version = istate->version;
108
  /* zero timestamp disables racy test in ce_write_index() */
109
0
  si->base->timestamp = istate->timestamp;
110
0
  ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
111
0
  si->base->cache_nr = istate->cache_nr;
112
113
  /*
114
   * The mem_pool needs to move with the allocated entries.
115
   */
116
0
  si->base->ce_mem_pool = istate->ce_mem_pool;
117
0
  istate->ce_mem_pool = NULL;
118
119
0
  COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
120
0
  mark_base_index_entries(si->base);
121
0
  for (i = 0; i < si->base->cache_nr; i++)
122
0
    si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
123
0
}
124
125
static void mark_entry_for_delete(size_t pos, void *data)
126
0
{
127
0
  struct index_state *istate = data;
128
0
  if (pos >= istate->cache_nr)
129
0
    die("position for delete %d exceeds base index size %d",
130
0
        (int)pos, istate->cache_nr);
131
0
  istate->cache[pos]->ce_flags |= CE_REMOVE;
132
0
  istate->split_index->nr_deletions++;
133
0
}
134
135
static void replace_entry(size_t pos, void *data)
136
0
{
137
0
  struct index_state *istate = data;
138
0
  struct split_index *si = istate->split_index;
139
0
  struct cache_entry *dst, *src;
140
141
0
  if (pos >= istate->cache_nr)
142
0
    die("position for replacement %d exceeds base index size %d",
143
0
        (int)pos, istate->cache_nr);
144
0
  if (si->nr_replacements >= si->saved_cache_nr)
145
0
    die("too many replacements (%d vs %d)",
146
0
        si->nr_replacements, si->saved_cache_nr);
147
0
  dst = istate->cache[pos];
148
0
  if (dst->ce_flags & CE_REMOVE)
149
0
    die("entry %d is marked as both replaced and deleted",
150
0
        (int)pos);
151
0
  src = si->saved_cache[si->nr_replacements];
152
0
  if (ce_namelen(src))
153
0
    die("corrupt link extension, entry %d should have "
154
0
        "zero length name", (int)pos);
155
0
  src->index = pos + 1;
156
0
  src->ce_flags |= CE_UPDATE_IN_BASE;
157
0
  src->ce_namelen = dst->ce_namelen;
158
0
  copy_cache_entry(dst, src);
159
0
  discard_cache_entry(src);
160
0
  si->nr_replacements++;
161
0
}
162
163
void merge_base_index(struct index_state *istate)
164
0
{
165
0
  struct split_index *si = istate->split_index;
166
0
  unsigned int i;
167
168
0
  mark_base_index_entries(si->base);
169
170
0
  si->saved_cache     = istate->cache;
171
0
  si->saved_cache_nr  = istate->cache_nr;
172
0
  istate->cache_nr    = si->base->cache_nr;
173
0
  istate->cache     = NULL;
174
0
  istate->cache_alloc = 0;
175
0
  ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
176
0
  COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
177
178
0
  si->nr_deletions = 0;
179
0
  si->nr_replacements = 0;
180
0
  ewah_each_bit(si->replace_bitmap, replace_entry, istate);
181
0
  ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
182
0
  if (si->nr_deletions)
183
0
    remove_marked_cache_entries(istate, 0);
184
185
0
  for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
186
0
    if (!ce_namelen(si->saved_cache[i]))
187
0
      die("corrupt link extension, entry %d should "
188
0
          "have non-zero length name", i);
189
0
    add_index_entry(istate, si->saved_cache[i],
190
0
        ADD_CACHE_OK_TO_ADD |
191
0
        ADD_CACHE_KEEP_CACHE_TREE |
192
        /*
193
         * we may have to replay what
194
         * merge-recursive.c:update_stages()
195
         * does, which has this flag on
196
         */
197
0
        ADD_CACHE_SKIP_DFCHECK);
198
0
    si->saved_cache[i] = NULL;
199
0
  }
200
201
0
  ewah_free(si->delete_bitmap);
202
0
  ewah_free(si->replace_bitmap);
203
0
  FREE_AND_NULL(si->saved_cache);
204
0
  si->delete_bitmap  = NULL;
205
0
  si->replace_bitmap = NULL;
206
0
  si->saved_cache_nr = 0;
207
0
}
208
209
/*
210
 * Compare most of the fields in two cache entries, i.e. all except the
211
 * hashmap_entry and the name.
212
 */
213
static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
214
0
{
215
0
  const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |
216
0
            CE_EXTENDED_FLAGS;
217
0
  unsigned int ce_flags = a->ce_flags;
218
0
  unsigned int base_flags = b->ce_flags;
219
0
  int ret;
220
221
  /* only on-disk flags matter */
222
0
  a->ce_flags &= ondisk_flags;
223
0
  b->ce_flags &= ondisk_flags;
224
0
  ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,
225
0
         offsetof(struct cache_entry, name) -
226
0
         offsetof(struct cache_entry, oid)) ||
227
0
    !oideq(&a->oid, &b->oid);
228
0
  a->ce_flags = ce_flags;
229
0
  b->ce_flags = base_flags;
230
231
0
  return ret;
232
0
}
233
234
void prepare_to_write_split_index(struct index_state *istate)
235
0
{
236
0
  struct split_index *si = init_split_index(istate);
237
0
  struct cache_entry **entries = NULL, *ce;
238
0
  int i, nr_entries = 0, nr_alloc = 0;
239
240
0
  si->delete_bitmap = ewah_new();
241
0
  si->replace_bitmap = ewah_new();
242
243
0
  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.
250
     */
251
0
    for (i = 0; i < istate->cache_nr; i++) {
252
0
      struct cache_entry *base;
253
0
      ce = istate->cache[i];
254
0
      if (!ce->index) {
255
        /*
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.
260
         *
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.
272
         */
273
0
        continue;
274
0
      }
275
0
      if (ce->index > si->base->cache_nr) {
276
0
        BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
277
0
            ce->index, si->base->cache_nr);
278
0
      }
279
0
      ce->ce_flags |= CE_MATCHED; /* or "shared" */
280
0
      base = si->base->cache[ce->index - 1];
281
0
      if (ce == base) {
282
        /* The entry is present in the shared index. */
283
0
        if (ce->ce_flags & CE_UPDATE_IN_BASE) {
284
          /*
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.
293
           */
294
0
        } else if (!ce_uptodate(ce) &&
295
0
             is_racy_timestamp(istate, ce)) {
296
          /*
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.
302
           */
303
0
          ce->ce_flags |= CE_UPDATE_IN_BASE;
304
0
        } else {
305
          /*
306
           * The entry is only present in the
307
           * shared index and it was not
308
           * refreshed.
309
           * Just leave it there.
310
           */
311
0
        }
312
0
        continue;
313
0
      }
314
0
      if (ce->ce_namelen != base->ce_namelen ||
315
0
          strcmp(ce->name, base->name)) {
316
0
        ce->index = 0;
317
0
        continue;
318
0
      }
319
      /*
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.
323
       */
324
0
      if (ce->ce_flags & CE_UPDATE_IN_BASE) {
325
        /*
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.
333
         */
334
0
      } else if (!ce_uptodate(ce) &&
335
0
           is_racy_timestamp(istate, ce)) {
336
        /*
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.
341
         */
342
0
        ce->ce_flags |= CE_UPDATE_IN_BASE;
343
0
      } else {
344
        /*
345
         * Thoroughly compare the cached data to see
346
         * whether it should be marked for inclusion
347
         * in the split index.
348
         *
349
         * This comparison might be unnecessary, as
350
         * code paths modifying the cached data do
351
         * set CE_UPDATE_IN_BASE as well.
352
         */
353
0
        if (compare_ce_content(ce, base))
354
0
          ce->ce_flags |= CE_UPDATE_IN_BASE;
355
0
      }
356
0
      discard_cache_entry(base);
357
0
      si->base->cache[ce->index - 1] = ce;
358
0
    }
359
0
    for (i = 0; i < si->base->cache_nr; i++) {
360
0
      ce = si->base->cache[i];
361
0
      if ((ce->ce_flags & CE_REMOVE) ||
362
0
          !(ce->ce_flags & CE_MATCHED))
363
0
        ewah_set(si->delete_bitmap, i);
364
0
      else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
365
0
        ewah_set(si->replace_bitmap, i);
366
0
        ce->ce_flags |= CE_STRIP_NAME;
367
0
        ALLOC_GROW(entries, nr_entries+1, nr_alloc);
368
0
        entries[nr_entries++] = ce;
369
0
      }
370
0
      if (is_null_oid(&ce->oid))
371
0
        istate->drop_cache_tree = 1;
372
0
    }
373
0
  }
374
375
0
  for (i = 0; i < istate->cache_nr; i++) {
376
0
    ce = istate->cache[i];
377
0
    if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
378
0
      assert(!(ce->ce_flags & CE_STRIP_NAME));
379
0
      ALLOC_GROW(entries, nr_entries+1, nr_alloc);
380
0
      entries[nr_entries++] = ce;
381
0
    }
382
0
    ce->ce_flags &= ~CE_MATCHED;
383
0
  }
384
385
  /*
386
   * take cache[] out temporarily, put entries[] in its place
387
   * for writing
388
   */
389
0
  si->saved_cache = istate->cache;
390
0
  si->saved_cache_nr = istate->cache_nr;
391
0
  istate->cache = entries;
392
0
  istate->cache_nr = nr_entries;
393
0
}
394
395
void finish_writing_split_index(struct index_state *istate)
396
0
{
397
0
  struct split_index *si = init_split_index(istate);
398
399
0
  ewah_free(si->delete_bitmap);
400
0
  ewah_free(si->replace_bitmap);
401
0
  si->delete_bitmap = NULL;
402
0
  si->replace_bitmap = NULL;
403
0
  free(istate->cache);
404
0
  istate->cache = si->saved_cache;
405
0
  istate->cache_nr = si->saved_cache_nr;
406
0
}
407
408
void discard_split_index(struct index_state *istate)
409
0
{
410
0
  struct split_index *si = istate->split_index;
411
0
  if (!si)
412
0
    return;
413
0
  istate->split_index = NULL;
414
0
  si->refcount--;
415
0
  if (si->refcount)
416
0
    return;
417
0
  if (si->base) {
418
0
    discard_index(si->base);
419
0
    free(si->base);
420
0
  }
421
0
  free(si);
422
0
}
423
424
void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
425
0
{
426
0
  if (ce->index &&
427
0
      istate->split_index &&
428
0
      istate->split_index->base &&
429
0
      ce->index <= istate->split_index->base->cache_nr &&
430
0
      ce == istate->split_index->base->cache[ce->index - 1])
431
0
    ce->ce_flags |= CE_REMOVE;
432
0
  else
433
0
    discard_cache_entry(ce);
434
0
}
435
436
void replace_index_entry_in_base(struct index_state *istate,
437
         struct cache_entry *old_entry,
438
         struct cache_entry *new_entry)
439
0
{
440
0
  if (old_entry->index &&
441
0
      istate->split_index &&
442
0
      istate->split_index->base &&
443
0
      old_entry->index <= istate->split_index->base->cache_nr) {
444
0
    new_entry->index = old_entry->index;
445
0
    if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
446
0
      discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
447
0
    istate->split_index->base->cache[new_entry->index - 1] = new_entry;
448
0
  }
449
0
}
450
451
void add_split_index(struct index_state *istate)
452
0
{
453
0
  if (!istate->split_index) {
454
0
    init_split_index(istate);
455
0
    istate->cache_changed |= SPLIT_INDEX_ORDERED;
456
0
  }
457
0
}
458
459
void remove_split_index(struct index_state *istate)
460
0
{
461
0
  if (istate->split_index) {
462
0
    if (istate->split_index->base) {
463
      /*
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[].
469
       */
470
0
      mem_pool_combine(istate->ce_mem_pool,
471
0
           istate->split_index->base->ce_mem_pool);
472
473
      /*
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.
479
       */
480
0
      istate->split_index->base->cache_nr = 0;
481
0
    }
482
483
    /*
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.
487
     */
488
0
    discard_split_index(istate);
489
490
0
    istate->cache_changed |= SOMETHING_CHANGED;
491
0
  }
492
0
}