Skip to content

Commit 96e85df

Browse files
authored
Merge pull request libgit2#6492 from cavaquinho/fix/bare-repo-oid-type
libgit2#6491: Sets oid_type on repos open with git_repository_open_bare
2 parents 795758d + 2ae3bbf commit 96e85df

2 files changed

Lines changed: 51 additions & 23 deletions

File tree

src/libgit2/repository.c

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,43 @@ static int find_repo(
733733
return error;
734734
}
735735

736+
static int obtain_config_and_set_oid_type(
737+
git_config **config_ptr,
738+
git_repository *repo)
739+
{
740+
int error;
741+
git_config *config = NULL;
742+
int version = 0;
743+
744+
/*
745+
* We'd like to have the config, but git doesn't particularly
746+
* care if it's not there, so we need to deal with that.
747+
*/
748+
749+
error = git_repository_config_snapshot(&config, repo);
750+
if (error < 0 && error != GIT_ENOTFOUND)
751+
goto out;
752+
753+
if (config &&
754+
(error = check_repositoryformatversion(&version, config)) < 0)
755+
goto out;
756+
757+
if ((error = check_extensions(config, version)) < 0)
758+
goto out;
759+
760+
if (version > 0) {
761+
if ((error = load_objectformat(repo, config)) < 0)
762+
goto out;
763+
} else {
764+
repo->oid_type = GIT_OID_SHA1;
765+
}
766+
767+
out:
768+
*config_ptr = config;
769+
770+
return error;
771+
}
772+
736773
int git_repository_open_bare(
737774
git_repository **repo_ptr,
738775
const char *bare_path)
@@ -741,6 +778,7 @@ int git_repository_open_bare(
741778
git_repository *repo = NULL;
742779
bool is_valid;
743780
int error;
781+
git_config *config;
744782

745783
if ((error = git_fs_path_prettify_dir(&path, bare_path, NULL)) < 0 ||
746784
(error = is_valid_repository_path(&is_valid, &path, &common_path)) < 0)
@@ -766,8 +804,15 @@ int git_repository_open_bare(
766804
repo->is_worktree = 0;
767805
repo->workdir = NULL;
768806

807+
if ((error = obtain_config_and_set_oid_type(&config, repo)) < 0)
808+
goto cleanup;
809+
769810
*repo_ptr = repo;
770-
return 0;
811+
812+
cleanup:
813+
git_config_free(config);
814+
815+
return error;
771816
}
772817

773818
static int _git_repository_open_ext_from_env(
@@ -974,7 +1019,6 @@ int git_repository_open_ext(
9741019
gitlink = GIT_STR_INIT, commondir = GIT_STR_INIT;
9751020
git_repository *repo = NULL;
9761021
git_config *config = NULL;
977-
int version = 0;
9781022

9791023
if (flags & GIT_REPOSITORY_OPEN_FROM_ENV)
9801024
return _git_repository_open_ext_from_env(repo_ptr, start_path);
@@ -1007,20 +1051,8 @@ int git_repository_open_ext(
10071051
goto cleanup;
10081052
repo->is_worktree = is_worktree;
10091053

1010-
/*
1011-
* We'd like to have the config, but git doesn't particularly
1012-
* care if it's not there, so we need to deal with that.
1013-
*/
1014-
1015-
error = git_repository_config_snapshot(&config, repo);
1016-
if (error < 0 && error != GIT_ENOTFOUND)
1017-
goto cleanup;
1018-
1019-
if (config &&
1020-
(error = check_repositoryformatversion(&version, config)) < 0)
1021-
goto cleanup;
1022-
1023-
if ((error = check_extensions(config, version)) < 0)
1054+
error = obtain_config_and_set_oid_type(&config, repo);
1055+
if (error < 0)
10241056
goto cleanup;
10251057

10261058
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0) {
@@ -1032,13 +1064,6 @@ int git_repository_open_ext(
10321064
goto cleanup;
10331065
}
10341066

1035-
if (version > 0) {
1036-
if ((error = load_objectformat(repo, config)) < 0)
1037-
goto cleanup;
1038-
} else {
1039-
repo->oid_type = GIT_OID_SHA1;
1040-
}
1041-
10421067
/*
10431068
* Ensure that the git directory and worktree are
10441069
* owned by the current user.

tests/libgit2/repo/open.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ void test_repo_open__no_config(void)
410410
git_str_dispose(&path);
411411

412412
cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
413+
cl_assert(git_repository_oid_type(repo) == GIT_OID_SHA1);
413414
cl_git_pass(git_repository_config(&config, repo));
414415

415416
cl_git_pass(git_config_set_string(config, "test.set", "42"));
@@ -433,11 +434,13 @@ void test_repo_open__force_bare(void)
433434

434435
cl_git_pass(git_repository_open(&barerepo, "alternate"));
435436
cl_assert(!git_repository_is_bare(barerepo));
437+
cl_assert(git_repository_oid_type(barerepo) == GIT_OID_SHA1);
436438
git_repository_free(barerepo);
437439

438440
cl_git_pass(git_repository_open_bare(
439441
&barerepo, "empty_standard_repo/.git"));
440442
cl_assert(git_repository_is_bare(barerepo));
443+
cl_assert(git_repository_oid_type(barerepo) == GIT_OID_SHA1);
441444
git_repository_free(barerepo);
442445

443446
cl_git_fail(git_repository_open_bare(&barerepo, "alternate/.git"));

0 commit comments

Comments
 (0)