Skip to content

Commit f2e3a21

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Reviewed-by: Victoria Dye <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent be61fd1 commit f2e3a21

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

builtin/sparse-checkout.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,17 +706,26 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
706706
}
707707

708708
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>)"),
710710
NULL
711711
};
712712

713713
static struct sparse_checkout_set_opts {
714+
int cone_mode;
715+
int sparse_index;
714716
int use_stdin;
715717
} set_opts;
716718

717719
static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
718720
{
721+
int default_patterns_nr = 2;
722+
const char *default_patterns[] = {"/*", "!/*/", NULL};
723+
719724
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")),
720729
OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
721730
N_("read patterns from standard in"),
722731
PARSE_OPT_NONEG),
@@ -725,11 +734,27 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
725734

726735
repo_read_index(the_repository);
727736

737+
set_opts.cone_mode = -1;
738+
set_opts.sparse_index = -1;
739+
728740
argc = parse_options(argc, argv, prefix,
729741
builtin_sparse_checkout_set_options,
730742
builtin_sparse_checkout_set_usage,
731743
PARSE_OPT_KEEP_UNKNOWN);
732744

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+
733758
return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
734759
}
735760

0 commit comments

Comments
 (0)