diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2022-12-03 14:32:32 +0000 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2022-12-03 20:37:15 +0000 |
| commit | 3b676c8d889ec4554fc0b66362a6a37a5e04e529 (patch) | |
| tree | 4fd5425cc0a8ea0290fc9506982c409fa75966d9 | |
| parent | 2770d131bc3b86d234758b9623e186df5f66c4d1 (diff) | |
| download | libgit2-3b676c8d889ec4554fc0b66362a6a37a5e04e529.tar.gz | |
repo: don't overwrite repo format version on reinit
Ensure that we maintain the `core.repositoryFormatVersion` value instead
of always overwriting it with the default.
| -rw-r--r-- | src/libgit2/repository.c | 24 | ||||
| -rw-r--r-- | tests/libgit2/repo/init.c | 28 |
2 files changed, 37 insertions, 15 deletions
diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index 8784122b9..7bcd6b9ec 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -76,8 +76,8 @@ static int load_objectformat(git_repository *repo, git_config *config); #define GIT_BRANCH_DEFAULT "master" -#define GIT_REPO_VERSION 0 -#define GIT_REPO_MAX_VERSION 1 +#define GIT_REPO_VERSION_DEFAULT 0 +#define GIT_REPO_VERSION_MAX 1 git_str git_repository__reserved_names_win32[] = { { DOT_GIT, 0, CONST_STRLEN(DOT_GIT) }, @@ -1016,7 +1016,8 @@ int git_repository_open_ext( if (error < 0 && error != GIT_ENOTFOUND) goto cleanup; - if (config && (error = check_repositoryformatversion(&version, config)) < 0) + if (config && + (error = check_repositoryformatversion(&version, config)) < 0) goto cleanup; if ((error = check_extensions(config, version)) < 0) @@ -1535,6 +1536,7 @@ static int check_repositoryformatversion(int *version, git_config *config) int error; error = git_config_get_int32(version, config, "core.repositoryformatversion"); + /* git ignores this if the config variable isn't there */ if (error == GIT_ENOTFOUND) return 0; @@ -1542,10 +1544,15 @@ static int check_repositoryformatversion(int *version, git_config *config) if (error < 0) return -1; - if (GIT_REPO_MAX_VERSION < *version) { + if (*version < 0) { + git_error_set(GIT_ERROR_REPOSITORY, + "invalid repository version %d", *version); + } + + if (GIT_REPO_VERSION_MAX < *version) { git_error_set(GIT_ERROR_REPOSITORY, "unsupported repository version %d; only versions up to %d are supported", - *version, GIT_REPO_MAX_VERSION); + *version, GIT_REPO_VERSION_MAX); return -1; } @@ -1959,12 +1966,13 @@ static int repo_init_config( git_config *config = NULL; bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0); bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0); - int version = 0; + int version = GIT_REPO_VERSION_DEFAULT; if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0) goto cleanup; - if (is_reinit && (error = check_repositoryformatversion(&version, config)) < 0) + if (is_reinit && + (error = check_repositoryformatversion(&version, config)) < 0) goto cleanup; if ((error = check_extensions(config, version)) < 0) @@ -1975,7 +1983,7 @@ static int repo_init_config( goto cleanup; } while (0) SET_REPO_CONFIG(bool, "core.bare", is_bare); - SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION); + SET_REPO_CONFIG(int32, "core.repositoryformatversion", version); if ((error = repo_init_fs_configs( config, cfg_path.ptr, repo_dir, work_dir, !is_reinit)) < 0) diff --git a/tests/libgit2/repo/init.c b/tests/libgit2/repo/init.c index adcd9e025..d07a03c60 100644 --- a/tests/libgit2/repo/init.c +++ b/tests/libgit2/repo/init.c @@ -142,7 +142,7 @@ void test_repo_init__reinit_bare_repo(void) cl_git_pass(git_repository_init(&g_repo, "reinit.git", 1)); } -void test_repo_init__reinit_too_recent_bare_repo(void) +void test_repo_init__reinit_nondefault_version(void) { git_config *config; @@ -150,19 +150,33 @@ void test_repo_init__reinit_too_recent_bare_repo(void) cl_git_pass(git_repository_init(&g_repo, "reinit.git", 1)); git_repository_config(&config, g_repo); + /* Set the config to a supported but not default version */ + cl_repo_set_string(g_repo, "core.repositoryformatversion", "1"); + git_repository_free(g_repo); + g_repo = NULL; + + /* Try to reinitialize the repository */ + cl_git_pass(git_repository_init(&g_repo, "reinit.git", 1)); + cl_assert_equal_i(1, cl_repo_get_int(g_repo, "core.repositoryformatversion")); + + cl_fixture_cleanup("reinit.git"); +} + +void test_repo_init__reinit_unsupported_version(void) +{ + /* Initialize the repository */ + cl_git_pass(git_repository_init(&g_repo, "reinit.git", 1)); + /* * Hack the config of the repository to make it look like it has - * been created by a recenter version of git/libgit2 + * been created by a too new and unsupported version of git/libgit2 */ - cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 42)); - - git_config_free(config); + cl_repo_set_string(g_repo, "core.repositoryformatversion", "42"); git_repository_free(g_repo); g_repo = NULL; - /* Try to reinitialize the repository */ + /* Try and fail to reinitialize the repository */ cl_git_fail(git_repository_init(&g_repo, "reinit.git", 1)); - cl_fixture_cleanup("reinit.git"); } |
