]> git.proxmox.com Git - libgit2.git/blame - tests/refs/peel.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / refs / peel.c
CommitLineData
31665948 1#include "clar_libgit2.h"
2
3static git_repository *g_repo;
7edb9071 4static git_repository *g_peel_repo;
31665948 5
6void test_refs_peel__initialize(void)
7{
8 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
7edb9071 9 cl_git_pass(git_repository_open(&g_peel_repo, cl_fixture("peeled.git")));
31665948 10}
11
12void test_refs_peel__cleanup(void)
13{
14 git_repository_free(g_repo);
9094d30b 15 g_repo = NULL;
7edb9071
JK
16 git_repository_free(g_peel_repo);
17 g_peel_repo = NULL;
31665948 18}
19
7edb9071
JK
20static void assert_peel_generic(
21 git_repository *repo,
31665948 22 const char *ref_name,
ac3d33df 23 git_object_t requested_type,
31665948 24 const char* expected_sha,
ac3d33df 25 git_object_t expected_type)
31665948 26{
27 git_oid expected_oid;
28 git_reference *ref;
29 git_object *peeled;
30
7edb9071 31 cl_git_pass(git_reference_lookup(&ref, repo, ref_name));
ac3d33df 32
31665948 33 cl_git_pass(git_reference_peel(&peeled, ref, requested_type));
34
35 cl_git_pass(git_oid_fromstr(&expected_oid, expected_sha));
0cee70eb 36 cl_assert_equal_oid(&expected_oid, git_object_id(peeled));
31665948 37
38 cl_assert_equal_i(expected_type, git_object_type(peeled));
39
40 git_object_free(peeled);
41 git_reference_free(ref);
42}
43
7edb9071
JK
44static void assert_peel(
45 const char *ref_name,
ac3d33df 46 git_object_t requested_type,
7edb9071 47 const char* expected_sha,
ac3d33df 48 git_object_t expected_type)
7edb9071
JK
49{
50 assert_peel_generic(g_repo, ref_name, requested_type,
51 expected_sha, expected_type);
52}
53
ac3d33df 54static void assert_peel_error(int error, const char *ref_name, git_object_t requested_type)
31665948 55{
56 git_reference *ref;
57 git_object *peeled;
58
59 cl_git_pass(git_reference_lookup(&ref, g_repo, ref_name));
ac3d33df 60
31665948 61 cl_assert_equal_i(error, git_reference_peel(&peeled, ref, requested_type));
62
63 git_reference_free(ref);
64}
65
66void test_refs_peel__can_peel_a_tag(void)
67{
ac3d33df
JK
68 assert_peel("refs/tags/test", GIT_OBJECT_TAG,
69 "b25fa35b38051e4ae45d4222e795f9df2e43f1d1", GIT_OBJECT_TAG);
70 assert_peel("refs/tags/test", GIT_OBJECT_COMMIT,
71 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
72 assert_peel("refs/tags/test", GIT_OBJECT_TREE,
73 "53fc32d17276939fc79ed05badaef2db09990016", GIT_OBJECT_TREE);
74 assert_peel("refs/tags/point_to_blob", GIT_OBJECT_BLOB,
75 "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJECT_BLOB);
31665948 76}
77
78void test_refs_peel__can_peel_a_branch(void)
79{
ac3d33df
JK
80 assert_peel("refs/heads/master", GIT_OBJECT_COMMIT,
81 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
82 assert_peel("refs/heads/master", GIT_OBJECT_TREE,
83 "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJECT_TREE);
31665948 84}
85
86void test_refs_peel__can_peel_a_symbolic_reference(void)
87{
ac3d33df
JK
88 assert_peel("HEAD", GIT_OBJECT_COMMIT,
89 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
90 assert_peel("HEAD", GIT_OBJECT_TREE,
91 "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162", GIT_OBJECT_TREE);
31665948 92}
93
94void test_refs_peel__cannot_peel_into_a_non_existing_target(void)
95{
ac3d33df 96 assert_peel_error(GIT_EINVALIDSPEC, "refs/tags/point_to_blob", GIT_OBJECT_TAG);
31665948 97}
98
99void test_refs_peel__can_peel_into_any_non_tag_object(void)
100{
ac3d33df
JK
101 assert_peel("refs/heads/master", GIT_OBJECT_ANY,
102 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OBJECT_COMMIT);
103 assert_peel("refs/tags/point_to_blob", GIT_OBJECT_ANY,
104 "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OBJECT_BLOB);
105 assert_peel("refs/tags/test", GIT_OBJECT_ANY,
106 "e90810b8df3e80c413d903f631643c716887138d", GIT_OBJECT_COMMIT);
31665948 107}
7edb9071
JK
108
109void test_refs_peel__can_peel_fully_peeled_packed_refs(void)
110{
111 assert_peel_generic(g_peel_repo,
ac3d33df 112 "refs/tags/tag-inside-tags", GIT_OBJECT_ANY,
7edb9071 113 "0df1a5865c8abfc09f1f2182e6a31be550e99f07",
ac3d33df 114 GIT_OBJECT_COMMIT);
7edb9071 115 assert_peel_generic(g_peel_repo,
ac3d33df 116 "refs/foo/tag-outside-tags", GIT_OBJECT_ANY,
7edb9071 117 "0df1a5865c8abfc09f1f2182e6a31be550e99f07",
ac3d33df 118 GIT_OBJECT_COMMIT);
7edb9071 119}
eae0bfdc
PP
120
121void test_refs_peel__can_peel_fully_peeled_tag_to_tag(void)
122{
123 assert_peel_generic(g_peel_repo,
ac3d33df 124 "refs/tags/tag-inside-tags", GIT_OBJECT_TAG,
eae0bfdc 125 "c2596aa0151888587ec5c0187f261e63412d9e11",
ac3d33df 126 GIT_OBJECT_TAG);
eae0bfdc 127 assert_peel_generic(g_peel_repo,
ac3d33df 128 "refs/foo/tag-outside-tags", GIT_OBJECT_TAG,
eae0bfdc 129 "c2596aa0151888587ec5c0187f261e63412d9e11",
ac3d33df 130 GIT_OBJECT_TAG);
eae0bfdc 131}