8000 repo: understand the `objectformat` extension · libgit2/libgit2@acb00e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit acb00e4

Browse files
committed
repo: understand the objectformat extension
Teach the repository about the `objectformat` extension, supporting `sha1` always and `sha256` when the experimental sha256 support is active.
1 parent 4083e46 commit acb00e4

File tree

5 files changed

+132
-11
lines changed

5 files changed

+132
-11
lines changed

include/git2/repository.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,14 @@ GIT_EXTERN(int) git_repository_ident(const char **name, const char **email, cons
949949
*/
950950
GIT_EXTERN(int) git_repository_set_ident(git_repository *repo, const char *name, const char *email);
951951

952+
/**
953+
* Gets the object type used by this repository.
954+
*
955+
* @param repo the repository
956+
* @return the object id type
957+
*/
958+
GIT_EXTERN(git_oid_t) git_repository_oid_type(git_repository *repo);
959+
952960
/** @} */
953961
GIT_END_DECL
954962
#endif

src/libgit2/repository.c

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static const struct {
6767
static int check_repositoryformatversion(int *version, git_config *config);
6868
static int check_extensions(git_config *config, int version);
6969
static int load_global_config(git_config **config);
70+
static int load_objectformat(git_repository *repo, git_config *config);
7071

7172
#define GIT_COMMONDIR_FILE "commondir"
7273
#define GIT_GITDIR_FILE "gitdir"
@@ -240,7 +241,7 @@ GIT_INLINE(int) validate_repo_path(git_str *path)
240241
*/
241242
static size_t suffix_len =
242243
CONST_STRLEN("objects/pack/pack-.pack.lock") +
243-
GIT_OID_SHA1_HEXSIZE;
244+
GIT_OID_MAX_HEXSIZE;
244245

245246
return git_fs_path_validate_str_length_with_suffix(
246247
path, suffix_len);
@@ -1030,6 +1031,13 @@ int git_repository_open_ext(
10301031
goto cleanup;
10311032
}
10321033

1034+
if (version > 0) {
1035+
if ((error = load_objectformat(repo, config)) < 0)
1036+
goto cleanup;
1037+
} else {
1038+
repo->oid_type = GIT_OID_SHA1;
1039+
}
1040+
10331041
/*
10341042
* Ensure that the git directory and worktree are
10351043
* owned by the current user.
@@ -1549,7 +1557,8 @@ static int check_repositoryformatversion(int *version, git_config *config)
15491557
}
15501558

15511559
static const char *builtin_extensions[] = {
1552-
"noop"
1560+
"noop",
1561+
"objectformat"
15531562
};
15541563

15551564
static git_vector user_extensions = GIT_VECTOR_INIT;
@@ -1613,6 +1622,32 @@ static int check_extensions(git_config *config, int version)
16131622
return git_config_foreach_match(config, "^extensions\\.", check_valid_extension, NULL);
16141623
}
16151624

1625+
static int load_objectformat(git_repository *repo, git_config *config)
1626+
{
1627+
git_config_entry *entry = NULL;
1628+
int error;
1629+
1630+
if ((error = git_config_get_entry(&entry, config, "extensions.objectformat")) < 0) {
1631+
if (error == GIT_ENOTFOUND) {
1632+
repo->oid_type = GIT_OID_SHA1;
1633+
git_error_clear();
1634+
error = 0;
1635+
}
1636+
1637+
goto done;
1638+
}
1639+
1640+
if ((repo->oid_type = git_oid_type_fromstr(entry->value)) == 0) {
1641+
git_error_set(GIT_ERROR_REPOSITORY,
1642+
"unknown object format '%s'", entry->value);
1643+
error = GIT_EINVALID;
1644+
}
1645+
1646+
done:
1647+
git_config_entry_free(entry);
1648+
return error;
1649+
}
1650+
16161651
int git_repository__extensions(char ***out, size_t *out_len)
16171652
{
16181653
git_vector extensions;
@@ -2922,14 +2957,14 @@ int git_repository__set_orig_head(git_repository *repo, const git_oid *orig_head
F438 29222957
{
29232958
git_filebuf file = GIT_FILEBUF_INIT;
29242959
git_str file_path = GIT_STR_INIT;
2925-
char orig_head_str[GIT_OID_SHA1_HEXSIZE];
2960+
char orig_head_str[GIT_OID_MAX_HEXSIZE];
29262961
int error = 0;
29272962

29282963
git_oid_fmt(orig_head_str, orig_head);
29292964

29302965
if ((error = git_str_joinpath(&file_path, repo->gitdir, GIT_ORIG_HEAD_FILE)) == 0 &&
29312966
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) == 0 &&
2932-
(error = git_filebuf_printf(&file, "%.*s\n", GIT_OID_SHA1_HEXSIZE, orig_head_str)) == 0)
2967+
(error = git_filebuf_printf(&file, "%.*s\n", (int)git_oid_hexsize(repo->oid_type), orig_head_str)) == 0)
29332968
error = git_filebuf_commit(&file);
29342969

29352970
if (error < 0)
@@ -3042,7 +3077,7 @@ int git_repository_hashfile(
30423077
goto cleanup;
30433078
}
30443079

3045-
error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, GIT_OID_SHA1, fl);
3080+
error = git_odb__hashfd_filtered(out, fd, (size_t)len, type, repo->oid_type, fl);
30463081

30473082
cleanup:
30483083
if (fd >= 0)
@@ -3389,3 +3424,8 @@ int git_repository_submodule_cache_clear(git_repository *repo)
33893424
repo->submodule_cache = NULL;
33903425
return error;
33913426
}
3427+
3428+
git_oid_t git_repository_oid_type(git_repository *repo)
3429+
{
3430+
return repo ? repo->oid_type : 0;
3431+
}

src/libgit2/repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ struct git_repository {
153153

154154
unsigned is_bare:1;
155155
unsigned is_worktree:1;
156+
git_oid_t oid_type;
156157

157158
unsigned int lru_counter;
158159

tests/libgit2/core/opts.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ void test_core_opts__extensions_query(void)
3434

3535
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));
3636

37-
cl_assert_equal_sz(out.count, 1);
37+
cl_assert_equal_sz(out.count, 2);
3838
cl_assert_equal_s("noop", out.strings[0]);
39+
cl_assert_equal_s("objectformat", out.strings[1]);
3940

4041
git_strarray_dispose(&out);
4142
}
@@ -48,9 +49,10 @@ void test_core_opts__extensions_add(void)
4849
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
4950
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));
5051

51-
cl_assert_equal_sz(out.count, 2);
52+
cl_assert_equal_sz(out.count, 3);
5253
cl_assert_equal_s("noop", out.strings[0]);
53-
cl_assert_equal_s("foo", out.strings[1]);
54+
cl_assert_equal_s("objectformat", out.strings[1]);
55+
cl_assert_equal_s("foo", out.strings[2]);
5456

5557
git_strarray_dispose(&out);
5658
}
@@ -63,9 +65,10 @@ void test_core_opts__extensions_remove(void)
6365
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, in, ARRAY_SIZE(in)));
6466
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, &out));
6567

66-
cl_assert_equal_sz(out.count, 2);
67-
cl_assert_equal_s("bar", out.strings[0]);
68-
cl_assert_equal_s("baz", out.strings[1]);
68+
cl_assert_equal_sz(out.count, 3);
69+
cl_assert_equal_s("objectformat", out.strings[0]);
70+
cl_assert_equal_s("bar", out.strings[1]);
71+
cl_assert_equal_s("baz", out.strings[2]);
6972

7073
git_strarray_dispose(&out);
7174
}

tests/libgit2/repo/objectformat.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "clar_libgit2.h"
2+
#include "futils.h"
3+
#include "sysdir.h"
4+
#include "repository.h"
5+
#include <ctype.h>
6+
7+
git_repository *repo;
8+
git_config *config;
9+
10+
void test_repo_objectformat__initialize(void)
11+
{
12+
repo = cl_git_sandbox_init("empty_bare.git");
13+
14+
cl_git_pass(git_repository_config(&config, repo));
15+
cl_git_pass(git_config_set_int32(config, "core.repositoryformatversion", 1));
16+
}
17+
18+
void test_repo_objectformat__cleanup(void)
19+
{
20+
git_config_free(config);
21+
cl_git_sandbox_cleanup();
22+
}
23+
24+
void test_repo_objectformat__unspecified(void)
25+
{
26+
git_repository *other;
27+
28+
cl_git_pass(git_repository_open(&other, "empty_bare.git"));
29+
cl_assert_equal_i(GIT_OID_SHA1, git_repository_oid_type(other));
30+
git_repository_free(other);
31+
}
32+
33+
void test_repo_objectformat__sha1(void)
34+
{
35+
git_repository *other;
36+
37+
cl_git_pass(git_config_set_string(config, "extensions.objectformat", "sha1"));
38+
39+
cl_git_pass(git_repository_open(&other, "empty_bare.git"));
40+
cl_assert_equal_i 57AE (GIT_OID_SHA1, git_repository_oid_type(other));
41+
git_repository_free(other);
42+
}
43+
44+
void test_repo_objectformat__sha256(void)
45+
{
46+
#ifndef GIT_EXPERIMENTAL_SHA256
47+
cl_skip();
48+
#else
49+
git_repository *other;
50+
51+
cl_git_pass(git_config_set_string(config, "extensions.objectformat", "sha256"));
52+
53+
cl_git_pass(git_repository_open(&other, "empty_bare.git"));
54+
cl_assert_equal_i(GIT_OID_SHA256, git_repository_oid_type(other));
55+
git_repository_free(other);
56+
#endif
57+
}
58+
59+
void test_repo_objectformat__invalid(void)
60+
{
61+
git_repository *other;
62+
63+
cl_git_pass(git_config_set_string(config, "extensions.objectformat", "bogus"));
64+
65+
cl_git_fail_with(GIT_EINVALID, git_repository_open(&other, "empty_bare.git"));
66+
cl_assert_equal_s("unknown object format 'bogus'", git_error_last()->message);
67+
git_repository_free(other);
68+
}
69+

0 commit comments

Comments
 (0)
0