]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/object/tag/write.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / object / tag / write.c
CommitLineData
b482c420
BS
1#include "clar_libgit2.h"
2
3static const char* tagger_name = "Vicent Marti";
4static const char* tagger_email = "vicent@github.com";
5static const char* tagger_message = "This is my tag.\n\nThere are many tags, but this one is mine\n";
6
7static const char *tag2_id = "7b4384978d2493e851f9cca7858815fac9b10980";
8static const char *tagged_commit = "e90810b8df3e80c413d903f631643c716887138d";
b482c420
BS
9
10static git_repository *g_repo;
11
ac3d33df 12/* Fixture setup and teardown */
6bb74993 13void test_object_tag_write__initialize(void)
b482c420
BS
14{
15 g_repo = cl_git_sandbox_init("testrepo");
16}
17
6bb74993 18void test_object_tag_write__cleanup(void)
b482c420
BS
19{
20 cl_git_sandbox_cleanup();
21}
22
6bb74993 23void test_object_tag_write__basic(void)
b482c420 24{
ac3d33df 25 /* write a tag to the repository and read it again */
b482c420
BS
26 git_tag *tag;
27 git_oid target_id, tag_id;
28 git_signature *tagger;
29 const git_signature *tagger1;
30 git_reference *ref_tag;
31 git_object *target;
32
33 git_oid_fromstr(&target_id, tagged_commit);
ac3d33df 34 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
b482c420
BS
35
36 /* create signature */
37 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
38
471bb8b1
VM
39 cl_git_pass(
40 git_tag_create(&tag_id, g_repo,
41 "the-tag", target, tagger, tagger_message, 0)
42 );
b482c420
BS
43
44 git_object_free(target);
45 git_signature_free(tagger);
46
47 cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
d9023dbe 48 cl_assert(git_oid_cmp(git_tag_target_id(tag), &target_id) == 0);
b482c420
BS
49
50 /* Check attributes were set correctly */
51 tagger1 = git_tag_tagger(tag);
52 cl_assert(tagger1 != NULL);
946a6dc4
VM
53 cl_assert_equal_s(tagger1->name, tagger_name);
54 cl_assert_equal_s(tagger1->email, tagger_email);
b482c420
BS
55 cl_assert(tagger1->when.time == 123456789);
56 cl_assert(tagger1->when.offset == 60);
57
946a6dc4 58 cl_assert_equal_s(git_tag_message(tag), tagger_message);
b482c420
BS
59
60 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag"));
2508cc66 61 cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
b482c420 62 cl_git_pass(git_reference_delete(ref_tag));
d00d5464 63 git_reference_free(ref_tag);
b482c420
BS
64
65 git_tag_free(tag);
66}
67
6bb74993 68void test_object_tag_write__overwrite(void)
b482c420 69{
ac3d33df 70 /* Attempt to write a tag bearing the same name than an already existing tag */
b482c420
BS
71 git_oid target_id, tag_id;
72 git_signature *tagger;
73 git_object *target;
74
75 git_oid_fromstr(&target_id, tagged_commit);
ac3d33df 76 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
b482c420
BS
77
78 /* create signature */
79 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
80
b73200c1 81 cl_assert_equal_i(GIT_EEXISTS, git_tag_create(
b482c420
BS
82 &tag_id, /* out id */
83 g_repo,
84 "e90810b",
85 target,
86 tagger,
87 tagger_message,
88 0));
89
90 git_object_free(target);
91 git_signature_free(tagger);
b482c420
BS
92}
93
6bb74993 94void test_object_tag_write__replace(void)
b482c420 95{
ac3d33df 96 /* Replace an already existing tag */
b482c420
BS
97 git_oid target_id, tag_id, old_tag_id;
98 git_signature *tagger;
99 git_reference *ref_tag;
100 git_object *target;
101
102 git_oid_fromstr(&target_id, tagged_commit);
ac3d33df 103 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
b482c420
BS
104
105 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
2508cc66 106 git_oid_cpy(&old_tag_id, git_reference_target(ref_tag));
b482c420
BS
107 git_reference_free(ref_tag);
108
109 /* create signature */
110 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
111
112 cl_git_pass(git_tag_create(
113 &tag_id, /* out id */
114 g_repo,
115 "e90810b",
116 target,
117 tagger,
118 tagger_message,
119 1));
120
121 git_object_free(target);
122 git_signature_free(tagger);
123
124 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
2508cc66
BS
125 cl_assert(git_oid_cmp(git_reference_target(ref_tag), &tag_id) == 0);
126 cl_assert(git_oid_cmp(git_reference_target(ref_tag), &old_tag_id) != 0);
b482c420
BS
127
128 git_reference_free(ref_tag);
129}
130
6bb74993 131void test_object_tag_write__lightweight(void)
b482c420 132{
ac3d33df 133 /* write a lightweight tag to the repository and read it again */
b482c420
BS
134 git_oid target_id, object_id;
135 git_reference *ref_tag;
136 git_object *target;
137
138 git_oid_fromstr(&target_id, tagged_commit);
ac3d33df 139 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
b482c420
BS
140
141 cl_git_pass(git_tag_create_lightweight(
142 &object_id,
143 g_repo,
144 "light-tag",
145 target,
146 0));
147
148 git_object_free(target);
149
150 cl_assert(git_oid_cmp(&object_id, &target_id) == 0);
151
152 cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/light-tag"));
2508cc66 153 cl_assert(git_oid_cmp(git_reference_target(ref_tag), &target_id) == 0);
b482c420
BS
154
155 cl_git_pass(git_tag_delete(g_repo, "light-tag"));
156
157 git_reference_free(ref_tag);
158}
159
6bb74993 160void test_object_tag_write__lightweight_over_existing(void)
b482c420 161{
ac3d33df 162 /* Attempt to write a lightweight tag bearing the same name than an already existing tag */
b482c420
BS
163 git_oid target_id, object_id, existing_object_id;
164 git_object *target;
165
166 git_oid_fromstr(&target_id, tagged_commit);
ac3d33df 167 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
b482c420 168
b73200c1 169 cl_assert_equal_i(GIT_EEXISTS, git_tag_create_lightweight(
b482c420
BS
170 &object_id,
171 g_repo,
172 "e90810b",
173 target,
174 0));
175
176 git_oid_fromstr(&existing_object_id, tag2_id);
177 cl_assert(git_oid_cmp(&object_id, &existing_object_id) == 0);
178
179 git_object_free(target);
180}
181
6bb74993 182void test_object_tag_write__delete(void)
b482c420 183{
ac3d33df 184 /* Delete an already existing tag */
b482c420
BS
185 git_reference *ref_tag;
186
187 cl_git_pass(git_tag_delete(g_repo, "e90810b"));
188
189 cl_git_fail(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b"));
190
191 git_reference_free(ref_tag);
192}
18d6f120 193
194void test_object_tag_write__creating_with_an_invalid_name_returns_EINVALIDSPEC(void)
195{
196 git_oid target_id, tag_id;
197 git_signature *tagger;
198 git_object *target;
199
200 git_oid_fromstr(&target_id, tagged_commit);
ac3d33df 201 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
18d6f120 202
203 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
204
205 cl_assert_equal_i(GIT_EINVALIDSPEC,
206 git_tag_create(&tag_id, g_repo,
207 "Inv@{id", target, tagger, tagger_message, 0)
208 );
209
210 cl_assert_equal_i(GIT_EINVALIDSPEC,
211 git_tag_create_lightweight(&tag_id, g_repo,
212 "Inv@{id", target, 0)
213 );
214
215 git_object_free(target);
216 git_signature_free(tagger);
217}
218
219void test_object_tag_write__deleting_with_an_invalid_name_returns_EINVALIDSPEC(void)
220{
221 cl_assert_equal_i(GIT_EINVALIDSPEC, git_tag_delete(g_repo, "Inv@{id"));
222}
b81cc1d6 223
e579e0f7 224static void create_annotation(git_oid *tag_id, const char *name)
b81cc1d6 225{
226 git_object *target;
227 git_oid target_id;
228 git_signature *tagger;
229
230 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
231
232 git_oid_fromstr(&target_id, tagged_commit);
ac3d33df 233 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
b81cc1d6 234
235 cl_git_pass(git_tag_annotation_create(tag_id, g_repo, name, target, tagger, "boom!"));
236 git_object_free(target);
237 git_signature_free(tagger);
238}
239
240void test_object_tag_write__creating_an_annotation_stores_the_new_object_in_the_odb(void)
241{
242 git_oid tag_id;
243 git_tag *tag;
244
245 create_annotation(&tag_id, "new_tag");
246
247 cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id));
248 cl_assert_equal_s("new_tag", git_tag_name(tag));
249
250 git_tag_free(tag);
251}
252
253void test_object_tag_write__creating_an_annotation_does_not_create_a_reference(void)
254{
255 git_oid tag_id;
256 git_reference *tag_ref;
257
258 create_annotation(&tag_id, "new_tag");
259 cl_git_fail_with(git_reference_lookup(&tag_ref, g_repo, "refs/tags/new_tag"), GIT_ENOTFOUND);
260}
ad5611d8
TR
261
262void test_object_tag_write__error_when_create_tag_with_invalid_name(void)
263{
264 git_oid target_id, tag_id;
265 git_signature *tagger;
266 git_object *target;
267
268 git_oid_fromstr(&target_id, tagged_commit);
269 cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJECT_COMMIT));
270 cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60));
271
272 cl_git_fail(
273 git_tag_create(&tag_id, g_repo,
274 "-dash", target, tagger, tagger_message, 0)
275 );
276
277 git_object_free(target);
278 git_signature_free(tagger);
279}