]> git.proxmox.com Git - libgit2.git/blob - tests/object/peel.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / object / peel.c
1 #include "clar_libgit2.h"
2
3 static git_repository *g_repo;
4
5 void test_object_peel__initialize(void)
6 {
7 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
8 }
9
10 void test_object_peel__cleanup(void)
11 {
12 git_repository_free(g_repo);
13 g_repo = NULL;
14 }
15
16 static void assert_peel(
17 const char *sha,
18 git_object_t requested_type,
19 const char* expected_sha,
20 git_object_t expected_type)
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));
27 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
28
29 cl_git_pass(git_object_peel(&peeled, obj, requested_type));
30
31 cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha));
32 cl_assert_equal_oid(&expected_oid, git_object_id(peeled));
33
34 cl_assert_equal_i(expected_type, git_object_type(peeled));
35
36 git_object_free(peeled);
37 git_object_free(obj);
38 }
39
40 static void assert_peel_error(int error, const char *sha, git_object_t requested_type)
41 {
42 git_oid oid;
43 git_object *obj;
44 git_object *peeled;
45
46 cl_git_pass(git_oid_fromstr(&oid, sha));
47 cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
48
49 cl_assert_equal_i(error, git_object_peel(&peeled, obj, requested_type));
50
51 git_object_free(obj);
52 }
53
54 void test_object_peel__peeling_an_object_into_its_own_type_returns_another_instance_of_it(void)
55 {
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);
64 }
65
66 void test_object_peel__tag(void)
67 {
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);
75 }
76
77 void test_object_peel__commit(void)
78 {
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);
87 }
88
89 void test_object_peel__tree(void)
90 {
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);
97 }
98
99 void test_object_peel__blob(void)
100 {
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);
107 }
108
109 void test_object_peel__target_any_object_for_type_change(void)
110 {
111 /* tag to commit */
112 assert_peel("7b4384978d2493e851f9cca7858815fac9b10980", GIT_OBJECT_ANY,
113 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
114
115 /* commit to tree */
116 assert_peel("e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_ANY,
117 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
118 }