sparse-checkout: enable `set` to initialize sparse-checkout mode

The previously suggested workflow: git sparse-checkout init ... git sparse-checkout set ... Suffered from three problems: 1) It would delete nearly all files in the first step, then restore them in the second. That was poor performance and forced unnecessary rebuilds. 2) The two-step process resulted in two progress bars, which was suboptimal from a UI point of view for wrappers that invoked both of these commands but only exposed a single command to their end users. 3) With cone mode, the first step would delete nearly all ignored files everywhere, because everything was considered to be outside of the specified sparsity paths. (The user was not allowed to specify any sparsity paths in the `init` step.) Avoid these problems by teaching `set` to understand the extra parameters that `init` takes and performing any necessary initialization if not already in a sparse checkout. Reviewed-by: Derrick Stolee <dstolee@microsoft.com> Reviewed-by: Victoria Dye <vdye@github.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Dec 14, 2021 at 04:09 UTC f2e3a218e80fc3fd357afe65c9a26b9a90a9f5f7
1 file changed +26 -1
builtin/sparse-checkout.c
+26 -1
@@ -706,17 +706,26 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
706 }
707
708 static char const * const builtin_sparse_checkout_set_usage[] = {
709 - N_("git sparse-checkout set (--stdin | <patterns>)"),
709 + N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] (--stdin | <patterns>)"),
710 NULL
711 };
712
713 static struct sparse_checkout_set_opts {
714 + int cone_mode;
715 + int sparse_index;
716 int use_stdin;
717 } set_opts;
718
719 static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
720 {
721 + int default_patterns_nr = 2;
722 + const char *default_patterns[] = {"/*", "!/*/", NULL};
723 +
724 static struct option builtin_sparse_checkout_set_options[] = {
725 + OPT_BOOL(0, "cone", &set_opts.cone_mode,
726 + N_("initialize the sparse-checkout in cone mode")),
727 + OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
728 + N_("toggle the use of a sparse index")),
729 OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
730 N_("read patterns from standard in"),
731 PARSE_OPT_NONEG),
@@ -725,11 +734,27 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
734
735 repo_read_index(the_repository);
736
737 + set_opts.cone_mode = -1;
738 + set_opts.sparse_index = -1;
739 +
740 argc = parse_options(argc, argv, prefix,
741 builtin_sparse_checkout_set_options,
742 builtin_sparse_checkout_set_usage,
743 PARSE_OPT_KEEP_UNKNOWN);
744
745 + if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
746 + return 1;
747 +
748 + /*
749 + * Cone mode automatically specifies the toplevel directory. For
750 + * non-cone mode, if nothing is specified, manually select just the
751 + * top-level directory (much as 'init' would do).
752 + */
753 + if (!core_sparse_checkout_cone && argc == 0) {
754 + argv = default_patterns;
755 + argc = default_patterns_nr;
756 + }
757 +
758 return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
759 }
760