]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: add sanity-checks on initial sparsity state
authorElijah Newren <newren@gmail.com>
Tue, 14 Dec 2021 04:09:05 +0000 (04:09 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Dec 2021 19:48:21 +0000 (11:48 -0800)
Most sparse-checkout subcommands (list, add, reapply) only make sense
when already in a sparse state.  Add a quick check that will error out
early if this is not the case.

Also document with a comment why we do not exit early in `disable` even
when core.sparseCheckout starts as false.

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
t/t1091-sparse-checkout-builtin.sh

index e252b82136e4fc091aaf68b0daf022bfd9bcdbb1..eb8fbd36b0bb837ee543a15ecee9b01a2ac61a24 100644 (file)
@@ -56,6 +56,9 @@ static int sparse_checkout_list(int argc, const char **argv)
        char *sparse_filename;
        int res;
 
+       if (!core_apply_sparse_checkout)
+               die(_("this worktree is not sparse"));
+
        argc = parse_options(argc, argv, NULL,
                             builtin_sparse_checkout_list_options,
                             builtin_sparse_checkout_list_usage, 0);
@@ -671,6 +674,9 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
                OPT_END(),
        };
 
+       if (!core_apply_sparse_checkout)
+               die(_("no sparse-checkout to add to"));
+
        repo_read_index(the_repository);
 
        argc = parse_options(argc, argv, prefix,
@@ -719,6 +725,9 @@ static int sparse_checkout_reapply(int argc, const char **argv)
                OPT_END(),
        };
 
+       if (!core_apply_sparse_checkout)
+               die(_("must be in a sparse-checkout to reapply sparsity patterns"));
+
        argc = parse_options(argc, argv, NULL,
                             builtin_sparse_checkout_reapply_options,
                             builtin_sparse_checkout_reapply_usage, 0);
@@ -740,6 +749,17 @@ static int sparse_checkout_disable(int argc, const char **argv)
        struct pattern_list pl;
        struct strbuf match_all = STRBUF_INIT;
 
+       /*
+        * We do not exit early if !core_apply_sparse_checkout; due to the
+        * ability for users to manually muck things up between
+        *   direct editing of .git/info/sparse-checkout
+        *   running read-tree -m u HEAD or update-index --skip-worktree
+        *   direct toggling of config options
+        * users might end up with an index with SKIP_WORKTREE bit set on
+        * some files and not know how to undo it.  So, here we just
+        * forcibly return to a dense checkout regardless of initial state.
+        */
+
        argc = parse_options(argc, argv, NULL,
                             builtin_sparse_checkout_disable_options,
                             builtin_sparse_checkout_disable_usage, 0);
index 272ba1b566b3eaf43798f9ecfb1a77c26db3f59b..90a281bcf644302a231c97f509cced74f40f6be1 100755 (executable)
@@ -41,7 +41,15 @@ test_expect_success 'setup' '
        )
 '
 
-test_expect_success 'git sparse-checkout list (empty)' '
+test_expect_success 'git sparse-checkout list (not sparse)' '
+       test_must_fail git -C repo sparse-checkout list >list 2>err &&
+       test_must_be_empty list &&
+       test_i18ngrep "this worktree is not sparse" err
+'
+
+test_expect_success 'git sparse-checkout list (not sparse)' '
+       git -C repo sparse-checkout set &&
+       rm repo/.git/info/sparse-checkout &&
        git -C repo sparse-checkout list >list 2>err &&
        test_must_be_empty list &&
        test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err