Skip to content

Commit 9bf2314

Browse files
calebdwttaylorr
authored andcommitted
setup: correctly reinitialize repository version
When reinitializing a repository, Git does not account for extensions other than `objectformat` and `refstorage` when determining the repository version. This can lead to a repository being downgraded to version 0 if extensions are set, causing Git future operations to fail. This patch teaches Git to check if other extensions are defined in the config to ensure that the repository version is set correctly. Signed-off-by: Caleb White <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent ca27c05 commit 9bf2314

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

setup.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,8 +2204,8 @@ void initialize_repository_version(int hash_algo,
22042204
enum ref_storage_format ref_storage_format,
22052205
int reinit)
22062206
{
2207-
char repo_version_string[10];
2208-
int repo_version = GIT_REPO_VERSION;
2207+
struct strbuf repo_version = STRBUF_INIT;
2208+
int target_version = GIT_REPO_VERSION;
22092209

22102210
/*
22112211
* Note that we initialize the repository version to 1 when the ref
@@ -2216,12 +2216,7 @@ void initialize_repository_version(int hash_algo,
22162216
*/
22172217
if (hash_algo != GIT_HASH_SHA1 ||
22182218
ref_storage_format != REF_STORAGE_FORMAT_FILES)
2219-
repo_version = GIT_REPO_VERSION_READ;
2220-
2221-
/* This forces creation of new config file */
2222-
xsnprintf(repo_version_string, sizeof(repo_version_string),
2223-
"%d", repo_version);
2224-
git_config_set("core.repositoryformatversion", repo_version_string);
2219+
target_version = GIT_REPO_VERSION_READ;
22252220

22262221
if (hash_algo != GIT_HASH_SHA1 && hash_algo != GIT_HASH_UNKNOWN)
22272222
git_config_set("extensions.objectformat",
@@ -2234,6 +2229,25 @@ void initialize_repository_version(int hash_algo,
22342229
ref_storage_format_to_name(ref_storage_format));
22352230
else if (reinit)
22362231
git_config_set_gently("extensions.refstorage", NULL);
2232+
2233+
if (reinit) {
2234+
struct strbuf config = STRBUF_INIT;
2235+
struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
2236+
2237+
strbuf_git_common_path(&config, the_repository, "config");
2238+
read_repository_format(&repo_fmt, config.buf);
2239+
2240+
if (repo_fmt.v1_only_extensions.nr)
2241+
target_version = GIT_REPO_VERSION_READ;
2242+
2243+
strbuf_release(&config);
2244+
clear_repository_format(&repo_fmt);
2245+
}
2246+
2247+
strbuf_addf(&repo_version, "%d", target_version);
2248+
git_config_set("core.repositoryformatversion", repo_version.buf);
2249+
2250+
strbuf_release(&repo_version);
22372251
}
22382252

22392253
static int is_reinit(void)
@@ -2333,7 +2347,7 @@ static int create_default_files(const char *template_path,
23332347
adjust_shared_perm(repo_get_git_dir(the_repository));
23342348
}
23352349

2336-
initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, 0);
2350+
initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);
23372351

23382352
/* Check filemode trustability */
23392353
path = git_path_buf(&buf, "config");

0 commit comments

Comments
 (0)