Commit

f9de4b7ff70a70d8e5c4abca418756e959aaea85

cache-tree: remove dependency on `the_repository`

The "cache-tree" subsystem still depends on `the_repository`. Adapt it
to instead use repositories provided via the context, either as a new
parameter or the one passed in via `struct index_state`.

Besides getting rid of `the_repository`, this also removes the last
dependency on registering submodule sources with the main object
database. When reading gitmodules from a submodule's index we implicitly
read that object via `the_repository`'s object database, which is of
course wrong. This works though because we would then register the
submodule's object database with the main object database, but a later
patch is going to get rid of that mechanism.

You can verify that we indeed no longer depend on this mechanism by
running tests with `GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=true`. Without
this patch we fail in t1092, with this patch we never register submodule
object databases anymore.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt <ps@pks.im> · 2026-09-11 05:51 UTC · committed by Junio C Hamano

Browse files
5 files changed, +57 −47
modified cache-tree.c
@@ -1,4 +1,3 @@
1 - #define USE_THE_REPOSITORY_VARIABLE
2 1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 2
4 3 #include "git-compat-util.h"
@@ -298,12 +297,14 @@ int cache_tree_fully_valid(struct index_state *istate)
298 297 istate->cache_tree);
299 298 }
300 299
301 - static int must_check_existence(const struct cache_entry *ce)
300 + static int must_check_existence(const struct cache_entry *ce, void *cb_data)
302 301 {
303 - return !(repo_has_promisor_remote(the_repository) && ce_skip_worktree(ce));
302 + struct repository *repo = cb_data;
303 + return !(repo_has_promisor_remote(repo) && ce_skip_worktree(ce));
304 304 }
305 305
306 - static int update_one(struct cache_tree *it,
306 + static int update_one(struct repository *repo,
307 + struct cache_tree *it,
307 308 struct cache_entry **cache,
308 309 int entries,
309 310 const char *base,
@@ -341,7 +342,7 @@ static int update_one(struct cache_tree *it,
341 342 }
342 343
343 344 if (0 <= it->entry_count &&
344 - odb_has_object(the_repository->objects, &it->oid,
345 + odb_has_object(repo->objects, &it->oid,
345 346 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
346 347 return it->entry_count;
347 348
@@ -382,7 +383,8 @@ static int update_one(struct cache_tree *it,
382 383 sub = find_subtree(it, path + baselen, sublen, 1);
383 384 if (!sub->cache_tree)
384 385 sub->cache_tree = cache_tree();
385 - subcnt = update_one(sub->cache_tree,
386 + subcnt = update_one(repo,
387 + sub->cache_tree,
386 388 cache + i, entries - i,
387 389 path,
388 390 baselen + sublen + 1,
@@ -446,10 +448,10 @@ static int update_one(struct cache_tree *it,
446 448 }
447 449
448 450 ce_missing_ok = mode == S_IFGITLINK || missing_ok ||
449 - !must_check_existence(ce);
451 + !must_check_existence(ce, repo);
450 452 if (is_null_oid(oid) ||
451 453 (!ce_missing_ok &&
452 - !odb_has_object(the_repository->objects, oid,
454 + !odb_has_object(repo->objects, oid,
453 455 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))) {
454 456 strbuf_release(&buffer);
455 457 if (expected_missing)
@@ -481,12 +483,12 @@ static int update_one(struct cache_tree *it,
481 483 /*
482 484 * "sub" can be an empty tree if all subentries are i-t-a.
483 485 */
484 - if (contains_ita && is_empty_tree_oid(oid, the_repository->hash_algo))
486 + if (contains_ita && is_empty_tree_oid(oid, repo->hash_algo))
485 487 continue;
486 488
487 489 strbuf_grow(&buffer, entlen + 100);
488 490 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
489 - strbuf_add(&buffer, oid->hash, the_hash_algo->rawsz);
491 + strbuf_add(&buffer, oid->hash, repo->hash_algo->rawsz);
490 492
491 493 #if DEBUG_CACHE_TREE
492 494 fprintf(stderr, "cache-tree update-one %o %.*s\n",
@@ -496,16 +498,16 @@ static int update_one(struct cache_tree *it,
496 498
497 499 if (repair) {
498 500 struct object_id oid;
499 - hash_object_file(the_hash_algo, buffer.buf, buffer.len,
501 + hash_object_file(repo->hash_algo, buffer.buf, buffer.len,
500 502 OBJ_TREE, &oid);
501 - if (odb_has_object(the_repository->objects, &oid, ODB_HAS_OBJECT_RECHECK_PACKED))
503 + if (odb_has_object(repo->objects, &oid, ODB_HAS_OBJECT_RECHECK_PACKED))
502 504 oidcpy(&it->oid, &oid);
503 505 else
504 506 to_invalidate = 1;
505 507 } else if (dryrun) {
506 - hash_object_file(the_hash_algo, buffer.buf, buffer.len,
508 + hash_object_file(repo->hash_algo, buffer.buf, buffer.len,
507 509 OBJ_TREE, &it->oid);
508 - } else if (odb_write_object_ext(the_repository->objects, buffer.buf, buffer.len, OBJ_TREE,
510 + } else if (odb_write_object_ext(repo->objects, buffer.buf, buffer.len, OBJ_TREE,
509 511 &it->oid, NULL, flags & WRITE_TREE_SILENT ? ODB_WRITE_OBJECT_SILENT : 0)) {
510 512 strbuf_release(&buffer);
511 513 return -1;
@@ -523,7 +525,7 @@ static int update_one(struct cache_tree *it,
523 525
524 526 int cache_tree_update(struct index_state *istate, int flags)
525 527 {
526 - int inflight = !!the_repository->objects->transaction;
528 + int inflight = !!istate->repo->objects->transaction;
527 529 struct odb_transaction *transaction;
528 530 int skip, i;
529 531
@@ -535,14 +537,14 @@ int cache_tree_update(struct index_state *istate, int flags)
535 537 if (!istate->cache_tree)
536 538 istate->cache_tree = cache_tree();
537 539
538 - if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(the_repository))
539 - prefetch_cache_entries(istate, must_check_existence);
540 + if (!(flags & WRITE_TREE_MISSING_OK) && repo_has_promisor_remote(istate->repo))
541 + prefetch_cache_entries(istate, must_check_existence, istate->repo);
540 542
541 543 trace_performance_enter();
542 544 trace2_region_enter("cache_tree", "update", istate->repo);
543 545 if (!inflight)
544 - odb_transaction_begin_or_die(the_repository->objects, &transaction, 0);
545 - i = update_one(istate->cache_tree, istate->cache, istate->cache_nr,
546 + odb_transaction_begin_or_die(istate->repo->objects, &transaction, 0);
547 + i = update_one(istate->repo, istate->cache_tree, istate->cache, istate->cache_nr,
546 548 "", 0, &skip, flags);
547 549 if (!inflight)
548 550 odb_transaction_commit_and_finalize_or_die(transaction);
@@ -554,7 +556,8 @@ int cache_tree_update(struct index_state *istate, int flags)
554 556 return 0;
555 557 }
556 558
557 - static void write_one(struct strbuf *buffer, struct cache_tree *it,
559 + static void write_one(struct repository *repo,
560 + struct strbuf *buffer, struct cache_tree *it,
558 561 const char *path, int pathlen)
559 562 {
560 563 int i;
@@ -580,7 +583,7 @@ static void write_one(struct strbuf *buffer, struct cache_tree *it,
580 583 #endif
581 584
582 585 if (0 <= it->entry_count) {
583 - strbuf_add(buffer, it->oid.hash, the_hash_algo->rawsz);
586 + strbuf_add(buffer, it->oid.hash, repo->hash_algo->rawsz);
584 587 }
585 588 for (i = 0; i < it->subtree_nr; i++) {
586 589 struct cache_tree_sub *down = it->down[i];
@@ -590,15 +593,16 @@ static void write_one(struct strbuf *buffer, struct cache_tree *it,
590 593 prev->name, prev->namelen) <= 0)
591 594 die("fatal - unsorted cache subtree");
592 595 }
593 - write_one(buffer, down->cache_tree, down->name, down->namelen);
596 + write_one(repo, buffer, down->cache_tree, down->name, down->namelen);
594 597 }
595 598 }
596 599
597 - void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
600 + void cache_tree_write(struct repository *repo,
601 + struct strbuf *sb, struct cache_tree *root)
598 602 {
599 - trace2_region_enter("cache_tree", "write", the_repository);
600 - write_one(sb, root, "", 0);
601 - trace2_region_leave("cache_tree", "write", the_repository);
603 + trace2_region_enter("cache_tree", "write", repo);
604 + write_one(repo, sb, root, "", 0);
605 + trace2_region_leave("cache_tree", "write", repo);
602 606 }
603 607
604 608 static int parse_int(const char **ptr, unsigned long *len_p, int *out)
@@ -632,13 +636,14 @@ static int parse_int(const char **ptr, unsigned long *len_p, int *out)
632 636 return 0;
633 637 }
634 638
635 - static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
639 + static struct cache_tree *read_one(struct repository *repo,
640 + const char **buffer, unsigned long *size_p)
636 641 {
637 642 const char *buf = *buffer;
638 643 unsigned long size = *size_p;
639 644 struct cache_tree *it;
640 645 int i, subtree_nr;
641 - const unsigned rawsz = the_hash_algo->rawsz;
646 + const unsigned rawsz = repo->hash_algo->rawsz;
642 647
643 648 it = NULL;
644 649 /* skip name, but make sure name exists */
@@ -665,7 +670,7 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
665 670 if (size < rawsz)
666 671 goto free_return;
667 672 oidread(&it->oid, (const unsigned char *)buf,
668 - the_repository->hash_algo);
673 + repo->hash_algo);
669 674 buf += rawsz;
670 675 size -= rawsz;
671 676 }
@@ -693,7 +698,7 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
693 698 struct cache_tree_sub *subtree;
694 699 const char *name = buf;
695 700
696 - sub = read_one(&buf, &size);
701 + sub = read_one(repo, &buf, &size);
697 702 if (!sub)
698 703 goto free_return;
699 704 subtree = cache_tree_sub(it, name);
@@ -710,16 +715,17 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
710 715 return NULL;
711 716 }
712 717
713 - struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
718 + struct cache_tree *cache_tree_read(struct repository *repo,
719 + const char *buffer, unsigned long size)
714 720 {
715 721 struct cache_tree *result;
716 722
717 723 if (buffer[0])
718 724 return NULL; /* not the whole tree */
719 725
720 - trace2_region_enter("cache_tree", "read", the_repository);
721 - result = read_one(&buffer, &size);
722 - trace2_region_leave("cache_tree", "read", the_repository);
726 + trace2_region_enter("cache_tree", "read", repo);
727 + result = read_one(repo, &buffer, &size);
728 + trace2_region_leave("cache_tree", "read", repo);
723 729
724 730 return result;
725 731 }
@@ -810,7 +816,7 @@ int write_index_as_tree(struct object_id *oid, struct index_state *index_state,
810 816 hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
811 817
812 818 entries = read_index_from(index_state, index_path,
813 - repo_get_git_dir(the_repository));
819 + repo_get_git_dir(index_state->repo));
814 820 if (entries < 0) {
815 821 ret = WRITE_TREE_UNREADABLE_INDEX;
816 822 goto out;
@@ -866,7 +872,7 @@ static void prime_cache_tree_rec(struct repository *r,
866 872 struct cache_tree_sub *sub;
867 873 struct tree *subtree = lookup_tree(r, &entry.oid);
868 874
869 - if (repo_parse_tree(the_repository, subtree) < 0)
875 + if (repo_parse_tree(r, subtree) < 0)
870 876 exit(128);
871 877 sub = cache_tree_sub(it, entry.path);
872 878 sub->cache_tree = cache_tree();
modified cache-tree.h
@@ -28,8 +28,9 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
28 28
29 29 int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen);
30 30
31 - void cache_tree_write(struct strbuf *, struct cache_tree *root);
32 - struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
31 + void cache_tree_write(struct repository *repo, struct strbuf *, struct cache_tree *root);
32 + struct cache_tree *cache_tree_read(struct repository *repo,
33 + const char *buffer, unsigned long size);
33 34
34 35 int cache_tree_fully_valid(struct index_state *);
35 36 int cache_tree_update(struct index_state *, int);
modified read-cache-ll.h
@@ -269,9 +269,10 @@ void validate_cache_entries(const struct index_state *istate);
269 269 * the given predicate. This function should only be called if
270 270 * repo_has_promisor_remote() returns true.
271 271 */
272 - typedef int (*must_prefetch_predicate)(const struct cache_entry *);
272 + typedef int (*must_prefetch_predicate)(const struct cache_entry *, void *cb_data);
273 273 void prefetch_cache_entries(const struct index_state *istate,
274 - must_prefetch_predicate must_prefetch);
274 + must_prefetch_predicate must_prefetch,
275 + void *cb_data);
275 276
276 277 /* Initialize and use the cache information */
277 278 struct lock_file;
modified read-cache.c
@@ -1748,7 +1748,7 @@ static int read_index_extension(struct index_state *istate,
1748 1748 {
1749 1749 switch (CACHE_EXT(ext)) {
1750 1750 case CACHE_EXT_TREE:
1751 - istate->cache_tree = cache_tree_read(data, sz);
1751 + istate->cache_tree = cache_tree_read(istate->repo, data, sz);
1752 1752 break;
1753 1753 case CACHE_EXT_RESOLVE_UNDO:
1754 1754 istate->resolve_undo = resolve_undo_read(data, sz, the_hash_algo);
@@ -3012,7 +3012,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3012 3012 !drop_cache_tree && istate->cache_tree) {
3013 3013 strbuf_reset(&sb);
3014 3014
3015 - cache_tree_write(&sb, istate->cache_tree);
3015 + cache_tree_write(istate->repo, &sb, istate->cache_tree);
3016 3016 err = write_index_ext_header(f, eoie_c, CACHE_EXT_TREE, sb.len) < 0;
3017 3017 hashwrite(f, sb.buf, sb.len);
3018 3018 if (err) {
@@ -3733,7 +3733,8 @@ static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_ta
3733 3733 }
3734 3734
3735 3735 void prefetch_cache_entries(const struct index_state *istate,
3736 - must_prefetch_predicate must_prefetch)
3736 + must_prefetch_predicate must_prefetch,
3737 + void *cb_data)
3737 3738 {
3738 3739 int i;
3739 3740 struct oid_array to_fetch = OID_ARRAY_INIT;
@@ -3741,7 +3742,7 @@ void prefetch_cache_entries(const struct index_state *istate,
3741 3742 for (i = 0; i < istate->cache_nr; i++) {
3742 3743 struct cache_entry *ce = istate->cache[i];
3743 3744
3744 - if (S_ISGITLINK(ce->ce_mode) || !must_prefetch(ce))
3745 + if (S_ISGITLINK(ce->ce_mode) || !must_prefetch(ce, cb_data))
3745 3746 continue;
3746 3747 if (!odb_read_object_info_extended(the_repository->objects,
3747 3748 &ce->oid, NULL,
modified unpack-trees.c
@@ -416,7 +416,8 @@ static void report_collided_checkout(struct index_state *index)
416 416 string_list_clear(&list, 0);
417 417 }
418 418
419 - static int must_checkout(const struct cache_entry *ce)
419 + static int must_checkout(const struct cache_entry *ce,
420 + void *cb_data UNUSED)
420 421 {
421 422 return ce->ce_flags & CE_UPDATE;
422 423 }
@@ -477,7 +478,7 @@ static int check_updates(struct unpack_trees_options *o,
477 478 * Prefetch the objects that are to be checked out in the loop
478 479 * below.
479 480 */
480 - prefetch_cache_entries(index, must_checkout);
481 + prefetch_cache_entries(index, must_checkout, NULL);
481 482
482 483 get_parallel_checkout_configs(&pc_workers, &pc_threshold);
483 484
@@ -487,7 +488,7 @@ static int check_updates(struct unpack_trees_options *o,
487 488 for (i = 0; i < index->cache_nr; i++) {
488 489 struct cache_entry *ce = index->cache[i];
489 490
490 - if (must_checkout(ce)) {
491 + if (must_checkout(ce, NULL)) {
491 492 size_t last_pc_queue_size = pc_queue_size();
492 493
493 494 if (ce->ce_flags & CE_WT_REMOVE)