@matheusbsbs / matheus-bagliano / commits / 99dfa6f970
Fork 0

sparse-checkout: use in-process update for disable subcommand

The 'git sparse-checkout disable' subcommand returns a user to a full working directory. The old process for doing this required updating the sparse-checkout file with the "/*" pattern and then updating the working directory with core.sparseCheckout enabled. Finally, the sparse-checkout file could be removed and the config setting disabled. However, it is valuable to keep a user's sparse-checkout file intact so they can re-enable the sparse-checkout they previously used with 'git sparse-checkout init'. This is now possible with the in-process mechanism for updating the working directory. Reported-by: Szeder Gábor <szeder.dev@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 21, 2019 at 22:04 UTC 99dfa6f9702ee81c44ef9382933e4e391ec5d6ee
3 files changed +18 -16
Documentation/git-sparse-checkout.txt
+4 -2
@@ -52,8 +52,10 @@ When the `--stdin` option is provided, the patterns are read from
52 standard in as a newline-delimited list instead of from the arguments.
53
54 'disable'::
55 - Remove the sparse-checkout file, set `core.sparseCheckout` to
56 - `false`, and restore the working directory to include all files.
55 + Disable the `core.sparseCheckout` config setting, and restore the
56 + working directory to include all files. Leaves the sparse-checkout
57 + file intact so a later 'git sparse-checkout init' command may
58 + return the working directory to the same state.
59
60 SPARSE CHECKOUT
61 ---------------
builtin/sparse-checkout.c
+12 -13
@@ -412,24 +412,23 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
412
413 static int sparse_checkout_disable(int argc, const char **argv)
414 {
415 - char *sparse_filename;
416 - FILE *fp;
415 + static const char *empty_base = "";
416 + struct pattern_list pl;
417 + struct strbuf match_all = STRBUF_INIT;
418
418 - if (set_config(MODE_ALL_PATTERNS))
419 - die(_("failed to change config"));
419 + memset(&pl, 0, sizeof(pl));
420 + hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
421 + hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
422 + pl.use_cone_patterns = 0;
423 + core_apply_sparse_checkout = 1;
424
421 - sparse_filename = get_sparse_checkout_filename();
422 - fp = xfopen(sparse_filename, "w");
423 - fprintf(fp, "/*\n");
424 - fclose(fp);
425 + strbuf_addstr(&match_all, "/*");
426 + add_pattern(strbuf_detach(&match_all, NULL), empty_base, 0, &pl, 0);
427
426 - core_apply_sparse_checkout = 1;
427 - if (update_working_directory(NULL))
428 + if (update_working_directory(&pl))
429 die(_("error while refreshing working directory"));
430
430 - unlink(sparse_filename);
431 - free(sparse_filename);
432 -
431 + clear_pattern_list(&pl);
432 return set_config(MODE_NO_PATTERNS);
433 }
434
t/t1091-sparse-checkout-builtin.sh
+2 -1
@@ -172,8 +172,9 @@ test_expect_success 'cone mode: warn on bad pattern' '
172 '
173
174 test_expect_success 'sparse-checkout disable' '
175 + test_when_finished rm -rf repo/.git/info/sparse-checkout &&
176 git -C repo sparse-checkout disable &&
176 - test_path_is_missing repo/.git/info/sparse-checkout &&
177 + test_path_is_file repo/.git/info/sparse-checkout &&
178 git -C repo config --list >config &&
179 test_must_fail git config core.sparseCheckout &&
180 ls repo >dir &&