setup: merge configuration of repository formats

The configuration of repository formats is split up across two functions `validate_hash_algorithm()` and `validate_ref_storage_format()`. This is fine as-is, but we are about to extend the logic to also read default values from the config. With the logic split across two functions, we would either have to pass in additional parameters read from the config, or read the config multiple times. Both of these options feel a bit unwieldy. Merge the code into a new function `repository_format_configure()` that is responsible for configuring the whole repository's format. Like this, we can easily read the config in a single place, only. Furthermore, move the calls to `repo_set_ref_storage_format()` and `repo_set_hash_algo()` into this new function as well, such that all the logic to configure the repository format is self-contained here. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 16, 2024 at 10:56 UTC 39e15b789ad3c3a192f4c8abde3daff83a053aaa
1 file changed +20 -27
setup.c
+20 -27
@@ -2284,14 +2284,17 @@ static void separate_git_dir(const char *git_dir, const char *git_link)
2284 write_file(git_link, "gitdir: %s", git_dir);
2285 }
2286
2287 -static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash)
2287 +static void repository_format_configure(struct repository_format *repo_fmt,
2288 + int hash, enum ref_storage_format ref_format)
2289 {
2289 - const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
2290 + const char *env;
2291 +
2292 /*
2293 * If we already have an initialized repo, don't allow the user to
2294 * specify a different algorithm, as that could cause corruption.
2295 * Otherwise, if the user has specified one on the command line, use it.
2296 */
2297 + env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
2298 if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
2299 die(_("attempt to reinitialize repository with different hash"));
2300 else if (hash != GIT_HASH_UNKNOWN)
@@ -2302,25 +2305,22 @@ static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash
2305 die(_("unknown hash algorithm '%s'"), env);
2306 repo_fmt->hash_algo = env_algo;
2307 }
2305 -}
2306 -
2307 -static void validate_ref_storage_format(struct repository_format *repo_fmt,
2308 - enum ref_storage_format format)
2309 -{
2310 - const char *name = getenv("GIT_DEFAULT_REF_FORMAT");
2308 + repo_set_hash_algo(the_repository, repo_fmt->hash_algo);
2309
2310 + env = getenv("GIT_DEFAULT_REF_FORMAT");
2311 if (repo_fmt->version >= 0 &&
2313 - format != REF_STORAGE_FORMAT_UNKNOWN &&
2314 - format != repo_fmt->ref_storage_format) {
2312 + ref_format != REF_STORAGE_FORMAT_UNKNOWN &&
2313 + ref_format != repo_fmt->ref_storage_format) {
2314 die(_("attempt to reinitialize repository with different reference storage format"));
2316 - } else if (format != REF_STORAGE_FORMAT_UNKNOWN) {
2317 - repo_fmt->ref_storage_format = format;
2318 - } else if (name) {
2319 - format = ref_storage_format_by_name(name);
2320 - if (format == REF_STORAGE_FORMAT_UNKNOWN)
2321 - die(_("unknown ref storage format '%s'"), name);
2322 - repo_fmt->ref_storage_format = format;
2315 + } else if (ref_format != REF_STORAGE_FORMAT_UNKNOWN) {
2316 + repo_fmt->ref_storage_format = ref_format;
2317 + } else if (env) {
2318 + ref_format = ref_storage_format_by_name(env);
2319 + if (ref_format == REF_STORAGE_FORMAT_UNKNOWN)
2320 + die(_("unknown ref storage format '%s'"), env);
2321 + repo_fmt->ref_storage_format = ref_format;
2322 }
2323 + repo_set_ref_storage_format(the_repository, repo_fmt->ref_storage_format);
2324 }
2325
2326 int init_db(const char *git_dir, const char *real_git_dir,
@@ -2353,22 +2353,15 @@ int init_db(const char *git_dir, const char *real_git_dir,
2353 }
2354 startup_info->have_repository = 1;
2355
2356 - /* Check to see if the repository version is right.
2356 + /*
2357 + * Check to see if the repository version is right.
2358 * Note that a newly created repository does not have
2359 * config file, so this will not fail. What we are catching
2360 * is an attempt to reinitialize new repository with an old tool.
2361 */
2362 check_repository_format(&repo_fmt);
2363
2363 - validate_hash_algorithm(&repo_fmt, hash);
2364 - validate_ref_storage_format(&repo_fmt, ref_storage_format);
2365 -
2366 - /*
2367 - * Now that we have set up both the hash algorithm and the ref storage
2368 - * format we can update the repository's settings accordingly.
2369 - */
2370 - repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
2371 - repo_set_ref_storage_format(the_repository, repo_fmt.ref_storage_format);
2364 + repository_format_configure(&repo_fmt, hash, ref_storage_format);
2365
2366 /*
2367 * Ensure `core.hidedotfiles` is processed. This must happen after we