sparse-checkout: refuse to add to bad patterns

When in cone mode sparse-checkout, it is unclear how 'git sparse-checkout add <dir1> ...' should behave if the existing sparse-checkout file does not match the cone mode patterns. Change the behavior to fail with an error message about the existing patterns. Also, all cone mode patterns start with a '/' character, so add that restriction. This is necessary for our example test 'cone mode: warn on bad pattern', but also requires modifying the example sparse-checkout file we use to test the warnings related to recognizing cone mode patterns. This error checking would cause a failure further down the test script because of a test that adds non-cone mode patterns without cleaning them up. Perform that cleanup as part of the test now. Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Dec 16, 2021 at 16:13 UTC a3eca5844526fd6111e7a1e8bdfa9813673a6f23
3 files changed +9 -3
builtin/sparse-checkout.c
+3
@@ -598,6 +598,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,
598 die(_("unable to load existing sparse-checkout patterns"));
599 free(sparse_filename);
600
601 + if (!existing.use_cone_patterns)
602 + die(_("existing sparse-checkout patterns do not use cone mode"));
603 +
604 hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
605 if (!hashmap_contains_parent(&pl->recursive_hashmap,
606 pe->pattern, &buffer) ||
dir.c
+1 -1
@@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
727 }
728
729 if (given->patternlen < 2 ||
730 - *given->pattern == '*' ||
730 + *given->pattern != '/' ||
731 strstr(given->pattern, "**")) {
732 /* Not a cone pattern. */
733 warning(_("unrecognized pattern: '%s'"), given->pattern);
t/t1091-sparse-checkout-builtin.sh
+5 -2
@@ -110,7 +110,8 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' '
110 git sparse-checkout init &&
111 git sparse-checkout add dir &&
112 git config core.sparseCheckoutCone true &&
113 - git sparse-checkout add dir
113 + test_must_fail git sparse-checkout add dir 2>err &&
114 + grep "existing sparse-checkout patterns do not use cone mode" err
115 )
116 '
117
@@ -176,12 +177,14 @@ test_expect_success 'set sparse-checkout using --stdin' '
177 '
178
179 test_expect_success 'add to sparse-checkout' '
179 - cat repo/.git/info/sparse-checkout >expect &&
180 + cat repo/.git/info/sparse-checkout >old &&
181 + test_when_finished cp old repo/.git/info/sparse-checkout &&
182 cat >add <<-\EOF &&
183 pattern1
184 /folder1/
185 pattern2
186 EOF
187 + cat old >expect &&
188 cat add >>expect &&
189 git -C repo sparse-checkout add --stdin <add &&
190 git -C repo sparse-checkout list >actual &&