cache-tree: allow writing in-memory index as tree

The function `write_in_core_index_as_tree()` takes a repository and writes its index into a tree object. What this function cannot do though is to take an _arbitrary_ in-memory index. Introduce a new `struct index_state` parameter so that the caller can pass a different index than the one belonging to the repository. This will be used in a subsequent commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 2, 2026 at 13:13 UTC a021e4f92cbacdb028b3efa49f619b076e72c9a6
3 files changed +6 -4
builtin/checkout.c
+2 -1
@@ -891,7 +891,8 @@ static int merge_working_tree(const struct checkout_opts *opts,
891 0);
892 init_ui_merge_options(&o, the_repository);
893 o.verbosity = 0;
894 - work = write_in_core_index_as_tree(the_repository);
894 + work = write_in_core_index_as_tree(the_repository,
895 + the_repository->index);
896
897 ret = reset_tree(new_tree,
898 opts, 1,
cache-tree.c
+2 -2
@@ -723,11 +723,11 @@ static int write_index_as_tree_internal(struct object_id *oid,
723 return 0;
724 }
725
726 -struct tree* write_in_core_index_as_tree(struct repository *repo) {
726 +struct tree *write_in_core_index_as_tree(struct repository *repo,
727 + struct index_state *index_state) {
728 struct object_id o;
729 int was_valid, ret;
730
730 - struct index_state *index_state = repo->index;
731 was_valid = index_state->cache_tree &&
732 cache_tree_fully_valid(index_state->cache_tree);
733
cache-tree.h
+2 -1
@@ -47,7 +47,8 @@ int cache_tree_verify(struct repository *, struct index_state *);
47 #define WRITE_TREE_UNMERGED_INDEX (-2)
48 #define WRITE_TREE_PREFIX_ERROR (-3)
49
50 -struct tree* write_in_core_index_as_tree(struct repository *repo);
50 +struct tree *write_in_core_index_as_tree(struct repository *repo,
51 + struct index_state *index_state);
52 int write_index_as_tree(struct object_id *oid, struct index_state *index_state, const char *index_path, int flags, const char *prefix);
53 void prime_cache_tree(struct repository *, struct index_state *, struct tree *);
54