Skip to content

Commit 39e15b7

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7689f6c commit 39e15b7

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

setup.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,14 +2284,17 @@ static void separate_git_dir(const char *git_dir, const char *git_link)
22842284
write_file(git_link, "gitdir: %s", git_dir);
22852285
}
22862286

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)
22882289
{
2289-
const char *env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
2290+
const char *env;
2291+
22902292
/*
22912293
* If we already have an initialized repo, don't allow the user to
22922294
* specify a different algorithm, as that could cause corruption.
22932295
* Otherwise, if the user has specified one on the command line, use it.
22942296
*/
2297+
env = getenv(GIT_DEFAULT_HASH_ENVIRONMENT);
22952298
if (repo_fmt->version >= 0 && hash != GIT_HASH_UNKNOWN && hash != repo_fmt->hash_algo)
22962299
die(_("attempt to reinitialize repository with different hash"));
22972300
else if (hash != GIT_HASH_UNKNOWN)
@@ -2302,25 +2305,22 @@ static void validate_hash_algorithm(struct repository_format *repo_fmt, int hash
23022305
die(_("unknown hash algorithm '%s'"), env);
23032306
repo_fmt->hash_algo = env_algo;
23042307
}
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);
23112309

2310+
env = getenv("GIT_DEFAULT_REF_FORMAT");
23122311
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) {
23152314
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;
23232322
}
2323+
repo_set_ref_storage_format(the_repository, repo_fmt->ref_storage_format);
23242324
}
23252325

23262326
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,
23532353
}
23542354
startup_info->have_repository = 1;
23552355

2356-
/* Check to see if the repository version is right.
2356+
/*
2357+
* Check to see if the repository version is right.
23572358
* Note that a newly created repository does not have
23582359
* config file, so this will not fail. What we are catching
23592360
* is an attempt to reinitialize new repository with an old tool.
23602361
*/
23612362
check_repository_format(&repo_fmt);
23622363

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);
23722365

23732366
/*
23742367
* Ensure `core.hidedotfiles` is processed. This must happen after we

0 commit comments

Comments
 (0)