diff mbox series

[3/3] sparse-checkout: refuse to add to bad patterns

Message ID d513b28b75189d066f9c66de44a1a578cbc38139.1632160658.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series Sparse checkout: fix mixed-mode pattern issues | expand

Commit Message

Derrick Stolee Sept. 20, 2021, 5:57 p.m. UTC
From: Derrick Stolee <dstolee@microsoft.com>

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.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 builtin/sparse-checkout.c          |  3 +++
 dir.c                              |  2 +-
 t/t1091-sparse-checkout-builtin.sh | 11 +++++++----
 3 files changed, 11 insertions(+), 5 deletions(-)

Comments

Taylor Blau Sept. 20, 2021, 6:59 p.m. UTC | #1
On Mon, Sep 20, 2021 at 05:57:38PM +0000, Derrick Stolee via GitGitGadget wrote:
> From: Derrick Stolee <dstolee@microsoft.com>
>
> 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.
>
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
>  builtin/sparse-checkout.c          |  3 +++
>  dir.c                              |  2 +-
>  t/t1091-sparse-checkout-builtin.sh | 11 +++++++----
>  3 files changed, 11 insertions(+), 5 deletions(-)
>
> diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
> index fe76c3eedda..2492ae828a9 100644
> --- a/builtin/sparse-checkout.c
> +++ b/builtin/sparse-checkout.c
> @@ -513,6 +513,9 @@ static void add_patterns_cone_mode(int argc, const char **argv,
>  		die(_("unable to load existing sparse-checkout patterns"));
>  	free(sparse_filename);
>
> +	if (!existing.use_cone_patterns)
> +		die(_("existing sparse-checkout patterns do not use cone mode"));
> +

Makes sense.

>  	hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
>  		if (!hashmap_contains_parent(&pl->recursive_hashmap,
>  					pe->pattern, &buffer) ||
> diff --git a/dir.c b/dir.c
> index 03c4d212672..93136442103 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -727,7 +727,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
>  	}
>
>  	if (given->patternlen < 2 ||
> -	    *given->pattern == '*' ||
> +	    *given->pattern != '/' ||

Makes sense that we require cone-mode patterns to start with '/', which
necessarily means we want to drop the other arm `*given->pattern == '*'`.

>  	    strstr(given->pattern, "**")) {
>  		/* Not a cone pattern. */
>  		warning(_("unrecognized pattern: '%s'"), given->pattern);
> diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
> index af0acd32bd9..780c6a1aaae 100755
> --- a/t/t1091-sparse-checkout-builtin.sh
> +++ b/t/t1091-sparse-checkout-builtin.sh
> @@ -108,14 +108,17 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' '
>  	git -C bad-patterns sparse-checkout init &&
>  	git -C bad-patterns sparse-checkout add dir &&
>  	git -C bad-patterns config core.sparseCheckoutCone true &&
> -	git -C bad-patterns sparse-checkout add dir &&
> +
> +	test_must_fail git -C bad-patterns sparse-checkout add dir 2>err &&
> +	grep "existing sparse-checkout patterns do not use cone mode" err &&
>
>  	git -C bad-patterns sparse-checkout init --cone &&
>  	cat >expect <<-\EOF &&
>  	/*
>  	!/*/
>  	EOF
> -	test_cmp expect bad-patterns/.git/info/sparse-checkout
> +	test_cmp expect bad-patterns/.git/info/sparse-checkout &&
> +	git -C bad-patterns sparse-checkout add dir

Good thinking to make sure that we can add `dir` after clearing. But
let's check the contents of the sparse-checkout file to make sure that
it was added in cone mode.

>  '
>
>  test_expect_success 'interaction with clone --no-checkout (unborn index)' '
> @@ -182,9 +185,9 @@ test_expect_success 'set sparse-checkout using --stdin' '
>  test_expect_success 'add to sparse-checkout' '
>  	cat repo/.git/info/sparse-checkout >expect &&
>  	cat >add <<-\EOF &&
> -	pattern1
> +	/pattern1
>  	/folder1/
> -	pattern2
> +	/pattern2
>  	EOF
>  	cat add >>expect &&
>  	git -C repo sparse-checkout add --stdin <add &&

Hmm. Does this new restriction apply to patterns given over the
command-line? (It looks like we handle the two slightly differently, so
I wonder if we are expecting users to write "git sparse-checkout add
/foo" instead of "... add foo" as a consequence).

Thanks,
Taylor
diff mbox series

Patch

diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index fe76c3eedda..2492ae828a9 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -513,6 +513,9 @@  static void add_patterns_cone_mode(int argc, const char **argv,
 		die(_("unable to load existing sparse-checkout patterns"));
 	free(sparse_filename);
 
+	if (!existing.use_cone_patterns)
+		die(_("existing sparse-checkout patterns do not use cone mode"));
+
 	hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
 		if (!hashmap_contains_parent(&pl->recursive_hashmap,
 					pe->pattern, &buffer) ||
diff --git a/dir.c b/dir.c
index 03c4d212672..93136442103 100644
--- a/dir.c
+++ b/dir.c
@@ -727,7 +727,7 @@  static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
 	}
 
 	if (given->patternlen < 2 ||
-	    *given->pattern == '*' ||
+	    *given->pattern != '/' ||
 	    strstr(given->pattern, "**")) {
 		/* Not a cone pattern. */
 		warning(_("unrecognized pattern: '%s'"), given->pattern);
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
index af0acd32bd9..780c6a1aaae 100755
--- a/t/t1091-sparse-checkout-builtin.sh
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -108,14 +108,17 @@  test_expect_success 'switching to cone mode with non-cone mode patterns' '
 	git -C bad-patterns sparse-checkout init &&
 	git -C bad-patterns sparse-checkout add dir &&
 	git -C bad-patterns config core.sparseCheckoutCone true &&
-	git -C bad-patterns sparse-checkout add dir &&
+
+	test_must_fail git -C bad-patterns sparse-checkout add dir 2>err &&
+	grep "existing sparse-checkout patterns do not use cone mode" err &&
 
 	git -C bad-patterns sparse-checkout init --cone &&
 	cat >expect <<-\EOF &&
 	/*
 	!/*/
 	EOF
-	test_cmp expect bad-patterns/.git/info/sparse-checkout
+	test_cmp expect bad-patterns/.git/info/sparse-checkout &&
+	git -C bad-patterns sparse-checkout add dir
 '
 
 test_expect_success 'interaction with clone --no-checkout (unborn index)' '
@@ -182,9 +185,9 @@  test_expect_success 'set sparse-checkout using --stdin' '
 test_expect_success 'add to sparse-checkout' '
 	cat repo/.git/info/sparse-checkout >expect &&
 	cat >add <<-\EOF &&
-	pattern1
+	/pattern1
 	/folder1/
-	pattern2
+	/pattern2
 	EOF
 	cat add >>expect &&
 	git -C repo sparse-checkout add --stdin <add &&