Coverage Report

Created: 2026-12-01 06:58

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