Skip to content

Commit 45c5e47

Browse files
newrengitster
authored andcommitted
sparse-checkout: add sanity-checks on initial sparsity state
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 <[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 0b624e0 commit 45c5e47

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

builtin/sparse-checkout.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ static int sparse_checkout_list(int argc, const char **argv)
5656
char *sparse_filename;
5757
int res;
5858

59+
if (!core_apply_sparse_checkout)
60+
die(_("this worktree is not sparse"));
61+
5962
argc = parse_options(argc, argv, NULL,
6063
builtin_sparse_checkout_list_options,
6164
builtin_sparse_checkout_list_usage, 0);
@@ -671,6 +674,9 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
671674
OPT_END(),
672675
};
673676

677+
if (!core_apply_sparse_checkout)
678+
die(_("no sparse-checkout to add to"));
679+
674680
repo_read_index(the_repository);
675681

676682
argc = parse_options(argc, argv, prefix,
@@ -719,6 +725,9 @@ static int sparse_checkout_reapply(int argc, const char **argv)
719725
OPT_END(),
720726
};
721727

728+
if (!core_apply_sparse_checkout)
729+
die(_("must be in a sparse-checkout to reapply sparsity patterns"));
730+
722731
argc = parse_options(argc, argv, NULL,
723732
builtin_sparse_checkout_reapply_options,
724733
builtin_sparse_checkout_reapply_usage, 0);
@@ -740,6 +749,17 @@ static int sparse_checkout_disable(int argc, const char **argv)
740749
struct pattern_list pl;
741750
struct strbuf match_all = STRBUF_INIT;
742751

752+
/*
753+
* We do not exit early if !core_apply_sparse_checkout; due to the
754+
* ability for users to manually muck things up between
755+
* direct editing of .git/info/sparse-checkout
756+
* running read-tree -m u HEAD or update-index --skip-worktree
757+
* direct toggling of config options
758+
* users might end up with an index with SKIP_WORKTREE bit set on
759+
* some files and not know how to undo it. So, here we just
760+
* forcibly return to a dense checkout regardless of initial state.
761+
*/
762+
743763
argc = parse_options(argc, argv, NULL,
744764
builtin_sparse_checkout_disable_options,
745765
builtin_sparse_checkout_disable_usage, 0);

t/t1091-sparse-checkout-builtin.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ test_expect_success 'setup' '
4141
)
4242
'
4343

44-
test_expect_success 'git sparse-checkout list (empty)' '
44+
test_expect_success 'git sparse-checkout list (not sparse)' '
45+
test_must_fail git -C repo sparse-checkout list >list 2>err &&
46+
test_must_be_empty list &&
47+
test_i18ngrep "this worktree is not sparse" err
48+
'
49+
50+
test_expect_success 'git sparse-checkout list (not sparse)' '
51+
git -C repo sparse-checkout set &&
52+
rm repo/.git/info/sparse-checkout &&
4553
git -C repo sparse-checkout list >list 2>err &&
4654
test_must_be_empty list &&
4755
test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err

0 commit comments

Comments
 (0)