Re: [PATCH 2/2] sparse-index.c: don't call prepare_repo_settings() twice in a row
From: Derrick Stolee <hidden>
Date: 2021-05-17 17:31:40
On 5/6/2021 4:49 AM, Ævar Arnfjörð Bjarmason wrote:
On Wed, May 05 2021, Derrick Stolee wrote:quoted
On 5/5/2021 8:11 AM, Ævar Arnfjörð Bjarmason wrote:quoted
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.
quoted
quoted
@@ -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.
quoted hunk ↗ jump to hunk
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.
quoted hunk ↗ jump to hunk
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