Skip to content

Commit a3eca58

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 391c3a1 commit a3eca58

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

builtin/sparse-checkout.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,
598598
die(_("unable to load existing sparse-checkout patterns"));
599599
free(sparse_filename);
600600

601+
if (!existing.use_cone_patterns)
602+
die(_("existing sparse-checkout patterns do not use cone mode"));
603+
601604
hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
602605
if (!hashmap_contains_parent(&pl->recursive_hashmap,
603606
pe->pattern, &buffer) ||

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
727727
}
728728

729729
if (given->patternlen < 2 ||
730-
*given->pattern == '*' ||
730+
*given->pattern != '/' ||
731731
strstr(given->pattern, "**")) {
732732
/* Not a cone pattern. */
733733
warning(_("unrecognized pattern: '%s'"), given->pattern);

t/t1091-sparse-checkout-builtin.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' '
110110
git sparse-checkout init &&
111111
git sparse-checkout add dir &&
112112
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
114115
)
115116
'
116117

@@ -176,12 +177,14 @@ test_expect_success 'set sparse-checkout using --stdin' '
176177
'
177178

178179
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 &&
180182
cat >add <<-\EOF &&
181183
pattern1
182184
/folder1/
183185
pattern2
184186
EOF
187+
cat old >expect &&
185188
cat add >>expect &&
186189
git -C repo sparse-checkout add --stdin <add &&
187190
git -C repo sparse-checkout list >actual &&

0 commit comments

Comments
 (0)