]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/object/peel.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / object / peel.c
CommitLineData
db9be945 1#include "clar_libgit2.h"
2
3static git_repository *g_repo;
4
5void test_object_peel__initialize(void)
6{
7 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
8}
9
10void test_object_peel__cleanup(void)
11{
12 git_repository_free(g_repo);
9094d30b 13 g_repo = NULL;
db9be945 14}
15
cf4c43ab 16static void assert_peel(
17 const char *sha,
ac3d33df 18 git_object_t requested_type,
cf4c43ab 19 const char* expected_sha,
ac3d33df 20 git_object_t expected_type)
db9be945 21{
22 git_oid oid, expected_oid;
23 git_object *obj;
24 git_object *peeled;
25
26 cl_git_pass(git_oid_fromstr(&oid, sha));
ac3d33df
JK
27 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
28
db9be945 29 cl_git_pass(git_object_peel(&peeled, obj, requested_type));
30
31 cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha));
0cee70eb 32 cl_assert_equal_oid(&expected_oid, git_object_id(peeled));
db9be945 33
cf4c43ab 34 cl_assert_equal_i(expected_type, git_object_type(peeled));
35
db9be945 36 git_object_free(peeled);
37 git_object_free(obj);
38}
39
ac3d33df 40static void assert_peel_error(int error, const char *sha, git_object_t requested_type)
db9be945 41{
42 git_oid oid;
43 git_object *obj;
44 git_object *peeled;
45
46 cl_git_pass(git_oid_fromstr(&oid, sha));
ac3d33df
JK
47 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
48
db9be945 49 cl_assert_equal_i(error, git_object_peel(&peeled, obj, requested_type));
50
51 git_object_free(obj);
52}
53
54void test_object_peel__peeling_an_object_into_its_own_type_returns_another_instance_of_it(void)
55{
ac3d33df
JK
56 assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT,
57 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
58 assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_TAG,
59 "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_TAG);
60 assert_peel("53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE,
61 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
62 assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB,
63 "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB);
db9be945 64}
65
753e17b0 66void test_object_peel__tag(void)
db9be945 67{
ac3d33df
JK
68 assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_COMMIT,
69 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
70 assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_TREE,
71 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
72 assert_peel_error(GIT_EPEEL, "7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_BLOB);
73 assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_ANY,
74 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
db9be945 75}
76
753e17b0 77void test_object_peel__commit(void)
db9be945 78{
ac3d33df
JK
79 assert_peel_error(GIT_EINVALIDSPEC, "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_BLOB);
80 assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_TREE,
81 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
82 assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT,
83 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
84 assert_peel_error(GIT_EINVALIDSPEC, "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_TAG);
85 assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_ANY,
86 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
db9be945 87}
88
753e17b0 89void test_object_peel__tree(void)
db9be945 90{
ac3d33df
JK
91 assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_BLOB);
92 assert_peel("53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE,
93 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
94 assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_COMMIT);
95 assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TAG);
96 assert_peel_error(GIT_EINVALIDSPEC, "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_ANY);
db9be945 97}
98
753e17b0 99void test_object_peel__blob(void)
db9be945 100{
ac3d33df
JK
101 assert_peel("0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB,
102 "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_BLOB);
103 assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_TREE);
104 assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_COMMIT);
105 assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_TAG);
106 assert_peel_error(GIT_EINVALIDSPEC, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", GIT_OBJECT_ANY);
db9be945 107}
108
d8057a5b 109void test_object_peel__target_any_object_for_type_change(void)
db9be945 110{
d8057a5b 111 /* tag to commit */
ac3d33df
JK
112 assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_ANY,
113 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
d8057a5b
RB
114
115 /* commit to tree */
ac3d33df
JK
116 assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_ANY,
117 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
bc05f30c 118}