diff mbox series

[v3,3/5] read-cache & fetch-negotiator: check "enum" values in switch()

Message ID patch-v3-3.5-d837d905825-20210919T084703Z-avarab@gmail.com (mailing list archive)
State Superseded
Headers show
Series repo-settings.c: refactor for clarity, get rid of hacks etc. | expand

Commit Message

Ævar Arnfjörð Bjarmason Sept. 19, 2021, 8:47 a.m. UTC
Change tweak_untracked_cache() in "read-cache.c" to use a switch() to
have the compiler assert that we checked all possible values in the
"enum untracked_cache_setting" type, and likewise remove the "default"
case in fetch_negotiator_init() in favor of checking for
"FETCH_NEGOTIATION_UNSET" and "FETCH_NEGOTIATION_NONE".

As will be discussed in a subsequent we'll only ever have either of
these set to FETCH_NEGOTIATION_NONE, FETCH_NEGOTIATION_UNSET and
UNTRACKED_CACHE_UNSET within the prepare_repo_settings() function
itself. In preparation for fixing that code let's add a BUG() here to
mark this as unreachable code.

See ad0fb659993 (repo-settings: parse core.untrackedCache, 2019-08-13)
for when the "unset" and "keep" handling for core.untrackedCache was
consolidated, and aaf633c2ad1 (repo-settings: create
feature.experimental setting, 2019-08-13) for the addition of the
"default" pattern in "fetch-negotiator.c".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 fetch-negotiator.c |  4 +++-
 read-cache.c       | 15 ++++++++++-----
 2 files changed, 13 insertions(+), 6 deletions(-)

Comments

Taylor Blau Sept. 20, 2021, 10:14 p.m. UTC | #1
On Sun, Sep 19, 2021 at 10:47:17AM +0200, Ævar Arnfjörð Bjarmason wrote:
> Change tweak_untracked_cache() in "read-cache.c" to use a switch() to
> have the compiler assert that we checked all possible values in the
> "enum untracked_cache_setting" type, and likewise remove the "default"
> case in fetch_negotiator_init() in favor of checking for
> "FETCH_NEGOTIATION_UNSET" and "FETCH_NEGOTIATION_NONE".
>
> As will be discussed in a subsequent we'll only ever have either of

s/subsequent/& patch/ ?

> these set to FETCH_NEGOTIATION_NONE, FETCH_NEGOTIATION_UNSET and
> UNTRACKED_CACHE_UNSET within the prepare_repo_settings() function
> itself. In preparation for fixing that code let's add a BUG() here to
> mark this as unreachable code.
>
> See ad0fb659993 (repo-settings: parse core.untrackedCache, 2019-08-13)
> for when the "unset" and "keep" handling for core.untrackedCache was
> consolidated, and aaf633c2ad1 (repo-settings: create
> feature.experimental setting, 2019-08-13) for the addition of the
> "default" pattern in "fetch-negotiator.c".
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  fetch-negotiator.c |  4 +++-
>  read-cache.c       | 15 ++++++++++-----
>  2 files changed, 13 insertions(+), 6 deletions(-)
>
> diff --git a/fetch-negotiator.c b/fetch-negotiator.c
> index 57ed5784e14..237f92b8696 100644
> --- a/fetch-negotiator.c
> +++ b/fetch-negotiator.c
> @@ -19,8 +19,10 @@ void fetch_negotiator_init(struct repository *r,
>  		return;
>
>  	case FETCH_NEGOTIATION_DEFAULT:
> -	default:
>  		default_negotiator_init(negotiator);
>  		return;
> +	case FETCH_NEGOTIATION_NONE:
> +	case FETCH_NEGOTIATION_UNSET:
> +		BUG("FETCH_NEGOTIATION_UNSET only in prepare_repo_settings()");

I was briefly confused why this BUG message mentioned
FETCH_NEGOTIATION_UNSET, since we only support FETCH_NEGOTIATION_DEFAULT
here.

But then I realized that it said "only in prepare_repo_settings()", and
we're in fetch_negotiator_init(). So this makes sense to me.

Other than the small typo in the patch message, this looks good to me.

Thanks,
Taylor
Ævar Arnfjörð Bjarmason Sept. 20, 2021, 11:33 p.m. UTC | #2
On Mon, Sep 20 2021, Taylor Blau wrote:

> On Sun, Sep 19, 2021 at 10:47:17AM +0200, Ævar Arnfjörð Bjarmason wrote:
>> Change tweak_untracked_cache() in "read-cache.c" to use a switch() to
>> have the compiler assert that we checked all possible values in the
>> "enum untracked_cache_setting" type, and likewise remove the "default"
>> case in fetch_negotiator_init() in favor of checking for
>> "FETCH_NEGOTIATION_UNSET" and "FETCH_NEGOTIATION_NONE".
>>
>> As will be discussed in a subsequent we'll only ever have either of
>
> s/subsequent/& patch/ ?

Thanks.

>> these set to FETCH_NEGOTIATION_NONE, FETCH_NEGOTIATION_UNSET and
>> UNTRACKED_CACHE_UNSET within the prepare_repo_settings() function
>> itself. In preparation for fixing that code let's add a BUG() here to
>> mark this as unreachable code.
>>
>> See ad0fb659993 (repo-settings: parse core.untrackedCache, 2019-08-13)
>> for when the "unset" and "keep" handling for core.untrackedCache was
>> consolidated, and aaf633c2ad1 (repo-settings: create
>> feature.experimental setting, 2019-08-13) for the addition of the
>> "default" pattern in "fetch-negotiator.c".
>>
>> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
>> ---
>>  fetch-negotiator.c |  4 +++-
>>  read-cache.c       | 15 ++++++++++-----
>>  2 files changed, 13 insertions(+), 6 deletions(-)
>>
>> diff --git a/fetch-negotiator.c b/fetch-negotiator.c
>> index 57ed5784e14..237f92b8696 100644
>> --- a/fetch-negotiator.c
>> +++ b/fetch-negotiator.c
>> @@ -19,8 +19,10 @@ void fetch_negotiator_init(struct repository *r,
>>  		return;
>>
>>  	case FETCH_NEGOTIATION_DEFAULT:
>> -	default:
>>  		default_negotiator_init(negotiator);
>>  		return;
>> +	case FETCH_NEGOTIATION_NONE:
>> +	case FETCH_NEGOTIATION_UNSET:
>> +		BUG("FETCH_NEGOTIATION_UNSET only in prepare_repo_settings()");
>
> I was briefly confused why this BUG message mentioned
> FETCH_NEGOTIATION_UNSET, since we only support FETCH_NEGOTIATION_DEFAULT
> here.
>
> But then I realized that it said "only in prepare_repo_settings()", and
> we're in fetch_negotiator_init(). So this makes sense to me.
>
> Other than the small typo in the patch message, this looks good to me.

I guess I'll also mention NONE here, so:

    BUG("FETCH_NEGOTIATION_{NONE,UNSET} only in prepare_repo_settings()");

Or elaborate a bit:

    BUG("FETCH_NEGOTIATION_{NONE,UNSET} used outside of prepare_repo_settings()!");

In any case this lives for just one commit, and is just here to
demonstrate the transition we're in.
diff mbox series

Patch

diff --git a/fetch-negotiator.c b/fetch-negotiator.c
index 57ed5784e14..237f92b8696 100644
--- a/fetch-negotiator.c
+++ b/fetch-negotiator.c
@@ -19,8 +19,10 @@  void fetch_negotiator_init(struct repository *r,
 		return;
 
 	case FETCH_NEGOTIATION_DEFAULT:
-	default:
 		default_negotiator_init(negotiator);
 		return;
+	case FETCH_NEGOTIATION_NONE:
+	case FETCH_NEGOTIATION_UNSET:
+		BUG("FETCH_NEGOTIATION_UNSET only in prepare_repo_settings()");
 	}
 }
diff --git a/read-cache.c b/read-cache.c
index 9048ef9e905..6918dc3d8b2 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -1944,13 +1944,18 @@  static void tweak_untracked_cache(struct index_state *istate)
 
 	prepare_repo_settings(r);
 
-	if (r->settings.core_untracked_cache  == UNTRACKED_CACHE_REMOVE) {
+	switch (r->settings.core_untracked_cache) {
+	case UNTRACKED_CACHE_REMOVE:
 		remove_untracked_cache(istate);
-		return;
-	}
-
-	if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
+		break;
+	case UNTRACKED_CACHE_WRITE:
 		add_untracked_cache(istate);
+		break;
+	case UNTRACKED_CACHE_KEEP:
+		break;
+	case UNTRACKED_CACHE_UNSET:
+		BUG("UNTRACKED_CACHE_UNSET only in prepare_repo_settings()");
+	}
 }
 
 static void tweak_split_index(struct index_state *istate)