]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: split out code for tweaking settings config
authorElijah Newren <newren@gmail.com>
Tue, 14 Dec 2021 04:09:07 +0000 (04:09 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Dec 2021 19:48:22 +0000 (11:48 -0800)
`init` has some code for handling updates to either cone mode or
the sparse-index setting.  We would like to be able to reuse this
elsewhere, namely in `set` and `reapply`.  Split this function out,
and make it slightly more general so it can handle being called from
the new callers.

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>
builtin/sparse-checkout.c

index 387903eafe755b15f3549a40b0a0cca96a5646cf..3b74779bb48b22d7f06dddd0b4329e37b586e832 100644 (file)
@@ -383,6 +383,41 @@ static int set_config(enum sparse_checkout_mode mode)
        return 0;
 }
 
+static int update_modes(int *cone_mode, int *sparse_index)
+{
+       int mode, record_mode;
+
+       /* Determine if we need to record the mode; ensure sparse checkout on */
+       record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
+
+       /* If not specified, use previous definition of cone mode */
+       if (*cone_mode == -1 && core_apply_sparse_checkout)
+               *cone_mode = core_sparse_checkout_cone;
+
+       /* Set cone/non-cone mode appropriately */
+       core_apply_sparse_checkout = 1;
+       if (*cone_mode == 1) {
+               mode = MODE_CONE_PATTERNS;
+               core_sparse_checkout_cone = 1;
+       } else {
+               mode = MODE_ALL_PATTERNS;
+       }
+       if (record_mode && set_config(mode))
+               return 1;
+
+       /* Set sparse-index/non-sparse-index mode if specified */
+       if (*sparse_index >= 0) {
+               if (set_sparse_index_config(the_repository, *sparse_index) < 0)
+                       die(_("failed to modify sparse-index config"));
+
+               /* force an index rewrite */
+               repo_read_index(the_repository);
+               the_repository->index->updated_workdir = 1;
+       }
+
+       return 0;
+}
+
 static char const * const builtin_sparse_checkout_init_usage[] = {
        N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
        NULL
@@ -399,7 +434,6 @@ static int sparse_checkout_init(int argc, const char **argv)
        char *sparse_filename;
        int res;
        struct object_id oid;
-       int mode;
        struct strbuf pattern = STRBUF_INIT;
 
        static struct option builtin_sparse_checkout_init_options[] = {
@@ -412,19 +446,14 @@ static int sparse_checkout_init(int argc, const char **argv)
 
        repo_read_index(the_repository);
 
+       init_opts.cone_mode = -1;
        init_opts.sparse_index = -1;
 
        argc = parse_options(argc, argv, NULL,
                             builtin_sparse_checkout_init_options,
                             builtin_sparse_checkout_init_usage, 0);
 
-       if (init_opts.cone_mode) {
-               mode = MODE_CONE_PATTERNS;
-               core_sparse_checkout_cone = 1;
-       } else
-               mode = MODE_ALL_PATTERNS;
-
-       if (set_config(mode))
+       if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
                return 1;
 
        memset(&pl, 0, sizeof(pl));
@@ -432,17 +461,6 @@ static int sparse_checkout_init(int argc, const char **argv)
        sparse_filename = get_sparse_checkout_filename();
        res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
 
-       if (init_opts.sparse_index >= 0) {
-               if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0)
-                       die(_("failed to modify sparse-index config"));
-
-               /* force an index rewrite */
-               repo_read_index(the_repository);
-               the_repository->index->updated_workdir = 1;
-       }
-
-       core_apply_sparse_checkout = 1;
-
        /* If we already have a sparse-checkout file, use it. */
        if (res >= 0) {
                free(sparse_filename);