sparse-index: add index.sparse config option

When enabled, this config option signals that index writes should attempt to use sparse-directory entries. 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 58300f4743231724686d9ddf481aeefa4f90d2f7
5 files changed +44 -6
Documentation/config/index.txt
+5
@@ -14,6 +14,11 @@ index.recordOffsetTable::
14 Defaults to 'true' if index.threads has been explicitly enabled,
15 'false' otherwise.
16
17 +index.sparse::
18 + When enabled, write the index using sparse-directory entries. This
19 + has no effect unless `core.sparseCheckout` and
20 + `core.sparseCheckoutCone` are both enabled. Defaults to 'false'.
21 +
22 index.threads::
23 Specifies the number of threads to spawn when loading the index.
24 This is meant to reduce index load time on multiprocessor machines.
cache.h
+1
@@ -1059,6 +1059,7 @@ struct repository_format {
1059 int worktree_config;
1060 int is_bare;
1061 int hash_algo;
1062 + int sparse_index;
1063 char *work_tree;
1064 struct string_list unknown_extensions;
1065 struct string_list v1_only_extensions;
repo-settings.c
+7
@@ -85,4 +85,11 @@ void prepare_repo_settings(struct repository *r)
85 * removed.
86 */
87 r->settings.command_requires_full_index = 1;
88 +
89 + /*
90 + * Initialize this as off.
91 + */
92 + r->settings.sparse_index = 0;
93 + if (!repo_config_get_bool(r, "index.sparse", &value) && value)
94 + r->settings.sparse_index = 1;
95 }
repository.h
+2 -1
@@ -42,7 +42,8 @@ struct repo_settings {
42
43 int core_multi_pack_index;
44
45 - unsigned command_requires_full_index:1;
45 + unsigned command_requires_full_index:1,
46 + sparse_index:1;
47 };
48
49 struct repository {
sparse-index.c
+29 -5
@@ -102,19 +102,43 @@ static int convert_to_sparse_rec(struct index_state *istate,
102 return num_converted - start_converted;
103 }
104
105 +static int enable_sparse_index(struct repository *repo)
106 +{
107 + const char *config_path = repo_git_path(repo, "config.worktree");
108 +
109 + git_config_set_in_file_gently(config_path,
110 + "index.sparse",
111 + "true");
112 +
113 + prepare_repo_settings(repo);
114 + repo->settings.sparse_index = 1;
115 + return 0;
116 +}
117 +
118 int convert_to_sparse(struct index_state *istate)
119 {
120 if (istate->split_index || istate->sparse_index ||
121 !core_apply_sparse_checkout || !core_sparse_checkout_cone)
122 return 0;
123
124 + if (!istate->repo)
125 + istate->repo = the_repository;
126 +
127 + /*
128 + * The GIT_TEST_SPARSE_INDEX environment variable triggers the
129 + * index.sparse config variable to be on.
130 + */
131 + if (git_env_bool("GIT_TEST_SPARSE_INDEX", 0)) {
132 + int err = enable_sparse_index(istate->repo);
133 + if (err < 0)
134 + return err;
135 + }
136 +
137 /*
112 - * For now, only create a sparse index with the
113 - * GIT_TEST_SPARSE_INDEX environment variable. We will relax
114 - * this once we have a proper way to opt-in (and later still,
115 - * opt-out).
138 + * Only convert to sparse if index.sparse is set.
139 */
117 - if (!git_env_bool("GIT_TEST_SPARSE_INDEX", 0))
140 + prepare_repo_settings(istate->repo);
141 + if (!istate->repo->settings.sparse_index)
142 return 0;
143
144 if (!istate->sparse_checkout_patterns) {