Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8351abc

Browse files
committed
Merge pull request libgit2#3249 from libgit2/cmn/repo-version-check
Check the repository version
2 parents 0c34fa5 + 16c73d3 commit 8351abc

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/repository.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
# include "win32/w32_util.h"
3333
#endif
3434

35+
static int check_repositoryformatversion(git_config *config);
36+
3537
#define GIT_FILE_CONTENT_PREFIX "gitdir:"
3638

3739
#define GIT_BRANCH_MASTER "master"
@@ -489,6 +491,7 @@ int git_repository_open_ext(
489491
git_buf path = GIT_BUF_INIT, parent = GIT_BUF_INIT,
490492
link_path = GIT_BUF_INIT;
491493
git_repository *repo;
494+
git_config *config = NULL;
492495

493496
if (repo_ptr)
494497
*repo_ptr = NULL;
@@ -510,22 +513,36 @@ int git_repository_open_ext(
510513
GITERR_CHECK_ALLOC(repo->path_gitlink);
511514
}
512515

516+
/*
517+
* We'd like to have the config, but git doesn't particularly
518+
* care if it's not there, so we need to deal with that.
519+
*/
520+
521+
error = git_repository_config_snapshot(&config, repo);
522+
if (error < 0 && error != GIT_ENOTFOUND)
523+
goto cleanup;
524+
525+
if (config && (error = check_repositoryformatversion(config)) < 0)
526+
goto cleanup;
527+
513528
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
514529
repo->is_bare = 1;
515530
else {
516-
git_config *config = NULL;
517531

518-
if ((error = git_repository_config_snapshot(&config, repo)) < 0 ||
519-
(error = load_config_data(repo, config)) < 0 ||
520-
(error = load_workdir(repo, config, &parent)) < 0)
521-
git_repository_free(repo);
522-
523-
git_config_free(config);
532+
if (config &&
533+
((error = load_config_data(repo, config)) < 0 ||
534+
(error = load_workdir(repo, config, &parent))) < 0)
535+
goto cleanup;
524536
}
525537

526-
if (!error)
527-
*repo_ptr = repo;
538+
cleanup:
528539
git_buf_free(&parent);
540+
git_config_free(config);
541+
542+
if (error < 0)
543+
git_repository_free(repo);
544+
else
545+
*repo_ptr = repo;
529546

530547
return error;
531548
}
@@ -931,9 +948,14 @@ bool git_repository__reserved_names(
931948

932949
static int check_repositoryformatversion(git_config *config)
933950
{
934-
int version;
951+
int version, error;
935952

936-
if (git_config_get_int32(&version, config, "core.repositoryformatversion") < 0)
953+
error = git_config_get_int32(&version, config, "core.repositoryformatversion");
954+
/* git ignores this if the config variable isn't there */
955+
if (error == GIT_ENOTFOUND)
956+
return 0;
957+
958+
if (error < 0)
937959
return -1;
938960

939961
if (GIT_REPO_VERSION < version) {

tests/repo/open.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ void test_repo_open__bare_empty_repo(void)
2020
cl_assert(git_repository_workdir(repo) == NULL);
2121
}
2222

23+
void test_repo_open__format_version_1(void)
24+
{
25+
git_buf path = GIT_BUF_INIT;
26+
git_repository *repo;
27+
git_config *config;
28+
29+
repo = cl_git_sandbox_init("empty_bare.git");
30+
31+
cl_git_pass(git_repository_open(&repo, "empty_bare.git"));
32+
cl_git_pass(git_repository_config__weakptr(&config, repo));
33+
34+
cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
35+
36+
git_repository_free(repo);
37+
cl_git_fail(git_repository_open(&repo, "empty_bare.git"));
38+
}
39+
2340
void test_repo_open__standard_empty_repo_through_gitdir(void)
2441
{
2542
git_repository *repo;

0 commit comments

Comments
 (0)