summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2022-12-03 14:32:32 +0000
committerEdward Thomson <ethomson@edwardthomson.com>2023-02-12 21:26:11 +0000
commit366973f3785b7a2cf95a118fc4e13210ca277529 (patch)
tree2dc60dabb3b542bde76ee6d5eca8b8eeddd2ff96
parent3dfc12d5e210a6e15c7c54f3514eeeb12fb2108a (diff)
downloadlibgit2-366973f3785b7a2cf95a118fc4e13210ca277529.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.c24
-rw-r--r--tests/libgit2/repo/init.c33
2 files changed, 42 insertions, 15 deletions
diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c
index 9c55aaca5..043cfd1fd 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)
@@ -1539,6 +1540,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;
@@ -1546,10 +1548,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;
}
@@ -1963,12 +1970,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)
@@ -1979,7 +1987,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..637d4a903 100644
--- a/tests/libgit2/repo/init.c
+++ b/tests/libgit2/repo/init.c
@@ -142,27 +142,46 @@ 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;
+ cl_set_cleanup(&cleanup_repository, "reinit.git");
+
/* Initialize the repository */
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_config_free(config);
+ 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)
+{
+ cl_set_cleanup(&cleanup_repository, "reinit.git");
+
+ /* 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");
}