sparse-index: add guard to ensure full index

Upcoming changes will introduce modifications to the index format that allow sparse directories. It will be useful to have a mechanism for converting those sparse index files into full indexes by walking the tree at those sparse directories. Name this method ensure_full_index() as it will guarantee that the index is fully expanded. This method is not implemented yet, and instead we focus on the scaffolding to declare it and call it at the appropriate time. Add a 'command_requires_full_index' member to struct repo_settings. This will be an indicator that we need the index in full mode to do certain index operations. This starts as being true for every command, then we will set it to false as some commands integrate with sparse indexes. If 'command_requires_full_index' is true, then we will immediately expand a sparse index to a full one upon reading from disk. This suffices for now, but we will want to add more callers to ensure_full_index() later. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Mar 30, 2021 at 13:10 UTC 3964fc2aae7c7420a4c5da1b9530e8e8de1ed367
6 files changed +36 -1
Makefile
+1
@@ -985,6 +985,7 @@ LIB_OBJS += setup.o
985 LIB_OBJS += shallow.o
986 LIB_OBJS += sideband.o
987 LIB_OBJS += sigchain.o
988 +LIB_OBJS += sparse-index.o
989 LIB_OBJS += split-index.o
990 LIB_OBJS += stable-qsort.o
991 LIB_OBJS += strbuf.o
repo-settings.c
+8
@@ -77,4 +77,12 @@ void prepare_repo_settings(struct repository *r)
77 UPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_KEEP);
78
79 UPDATE_DEFAULT_BOOL(r->settings.fetch_negotiation_algorithm, FETCH_NEGOTIATION_DEFAULT);
80 +
81 + /*
82 + * This setting guards all index reads to require a full index
83 + * over a sparse index. After suitable guards are placed in the
84 + * codebase around uses of the index, this setting will be
85 + * removed.
86 + */
87 + r->settings.command_requires_full_index = 1;
88 }
repository.c
+10 -1
@@ -10,6 +10,7 @@
10 #include "object.h"
11 #include "lockfile.h"
12 #include "submodule-config.h"
13 +#include "sparse-index.h"
14
15 /* The main repository */
16 static struct repository the_repo;
@@ -261,6 +262,8 @@ void repo_clear(struct repository *repo)
262
263 int repo_read_index(struct repository *repo)
264 {
265 + int res;
266 +
267 if (!repo->index)
268 repo->index = xcalloc(1, sizeof(*repo->index));
269
@@ -270,7 +273,13 @@ int repo_read_index(struct repository *repo)
273 else if (repo->index->repo != repo)
274 BUG("repo's index should point back at itself");
275
273 - return read_index_from(repo->index, repo->index_file, repo->gitdir);
276 + res = read_index_from(repo->index, repo->index_file, repo->gitdir);
277 +
278 + prepare_repo_settings(repo);
279 + if (repo->settings.command_requires_full_index)
280 + ensure_full_index(repo->index);
281 +
282 + return res;
283 }
284
285 int repo_hold_locked_index(struct repository *repo,
repository.h
+2
@@ -41,6 +41,8 @@ struct repo_settings {
41 enum fetch_negotiation_setting fetch_negotiation_algorithm;
42
43 int core_multi_pack_index;
44 +
45 + unsigned command_requires_full_index:1;
46 };
47
48 struct repository {
sparse-index.c new
+8
@@ -0,0 +1,8 @@
1 +#include "cache.h"
2 +#include "repository.h"
3 +#include "sparse-index.h"
4 +
5 +void ensure_full_index(struct index_state *istate)
6 +{
7 + /* intentionally left blank */
8 +}
sparse-index.h new
+7
@@ -0,0 +1,7 @@
1 +#ifndef SPARSE_INDEX_H__
2 +#define SPARSE_INDEX_H__
3 +
4 +struct index_state;
5 +void ensure_full_index(struct index_state *istate);
6 +
7 +#endif