Skip to content

Commit d897f2c

Browse files
calebdwgitster
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: Junio C Hamano <[email protected]>
1 parent 090d24e commit d897f2c

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
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");

t/t5504-fetch-receive-strict.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ test_expect_success 'fsck with invalid or bogus skipList input' '
171171
test_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&
172172
test_grep "could not open.*: does-not-exist" err &&
173173
test_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&
174-
test_grep "invalid object name: \[core\]" err
174+
test_grep "invalid object name: " err
175175
'
176176

177177
test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '
@@ -234,7 +234,7 @@ test_expect_success 'push with receive.fsck.skipList' '
234234
test_grep "could not open.*: does-not-exist" err &&
235235
git --git-dir=dst/.git config receive.fsck.skipList config &&
236236
test_must_fail git push --porcelain dst bogus 2>err &&
237-
test_grep "invalid object name: \[core\]" err &&
237+
test_grep "invalid object name: " err &&
238238
239239
git --git-dir=dst/.git config receive.fsck.skipList SKIP &&
240240
git push --porcelain dst bogus
@@ -263,7 +263,7 @@ test_expect_success 'fetch with fetch.fsck.skipList' '
263263
test_grep "could not open.*: does-not-exist" err &&
264264
git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&
265265
test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec 2>err &&
266-
test_grep "invalid object name: \[core\]" err &&
266+
test_grep "invalid object name: " err &&
267267
268268
git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&
269269
git --git-dir=dst/.git fetch "file://$(pwd)" $refspec

0 commit comments

Comments
 (0)