@@ -33,19 +33,14 @@ static int convert_to_sparse_rec(struct index_state *istate,
3333{
3434 int i , can_convert = 1 ;
3535 int start_converted = num_converted ;
36- enum pattern_match_result match ;
37- int dtype = DT_UNKNOWN ;
3836 struct strbuf child_path = STRBUF_INIT ;
39- struct pattern_list * pl = istate -> sparse_checkout_patterns ;
4037
4138 /*
4239 * Is the current path outside of the sparse cone?
4340 * Then check if the region can be replaced by a sparse
4441 * directory entry (everything is sparse and merged).
4542 */
46- match = path_matches_pattern_list (ct_path , ct_pathlen ,
47- NULL , & dtype , pl , istate );
48- if (match != NOT_MATCHED )
43+ if (path_in_sparse_checkout (ct_path , istate ))
4944 can_convert = 0 ;
5045
5146 for (i = start ; can_convert && i < end ; i ++ ) {
@@ -127,41 +122,51 @@ static int index_has_unmerged_entries(struct index_state *istate)
127122 return 0 ;
128123}
129124
130- int convert_to_sparse (struct index_state * istate )
125+ int convert_to_sparse (struct index_state * istate , int flags )
131126{
132127 int test_env ;
133- if (istate -> split_index || istate -> sparse_index ||
128+ if (istate -> sparse_index || ! istate -> cache_nr ||
134129 !core_apply_sparse_checkout || !core_sparse_checkout_cone )
135130 return 0 ;
136131
137132 if (!istate -> repo )
138133 istate -> repo = the_repository ;
139134
140- /*
141- * The GIT_TEST_SPARSE_INDEX environment variable triggers the
142- * index.sparse config variable to be on.
143- */
144- test_env = git_env_bool ("GIT_TEST_SPARSE_INDEX" , -1 );
145- if (test_env >= 0 )
146- set_sparse_index_config (istate -> repo , test_env );
147-
148- /*
149- * Only convert to sparse if index.sparse is set.
150- */
151- prepare_repo_settings (istate -> repo );
152- if (!istate -> repo -> settings .sparse_index )
153- return 0 ;
135+ if (!(flags & SPARSE_INDEX_MEMORY_ONLY )) {
136+ /*
137+ * The sparse index is not (yet) integrated with a split index.
138+ */
139+ if (istate -> split_index )
140+ return 0 ;
141+ /*
142+ * The GIT_TEST_SPARSE_INDEX environment variable triggers the
143+ * index.sparse config variable to be on.
144+ */
145+ test_env = git_env_bool ("GIT_TEST_SPARSE_INDEX" , -1 );
146+ if (test_env >= 0 )
147+ set_sparse_index_config (istate -> repo , test_env );
154148
155- if (!istate -> sparse_checkout_patterns ) {
156- istate -> sparse_checkout_patterns = xcalloc (1 , sizeof (struct pattern_list ));
157- if (get_sparse_checkout_patterns (istate -> sparse_checkout_patterns ) < 0 )
149+ /*
150+ * Only convert to sparse if index.sparse is set.
151+ */
152+ prepare_repo_settings (istate -> repo );
153+ if (!istate -> repo -> settings .sparse_index )
158154 return 0 ;
159155 }
160156
161- if (!istate -> sparse_checkout_patterns -> use_cone_patterns ) {
162- warning (_ ("attempting to use sparse-index without cone mode" ));
163- return -1 ;
164- }
157+ if (init_sparse_checkout_patterns (istate ))
158+ return 0 ;
159+
160+ /*
161+ * We need cone-mode patterns to use sparse-index. If a user edits
162+ * their sparse-checkout file manually, then we can detect during
163+ * parsing that they are not actually using cone-mode patterns and
164+ * hence we need to abort this conversion _without error_. Warnings
165+ * already exist in the pattern parsing to inform the user of their
166+ * bad patterns.
167+ */
168+ if (!istate -> sparse_checkout_patterns -> use_cone_patterns )
169+ return 0 ;
165170
166171 /*
167172 * NEEDSWORK: If we have unmerged entries, then stay full.
@@ -172,10 +177,15 @@ int convert_to_sparse(struct index_state *istate)
172177
173178 /* Clear and recompute the cache-tree */
174179 cache_tree_free (& istate -> cache_tree );
175- if (cache_tree_update (istate , 0 )) {
176- warning (_ ("unable to update cache-tree, staying full" ));
177- return -1 ;
178- }
180+ /*
181+ * Silently return if there is a problem with the cache tree update,
182+ * which might just be due to a conflict state in some entry.
183+ *
184+ * This might create new tree objects, so be sure to use
185+ * WRITE_TREE_MISSING_OK.
186+ */
187+ if (cache_tree_update (istate , WRITE_TREE_MISSING_OK ))
188+ return 0 ;
179189
180190 remove_fsmonitor (istate );
181191
0 commit comments