sparse-checkout: disable sparse-index

We use 'git sparse-checkout init --cone --sparse-index' to toggle the sparse-index feature. It makes sense to also disable it when running 'git sparse-checkout disable'. This is particularly important because it removes the extensions.sparseIndex config option, allowing other tools to use this Git repository again. This does mean that 'git sparse-checkout init' will not re-enable the sparse-index feature, even if it was previously enabled. While testing this feature, I noticed that the sparse-index was not being written on the first run, but by a second. This was caught by the call to 'test-tool read-cache --table'. This requires adjusting some assignments to core_apply_sparse_checkout and pl.use_cone_patterns in the sparse_checkout_init() logic. 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:11 UTC dcc5fd5fd26b71d75f9e70abd0e09e553b5f40ca
2 files changed +22 -1
builtin/sparse-checkout.c
+9 -1
@@ -280,6 +280,9 @@ static int set_config(enum sparse_checkout_mode mode)
280 "core.sparseCheckoutCone",
281 mode == MODE_CONE_PATTERNS ? "true" : NULL);
282
283 + if (mode == MODE_NO_PATTERNS)
284 + set_sparse_index_config(the_repository, 0);
285 +
286 return 0;
287 }
288
@@ -341,10 +344,11 @@ static int sparse_checkout_init(int argc, const char **argv)
344 the_repository->index->updated_workdir = 1;
345 }
346
347 + core_apply_sparse_checkout = 1;
348 +
349 /* If we already have a sparse-checkout file, use it. */
350 if (res >= 0) {
351 free(sparse_filename);
347 - core_apply_sparse_checkout = 1;
352 return update_working_directory(NULL);
353 }
354
@@ -366,6 +370,7 @@ static int sparse_checkout_init(int argc, const char **argv)
370 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
371 strbuf_addstr(&pattern, "!/*/");
372 add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
373 + pl.use_cone_patterns = init_opts.cone_mode;
374
375 return write_patterns_and_update(&pl);
376 }
@@ -632,6 +637,9 @@ static int sparse_checkout_disable(int argc, const char **argv)
637 strbuf_addstr(&match_all, "/*");
638 add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
639
640 + prepare_repo_settings(the_repository);
641 + the_repository->settings.sparse_index = 0;
642 +
643 if (update_working_directory(&pl))
644 die(_("error while refreshing working directory"));
645
t/t1091-sparse-checkout-builtin.sh
+13
@@ -205,6 +205,19 @@ test_expect_success 'sparse-checkout disable' '
205 check_files repo a deep folder1 folder2
206 '
207
208 +test_expect_success 'sparse-index enabled and disabled' '
209 + git -C repo sparse-checkout init --cone --sparse-index &&
210 + test_cmp_config -C repo true index.sparse &&
211 + test-tool -C repo read-cache --table >cache &&
212 + grep " tree " cache &&
213 +
214 + git -C repo sparse-checkout disable &&
215 + test-tool -C repo read-cache --table >cache &&
216 + ! grep " tree " cache &&
217 + git -C repo config --list >config &&
218 + ! grep index.sparse config
219 +'
220 +
221 test_expect_success 'cone mode: init and set' '
222 git -C repo sparse-checkout init --cone &&
223 git -C repo config --list >config &&