]> git.proxmox.com Git - libgit2.git/blob - tests/notes/notes.c
Merge pull request #2459 from libgit2/cmn/http-url-path
[libgit2.git] / tests / notes / notes.c
1 #include "clar_libgit2.h"
2
3 static git_repository *_repo;
4 static git_signature *_sig;
5
6 void test_notes_notes__initialize(void)
7 {
8 _repo = cl_git_sandbox_init("testrepo.git");
9 cl_git_pass(git_signature_now(&_sig, "alice", "alice@example.com"));
10 }
11
12 void test_notes_notes__cleanup(void)
13 {
14 git_signature_free(_sig);
15 _sig = NULL;
16
17 cl_git_sandbox_cleanup();
18 }
19
20 static void assert_note_equal(git_note *note, char *message, git_oid *note_oid) {
21 git_blob *blob;
22
23 cl_assert_equal_s(git_note_message(note), message);
24 cl_assert_equal_oid(git_note_id(note), note_oid);
25
26 cl_git_pass(git_blob_lookup(&blob, _repo, note_oid));
27 cl_assert_equal_s(git_note_message(note), (const char *)git_blob_rawcontent(blob));
28
29 git_blob_free(blob);
30 }
31
32 static void create_note(git_oid *note_oid, const char *canonical_namespace, const char *target_sha, const char *message)
33 {
34 git_oid oid;
35
36 cl_git_pass(git_oid_fromstr(&oid, target_sha));
37 cl_git_pass(git_note_create(note_oid, _repo, _sig, _sig, canonical_namespace, &oid, message, 0));
38 }
39
40 static struct {
41 const char *note_sha;
42 const char *annotated_object_sha;
43 }
44 list_expectations[] = {
45 { "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045" },
46 { "1c73b1f51762155d357bcd1fd4f2c409ef80065b", "9fd738e8f7967c078dceed8190330fc8648ee56a" },
47 { "257b43746b6b46caa4aa788376c647cce0a33e2b", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750" },
48 { "1ec1c8e03f461f4f5d3f3702172483662e7223f3", "c47800c7266a2be04c571c04d5a6614691ea99bd" },
49 { NULL, NULL }
50 };
51
52 #define EXPECTATIONS_COUNT (sizeof(list_expectations)/sizeof(list_expectations[0])) - 1
53
54 static int note_list_cb(
55 const git_oid *blob_id, const git_oid *annotated_obj_id, void *payload)
56 {
57 git_oid expected_note_oid, expected_target_oid;
58
59 unsigned int *count = (unsigned int *)payload;
60
61 cl_assert(*count < EXPECTATIONS_COUNT);
62
63 cl_git_pass(git_oid_fromstr(&expected_note_oid, list_expectations[*count].note_sha));
64 cl_assert_equal_oid(&expected_note_oid, blob_id);
65
66 cl_git_pass(git_oid_fromstr(&expected_target_oid, list_expectations[*count].annotated_object_sha));
67 cl_assert_equal_oid(&expected_target_oid, annotated_obj_id);
68
69 (*count)++;
70
71 return 0;
72 }
73
74 /*
75 * $ git notes --ref i-can-see-dead-notes add -m "I decorate a65f" a65fedf39aefe402d3bb6e24df4d4f5fe4547750
76 * $ git notes --ref i-can-see-dead-notes add -m "I decorate c478" c47800c7266a2be04c571c04d5a6614691ea99bd
77 * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 9fd738e8f7967c078dceed8190330fc8648ee56a
78 * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
79 *
80 * $ git notes --ref i-can-see-dead-notes list
81 * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
82 * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
83 * 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
84 * 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
85 *
86 * $ git ls-tree refs/notes/i-can-see-dead-notes
87 * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
88 * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
89 * 100644 blob 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
90 * 100644 blob 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
91 */
92 void test_notes_notes__can_retrieve_a_list_of_notes_for_a_given_namespace(void)
93 {
94 git_oid note_oid1, note_oid2, note_oid3, note_oid4;
95 unsigned int retrieved_notes = 0;
96
97 create_note(&note_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n");
98 create_note(&note_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n");
99 create_note(&note_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n");
100 create_note(&note_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
101
102 cl_git_pass(git_note_foreach
103 (_repo, "refs/notes/i-can-see-dead-notes", note_list_cb, &retrieved_notes));
104
105 cl_assert_equal_i(4, retrieved_notes);
106 }
107
108 static int note_cancel_cb(
109 const git_oid *blob_id, const git_oid *annotated_obj_id, void *payload)
110 {
111 unsigned int *count = (unsigned int *)payload;
112
113 GIT_UNUSED(blob_id);
114 GIT_UNUSED(annotated_obj_id);
115
116 (*count)++;
117
118 return (*count > 2);
119 }
120
121 void test_notes_notes__can_cancel_foreach(void)
122 {
123 git_oid note_oid1, note_oid2, note_oid3, note_oid4;
124 unsigned int retrieved_notes = 0;
125
126 create_note(&note_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n");
127 create_note(&note_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n");
128 create_note(&note_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n");
129 create_note(&note_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
130
131 cl_assert_equal_i(
132 1,
133 git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes",
134 note_cancel_cb, &retrieved_notes));
135 }
136
137 void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_returns_ENOTFOUND(void)
138 {
139 int error;
140 unsigned int retrieved_notes = 0;
141
142 error = git_note_foreach(_repo, "refs/notes/i-am-not", note_list_cb, &retrieved_notes);
143 cl_git_fail(error);
144 cl_assert_equal_i(GIT_ENOTFOUND, error);
145
146 cl_assert_equal_i(0, retrieved_notes);
147 }
148
149 void test_notes_notes__inserting_a_note_without_passing_a_namespace_uses_the_default_namespace(void)
150 {
151 git_oid note_oid, target_oid;
152 git_note *note, *default_namespace_note;
153 const char *default_ref;
154
155 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
156 cl_git_pass(git_note_default_ref(&default_ref, _repo));
157
158 create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
159
160 cl_git_pass(git_note_read(&note, _repo, NULL, &target_oid));
161 cl_git_pass(git_note_read(&default_namespace_note, _repo, default_ref, &target_oid));
162
163 assert_note_equal(note, "hello world\n", &note_oid);
164 assert_note_equal(default_namespace_note, "hello world\n", &note_oid);
165
166 git_note_free(note);
167 git_note_free(default_namespace_note);
168 }
169
170 void test_notes_notes__can_insert_a_note_with_a_custom_namespace(void)
171 {
172 git_oid note_oid, target_oid;
173 git_note *note;
174
175 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
176
177 create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world on a custom namespace\n");
178
179 cl_git_pass(git_note_read(&note, _repo, "refs/notes/some/namespace", &target_oid));
180
181 assert_note_equal(note, "hello world on a custom namespace\n", &note_oid);
182
183 git_note_free(note);
184 }
185
186 /*
187 * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
188 * 08b041783f40edfe12bb406c9c9a8a040177c125
189 */
190 void test_notes_notes__creating_a_note_on_a_target_which_already_has_one_returns_EEXISTS(void)
191 {
192 int error;
193 git_oid note_oid, target_oid;
194
195 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
196
197 create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
198 error = git_note_create(&note_oid, _repo, _sig, _sig, NULL, &target_oid, "hello world\n", 0);
199 cl_git_fail(error);
200 cl_assert_equal_i(GIT_EEXISTS, error);
201
202 create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
203 error = git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &target_oid, "hello world\n", 0);
204 cl_git_fail(error);
205 cl_assert_equal_i(GIT_EEXISTS, error);
206 }
207
208
209 void test_notes_notes__creating_a_note_on_a_target_can_overwrite_existing_note(void)
210 {
211 git_oid note_oid, target_oid;
212 git_note *note, *namespace_note;
213
214 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
215
216 create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n");
217 cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, NULL, &target_oid, "hello new world\n", 1));
218
219 cl_git_pass(git_note_read(&note, _repo, NULL, &target_oid));
220 assert_note_equal(note, "hello new world\n", &note_oid);
221
222 create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n");
223 cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/some/namespace", &target_oid, "hello new ref world\n", 1));
224
225 cl_git_pass(git_note_read(&namespace_note, _repo, "refs/notes/some/namespace", &target_oid));
226 assert_note_equal(namespace_note, "hello new ref world\n", &note_oid);
227
228 git_note_free(note);
229 git_note_free(namespace_note);
230 }
231
232 static char *messages[] = {
233 "08c041783f40edfe12bb406c9c9a8a040177c125",
234 "96c45fbe09ab7445fc7c60fd8d17f32494399343",
235 "48cc7e38dcfc1ec87e70ec03e08c3e83d7a16aa1",
236 "24c3eaafb681c3df668f9df96f58e7b8c756eb04",
237 "96ca1b6ccc7858ae94684777f85ac0e7447f7040",
238 "7ac2db4378a08bb244a427c357e0082ee0d57ac6",
239 "e6cba23dbf4ef84fe35e884f017f4e24dc228572",
240 "c8cf3462c7d8feba716deeb2ebe6583bd54589e2",
241 "39c16b9834c2d665ac5f68ad91dc5b933bad8549",
242 "f3c582b1397df6a664224ebbaf9d4cc952706597",
243 "29cec67037fe8e89977474988219016ae7f342a6",
244 "36c4cd238bf8e82e27b740e0741b025f2e8c79ab",
245 "f1c45a47c02e01d5a9a326f1d9f7f756373387f8",
246 "4aca84406f5daee34ab513a60717c8d7b1763ead",
247 "84ce167da452552f63ed8407b55d5ece4901845f",
248 NULL
249 };
250
251 #define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1
252
253 /*
254 * $ git ls-tree refs/notes/fanout
255 * 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84
256 *
257 * $ git ls-tree 4b22b35
258 * 040000 tree d71aab4f9b04b45ce09bcaa636a9be6231474759 96
259 *
260 * $ git ls-tree d71aab4
261 * 100644 blob 08b041783f40edfe12bb406c9c9a8a040177c125 071c1b46c854b31185ea97743be6a8774479
262 */
263 void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void)
264 {
265 size_t i;
266 git_oid note_oid, target_oid;
267 git_note *_note;
268
269 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
270
271 for (i = 0; i < MESSAGES_COUNT; i++) {
272 cl_git_pass(git_note_create(&note_oid, _repo, _sig, _sig, "refs/notes/fanout", &target_oid, messages[i], 0));
273 cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid));
274 git_note_free(_note);
275
276 git_oid_cpy(&target_oid, &note_oid);
277 }
278 }
279
280 /*
281 * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
282 * 08b041783f40edfe12bb406c9c9a8a040177c125
283 */
284 void test_notes_notes__can_read_a_note_in_an_existing_fanout(void)
285 {
286 git_oid note_oid, target_oid;
287 git_note *note;
288
289 cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
290 cl_git_pass(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
291
292 cl_git_pass(git_oid_fromstr(&note_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
293 cl_assert_equal_oid(git_note_id(note), &note_oid);
294
295 git_note_free(note);
296 }
297
298 void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void)
299 {
300 git_oid target_oid;
301 git_note *note;
302
303 cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
304 cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
305
306 cl_git_fail(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
307 }
308
309 void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(void)
310 {
311 int error;
312 git_oid target_oid;
313
314 cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
315 cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
316
317 error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid);
318 cl_git_fail(error);
319 cl_assert_equal_i(GIT_ENOTFOUND, error);
320 }
321
322 void test_notes_notes__can_iterate_default_namespace(void)
323 {
324 git_note_iterator *iter;
325 git_note *note;
326 git_oid note_id, annotated_id;
327 git_oid note_created[2];
328 const char* note_message[] = {
329 "I decorate a65f\n",
330 "I decorate c478\n"
331 };
332 int i, err;
333
334 create_note(&note_created[0], "refs/notes/commits",
335 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
336 create_note(&note_created[1], "refs/notes/commits",
337 "c47800c7266a2be04c571c04d5a6614691ea99bd", note_message[1]);
338
339 cl_git_pass(git_note_iterator_new(&iter, _repo, NULL));
340
341 for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
342 cl_git_pass(git_note_read(&note, _repo, NULL, &annotated_id));
343 cl_assert_equal_s(git_note_message(note), note_message[i]);
344 git_note_free(note);
345 }
346
347 cl_assert_equal_i(GIT_ITEROVER, err);
348 cl_assert_equal_i(2, i);
349 git_note_iterator_free(iter);
350 }
351
352 void test_notes_notes__can_iterate_custom_namespace(void)
353 {
354 git_note_iterator *iter;
355 git_note *note;
356 git_oid note_id, annotated_id;
357 git_oid note_created[2];
358 const char* note_message[] = {
359 "I decorate a65f\n",
360 "I decorate c478\n"
361 };
362 int i, err;
363
364 create_note(&note_created[0], "refs/notes/beer",
365 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
366 create_note(&note_created[1], "refs/notes/beer",
367 "c47800c7266a2be04c571c04d5a6614691ea99bd", note_message[1]);
368
369 cl_git_pass(git_note_iterator_new(&iter, _repo, "refs/notes/beer"));
370
371 for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
372 cl_git_pass(git_note_read(&note, _repo, "refs/notes/beer", &annotated_id));
373 cl_assert_equal_s(git_note_message(note), note_message[i]);
374 git_note_free(note);
375 }
376
377 cl_assert_equal_i(GIT_ITEROVER, err);
378 cl_assert_equal_i(2, i);
379 git_note_iterator_free(iter);
380 }
381
382 void test_notes_notes__empty_iterate(void)
383 {
384 git_note_iterator *iter;
385
386 cl_git_fail(git_note_iterator_new(&iter, _repo, "refs/notes/commits"));
387 }