@@ -108,18 +108,30 @@ int set_sparse_index_config(struct repository *repo, int enable)
108108 char * config_path = repo_git_path (repo , "config.worktree" );
109109 res = git_config_set_in_file_gently (config_path ,
110110 "index.sparse" ,
111- enable ? "true" : NULL );
111+ enable ? "true" : "false" );
112112 free (config_path );
113113
114114 prepare_repo_settings (repo );
115115 repo -> settings .sparse_index = enable ;
116116 return res ;
117117}
118118
119+ static int index_has_unmerged_entries (struct index_state * istate )
120+ {
121+ int i ;
122+ for (i = 0 ; i < istate -> cache_nr ; i ++ ) {
123+ if (ce_stage (istate -> cache [i ]))
124+ return 1 ;
125+ }
126+
127+ return 0 ;
128+ }
129+
119130int convert_to_sparse (struct index_state * istate )
120131{
121132 int test_env ;
122- if (istate -> split_index || istate -> sparse_index ||
133+
134+ if (istate -> split_index || istate -> sparse_index || !istate -> cache_nr ||
123135 !core_apply_sparse_checkout || !core_sparse_checkout_cone )
124136 return 0 ;
125137
@@ -147,15 +159,35 @@ int convert_to_sparse(struct index_state *istate)
147159 return 0 ;
148160 }
149161
150- if (!istate -> sparse_checkout_patterns -> use_cone_patterns ) {
151- warning (_ ("attempting to use sparse-index without cone mode" ));
152- return -1 ;
153- }
162+ /*
163+ * We need cone-mode patterns to use sparse-index. If a user edits
164+ * their sparse-checkout file manually, then we can detect during
165+ * parsing that they are not actually using cone-mode patterns and
166+ * hence we need to abort this conversion _without error_. Warnings
167+ * already exist in the pattern parsing to inform the user of their
168+ * bad patterns.
169+ */
170+ if (!istate -> sparse_checkout_patterns -> use_cone_patterns )
171+ return 0 ;
154172
155- if (cache_tree_update (istate , 0 )) {
156- warning (_ ("unable to update cache-tree, staying full" ));
157- return -1 ;
158- }
173+ /*
174+ * NEEDSWORK: If we have unmerged entries, then stay full.
175+ * Unmerged entries prevent the cache-tree extension from working.
176+ */
177+ if (index_has_unmerged_entries (istate ))
178+ return 0 ;
179+
180+ /* Clear and recompute the cache-tree */
181+ cache_tree_free (& istate -> cache_tree );
182+ /*
183+ * Silently return if there is a problem with the cache tree update,
184+ * which might just be due to a conflict state in some entry.
185+ *
186+ * This might create new tree objects, so be sure to use
187+ * WRITE_TREE_MISSING_OK.
188+ */
189+ if (cache_tree_update (istate , WRITE_TREE_MISSING_OK ))
190+ return 0 ;
159191
160192 remove_fsmonitor (istate );
161193
@@ -168,6 +200,10 @@ int convert_to_sparse(struct index_state *istate)
168200 cache_tree_free (& istate -> cache_tree );
169201 cache_tree_update (istate , 0 );
170202
203+ istate -> fsmonitor_has_run_once = 0 ;
204+ FREE_AND_NULL (istate -> fsmonitor_dirty );
205+ FREE_AND_NULL (istate -> fsmonitor_last_update );
206+
171207 istate -> sparse_index = 1 ;
172208 trace2_region_leave ("index" , "convert_to_sparse" , istate -> repo );
173209 return 0 ;
@@ -195,7 +231,7 @@ static int add_path_to_index(const struct object_id *oid,
195231 strbuf_addstr (base , path );
196232
197233 ce = make_cache_entry (istate , mode , oid , base -> buf , 0 , 0 );
198- ce -> ce_flags |= CE_SKIP_WORKTREE ;
234+ ce -> ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED ;
199235 set_index_entry (istate , istate -> cache_nr ++ , ce );
200236
201237 strbuf_setlen (base , len );
@@ -239,7 +275,7 @@ void ensure_full_index(struct index_state *istate)
239275 warning (_ ("index entry is a directory, but not sparse (%08x)" ),
240276 ce -> ce_flags );
241277
242- /* recursively walk into cd ->name */
278+ /* recursively walk into ce ->name */
243279 tree = lookup_tree (istate -> repo , & ce -> oid );
244280
245281 memset (& ps , 0 , sizeof (ps ));
@@ -264,6 +300,9 @@ void ensure_full_index(struct index_state *istate)
264300 istate -> cache = full -> cache ;
265301 istate -> cache_nr = full -> cache_nr ;
266302 istate -> cache_alloc = full -> cache_alloc ;
303+ istate -> fsmonitor_has_run_once = 0 ;
304+ FREE_AND_NULL (istate -> fsmonitor_dirty );
305+ FREE_AND_NULL (istate -> fsmonitor_last_update );
267306
268307 strbuf_release (& base );
269308 free (full );
0 commit comments