1 #include "clar_libgit2.h"
2 
3 #include "tag.h"
4 
5 static const char *tag1_id = "b25fa35b38051e4ae45d4222e795f9df2e43f1d1";
6 static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
7 static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
8 static const char *bad_tag_id = "eda9f45a2a98d4c17a09d681d88569fa4ea91755";
9 static const char *badly_tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
10 static const char *short_tag_id = "5da7760512a953e3c7c4e47e4392c7a4338fb729";
11 static const char *short_tagged_commit = "4a5ed60bafcf4638b7c8356bd4ce1916bfede93c";
12 static const char *taggerless = "4a23e2e65ad4e31c4c9db7dc746650bfad082679";
13 
14 static git_repository *g_repo;
15 
16 /* Fixture setup and teardown */
test_object_tag_read__initialize(void)17 void test_object_tag_read__initialize(void)
18 {
19 	g_repo = cl_git_sandbox_init("testrepo");
20 }
21 
test_object_tag_read__cleanup(void)22 void test_object_tag_read__cleanup(void)
23 {
24 	cl_git_sandbox_cleanup();
25 }
26 
27 
test_object_tag_read__parse(void)28 void test_object_tag_read__parse(void)
29 {
30 	/* read and parse a tag from the repository */
31 	git_tag *tag1, *tag2;
32 	git_commit *commit;
33 	git_oid id1, id2, id_commit;
34 
35 	git_oid_fromstr(&id1, tag1_id);
36 	git_oid_fromstr(&id2, tag2_id);
37 	git_oid_fromstr(&id_commit, tagged_commit);
38 
39 	cl_git_pass(git_tag_lookup(&tag1, g_repo, &id1));
40 
41 	cl_assert_equal_s(git_tag_name(tag1), "test");
42 	cl_assert(git_tag_target_type(tag1) == GIT_OBJECT_TAG);
43 
44 	cl_git_pass(git_tag_target((git_object **)&tag2, tag1));
45 	cl_assert(tag2 != NULL);
46 
47 	cl_assert(git_oid_cmp(&id2, git_tag_id(tag2)) == 0);
48 
49 	cl_git_pass(git_tag_target((git_object **)&commit, tag2));
50 	cl_assert(commit != NULL);
51 
52 	cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
53 
54 	git_tag_free(tag1);
55 	git_tag_free(tag2);
56 	git_commit_free(commit);
57 }
58 
test_object_tag_read__parse_without_tagger(void)59 void test_object_tag_read__parse_without_tagger(void)
60 {
61 	/* read and parse a tag without a tagger field */
62 	git_repository *bad_tag_repo;
63 	git_tag *bad_tag;
64 	git_commit *commit;
65 	git_oid id, id_commit;
66 
67 	/* TODO: This is a little messy */
68 	cl_git_pass(git_repository_open(&bad_tag_repo, cl_fixture("bad_tag.git")));
69 
70 	git_oid_fromstr(&id, bad_tag_id);
71 	git_oid_fromstr(&id_commit, badly_tagged_commit);
72 
73 	cl_git_pass(git_tag_lookup(&bad_tag, bad_tag_repo, &id));
74 	cl_assert(bad_tag != NULL);
75 
76 	cl_assert_equal_s(git_tag_name(bad_tag), "e90810b");
77 	cl_assert(git_oid_cmp(&id, git_tag_id(bad_tag)) == 0);
78 	cl_assert(bad_tag->tagger == NULL);
79 
80 	cl_git_pass(git_tag_target((git_object **)&commit, bad_tag));
81 	cl_assert(commit != NULL);
82 
83 	cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
84 
85 
86 	git_tag_free(bad_tag);
87 	git_commit_free(commit);
88 	git_repository_free(bad_tag_repo);
89 }
90 
test_object_tag_read__parse_without_message(void)91 void test_object_tag_read__parse_without_message(void)
92 {
93 	/* read and parse a tag without a message field */
94 	git_repository *short_tag_repo;
95 	git_tag *short_tag;
96 	git_commit *commit;
97 	git_oid id, id_commit;
98 
99 	/* TODO: This is a little messy */
100 	cl_git_pass(git_repository_open(&short_tag_repo, cl_fixture("short_tag.git")));
101 
102 	git_oid_fromstr(&id, short_tag_id);
103 	git_oid_fromstr(&id_commit, short_tagged_commit);
104 
105 	cl_git_pass(git_tag_lookup(&short_tag, short_tag_repo, &id));
106 	cl_assert(short_tag != NULL);
107 
108 	cl_assert_equal_s(git_tag_name(short_tag), "no_description");
109 	cl_assert(git_oid_cmp(&id, git_tag_id(short_tag)) == 0);
110 	cl_assert(short_tag->message == NULL);
111 
112 	cl_git_pass(git_tag_target((git_object **)&commit, short_tag));
113 	cl_assert(commit != NULL);
114 
115 	cl_assert(git_oid_cmp(&id_commit, git_commit_id(commit)) == 0);
116 
117 	git_tag_free(short_tag);
118 	git_commit_free(commit);
119 	git_repository_free(short_tag_repo);
120 }
121 
test_object_tag_read__without_tagger_nor_message(void)122 void test_object_tag_read__without_tagger_nor_message(void)
123 {
124 	git_tag *tag;
125 	git_oid id;
126 	git_repository *repo;
127 
128 	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
129 
130 	cl_git_pass(git_oid_fromstr(&id, taggerless));
131 
132 	cl_git_pass(git_tag_lookup(&tag, repo, &id));
133 
134 	cl_assert_equal_s(git_tag_name(tag), "taggerless");
135 	cl_assert(git_tag_target_type(tag) == GIT_OBJECT_COMMIT);
136 
137 	cl_assert(tag->message == NULL);
138 	cl_assert(tag->tagger == NULL);
139 
140 	git_tag_free(tag);
141 	git_repository_free(repo);
142 }
143 
144 static const char *silly_tag = "object c054ccaefbf2da31c3b19178f9e3ef20a3867924\n\
145 type commit\n\
146 tag v1_0_1\n\
147 tagger Jamis Buck <jamis@37signals.com> 1107717917\n\
148 diff --git a/lib/sqlite3/version.rb b/lib/sqlite3/version.rb\n\
149 index 0b3bf69..4ee8fc2 100644\n\
150 --- a/lib/sqlite3/version.rb\n\
151 +++ b/lib/sqlite3/version.rb\n\
152 @@ -36,7 +36,7 @@ module SQLite3\n\
153  \n\
154      MAJOR = 1\n\
155      MINOR = 0\n\
156 -    TINY  = 0\n\
157 +    TINY  = 1\n\
158  \n\
159      STRING = [ MAJOR, MINOR, TINY ].join( \".\" )\n\
160  \n\
161  -0600\n\
162 \n\
163 v1_0_1 release\n";
164 
test_object_tag_read__extra_header_fields(void)165 void test_object_tag_read__extra_header_fields(void)
166 {
167 	git_tag *tag;
168 	git_odb *odb;
169 	git_oid id;
170 
171 	cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));
172 
173 	cl_git_pass(git_odb_write(&id, odb, silly_tag, strlen(silly_tag), GIT_OBJECT_TAG));
174 	cl_git_pass(git_tag_lookup(&tag, g_repo, &id));
175 
176 	cl_assert_equal_s("v1_0_1 release\n", git_tag_message(tag));
177 
178 	git_tag_free(tag);
179 }
180