repository: add minimal reinitialization of repository This currently only ensures that the version of the repository format isn't greater than zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
diff --git a/src/repository.c b/src/repository.c
index 97d70c4..74f0d8f 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -24,6 +24,8 @@
#define GIT_BRANCH_MASTER "master"
+#define GIT_CONFIG_CORE_REPOSITORYFORMATVERSION "core.repositoryformatversion"
+#define GIT_REPOSITORYFORMATVERSION 0
static void drop_odb(git_repository *repo)
{
@@ -628,12 +630,46 @@ cleanup:
return error;
}
-static int repo_init_reinit(const char *repository_path, int is_bare)
+static int check_repositoryformatversion(git_repository *repo)
{
- /* TODO: reinit the repository */
- return git__throw(GIT_ENOTIMPLEMENTED,
- "Failed to reinitialize the %srepository at '%s'. "
- "This feature is not yet implemented",
+ git_config *config;
+ int version, error = GIT_SUCCESS;
+
+ if ((error = git_repository_config(&config, repo)) < GIT_SUCCESS)
+ return git__throw(error, "Failed to open config file.");
+
+ error = git_config_get_int32(config, GIT_CONFIG_CORE_REPOSITORYFORMATVERSION, &version);
+
+ if (GIT_REPOSITORYFORMATVERSION < version)
+ error = git__throw(GIT_ERROR, "Unsupported git repository version (Expected version <= %d, found %d).", GIT_REPOSITORYFORMATVERSION, version);
+
+ git_config_free(config);
+
+ return error;
+}
+
+static int repo_init_reinit(git_repository **repo_out, const char *repository_path, int is_bare)
+{
+ int error;
+ git_repository *repo = NULL;
+
+ if ((error = git_repository_open(&repo, repository_path)) < GIT_SUCCESS)
+ goto error;
+
+ if ((error = check_repositoryformatversion(repo)) < GIT_SUCCESS)
+ goto error;
+
+ /* TODO: reinitialize the templates */
+
+ *repo_out = repo;
+
+ return GIT_SUCCESS;
+
+error:
+ git_repository_free(repo);
+
+ return git__rethrow(error,
+ "Failed to reinitialize the %srepository at '%s'. ",
is_bare ? "bare " : "", repository_path);
}
@@ -673,7 +709,7 @@ static int repo_init_config(const char *git_dir, int is_bare)
goto cleanup;
SET_REPO_CONFIG(bool, "core.bare", is_bare);
- SET_REPO_CONFIG(int32, "core.repositoryformatversion", 0);
+ SET_REPO_CONFIG(int32, GIT_CONFIG_CORE_REPOSITORYFORMATVERSION, GIT_REPOSITORYFORMATVERSION);
/* TODO: what other defaults? */
cleanup:
@@ -735,7 +771,7 @@ int git_repository_init(git_repository **repo_out, const char *path, unsigned is
if (git_path_isdir(repository_path.ptr) == GIT_SUCCESS) {
if (quickcheck_repository_dir(&repository_path) == GIT_SUCCESS) {
- error = repo_init_reinit(repository_path.ptr, is_bare);
+ error = repo_init_reinit(repo_out, repository_path.ptr, is_bare);
git_buf_free(&repository_path);
return error;
}
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index 7e0a945..a12a2c2 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -1,6 +1,7 @@
#include "clar_libgit2.h"
#include "fileops.h"
#include "repository.h"
+#include "config.h"
enum repo_mode {
STANDARD_REPOSITORY = 0,
@@ -105,3 +106,38 @@ void test_repo_init__bare_repo_escaping_current_workdir(void)
cleanup_repository("a");
}
+
+void test_repo_init__reinit_bare_repo(void)
+{
+ cl_set_cleanup(&cleanup_repository, "reinit.git");
+
+ /* Initialize the repository */
+ cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
+ git_repository_free(_repo);
+
+ /* Reinitialize the repository */
+ cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
+}
+
+void test_repo_init__reinit_too_recent_bare_repo(void)
+{
+ git_config *config;
+
+ /* Initialize the repository */
+ cl_git_pass(git_repository_init(&_repo, "reinit.git", 1));
+ git_repository_config(&config, _repo);
+
+ /*
+ * Hack the config of the repository to make it look like it has
+ * been created by a recenter version of git/libgit2
+ */
+ cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 42));
+
+ git_config_free(config);
+ git_repository_free(_repo);
+
+ /* Try to reinitialize the repository */
+ cl_git_fail(git_repository_init(&_repo, "reinit.git", 1));
+
+ cl_fixture_cleanup("reinit.git");
+}