git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/2] sparse-index.c: minor prepare_repo_settings() fixes
@ 2021-05-05 12:11 Ævar Arnfjörð Bjarmason
  2021-05-05 12:11 ` [PATCH 1/2] sparse-index.c: remove set_index_sparse_config() Ævar Arnfjörð Bjarmason
  2021-05-05 12:11 ` [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 8+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-05-05 12:11 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Derrick Stolee, Ævar Arnfjörð Bjarmason

A couple of minor fixes for the already-landed recent sparse-index.c
work. The sparse setup code is (re-)setting variables it's already set
for no reason, and re-doing work it doesn't need to.

Ævar Arnfjörð Bjarmason (2):
  sparse-index.c: remove set_index_sparse_config()
  sparse-index.c: don't call prepare_repo_settings() twice in a row

 sparse-index.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)

-- 
2.31.1.838.g7ac6e98bb53


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] sparse-index.c: remove set_index_sparse_config()
  2021-05-05 12:11 [PATCH 0/2] sparse-index.c: minor prepare_repo_settings() fixes Ævar Arnfjörð Bjarmason
@ 2021-05-05 12:11 ` Ævar Arnfjörð Bjarmason
  2021-05-05 14:01   ` Derrick Stolee
  2021-05-05 12:11 ` [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row Ævar Arnfjörð Bjarmason
  1 sibling, 1 reply; 8+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-05-05 12:11 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Derrick Stolee, Ævar Arnfjörð Bjarmason

Remove the set_index_sparse_config() function by folding it into
set_sparse_index_config(), which was its only user.

Since 122ba1f7b52 (sparse-checkout: toggle sparse index from builtin,
2021-03-30) the flow of this code hasn't made much sense, we'd get
"enabled" in set_sparse_index_config(), proceed to call
set_index_sparse_config() with it.

There we'd call prepare_repo_settings() and set
"repo->settings.sparse_index = 1", only to needlessly call
prepare_repo_settings() again in set_sparse_index_config() (where it
would early abort), and finally setting "repo->settings.sparse_index =
enabled".

Instead we can just call prepare_repo_settings() once, and set the
variable to "enabled" in the first place.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 sparse-index.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)

diff --git a/sparse-index.c b/sparse-index.c
index 6f21397e2ee..b0d5dc5f081 100644
--- a/sparse-index.c
+++ b/sparse-index.c
@@ -102,7 +102,7 @@ static int convert_to_sparse_rec(struct index_state *istate,
 	return num_converted - start_converted;
 }
 
-static int set_index_sparse_config(struct repository *repo, int enable)
+int set_sparse_index_config(struct repository *repo, int enable)
 {
 	int res;
 	char *config_path = repo_git_path(repo, "config.worktree");
@@ -111,15 +111,6 @@ static int set_index_sparse_config(struct repository *repo, int enable)
 					    enable ? "true" : NULL);
 	free(config_path);
 
-	prepare_repo_settings(repo);
-	repo->settings.sparse_index = 1;
-	return res;
-}
-
-int set_sparse_index_config(struct repository *repo, int enable)
-{
-	int res = set_index_sparse_config(repo, enable);
-
 	prepare_repo_settings(repo);
 	repo->settings.sparse_index = enable;
 	return res;
-- 
2.31.1.838.g7ac6e98bb53


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row
  2021-05-05 12:11 [PATCH 0/2] sparse-index.c: minor prepare_repo_settings() fixes Ævar Arnfjörð Bjarmason
  2021-05-05 12:11 ` [PATCH 1/2] sparse-index.c: remove set_index_sparse_config() Ævar Arnfjörð Bjarmason
@ 2021-05-05 12:11 ` Ævar Arnfjörð Bjarmason
  2021-05-05 14:00   ` Derrick Stolee
  1 sibling, 1 reply; 8+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-05-05 12:11 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Derrick Stolee, Ævar Arnfjörð Bjarmason

Change code added in 58300f47432 (sparse-index: add index.sparse
config option, 2021-03-30) to only call prepare_repo_settings()
once. We know that our own set_sparse_index_config() has just finished
calling it, so we don't need to call it if we're acting on "test_env".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 sparse-index.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sparse-index.c b/sparse-index.c
index b0d5dc5f081..5bad05de645 100644
--- a/sparse-index.c
+++ b/sparse-index.c
@@ -133,11 +133,12 @@ int convert_to_sparse(struct index_state *istate)
 	test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
 	if (test_env >= 0)
 		set_sparse_index_config(istate->repo, test_env);
+	else
+		prepare_repo_settings(istate->repo);
 
 	/*
 	 * Only convert to sparse if index.sparse is set.
 	 */
-	prepare_repo_settings(istate->repo);
 	if (!istate->repo->settings.sparse_index)
 		return 0;
 
-- 
2.31.1.838.g7ac6e98bb53


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row
  2021-05-05 12:11 ` [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row Ævar Arnfjörð Bjarmason
@ 2021-05-05 14:00   ` Derrick Stolee
  2021-05-06  3:55     ` Junio C Hamano
  2021-05-06  8:49     ` Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 8+ messages in thread
From: Derrick Stolee @ 2021-05-05 14:00 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason, git; +Cc: Junio C Hamano

On 5/5/2021 8:11 AM, Ævar Arnfjörð Bjarmason wrote:
> Change code added in 58300f47432 (sparse-index: add index.sparse
> config option, 2021-03-30) to only call prepare_repo_settings()
> once. We know that our own set_sparse_index_config() has just finished
> calling it, so we don't need to call it if we're acting on "test_env".

I'm not sure about the value here. prepare_repo_settings() returns
quickly if the settings have already been prepared, so a second call
is negligible in cost.

> @@ -133,11 +133,12 @@ int convert_to_sparse(struct index_state *istate)
>  	test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
>  	if (test_env >= 0)
>  		set_sparse_index_config(istate->repo, test_env);
> +	else
> +		prepare_repo_settings(istate->repo);

The change presented here to either call set_sparse_index_config()
_or_ prepare_repo_settings() seems like it knows too much about
how set_sparse_index_config() works.

Thanks,
-Stolee


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] sparse-index.c: remove set_index_sparse_config()
  2021-05-05 12:11 ` [PATCH 1/2] sparse-index.c: remove set_index_sparse_config() Ævar Arnfjörð Bjarmason
@ 2021-05-05 14:01   ` Derrick Stolee
  0 siblings, 0 replies; 8+ messages in thread
From: Derrick Stolee @ 2021-05-05 14:01 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason, git; +Cc: Junio C Hamano

On 5/5/2021 8:11 AM, Ævar Arnfjörð Bjarmason wrote:
> Remove the set_index_sparse_config() function by folding it into
> set_sparse_index_config(), which was its only user.
> 
> Since 122ba1f7b52 (sparse-checkout: toggle sparse index from builtin,
> 2021-03-30) the flow of this code hasn't made much sense, we'd get
> "enabled" in set_sparse_index_config(), proceed to call
> set_index_sparse_config() with it.
> 
> There we'd call prepare_repo_settings() and set
> "repo->settings.sparse_index = 1", only to needlessly call
> prepare_repo_settings() again in set_sparse_index_config() (where it
> would early abort), and finally setting "repo->settings.sparse_index =
> enabled".
> 
> Instead we can just call prepare_repo_settings() once, and set the
> variable to "enabled" in the first place.
> 
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  sparse-index.c | 11 +----------
>  1 file changed, 1 insertion(+), 10 deletions(-)
> 
> diff --git a/sparse-index.c b/sparse-index.c
> index 6f21397e2ee..b0d5dc5f081 100644
> --- a/sparse-index.c
> +++ b/sparse-index.c
> @@ -102,7 +102,7 @@ static int convert_to_sparse_rec(struct index_state *istate,
>  	return num_converted - start_converted;
>  }
>  
> -static int set_index_sparse_config(struct repository *repo, int enable)
> +int set_sparse_index_config(struct repository *repo, int enable)
>  {
>  	int res;
>  	char *config_path = repo_git_path(repo, "config.worktree");
> @@ -111,15 +111,6 @@ static int set_index_sparse_config(struct repository *repo, int enable)
>  					    enable ? "true" : NULL);
>  	free(config_path);
>  
> -	prepare_repo_settings(repo);
> -	repo->settings.sparse_index = 1;
> -	return res;
> -}
> -
> -int set_sparse_index_config(struct repository *repo, int enable)
> -{
> -	int res = set_index_sparse_config(repo, enable);
> -
>  	prepare_repo_settings(repo);
>  	repo->settings.sparse_index = enable;
>  	return res;
> 

This cleanup makes sense to me. The previous code was confusing and
this is an improvement.

Thanks,
-Stolee

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row
  2021-05-05 14:00   ` Derrick Stolee
@ 2021-05-06  3:55     ` Junio C Hamano
  2021-05-06  8:49     ` Ævar Arnfjörð Bjarmason
  1 sibling, 0 replies; 8+ messages in thread
From: Junio C Hamano @ 2021-05-06  3:55 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: Ævar Arnfjörð Bjarmason, git

Derrick Stolee <stolee@gmail.com> writes:

> On 5/5/2021 8:11 AM, Ævar Arnfjörð Bjarmason wrote:
>> Change code added in 58300f47432 (sparse-index: add index.sparse
>> config option, 2021-03-30) to only call prepare_repo_settings()
>> once. We know that our own set_sparse_index_config() has just finished
>> calling it, so we don't need to call it if we're acting on "test_env".
>
> I'm not sure about the value here. prepare_repo_settings() returns
> quickly if the settings have already been prepared, so a second call
> is negligible in cost.
>
>> @@ -133,11 +133,12 @@ int convert_to_sparse(struct index_state *istate)
>>  	test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
>>  	if (test_env >= 0)
>>  		set_sparse_index_config(istate->repo, test_env);
>> +	else
>> +		prepare_repo_settings(istate->repo);
>
> The change presented here to either call set_sparse_index_config()
> _or_ prepare_repo_settings() seems like it knows too much about
> how set_sparse_index_config() works.

I tend to agree on both counts.

Thanks.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row
  2021-05-05 14:00   ` Derrick Stolee
  2021-05-06  3:55     ` Junio C Hamano
@ 2021-05-06  8:49     ` Ævar Arnfjörð Bjarmason
  2021-05-17 17:31       ` Derrick Stolee
  1 sibling, 1 reply; 8+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-05-06  8:49 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: git, Junio C Hamano


On Wed, May 05 2021, Derrick Stolee wrote:

> On 5/5/2021 8:11 AM, Ævar Arnfjörð Bjarmason wrote:
>> Change code added in 58300f47432 (sparse-index: add index.sparse
>> config option, 2021-03-30) to only call prepare_repo_settings()
>> once. We know that our own set_sparse_index_config() has just finished
>> calling it, so we don't need to call it if we're acting on "test_env".
>
> I'm not sure about the value here. prepare_repo_settings() returns
> quickly if the settings have already been prepared, so a second call
> is negligible in cost.

I changed that while I was at it to make it easier to read, it's not an
optimization. I.e. one wonders what the side-effect is of calling
prepare_repo_settings() twice, discovers there isn't one...

>> @@ -133,11 +133,12 @@ int convert_to_sparse(struct index_state *istate)
>>  	test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
>>  	if (test_env >= 0)
>>  		set_sparse_index_config(istate->repo, test_env);
>> +	else
>> +		prepare_repo_settings(istate->repo);
>
> The change presented here to either call set_sparse_index_config()
> _or_ prepare_repo_settings() seems like it knows too much about
> how set_sparse_index_config() works.

It seems reasonable to assume that a function to set config has
initialized (or died, if it couldn't) enough of our config state to do
its job.

Besides, it's a few lines above the changed code in the same file. 

But looking at this again 2/3 callers of set_sparse_index_config()
aren't checking the return value. Wouldn't something like [1] on top be
a good idea here?

Also, is GIT_TEST_SPARSE_INDEX=true itself supposed to work? Running the
test suite with it fails 3 test files for me, all /sparse/, i.e. tests
that (presumably) assume it's not already turned on by this code.

1. 

diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index a4bdd7c4940..bea1e7dd00e 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -280,8 +280,9 @@ static int set_config(enum sparse_checkout_mode mode)
 				      "core.sparseCheckoutCone",
 				      mode == MODE_CONE_PATTERNS ? "true" : NULL);
 
-	if (mode == MODE_NO_PATTERNS)
-		set_sparse_index_config(the_repository, 0);
+	if (mode == MODE_NO_PATTERNS &&
+	    set_sparse_index_config(the_repository, 0) < 0)
+		die(_("could not set index.sparse=0"));
 
 	return 0;
 }
diff --git a/sparse-index.c b/sparse-index.c
index 5bad05de645..3938bcec962 100644
--- a/sparse-index.c
+++ b/sparse-index.c
@@ -131,10 +131,13 @@ int convert_to_sparse(struct index_state *istate)
 	 * index.sparse config variable to be on.
 	 */
 	test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
-	if (test_env >= 0)
-		set_sparse_index_config(istate->repo, test_env);
-	else
+	if (test_env >= 0) {
+		if (set_sparse_index_config(istate->repo, test_env) < 0)
+			die(_("could not set index.sparse based on GIT_TEST_SPARSE_INDEX=%d"),
+			    test_env);
+	} else {
 		prepare_repo_settings(istate->repo);
+	}
 
 	/*
 	 * Only convert to sparse if index.sparse is set.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row
  2021-05-06  8:49     ` Ævar Arnfjörð Bjarmason
@ 2021-05-17 17:31       ` Derrick Stolee
  0 siblings, 0 replies; 8+ messages in thread
From: Derrick Stolee @ 2021-05-17 17:31 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason; +Cc: git, Junio C Hamano

On 5/6/2021 4:49 AM, Ævar Arnfjörð Bjarmason wrote:
> 
> On Wed, May 05 2021, Derrick Stolee wrote:
> 
>> On 5/5/2021 8:11 AM, Ævar Arnfjörð Bjarmason wrote:
>>> Change code added in 58300f47432 (sparse-index: add index.sparse
>>> config option, 2021-03-30) to only call prepare_repo_settings()
>>> once. We know that our own set_sparse_index_config() has just finished
>>> calling it, so we don't need to call it if we're acting on "test_env".
>>
>> I'm not sure about the value here. prepare_repo_settings() returns
>> quickly if the settings have already been prepared, so a second call
>> is negligible in cost.
> 
> I changed that while I was at it to make it easier to read, it's not an
> optimization. I.e. one wonders what the side-effect is of calling
> prepare_repo_settings() twice, discovers there isn't one...

This is typical of the "prepare_" pattern, such as prepare_packed_git()
or prepare_commit_graph(). It's saying "be sure this is initialized"
and that initialization only needs to happen once.
 
>>> @@ -133,11 +133,12 @@ int convert_to_sparse(struct index_state *istate)
>>>  	test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
>>>  	if (test_env >= 0)
>>>  		set_sparse_index_config(istate->repo, test_env);
>>> +	else
>>> +		prepare_repo_settings(istate->repo);
>>
>> The change presented here to either call set_sparse_index_config()
>> _or_ prepare_repo_settings() seems like it knows too much about
>> how set_sparse_index_config() works.
> 
> It seems reasonable to assume that a function to set config has
> initialized (or died, if it couldn't) enough of our config state to do
> its job.
> 
> Besides, it's a few lines above the changed code in the same file. 
> 
> But looking at this again 2/3 callers of set_sparse_index_config()
> aren't checking the return value. Wouldn't something like [1] on top be
> a good idea here?
> 
> Also, is GIT_TEST_SPARSE_INDEX=true itself supposed to work? Running the
> test suite with it fails 3 test files for me, all /sparse/, i.e. tests
> that (presumably) assume it's not already turned on by this code.

I haven't ensured that this works in all cases. I was going to try and
find a way to make the tests be better about having a meaningful data
shape that applies to the sparse-index, but I should go and verify that
the tests work with this enabled.

> 
> 1. 
> 
> diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
> index a4bdd7c4940..bea1e7dd00e 100644
> --- a/builtin/sparse-checkout.c
> +++ b/builtin/sparse-checkout.c
> @@ -280,8 +280,9 @@ static int set_config(enum sparse_checkout_mode mode)
>  				      "core.sparseCheckoutCone",
>  				      mode == MODE_CONE_PATTERNS ? "true" : NULL);
>  
> -	if (mode == MODE_NO_PATTERNS)
> -		set_sparse_index_config(the_repository, 0);
> +	if (mode == MODE_NO_PATTERNS &&
> +	    set_sparse_index_config(the_repository, 0) < 0)
> +		die(_("could not set index.sparse=0"));

This seems reasonable, because we are in a builtin saying "please disable
the sparse-index". Is there not an instance here that _enables_ it?

I would change the string to be "could not disable index.sparse" or
something similar.

>  	return 0;
>  }
> diff --git a/sparse-index.c b/sparse-index.c
> index 5bad05de645..3938bcec962 100644
> --- a/sparse-index.c
> +++ b/sparse-index.c
> @@ -131,10 +131,13 @@ int convert_to_sparse(struct index_state *istate)
>  	 * index.sparse config variable to be on.
>  	 */
>  	test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
> -	if (test_env >= 0)
> -		set_sparse_index_config(istate->repo, test_env);
> -	else
> +	if (test_env >= 0) {
> +		if (set_sparse_index_config(istate->repo, test_env) < 0)
> +			die(_("could not set index.sparse based on GIT_TEST_SPARSE_INDEX=%d"),
> +			    test_env);
> +	} else {
>  		prepare_repo_settings(istate->repo);
> +	}

This one, I'm not so sure. There might be reasons why the GIT_TEST_*
variable won't work to set the config and we don't want that to be
a reason the test fails. There is no need for a translated string
here, either.

Thanks,
-Stolee

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-05-17 17:31 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-05 12:11 [PATCH 0/2] sparse-index.c: minor prepare_repo_settings() fixes Ævar Arnfjörð Bjarmason
2021-05-05 12:11 ` [PATCH 1/2] sparse-index.c: remove set_index_sparse_config() Ævar Arnfjörð Bjarmason
2021-05-05 14:01   ` Derrick Stolee
2021-05-05 12:11 ` [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row Ævar Arnfjörð Bjarmason
2021-05-05 14:00   ` Derrick Stolee
2021-05-06  3:55     ` Junio C Hamano
2021-05-06  8:49     ` Ævar Arnfjörð Bjarmason
2021-05-17 17:31       ` Derrick Stolee

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).