setup: don't apply "GIT_REFERENCE_BACKEND" without a repository

When discovering a repository we eventually also apply the "GIT_REFERENCE_BACKEND" environment variable to the repository. There's two problems with that: - We do this unconditionally, which is rather pointless: we really only have to configure the repository when we have found one. - We have already applied the repository format at that point in time, so we need to manually reapply it. Move the logic around so that we only apply the environment variable when a repository was discovered. This also allows us to drop the explcit call to `repo_set_ref_storage_format()` because we now adjust the format before we apply it via `apply_repository_format()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 25, 2026 at 11:20 UTC 7d9ffe68f2fc525c676a71996550bc9a4d350d11
1 file changed +19 -20
setup.c
+19 -20
@@ -1906,7 +1906,6 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
1906 static struct strbuf cwd = STRBUF_INIT;
1907 struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
1908 const char *prefix = NULL;
1909 - const char *ref_backend_uri;
1909 struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
1910
1911 /*
@@ -2032,6 +2031,25 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2031
2032 if (startup_info->have_repository) {
2033 struct strbuf err = STRBUF_INIT;
2034 + const char *ref_backend_uri;
2035 +
2036 + /*
2037 + * The env variable should override the repository config
2038 + * for 'extensions.refStorage'.
2039 + */
2040 + ref_backend_uri = getenv(GIT_REFERENCE_BACKEND_ENVIRONMENT);
2041 + if (ref_backend_uri) {
2042 + char *format;
2043 +
2044 + free(repo_fmt.ref_storage_payload);
2045 +
2046 + parse_reference_uri(ref_backend_uri, &format, &repo_fmt.ref_storage_payload);
2047 + repo_fmt.ref_storage_format = ref_storage_format_by_name(format);
2048 + if (repo_fmt.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
2049 + die(_("unknown ref storage format: '%s'"), format);
2050 +
2051 + free(format);
2052 + }
2053
2054 if (apply_repository_format(repo, &repo_fmt,
2055 APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0)
@@ -2057,25 +2075,6 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2075 setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
2076 }
2077
2060 - /*
2061 - * The env variable should override the repository config
2062 - * for 'extensions.refStorage'.
2063 - */
2064 - ref_backend_uri = getenv(GIT_REFERENCE_BACKEND_ENVIRONMENT);
2065 - if (ref_backend_uri) {
2066 - char *backend, *payload;
2067 - enum ref_storage_format format;
2068 -
2069 - parse_reference_uri(ref_backend_uri, &backend, &payload);
2070 - format = ref_storage_format_by_name(backend);
2071 - if (format == REF_STORAGE_FORMAT_UNKNOWN)
2072 - die(_("unknown ref storage format: '%s'"), backend);
2073 - repo_set_ref_storage_format(repo, format, payload);
2074 -
2075 - free(backend);
2076 - free(payload);
2077 - }
2078 -
2078 setup_original_cwd(repo);
2079
2080 strbuf_release(&dir);