Coverage Report

Created: 2026-10-17 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/cache-tree.c
Line
Count
Source
1
#define DISABLE_SIGN_COMPARE_WARNINGS
2
3
#include "git-compat-util.h"
4
#include "gettext.h"
5
#include "hex.h"
6
#include "lockfile.h"
7
#include "tree.h"
8
#include "tree-walk.h"
9
#include "cache-tree.h"
10
#include "object-file.h"
11
#include "odb.h"
12
#include "odb/transaction.h"
13
#include "read-cache-ll.h"
14
#include "replace-object.h"
15
#include "repository.h"
16
#include "promisor-remote.h"
17
#include "trace.h"
18
#include "trace2.h"
19
20
#ifndef DEBUG_CACHE_TREE
21
#define DEBUG_CACHE_TREE 0
22
#endif
23
24
struct cache_tree *cache_tree(void)
25
0
{
26
0
  struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
27
0
  it->entry_count = -1;
28
0
  return it;
29
0
}
30
31
void cache_tree_free(struct cache_tree **it_p)
32
60
{
33
60
  int i;
34
60
  struct cache_tree *it = *it_p;
35
36
60
  if (!it)
37
60
    return;
38
0
  for (i = 0; i < it->subtree_nr; i++)
39
0
    if (it->down[i]) {
40
0
      cache_tree_free(&it->down[i]->cache_tree);
41
0
      free(it->down[i]);
42
0
    }
43
0
  free(it->down);
44
0
  free(it);
45
0
  *it_p = NULL;
46
0
}
47
48
static int subtree_name_cmp(const char *one, int onelen,
49
          const char *two, int twolen)
50
0
{
51
0
  if (onelen < twolen)
52
0
    return -1;
53
0
  if (twolen < onelen)
54
0
    return 1;
55
0
  return memcmp(one, two, onelen);
56
0
}
57
58
int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen)
59
0
{
60
0
  struct cache_tree_sub **down = it->down;
61
0
  int lo, hi;
62
0
  lo = 0;
63
0
  hi = it->subtree_nr;
64
0
  while (lo < hi) {
65
0
    int mi = lo + (hi - lo) / 2;
66
0
    struct cache_tree_sub *mdl = down[mi];
67
0
    int cmp = subtree_name_cmp(path, pathlen,
68
0
             mdl->name, mdl->namelen);
69
0
    if (!cmp)
70
0
      return mi;
71
0
    if (cmp < 0)
72
0
      hi = mi;
73
0
    else
74
0
      lo = mi + 1;
75
0
  }
76
0
  return -lo-1;
77
0
}
78
79
static struct cache_tree_sub *find_subtree(struct cache_tree *it,
80
             const char *path,
81
             int pathlen,
82
             int create)
83
0
{
84
0
  struct cache_tree_sub *down;
85
0
  int pos = cache_tree_subtree_pos(it, path, pathlen);
86
0
  if (0 <= pos)
87
0
    return it->down[pos];
88
0
  if (!create)
89
0
    return NULL;
90
91
0
  pos = -pos-1;
92
0
  ALLOC_GROW(it->down, it->subtree_nr + 1, it->subtree_alloc);
93
0
  it->subtree_nr++;
94
95
0
  FLEX_ALLOC_MEM(down, name, path, pathlen);
96
0
  down->cache_tree = NULL;
97
0
  down->namelen = pathlen;
98
99
0
  if (pos < it->subtree_nr)
100
0
    MOVE_ARRAY(it->down + pos + 1, it->down + pos,
101
0
         it->subtree_nr - pos - 1);
102
0
  it->down[pos] = down;
103
0
  return down;
104
0
}
105
106
struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
107
0
{
108
0
  int pathlen = strlen(path);
109
0
  return find_subtree(it, path, pathlen, 1);
110
0
}
111
112
static int do_invalidate_path(struct cache_tree *it, const char *path)
113
0
{
114
  /* a/b/c
115
   * ==> invalidate self
116
   * ==> find "a", have it invalidate "b/c"
117
   * a
118
   * ==> invalidate self
119
   * ==> if "a" exists as a subtree, remove it.
120
   */
121
0
  const char *slash;
122
0
  int namelen;
123
0
  struct cache_tree_sub *down;
124
125
#if DEBUG_CACHE_TREE
126
  fprintf(stderr, "cache-tree invalidate <%s>\n", path);
127
#endif
128
129
0
  if (!it)
130
0
    return 0;
131
0
  slash = strchrnul(path, '/');
132
0
  namelen = slash - path;
133
0
  it->entry_count = -1;
134
0
  if (!*slash) {
135
0
    int pos;
136
0
    pos = cache_tree_subtree_pos(it, path, namelen);
137
0
    if (0 <= pos) {
138
0
      cache_tree_free(&it->down[pos]->cache_tree);
139
0
      free(it->down[pos]);
140
      /* 0 1 2 3 4 5
141
       *       ^     ^subtree_nr = 6
142
       *       pos
143
       * move 4 and 5 up one place (2 entries)
144
       * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
145
       */
146
0
      MOVE_ARRAY(it->down + pos, it->down + pos + 1,
147
0
           it->subtree_nr - pos - 1);
148
0
      it->subtree_nr--;
149
0
    }
150
0
    return 1;
151
0
  }
152
0
  down = find_subtree(it, path, namelen, 0);
153
0
  if (down)
154
0
    do_invalidate_path(down->cache_tree, slash + 1);
155
0
  return 1;
156
0
}
157
158
void cache_tree_invalidate_path(struct index_state *istate, const char *path)
159
0
{
160
0
  if (do_invalidate_path(istate->cache_tree, path))
161
0
    istate->cache_changed |= CACHE_TREE_CHANGED;
162
0
}
163
164
/*
165
 * Check whether this_ce and the next entry in the index form a D/F
166
 * conflict ("path" vs "path/file").  Returns the conflicting "path/..."
167
 * name when one is found, or NULL otherwise.
168
 *
169
 * The cache is sorted, so "path/file" sorts after "path" and the
170
 * conflict is usually visible as adjacent entries.  But other entries
171
 * can sort between them -- e.g. "path-internal" sits between "path"
172
 * and "path/file" because '-' (0x2D) precedes '/' (0x2F) -- so when
173
 * the immediately following entry shares our prefix but starts with a
174
 * character that sorts before '/', binary search for "path/" instead.
175
 */
176
static const char *find_df_conflict(struct index_state *istate,
177
            const struct cache_entry *this_ce,
178
            const struct cache_entry *next_ce)
179
0
{
180
0
  const char *this_name = this_ce->name;
181
0
  const char *next_name = next_ce->name;
182
0
  int this_len = ce_namelen(this_ce);
183
0
  const struct cache_entry *other;
184
0
  struct strbuf probe = STRBUF_INIT;
185
0
  int pos;
186
187
0
  if (this_len >= ce_namelen(next_ce) ||
188
0
      next_name[this_len] > '/' ||
189
0
      strncmp(this_name, next_name, this_len))
190
0
    return NULL;
191
192
0
  if (next_name[this_len] == '/')
193
0
    return next_name;
194
195
0
  strbuf_add(&probe, this_name, this_len);
196
0
  strbuf_addch(&probe, '/');
197
0
  pos = index_name_pos_sparse(istate, probe.buf, probe.len);
198
0
  strbuf_release(&probe);
199
200
0
  if (pos < 0)
201
0
    pos = -pos - 1;
202
0
  if (pos >= (int)istate->cache_nr)
203
0
    return NULL;
204
0
  other = istate->cache[pos];
205
0
  if (ce_namelen(other) > this_len &&
206
0
      other->name[this_len] == '/' &&
207
0
      !strncmp(this_name, other->name, this_len))
208
0
    return other->name;
209
0
  return NULL;
210
0
}
211
212
static int verify_cache(struct index_state *istate, int flags)
213
0
{
214
0
  unsigned i, funny;
215
0
  int silent = flags & WRITE_TREE_SILENT;
216
217
  /* Verify that the tree is merged */
218
0
  funny = 0;
219
0
  for (i = 0; i < istate->cache_nr; i++) {
220
0
    const struct cache_entry *ce = istate->cache[i];
221
0
    if (ce_stage(ce)) {
222
0
      if (silent)
223
0
        return -1;
224
0
      if (10 < ++funny) {
225
0
        fprintf(stderr, "...\n");
226
0
        break;
227
0
      }
228
0
      fprintf(stderr, "%s: unmerged (%s)\n",
229
0
        ce->name, oid_to_hex(&ce->oid));
230
0
    }
231
0
  }
232
0
  if (funny)
233
0
    return -1;
234
235
  /* Also verify that the cache does not have path and path/file
236
   * at the same time.  At this point we know the cache has only
237
   * stage 0 entries.
238
   */
239
0
  funny = 0;
240
0
  for (i = 0; i + 1 < istate->cache_nr; i++) {
241
0
    const struct cache_entry *this_ce = istate->cache[i];
242
0
    const struct cache_entry *next_ce = istate->cache[i + 1];
243
0
    const char *conflict_name;
244
245
0
    conflict_name = find_df_conflict(istate, this_ce, next_ce);
246
0
    if (conflict_name) {
247
0
      if (10 < ++funny) {
248
0
        fprintf(stderr, "...\n");
249
0
        break;
250
0
      }
251
0
      fprintf(stderr, "You have both %s and %s\n",
252
0
        this_ce->name, conflict_name);
253
0
    }
254
0
  }
255
0
  if (funny)
256
0
    return -1;
257
0
  return 0;
258
0
}
259
260
static void discard_unused_subtrees(struct cache_tree *it)
261
0
{
262
0
  struct cache_tree_sub **down = it->down;
263
0
  int nr = it->subtree_nr;
264
0
  int dst, src;
265
0
  for (dst = src = 0; src < nr; src++) {
266
0
    struct cache_tree_sub *s = down[src];
267
0
    if (s->used)
268
0
      down[dst++] = s;
269
0
    else {
270
0
      cache_tree_free(&s->cache_tree);
271
0
      free(s);
272
0
      it->subtree_nr--;
273
0
    }
274
0
  }
275
0
}
276
277
static int cache_tree_fully_valid_recursive(struct object_database *odb,
278
              struct cache_tree *it)
279
0
{
280
0
  int i;
281
0
  if (!it)
282
0
    return 0;
283
0
  if (it->entry_count < 0 ||
284
0
      !odb_has_object(odb, &it->oid,
285
0
          ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
286
0
    return 0;
287
0
  for (i = 0; i < it->subtree_nr; i++) {
288
0
    if (!cache_tree_fully_valid_recursive(odb, it->down[i]->cache_tree))
289
0
      return 0;
290
0
  }
291
0
  return 1;
292
0
}
293
294
int cache_tree_fully_valid(struct index_state *istate)
295
0
{
296
0
  return cache_tree_fully_valid_recursive(istate->repo->objects,
297
0
            istate->cache_tree);
298
0
}
299
300
static int must_check_existence(const struct cache_entry *ce, void *cb_data)
301
0
{
302
0
  struct repository *repo = cb_data;
303
0
  return !(repo_has_promisor_remote(repo) && ce_skip_worktree(ce));
304
0
}
305
306
static int update_one(struct repository *repo,
307
          struct cache_tree *it,
308
          struct cache_entry **cache,
309
          int entries,
310
          const char *base,
311
          int baselen,
312
          int *skip_count,
313
          int flags)
314
0
{
315
0
  struct strbuf buffer;
316
0
  int missing_ok = flags & WRITE_TREE_MISSING_OK;
317
0
  int dryrun = flags & WRITE_TREE_DRY_RUN;
318
0
  int repair = flags & WRITE_TREE_REPAIR;
319
0
  int to_invalidate = 0;
320
0
  int i;
321
322
0
  assert(!(dryrun && repair));
323
324
0
  *skip_count = 0;
325
326
  /*
327
   * If the first entry of this region is a sparse directory
328
   * entry corresponding exactly to 'base', then this cache_tree
329
   * struct is a "leaf" in the data structure, pointing to the
330
   * tree OID specified in the entry.
331
   */
332
0
  if (entries > 0) {
333
0
    const struct cache_entry *ce = cache[0];
334
335
0
    if (S_ISSPARSEDIR(ce->ce_mode) &&
336
0
        ce->ce_namelen == baselen &&
337
0
        !strncmp(ce->name, base, baselen)) {
338
0
      it->entry_count = 1;
339
0
      oidcpy(&it->oid, &ce->oid);
340
0
      return 1;
341
0
    }
342
0
  }
343
344
0
  if (0 <= it->entry_count &&
345
0
      odb_has_object(repo->objects, &it->oid,
346
0
         ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
347
0
    return it->entry_count;
348
349
  /*
350
   * We first scan for subtrees and update them; we start by
351
   * marking existing subtrees -- the ones that are unmarked
352
   * should not be in the result.
353
   */
354
0
  for (i = 0; i < it->subtree_nr; i++)
355
0
    it->down[i]->used = 0;
356
357
  /*
358
   * Find the subtrees and update them.
359
   */
360
0
  i = 0;
361
0
  while (i < entries) {
362
0
    const struct cache_entry *ce = cache[i];
363
0
    struct cache_tree_sub *sub;
364
0
    const char *path, *slash;
365
0
    int pathlen, sublen, subcnt, subskip;
366
367
0
    path = ce->name;
368
0
    pathlen = ce_namelen(ce);
369
0
    if (pathlen <= baselen || memcmp(base, path, baselen))
370
0
      break; /* at the end of this level */
371
372
0
    slash = strchr(path + baselen, '/');
373
0
    if (!slash) {
374
0
      i++;
375
0
      continue;
376
0
    }
377
    /*
378
     * a/bbb/c (base = a/, slash = /c)
379
     * ==>
380
     * path+baselen = bbb/c, sublen = 3
381
     */
382
0
    sublen = slash - (path + baselen);
383
0
    sub = find_subtree(it, path + baselen, sublen, 1);
384
0
    if (!sub->cache_tree)
385
0
      sub->cache_tree = cache_tree();
386
0
    subcnt = update_one(repo,
387
0
            sub->cache_tree,
388
0
            cache + i, entries - i,
389
0
            path,
390
0
            baselen + sublen + 1,
391
0
            &subskip,
392
0
            flags);
393
0
    if (subcnt < 0)
394
0
      return subcnt;
395
0
    if (!subcnt)
396
0
      die("index cache-tree records empty sub-tree");
397
0
    i += subcnt;
398
0
    sub->count = subcnt; /* to be used in the next loop */
399
0
    *skip_count += subskip;
400
0
    sub->used = 1;
401
0
  }
402
403
0
  discard_unused_subtrees(it);
404
405
  /*
406
   * Then write out the tree object for this level.
407
   */
408
0
  strbuf_init(&buffer, 8192);
409
410
0
  i = 0;
411
0
  while (i < entries) {
412
0
    const struct cache_entry *ce = cache[i];
413
0
    struct cache_tree_sub *sub = NULL;
414
0
    const char *path, *slash;
415
0
    int pathlen, entlen;
416
0
    const struct object_id *oid;
417
0
    unsigned mode;
418
0
    int expected_missing = 0;
419
0
    int contains_ita = 0;
420
0
    int ce_missing_ok;
421
422
0
    path = ce->name;
423
0
    pathlen = ce_namelen(ce);
424
0
    if (pathlen <= baselen || memcmp(base, path, baselen))
425
0
      break; /* at the end of this level */
426
427
0
    slash = strchr(path + baselen, '/');
428
0
    if (slash) {
429
0
      entlen = slash - (path + baselen);
430
0
      sub = find_subtree(it, path + baselen, entlen, 0);
431
0
      if (!sub)
432
0
        die("cache-tree.c: '%.*s' in '%s' not found",
433
0
            entlen, path + baselen, path);
434
0
      i += sub->count;
435
0
      oid = &sub->cache_tree->oid;
436
0
      mode = S_IFDIR;
437
0
      contains_ita = sub->cache_tree->entry_count < 0;
438
0
      if (contains_ita) {
439
0
        to_invalidate = 1;
440
0
        expected_missing = 1;
441
0
      }
442
0
    }
443
0
    else {
444
0
      oid = &ce->oid;
445
0
      mode = ce->ce_mode;
446
0
      entlen = pathlen - baselen;
447
0
      i++;
448
0
    }
449
450
0
    ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
451
0
      !must_check_existence(ce, repo);
452
0
    if (is_null_oid(oid) ||
453
0
        (!ce_missing_ok &&
454
0
         !odb_has_object(repo->objects, oid,
455
0
             ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))) {
456
0
      strbuf_release(&buffer);
457
0
      if (expected_missing)
458
0
        return -1;
459
0
      return error("invalid object %06o %s for '%.*s'",
460
0
        mode, oid_to_hex(oid), entlen+baselen, path);
461
0
    }
462
463
    /*
464
     * CE_REMOVE entries are removed before the index is
465
     * written to disk. Skip them to remain consistent
466
     * with the future on-disk index.
467
     */
468
0
    if (ce->ce_flags & CE_REMOVE) {
469
0
      *skip_count = *skip_count + 1;
470
0
      continue;
471
0
    }
472
473
    /*
474
     * CE_INTENT_TO_ADD entries exist in on-disk index but
475
     * they are not part of generated trees. Invalidate up
476
     * to root to force cache-tree users to read elsewhere.
477
     */
478
0
    if (!sub && ce_intent_to_add(ce)) {
479
0
      to_invalidate = 1;
480
0
      continue;
481
0
    }
482
483
    /*
484
     * "sub" can be an empty tree if all subentries are i-t-a.
485
     */
486
0
    if (contains_ita && is_empty_tree_oid(oid, repo->hash_algo))
487
0
      continue;
488
489
0
    strbuf_grow(&buffer, entlen + 100);
490
0
    strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
491
0
    strbuf_add(&buffer, oid->hash, repo->hash_algo->rawsz);
492
493
#if DEBUG_CACHE_TREE
494
    fprintf(stderr, "cache-tree update-one %o %.*s\n",
495
      mode, entlen, path + baselen);
496
#endif
497
0
  }
498
499
0
  if (repair) {
500
0
    struct object_id oid;
501
0
    hash_object_file(repo->hash_algo, buffer.buf, buffer.len,
502
0
         OBJ_TREE, &oid);
503
0
    if (odb_has_object(repo->objects, &oid, ODB_HAS_OBJECT_RECHECK_PACKED))
504
0
      oidcpy(&it->oid, &oid);
505
0
    else
506
0
      to_invalidate = 1;
507
0
  } else if (dryrun) {
508
0
    hash_object_file(repo->hash_algo, buffer.buf, buffer.len,
509
0
         OBJ_TREE, &it->oid);
510
0
  } else if (odb_write_object_ext(repo->objects, buffer.buf, buffer.len, OBJ_TREE,
511
0
          &it->oid, NULL, flags & WRITE_TREE_SILENT ? ODB_WRITE_OBJECT_SILENT : 0)) {
512
0
    strbuf_release(&buffer);
513
0
    return -1;
514
0
  }
515
516
0
  strbuf_release(&buffer);
517
0
  it->entry_count = to_invalidate ? -1 : i - *skip_count;
518
#if DEBUG_CACHE_TREE
519
  fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
520
    it->entry_count, it->subtree_nr,
521
    oid_to_hex(&it->oid));
522
#endif
523
0
  return i;
524
0
}
525
526
int cache_tree_update(struct index_state *istate, int flags)
527
0
{
528
0
  int inflight = !!istate->repo->objects->transaction;
529
0
  struct odb_transaction *transaction;
530
0
  int skip, i;
531
532
0
  i = verify_cache(istate, flags);
533
534
0
  if (i)
535
0
    return i;
536
537
0
  if (!istate->cache_tree)
538
0
    istate->cache_tree = cache_tree();
539
540
0
  if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(istate->repo))
541
0
    prefetch_cache_entries(istate, must_check_existence, istate->repo);
542
543
0
  trace_performance_enter();
544
0
  trace2_region_enter("cache_tree", "update", istate->repo);
545
0
  if (!inflight)
546
0
    odb_transaction_begin_or_die(istate->repo->objects, &transaction, 0);
547
0
  i = update_one(istate->repo, istate->cache_tree, istate->cache, istate->cache_nr,
548
0
           "", 0, &skip, flags);
549
0
  if (!inflight)
550
0
    odb_transaction_commit_and_finalize_or_die(transaction);
551
0
  trace2_region_leave("cache_tree", "update", istate->repo);
552
0
  trace_performance_leave("cache_tree_update");
553
0
  if (i < 0)
554
0
    return i;
555
0
  istate->cache_changed |= CACHE_TREE_CHANGED;
556
0
  return 0;
557
0
}
558
559
static void write_one(struct repository *repo,
560
          struct strbuf *buffer, struct cache_tree *it,
561
          const char *path, int pathlen)
562
0
{
563
0
  int i;
564
565
  /* One "cache-tree" entry consists of the following:
566
   * path (NUL terminated)
567
   * entry_count, subtree_nr ("%d %d\n")
568
   * tree-sha1 (missing if invalid)
569
   * subtree_nr "cache-tree" entries for subtrees.
570
   */
571
0
  strbuf_grow(buffer, pathlen + 100);
572
0
  strbuf_add(buffer, path, pathlen);
573
0
  strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
574
575
#if DEBUG_CACHE_TREE
576
  if (0 <= it->entry_count)
577
    fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
578
      pathlen, path, it->entry_count, it->subtree_nr,
579
      oid_to_hex(&it->oid));
580
  else
581
    fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
582
      pathlen, path, it->subtree_nr);
583
#endif
584
585
0
  if (0 <= it->entry_count) {
586
0
    strbuf_add(buffer, it->oid.hash, repo->hash_algo->rawsz);
587
0
  }
588
0
  for (i = 0; i < it->subtree_nr; i++) {
589
0
    struct cache_tree_sub *down = it->down[i];
590
0
    if (i) {
591
0
      struct cache_tree_sub *prev = it->down[i-1];
592
0
      if (subtree_name_cmp(down->name, down->namelen,
593
0
               prev->name, prev->namelen) <= 0)
594
0
        die("fatal - unsorted cache subtree");
595
0
    }
596
0
    write_one(repo, buffer, down->cache_tree, down->name, down->namelen);
597
0
  }
598
0
}
599
600
void cache_tree_write(struct repository *repo,
601
          struct strbuf *sb, struct cache_tree *root)
602
0
{
603
0
  trace2_region_enter("cache_tree", "write", repo);
604
0
  write_one(repo, sb, root, "", 0);
605
0
  trace2_region_leave("cache_tree", "write", repo);
606
0
}
607
608
static int parse_int(const char **ptr, unsigned long *len_p, int *out)
609
0
{
610
0
  const char *s = *ptr;
611
0
  unsigned long len = *len_p;
612
0
  int ret = 0;
613
0
  int sign = 1;
614
615
0
  while (len && *s == '-') {
616
0
    sign *= -1;
617
0
    s++;
618
0
    len--;
619
0
  }
620
621
0
  while (len) {
622
0
    if (!isdigit(*s))
623
0
      break;
624
0
    ret *= 10;
625
0
    ret += *s - '0';
626
0
    s++;
627
0
    len--;
628
0
  }
629
630
0
  if (s == *ptr)
631
0
    return -1;
632
633
0
  *ptr = s;
634
0
  *len_p = len;
635
0
  *out = sign * ret;
636
0
  return 0;
637
0
}
638
639
static struct cache_tree *read_one(struct repository *repo,
640
           const char **buffer, unsigned long *size_p)
641
0
{
642
0
  const char *buf = *buffer;
643
0
  unsigned long size = *size_p;
644
0
  struct cache_tree *it;
645
0
  int i, subtree_nr;
646
0
  const unsigned rawsz = repo->hash_algo->rawsz;
647
648
0
  it = NULL;
649
  /* skip name, but make sure name exists */
650
0
  while (size && *buf) {
651
0
    size--;
652
0
    buf++;
653
0
  }
654
0
  if (!size)
655
0
    goto free_return;
656
0
  buf++; size--;
657
0
  it = cache_tree();
658
659
0
  if (parse_int(&buf, &size, &it->entry_count) < 0)
660
0
    goto free_return;
661
0
  if (!size || *buf != ' ')
662
0
    goto free_return;
663
0
  buf++; size--;
664
0
  if (parse_int(&buf, &size, &subtree_nr) < 0)
665
0
    goto free_return;
666
0
  if (!size || *buf != '\n')
667
0
    goto free_return;
668
0
  buf++; size--;
669
0
  if (0 <= it->entry_count) {
670
0
    if (size < rawsz)
671
0
      goto free_return;
672
0
    oidread(&it->oid, (const unsigned char *)buf,
673
0
      repo->hash_algo);
674
0
    buf += rawsz;
675
0
    size -= rawsz;
676
0
  }
677
678
#if DEBUG_CACHE_TREE
679
  if (0 <= it->entry_count)
680
    fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
681
      *buffer, it->entry_count, subtree_nr,
682
      oid_to_hex(&it->oid));
683
  else
684
    fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
685
      *buffer, subtree_nr);
686
#endif
687
688
  /*
689
   * Just a heuristic -- we do not add directories that often but
690
   * we do not want to have to extend it immediately when we do,
691
   * hence +2.
692
   */
693
0
  it->subtree_alloc = subtree_nr + 2;
694
0
  CALLOC_ARRAY(it->down, it->subtree_alloc);
695
0
  for (i = 0; i < subtree_nr; i++) {
696
    /* read each subtree */
697
0
    struct cache_tree *sub;
698
0
    struct cache_tree_sub *subtree;
699
0
    const char *name = buf;
700
701
0
    sub = read_one(repo, &buf, &size);
702
0
    if (!sub)
703
0
      goto free_return;
704
0
    subtree = cache_tree_sub(it, name);
705
0
    subtree->cache_tree = sub;
706
0
  }
707
0
  if (subtree_nr != it->subtree_nr)
708
0
    die("cache-tree: internal error");
709
0
  *buffer = buf;
710
0
  *size_p = size;
711
0
  return it;
712
713
0
 free_return:
714
0
  cache_tree_free(&it);
715
0
  return NULL;
716
0
}
717
718
struct cache_tree *cache_tree_read(struct repository *repo,
719
           const char *buffer, unsigned long size)
720
0
{
721
0
  struct cache_tree *result;
722
723
0
  if (buffer[0])
724
0
    return NULL; /* not the whole tree */
725
726
0
  trace2_region_enter("cache_tree", "read", repo);
727
0
  result = read_one(repo, &buffer, &size);
728
0
  trace2_region_leave("cache_tree", "read", repo);
729
730
0
  return result;
731
0
}
732
733
static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
734
0
{
735
0
  if (!it)
736
0
    return NULL;
737
0
  while (*path) {
738
0
    const char *slash;
739
0
    struct cache_tree_sub *sub;
740
741
0
    slash = strchrnul(path, '/');
742
    /*
743
     * Between path and slash is the name of the subtree
744
     * to look for.
745
     */
746
0
    sub = find_subtree(it, path, slash - path, 0);
747
0
    if (!sub)
748
0
      return NULL;
749
0
    it = sub->cache_tree;
750
751
0
    path = slash;
752
0
    while (*path == '/')
753
0
      path++;
754
0
  }
755
0
  return it;
756
0
}
757
758
static int write_index_as_tree_internal(struct object_id *oid,
759
          struct index_state *index_state,
760
          int cache_tree_valid,
761
          int flags,
762
          const char *prefix)
763
0
{
764
0
  if (flags & WRITE_TREE_IGNORE_CACHE_TREE) {
765
0
    cache_tree_free(&index_state->cache_tree);
766
0
    cache_tree_valid = 0;
767
0
  }
768
769
0
  if (!cache_tree_valid && cache_tree_update(index_state, flags) < 0)
770
0
    return WRITE_TREE_UNMERGED_INDEX;
771
772
0
  if (prefix) {
773
0
    struct cache_tree *subtree;
774
0
    subtree = cache_tree_find(index_state->cache_tree, prefix);
775
0
    if (!subtree)
776
0
      return WRITE_TREE_PREFIX_ERROR;
777
0
    oidcpy(oid, &subtree->oid);
778
0
  }
779
0
  else
780
0
    oidcpy(oid, &index_state->cache_tree->oid);
781
782
0
  return 0;
783
0
}
784
785
struct tree *write_in_core_index_as_tree(struct repository *repo,
786
0
           struct index_state *index_state) {
787
0
  struct object_id o;
788
0
  int was_valid, ret;
789
790
0
  was_valid = index_state->cache_tree &&
791
0
        cache_tree_fully_valid(index_state);
792
793
0
  ret = write_index_as_tree_internal(&o, index_state, was_valid, 0, NULL);
794
0
  if (ret == WRITE_TREE_UNMERGED_INDEX) {
795
0
    int i;
796
0
    bug("there are unmerged index entries:");
797
0
    for (i = 0; i < index_state->cache_nr; i++) {
798
0
      const struct cache_entry *ce = index_state->cache[i];
799
0
      if (ce_stage(ce))
800
0
        bug("%d %.*s", ce_stage(ce),
801
0
            (int)ce_namelen(ce), ce->name);
802
0
    }
803
0
    BUG("unmerged index entries when writing in-core index");
804
0
  }
805
806
0
  return lookup_tree(repo, &index_state->cache_tree->oid);
807
0
}
808
809
810
int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix)
811
0
{
812
0
  int entries, was_valid;
813
0
  struct lock_file lock_file = LOCK_INIT;
814
0
  int ret;
815
816
0
  hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
817
818
0
  entries = read_index_from(index_state, index_path,
819
0
          repo_get_git_dir(index_state->repo));
820
0
  if (entries < 0) {
821
0
    ret = WRITE_TREE_UNREADABLE_INDEX;
822
0
    goto out;
823
0
  }
824
825
0
  was_valid = !(flags & WRITE_TREE_IGNORE_CACHE_TREE) &&
826
0
        index_state->cache_tree &&
827
0
        cache_tree_fully_valid(index_state);
828
829
0
  ret = write_index_as_tree_internal(oid, index_state, was_valid, flags,
830
0
             prefix);
831
0
  if (!ret && !was_valid) {
832
0
    write_locked_index(index_state, &lock_file, COMMIT_LOCK);
833
    /* Not being able to write is fine -- we are only interested
834
     * in updating the cache-tree part, and if the next caller
835
     * ends up using the old index with unupdated cache-tree part
836
     * it misses the work we did here, but that is just a
837
     * performance penalty and not a big deal.
838
     */
839
0
  }
840
841
0
out:
842
0
  rollback_lock_file(&lock_file);
843
0
  return ret;
844
0
}
845
846
static void prime_cache_tree_sparse_dir(struct cache_tree *it,
847
          struct tree *tree)
848
0
{
849
850
0
  oidcpy(&it->oid, &tree->object.oid);
851
0
  it->entry_count = 1;
852
0
}
853
854
static void prime_cache_tree_rec(struct repository *r,
855
         struct cache_tree *it,
856
         struct tree *tree,
857
         struct strbuf *tree_path)
858
0
{
859
0
  struct tree_desc desc;
860
0
  struct name_entry entry;
861
0
  int cnt;
862
0
  size_t base_path_len = tree_path->len;
863
864
0
  oidcpy(&it->oid, &tree->object.oid);
865
866
0
  init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);
867
0
  cnt = 0;
868
0
  while (tree_entry(&desc, &entry)) {
869
0
    if (!S_ISDIR(entry.mode))
870
0
      cnt++;
871
0
    else {
872
0
      struct cache_tree_sub *sub;
873
0
      struct tree *subtree = lookup_tree(r, &entry.oid);
874
875
0
      if (repo_parse_tree(r, subtree) < 0)
876
0
        exit(128);
877
0
      sub = cache_tree_sub(it, entry.path);
878
0
      sub->cache_tree = cache_tree();
879
880
      /*
881
       * Recursively-constructed subtree path is only needed when working
882
       * in a sparse index (where it's used to determine whether the
883
       * subtree is a sparse directory in the index).
884
       */
885
0
      if (r->index->sparse_index) {
886
0
        strbuf_setlen(tree_path, base_path_len);
887
0
        strbuf_add(tree_path, entry.path, entry.pathlen);
888
0
        strbuf_addch(tree_path, '/');
889
0
      }
890
891
      /*
892
       * If a sparse index is in use, the directory being processed may be
893
       * sparse. To confirm that, we can check whether an entry with that
894
       * exact name exists in the index. If it does, the created subtree
895
       * should be sparse. Otherwise, cache tree expansion should continue
896
       * as normal.
897
       */
898
0
      if (r->index->sparse_index &&
899
0
          index_entry_exists(r->index, tree_path->buf, tree_path->len))
900
0
        prime_cache_tree_sparse_dir(sub->cache_tree, subtree);
901
0
      else
902
0
        prime_cache_tree_rec(r, sub->cache_tree, subtree, tree_path);
903
0
      cnt += sub->cache_tree->entry_count;
904
0
    }
905
0
  }
906
907
0
  it->entry_count = cnt;
908
0
}
909
910
void prime_cache_tree(struct repository *r,
911
          struct index_state *istate,
912
          struct tree *tree)
913
0
{
914
0
  struct strbuf tree_path = STRBUF_INIT;
915
916
0
  trace2_region_enter("cache-tree", "prime_cache_tree", r);
917
0
  cache_tree_free(&istate->cache_tree);
918
0
  istate->cache_tree = cache_tree();
919
920
0
  prime_cache_tree_rec(r, istate->cache_tree, tree, &tree_path);
921
0
  strbuf_release(&tree_path);
922
0
  istate->cache_changed |= CACHE_TREE_CHANGED;
923
0
  trace2_region_leave("cache-tree", "prime_cache_tree", r);
924
0
}
925
926
/*
927
 * find the cache_tree that corresponds to the current level without
928
 * exploding the full path into textual form.  The root of the
929
 * cache tree is given as "root", and our current level is "info".
930
 * (1) When at root level, info->prev is NULL, so it is "root" itself.
931
 * (2) Otherwise, find the cache_tree that corresponds to one level
932
 *     above us, and find ourselves in there.
933
 */
934
static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
935
               struct traverse_info *info)
936
0
{
937
0
  struct cache_tree *our_parent;
938
939
0
  if (!info->prev)
940
0
    return root;
941
0
  our_parent = find_cache_tree_from_traversal(root, info->prev);
942
0
  return cache_tree_find(our_parent, info->name);
943
0
}
944
945
int cache_tree_matches_traversal(struct cache_tree *root,
946
         struct name_entry *ent,
947
         struct traverse_info *info)
948
0
{
949
0
  struct cache_tree *it;
950
951
0
  it = find_cache_tree_from_traversal(root, info);
952
0
  it = cache_tree_find(it, ent->path);
953
0
  if (it && it->entry_count > 0 && oideq(&ent->oid, &it->oid))
954
0
    return it->entry_count;
955
0
  return 0;
956
0
}
957
958
static int verify_one_sparse(struct index_state *istate,
959
           struct strbuf *path,
960
           int pos)
961
0
{
962
0
  struct cache_entry *ce = istate->cache[pos];
963
0
  if (!S_ISSPARSEDIR(ce->ce_mode))
964
0
    return error(_("directory '%s' is present in index, but not sparse"),
965
0
           path->buf);
966
0
  return 0;
967
0
}
968
969
/*
970
 * Returns:
971
 *  0 - Verification completed.
972
 *  1 - Restart verification - a call to ensure_full_index() freed the cache
973
 *      tree that is being verified and verification needs to be restarted from
974
 *      the new toplevel cache tree.
975
 *  -1 - Verification failed.
976
 */
977
static int verify_one(struct repository *r,
978
          struct index_state *istate,
979
          struct cache_tree *it,
980
          struct strbuf *path)
981
0
{
982
0
  int i, pos, len = path->len;
983
0
  struct strbuf tree_buf = STRBUF_INIT;
984
0
  struct object_id new_oid;
985
0
  int ret;
986
987
0
  for (i = 0; i < it->subtree_nr; i++) {
988
0
    strbuf_addf(path, "%s/", it->down[i]->name);
989
0
    ret = verify_one(r, istate, it->down[i]->cache_tree, path);
990
0
    if (ret)
991
0
      goto out;
992
993
0
    strbuf_setlen(path, len);
994
0
  }
995
996
0
  if (it->entry_count < 0 ||
997
      /* no verification on tests (t7003) that replace trees */
998
0
      lookup_replace_object(r, &it->oid) != &it->oid) {
999
0
    ret = 0;
1000
0
    goto out;
1001
0
  }
1002
1003
0
  if (path->len) {
1004
    /*
1005
     * If the index is sparse and the cache tree is not
1006
     * index_name_pos() may trigger ensure_full_index() which will
1007
     * free the tree that is being verified.
1008
     */
1009
0
    int is_sparse = istate->sparse_index;
1010
0
    pos = index_name_pos(istate, path->buf, path->len);
1011
0
    if (is_sparse && !istate->sparse_index) {
1012
0
      ret = 1;
1013
0
      goto out;
1014
0
    }
1015
1016
0
    if (pos >= 0) {
1017
0
      ret = verify_one_sparse(istate, path, pos);
1018
0
      goto out;
1019
0
    }
1020
1021
0
    pos = -pos - 1;
1022
0
  } else {
1023
0
    pos = 0;
1024
0
  }
1025
1026
0
  if (it->entry_count + pos > istate->cache_nr) {
1027
0
    ret = error(_("corrupted cache-tree has entries not present in index"));
1028
0
    goto out;
1029
0
  }
1030
1031
0
  i = 0;
1032
0
  while (i < it->entry_count) {
1033
0
    struct cache_entry *ce = istate->cache[pos + i];
1034
0
    const char *slash;
1035
0
    struct cache_tree_sub *sub = NULL;
1036
0
    const struct object_id *oid;
1037
0
    const char *name;
1038
0
    unsigned mode;
1039
0
    int entlen;
1040
1041
0
    if (ce->ce_flags & (CE_STAGEMASK | CE_INTENT_TO_ADD | CE_REMOVE)) {
1042
0
      ret = error(_("%s with flags 0x%x should not be in cache-tree"),
1043
0
            ce->name, ce->ce_flags);
1044
0
      goto out;
1045
0
    }
1046
1047
0
    name = ce->name + path->len;
1048
0
    slash = strchr(name, '/');
1049
0
    if (slash) {
1050
0
      entlen = slash - name;
1051
1052
0
      sub = find_subtree(it, ce->name + path->len, entlen, 0);
1053
0
      if (!sub || sub->cache_tree->entry_count < 0) {
1054
0
        ret = error(_("bad subtree '%.*s'"), entlen, name);
1055
0
        goto out;
1056
0
      }
1057
1058
0
      oid = &sub->cache_tree->oid;
1059
0
      mode = S_IFDIR;
1060
0
      i += sub->cache_tree->entry_count;
1061
0
    } else {
1062
0
      oid = &ce->oid;
1063
0
      mode = ce->ce_mode;
1064
0
      entlen = ce_namelen(ce) - path->len;
1065
0
      i++;
1066
0
    }
1067
0
    strbuf_addf(&tree_buf, "%o %.*s%c", mode, entlen, name, '\0');
1068
0
    strbuf_add(&tree_buf, oid->hash, r->hash_algo->rawsz);
1069
0
  }
1070
1071
0
  hash_object_file(r->hash_algo, tree_buf.buf, tree_buf.len, OBJ_TREE,
1072
0
       &new_oid);
1073
1074
0
  if (!oideq(&new_oid, &it->oid)) {
1075
0
    ret = error(_("cache-tree for path %.*s does not match. "
1076
0
            "Expected %s got %s"), len, path->buf,
1077
0
          oid_to_hex(&new_oid), oid_to_hex(&it->oid));
1078
0
    goto out;
1079
0
  }
1080
1081
0
  ret = 0;
1082
0
out:
1083
0
  strbuf_setlen(path, len);
1084
0
  strbuf_release(&tree_buf);
1085
0
  return ret;
1086
0
}
1087
1088
int cache_tree_verify(struct repository *r, struct index_state *istate)
1089
0
{
1090
0
  struct strbuf path = STRBUF_INIT;
1091
0
  int ret;
1092
1093
0
  if (!istate->cache_tree) {
1094
0
    ret = 0;
1095
0
    goto out;
1096
0
  }
1097
1098
0
  ret = verify_one(r, istate, istate->cache_tree, &path);
1099
0
  if (ret < 0)
1100
0
    goto out;
1101
0
  if (ret > 0) {
1102
0
    strbuf_reset(&path);
1103
1104
0
    ret = verify_one(r, istate, istate->cache_tree, &path);
1105
0
    if (ret < 0)
1106
0
      goto out;
1107
0
    if (ret > 0)
1108
0
      BUG("ensure_full_index() called twice while verifying cache tree");
1109
0
  }
1110
1111
0
  ret = 0;
1112
1113
0
out:
1114
0
  strbuf_release(&path);
1115
0
  return ret;
1116
0
}