]> git.proxmox.com Git - libgit2.git/blob - tests/notes/notes.c
a36cddb8ad401a8e77cbe5c0c350db25a6f63878
[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, canonical_namespace, _sig, _sig, &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 struct note_create_payload {
75 const char *note_oid;
76 const char *object_oid;
77 unsigned seen;
78 };
79
80 static int note_list_create_cb(
81 const git_oid *blob_oid, const git_oid *annotated_obj_id, void *payload)
82 {
83 git_oid expected_note_oid, expected_target_oid;
84 struct note_create_payload *notes = payload;
85 size_t i;
86
87 for (i = 0; notes[i].note_oid != NULL; i++) {
88 cl_git_pass(git_oid_fromstr(&expected_note_oid, notes[i].note_oid));
89
90 if (git_oid_cmp(&expected_note_oid, blob_oid) != 0)
91 continue;
92
93 cl_git_pass(git_oid_fromstr(&expected_target_oid, notes[i].object_oid));
94
95 if (git_oid_cmp(&expected_target_oid, annotated_obj_id) != 0)
96 continue;
97
98 notes[i].seen = 1;
99 return 0;
100 }
101
102 cl_fail("Did not see expected note");
103 return 0;
104 }
105
106 static void assert_notes_seen(struct note_create_payload payload[], size_t n)
107 {
108 size_t seen = 0, i;
109
110 for (i = 0; payload[i].note_oid != NULL; i++) {
111 if (payload[i].seen)
112 seen++;
113 }
114
115 cl_assert_equal_i(seen, n);
116 }
117
118 void test_notes_notes__can_create_a_note(void)
119 {
120 git_oid note_oid;
121 static struct note_create_payload can_create_a_note[] = {
122 { "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
123 { NULL, NULL, 0 }
124 };
125
126 create_note(&note_oid, "refs/notes/i-can-see-dead-notes", can_create_a_note[0].object_oid, "I decorate 4a20\n");
127
128 cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note));
129
130 assert_notes_seen(can_create_a_note, 1);
131 }
132
133 void test_notes_notes__can_create_a_note_from_commit(void)
134 {
135 git_oid oid;
136 git_oid notes_commit_out;
137 git_reference *ref;
138 static struct note_create_payload can_create_a_note_from_commit[] = {
139 { "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
140 { NULL, NULL, 0 }
141 };
142
143 cl_git_pass(git_oid_fromstr(&oid, can_create_a_note_from_commit[0].object_oid));
144
145 cl_git_pass(git_note_commit_create(&notes_commit_out, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
146
147 /* create_from_commit will not update any ref,
148 * so we must manually create the ref, that points to the commit */
149 cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", &notes_commit_out, 0, NULL));
150
151 cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note_from_commit));
152
153 assert_notes_seen(can_create_a_note_from_commit, 1);
154
155 git_reference_free(ref);
156 }
157
158
159 /* Test that we can create a note from a commit, given an existing commit */
160 void test_notes_notes__can_create_a_note_from_commit_given_an_existing_commit(void)
161 {
162 git_oid oid;
163 git_oid notes_commit_out;
164 git_commit *existing_notes_commit = NULL;
165 git_reference *ref;
166 static struct note_create_payload can_create_a_note_from_commit_given_an_existing_commit[] = {
167 { "1c9b1bc36730582a42d56eeee0dc58673d7ae869", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", 0 },
168 { "1aaf94147c21f981e0a20bf57b89137c5a6aae52", "9fd738e8f7967c078dceed8190330fc8648ee56a", 0 },
169 { NULL, NULL, 0 }
170 };
171
172 cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
173
174 cl_git_pass(git_note_commit_create(&notes_commit_out, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 0));
175
176 cl_git_pass(git_oid_fromstr(&oid, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
177
178 git_commit_lookup(&existing_notes_commit, _repo, &notes_commit_out);
179
180 cl_assert(existing_notes_commit);
181
182 cl_git_pass(git_note_commit_create(&notes_commit_out, NULL, _repo, existing_notes_commit, _sig, _sig, &oid, "I decorate 9fd7\n", 0));
183
184 /* create_from_commit will not update any ref,
185 * so we must manually create the ref, that points to the commit */
186 cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", &notes_commit_out, 0, NULL));
187
188 cl_git_pass(git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes", note_list_create_cb, &can_create_a_note_from_commit_given_an_existing_commit));
189
190 assert_notes_seen(can_create_a_note_from_commit_given_an_existing_commit, 2);
191
192 git_commit_free(existing_notes_commit);
193 git_reference_free(ref);
194 }
195
196 /*
197 * $ git notes --ref i-can-see-dead-notes add -m "I decorate a65f" a65fedf39aefe402d3bb6e24df4d4f5fe4547750
198 * $ git notes --ref i-can-see-dead-notes add -m "I decorate c478" c47800c7266a2be04c571c04d5a6614691ea99bd
199 * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 9fd738e8f7967c078dceed8190330fc8648ee56a
200 * $ git notes --ref i-can-see-dead-notes add -m "I decorate 9fd7 and 4a20" 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
201 *
202 * $ git notes --ref i-can-see-dead-notes list
203 * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
204 * 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
205 * 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
206 * 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
207 *
208 * $ git ls-tree refs/notes/i-can-see-dead-notes
209 * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
210 * 100644 blob 1c73b1f51762155d357bcd1fd4f2c409ef80065b 9fd738e8f7967c078dceed8190330fc8648ee56a
211 * 100644 blob 257b43746b6b46caa4aa788376c647cce0a33e2b a65fedf39aefe402d3bb6e24df4d4f5fe4547750
212 * 100644 blob 1ec1c8e03f461f4f5d3f3702172483662e7223f3 c47800c7266a2be04c571c04d5a6614691ea99bd
213 */
214 void test_notes_notes__can_retrieve_a_list_of_notes_for_a_given_namespace(void)
215 {
216 git_oid note_oid1, note_oid2, note_oid3, note_oid4;
217 unsigned int retrieved_notes = 0;
218
219 create_note(&note_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n");
220 create_note(&note_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n");
221 create_note(&note_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n");
222 create_note(&note_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
223
224 cl_git_pass(git_note_foreach
225 (_repo, "refs/notes/i-can-see-dead-notes", note_list_cb, &retrieved_notes));
226
227 cl_assert_equal_i(4, retrieved_notes);
228 }
229
230 static int note_cancel_cb(
231 const git_oid *blob_id, const git_oid *annotated_obj_id, void *payload)
232 {
233 unsigned int *count = (unsigned int *)payload;
234
235 GIT_UNUSED(blob_id);
236 GIT_UNUSED(annotated_obj_id);
237
238 (*count)++;
239
240 return (*count > 2);
241 }
242
243 void test_notes_notes__can_cancel_foreach(void)
244 {
245 git_oid note_oid1, note_oid2, note_oid3, note_oid4;
246 unsigned int retrieved_notes = 0;
247
248 create_note(&note_oid1, "refs/notes/i-can-see-dead-notes", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "I decorate a65f\n");
249 create_note(&note_oid2, "refs/notes/i-can-see-dead-notes", "c47800c7266a2be04c571c04d5a6614691ea99bd", "I decorate c478\n");
250 create_note(&note_oid3, "refs/notes/i-can-see-dead-notes", "9fd738e8f7967c078dceed8190330fc8648ee56a", "I decorate 9fd7 and 4a20\n");
251 create_note(&note_oid4, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 9fd7 and 4a20\n");
252
253 cl_assert_equal_i(
254 1,
255 git_note_foreach(_repo, "refs/notes/i-can-see-dead-notes",
256 note_cancel_cb, &retrieved_notes));
257 }
258
259 void test_notes_notes__retrieving_a_list_of_notes_for_an_unknown_namespace_returns_ENOTFOUND(void)
260 {
261 int error;
262 unsigned int retrieved_notes = 0;
263
264 error = git_note_foreach(_repo, "refs/notes/i-am-not", note_list_cb, &retrieved_notes);
265 cl_git_fail(error);
266 cl_assert_equal_i(GIT_ENOTFOUND, error);
267
268 cl_assert_equal_i(0, retrieved_notes);
269 }
270
271 void test_notes_notes__inserting_a_note_without_passing_a_namespace_uses_the_default_namespace(void)
272 {
273 git_oid note_oid, target_oid;
274 git_note *note, *default_namespace_note;
275 git_buf default_ref = GIT_BUF_INIT;
276
277 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
278 cl_git_pass(git_note_default_ref(&default_ref, _repo));
279
280 create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
281
282 cl_git_pass(git_note_read(&note, _repo, NULL, &target_oid));
283 cl_git_pass(git_note_read(&default_namespace_note, _repo, default_ref.ptr, &target_oid));
284
285 assert_note_equal(note, "hello world\n", &note_oid);
286 assert_note_equal(default_namespace_note, "hello world\n", &note_oid);
287
288 git_buf_dispose(&default_ref);
289 git_note_free(note);
290 git_note_free(default_namespace_note);
291 }
292
293 void test_notes_notes__can_insert_a_note_with_a_custom_namespace(void)
294 {
295 git_oid note_oid, target_oid;
296 git_note *note;
297
298 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
299
300 create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world on a custom namespace\n");
301
302 cl_git_pass(git_note_read(&note, _repo, "refs/notes/some/namespace", &target_oid));
303
304 assert_note_equal(note, "hello world on a custom namespace\n", &note_oid);
305
306 git_note_free(note);
307 }
308
309 /*
310 * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
311 * 08b041783f40edfe12bb406c9c9a8a040177c125
312 */
313 void test_notes_notes__creating_a_note_on_a_target_which_already_has_one_returns_EEXISTS(void)
314 {
315 int error;
316 git_oid note_oid, target_oid;
317
318 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
319
320 create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
321 error = git_note_create(&note_oid, _repo, NULL, _sig, _sig, &target_oid, "hello world\n", 0);
322 cl_git_fail(error);
323 cl_assert_equal_i(GIT_EEXISTS, error);
324
325 create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello world\n");
326 error = git_note_create(&note_oid, _repo, "refs/notes/some/namespace", _sig, _sig, &target_oid, "hello world\n", 0);
327 cl_git_fail(error);
328 cl_assert_equal_i(GIT_EEXISTS, error);
329 }
330
331
332 void test_notes_notes__creating_a_note_on_a_target_can_overwrite_existing_note(void)
333 {
334 git_oid note_oid, target_oid;
335 git_note *note, *namespace_note;
336
337 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
338
339 create_note(&note_oid, NULL, "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n");
340 cl_git_pass(git_note_create(&note_oid, _repo, NULL, _sig, _sig, &target_oid, "hello new world\n", 1));
341
342 cl_git_pass(git_note_read(&note, _repo, NULL, &target_oid));
343 assert_note_equal(note, "hello new world\n", &note_oid);
344
345 create_note(&note_oid, "refs/notes/some/namespace", "08b041783f40edfe12bb406c9c9a8a040177c125", "hello old world\n");
346 cl_git_pass(git_note_create(&note_oid, _repo, "refs/notes/some/namespace", _sig, _sig, &target_oid, "hello new ref world\n", 1));
347
348 cl_git_pass(git_note_read(&namespace_note, _repo, "refs/notes/some/namespace", &target_oid));
349 assert_note_equal(namespace_note, "hello new ref world\n", &note_oid);
350
351 git_note_free(note);
352 git_note_free(namespace_note);
353 }
354
355 static char *messages[] = {
356 "08c041783f40edfe12bb406c9c9a8a040177c125",
357 "96c45fbe09ab7445fc7c60fd8d17f32494399343",
358 "48cc7e38dcfc1ec87e70ec03e08c3e83d7a16aa1",
359 "24c3eaafb681c3df668f9df96f58e7b8c756eb04",
360 "96ca1b6ccc7858ae94684777f85ac0e7447f7040",
361 "7ac2db4378a08bb244a427c357e0082ee0d57ac6",
362 "e6cba23dbf4ef84fe35e884f017f4e24dc228572",
363 "c8cf3462c7d8feba716deeb2ebe6583bd54589e2",
364 "39c16b9834c2d665ac5f68ad91dc5b933bad8549",
365 "f3c582b1397df6a664224ebbaf9d4cc952706597",
366 "29cec67037fe8e89977474988219016ae7f342a6",
367 "36c4cd238bf8e82e27b740e0741b025f2e8c79ab",
368 "f1c45a47c02e01d5a9a326f1d9f7f756373387f8",
369 "4aca84406f5daee34ab513a60717c8d7b1763ead",
370 "84ce167da452552f63ed8407b55d5ece4901845f",
371 NULL
372 };
373
374 #define MESSAGES_COUNT (sizeof(messages)/sizeof(messages[0])) - 1
375
376 /* Test that we can read a note */
377 void test_notes_notes__can_read_a_note(void)
378 {
379 git_oid note_oid, target_oid;
380 git_note *note;
381
382 create_note(&note_oid, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 4a20\n");
383
384 cl_git_pass(git_oid_fromstr(&target_oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
385
386 cl_git_pass(git_note_read(&note, _repo, "refs/notes/i-can-see-dead-notes", &target_oid));
387
388 cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
389
390 git_note_free(note);
391 }
392
393 /* Test that we can read a note with from commit api */
394 void test_notes_notes__can_read_a_note_from_a_commit(void)
395 {
396 git_oid oid, notes_commit_oid;
397 git_commit *notes_commit;
398 git_note *note;
399
400 cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
401 cl_git_pass(git_note_commit_create(&notes_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
402 cl_git_pass(git_commit_lookup(&notes_commit, _repo, &notes_commit_oid));
403 cl_assert(notes_commit);
404
405 cl_git_pass(git_note_commit_read(&note, _repo, notes_commit, &oid));
406 cl_assert_equal_s(git_note_message(note), "I decorate 4a20\n");
407
408 git_commit_free(notes_commit);
409 git_note_free(note);
410 }
411
412 /* Test that we can read a commit with no note fails */
413 void test_notes_notes__attempt_to_read_a_note_from_a_commit_with_no_note_fails(void)
414 {
415 git_oid oid, notes_commit_oid;
416 git_commit *notes_commit;
417 git_note *note;
418
419 cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
420
421 cl_git_pass(git_note_commit_create(&notes_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 1));
422
423 git_commit_lookup(&notes_commit, _repo, &notes_commit_oid);
424
425 cl_git_pass(git_note_commit_remove(&notes_commit_oid, _repo, notes_commit, _sig, _sig, &oid));
426 git_commit_free(notes_commit);
427
428 git_commit_lookup(&notes_commit, _repo, &notes_commit_oid);
429
430 cl_assert(notes_commit);
431
432 cl_git_fail_with(GIT_ENOTFOUND, git_note_commit_read(&note, _repo, notes_commit, &oid));
433
434 git_commit_free(notes_commit);
435 }
436
437 /*
438 * $ git ls-tree refs/notes/fanout
439 * 040000 tree 4b22b35d44b5a4f589edf3dc89196399771796ea 84
440 *
441 * $ git ls-tree 4b22b35
442 * 040000 tree d71aab4f9b04b45ce09bcaa636a9be6231474759 96
443 *
444 * $ git ls-tree d71aab4
445 * 100644 blob 08b041783f40edfe12bb406c9c9a8a040177c125 071c1b46c854b31185ea97743be6a8774479
446 */
447 void test_notes_notes__can_insert_a_note_in_an_existing_fanout(void)
448 {
449 size_t i;
450 git_oid note_oid, target_oid;
451 git_note *_note;
452
453 cl_git_pass(git_oid_fromstr(&target_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
454
455 for (i = 0; i < MESSAGES_COUNT; i++) {
456 cl_git_pass(git_note_create(&note_oid, _repo, "refs/notes/fanout", _sig, _sig, &target_oid, messages[i], 0));
457 cl_git_pass(git_note_read(&_note, _repo, "refs/notes/fanout", &target_oid));
458 git_note_free(_note);
459
460 git_oid_cpy(&target_oid, &note_oid);
461 }
462 }
463
464 /*
465 * $ git notes --ref fanout list 8496071c1b46c854b31185ea97743be6a8774479
466 * 08b041783f40edfe12bb406c9c9a8a040177c125
467 */
468 void test_notes_notes__can_read_a_note_in_an_existing_fanout(void)
469 {
470 git_oid note_oid, target_oid;
471 git_note *note;
472
473 cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
474 cl_git_pass(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
475
476 cl_git_pass(git_oid_fromstr(&note_oid, "08b041783f40edfe12bb406c9c9a8a040177c125"));
477 cl_assert_equal_oid(git_note_id(note), &note_oid);
478
479 git_note_free(note);
480 }
481
482 /* Can remove a note */
483 void test_notes_notes__can_remove_a_note(void)
484 {
485 git_oid note_oid, target_oid;
486 git_note *note;
487
488 create_note(&note_oid, "refs/notes/i-can-see-dead-notes", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", "I decorate 4a20\n");
489
490 cl_git_pass(git_oid_fromstr(&target_oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
491 cl_git_pass(git_note_remove(_repo, "refs/notes/i-can-see-dead-notes", _sig, _sig, &target_oid));
492
493 cl_git_fail(git_note_read(&note, _repo, "refs/notes/i-can-see-dead-notes", &target_oid));
494 }
495
496 /* Can remove a note from a commit */
497 void test_notes_notes__can_remove_a_note_from_commit(void)
498 {
499 git_oid oid, notes_commit_oid;
500 git_note *note = NULL;
501 git_commit *existing_notes_commit;
502 git_reference *ref;
503
504 cl_git_pass(git_oid_fromstr(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
505
506 cl_git_pass(git_note_commit_create(&notes_commit_oid, NULL, _repo, NULL, _sig, _sig, &oid, "I decorate 4a20\n", 0));
507
508 cl_git_pass(git_commit_lookup(&existing_notes_commit, _repo, &notes_commit_oid));
509
510 cl_assert(existing_notes_commit);
511
512 cl_git_pass(git_note_commit_remove(&notes_commit_oid, _repo, existing_notes_commit, _sig, _sig, &oid));
513
514 /* remove_from_commit will not update any ref,
515 * so we must manually create the ref, that points to the commit */
516 cl_git_pass(git_reference_create(&ref, _repo, "refs/notes/i-can-see-dead-notes", &notes_commit_oid, 0, NULL));
517
518 cl_git_fail(git_note_read(&note, _repo, "refs/notes/i-can-see-dead-notes", &oid));
519
520 git_commit_free(existing_notes_commit);
521 git_reference_free(ref);
522 git_note_free(note);
523 }
524
525
526 void test_notes_notes__can_remove_a_note_in_an_existing_fanout(void)
527 {
528 git_oid target_oid;
529 git_note *note;
530
531 cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
532 cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
533
534 cl_git_fail(git_note_read(&note, _repo, "refs/notes/fanout", &target_oid));
535 }
536
537 void test_notes_notes__removing_a_note_which_doesnt_exists_returns_ENOTFOUND(void)
538 {
539 int error;
540 git_oid target_oid;
541
542 cl_git_pass(git_oid_fromstr(&target_oid, "8496071c1b46c854b31185ea97743be6a8774479"));
543 cl_git_pass(git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid));
544
545 error = git_note_remove(_repo, "refs/notes/fanout", _sig, _sig, &target_oid);
546 cl_git_fail(error);
547 cl_assert_equal_i(GIT_ENOTFOUND, error);
548 }
549
550 void test_notes_notes__can_iterate_default_namespace(void)
551 {
552 git_note_iterator *iter;
553 git_note *note;
554 git_oid note_id, annotated_id;
555 git_oid note_created[2];
556 const char* note_message[] = {
557 "I decorate a65f\n",
558 "I decorate c478\n"
559 };
560 int i, err;
561
562 create_note(&note_created[0], "refs/notes/commits",
563 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
564 create_note(&note_created[1], "refs/notes/commits",
565 "c47800c7266a2be04c571c04d5a6614691ea99bd", note_message[1]);
566
567 cl_git_pass(git_note_iterator_new(&iter, _repo, NULL));
568
569 for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
570 cl_git_pass(git_note_read(&note, _repo, NULL, &annotated_id));
571 cl_assert_equal_s(git_note_message(note), note_message[i]);
572 git_note_free(note);
573 }
574
575 cl_assert_equal_i(GIT_ITEROVER, err);
576 cl_assert_equal_i(2, i);
577 git_note_iterator_free(iter);
578 }
579
580 void test_notes_notes__can_iterate_custom_namespace(void)
581 {
582 git_note_iterator *iter;
583 git_note *note;
584 git_oid note_id, annotated_id;
585 git_oid note_created[2];
586 const char* note_message[] = {
587 "I decorate a65f\n",
588 "I decorate c478\n"
589 };
590 int i, err;
591
592 create_note(&note_created[0], "refs/notes/beer",
593 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", note_message[0]);
594 create_note(&note_created[1], "refs/notes/beer",
595 "c47800c7266a2be04c571c04d5a6614691ea99bd", note_message[1]);
596
597 cl_git_pass(git_note_iterator_new(&iter, _repo, "refs/notes/beer"));
598
599 for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
600 cl_git_pass(git_note_read(&note, _repo, "refs/notes/beer", &annotated_id));
601 cl_assert_equal_s(git_note_message(note), note_message[i]);
602 git_note_free(note);
603 }
604
605 cl_assert_equal_i(GIT_ITEROVER, err);
606 cl_assert_equal_i(2, i);
607 git_note_iterator_free(iter);
608 }
609
610 void test_notes_notes__empty_iterate(void)
611 {
612 git_note_iterator *iter;
613
614 cl_git_fail(git_note_iterator_new(&iter, _repo, "refs/notes/commits"));
615 }
616
617 void test_notes_notes__iterate_from_commit(void)
618 {
619 git_note_iterator *iter;
620 git_note *note;
621 git_oid note_id, annotated_id;
622 git_oid oids[2];
623 git_oid notes_commit_oids[2];
624 git_commit *notes_commits[2];
625 const char* note_message[] = {
626 "I decorate a65f\n",
627 "I decorate c478\n"
628 };
629 int i, err;
630
631 cl_git_pass(git_oid_fromstr(&(oids[0]), "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
632 cl_git_pass(git_oid_fromstr(&(oids[1]), "c47800c7266a2be04c571c04d5a6614691ea99bd"));
633
634 cl_git_pass(git_note_commit_create(&notes_commit_oids[0], NULL, _repo, NULL, _sig, _sig, &(oids[0]), note_message[0], 0));
635
636 git_commit_lookup(&notes_commits[0], _repo, &notes_commit_oids[0]);
637 cl_assert(notes_commits[0]);
638
639 cl_git_pass(git_note_commit_create(&notes_commit_oids[1], NULL, _repo, notes_commits[0], _sig, _sig, &(oids[1]), note_message[1], 0));
640
641 git_commit_lookup(&notes_commits[1], _repo, &notes_commit_oids[1]);
642 cl_assert(notes_commits[1]);
643
644 cl_git_pass(git_note_commit_iterator_new(&iter, notes_commits[1]));
645
646 for (i = 0; (err = git_note_next(&note_id, &annotated_id, iter)) >= 0; ++i) {
647 cl_git_pass(git_note_commit_read(&note, _repo, notes_commits[1], &annotated_id));
648 cl_assert_equal_s(git_note_message(note), note_message[i]);
649 git_note_free(note);
650 }
651
652 cl_assert_equal_i(GIT_ITEROVER, err);
653 cl_assert_equal_i(2, i);
654
655 git_note_iterator_free(iter);
656 git_commit_free(notes_commits[0]);
657 git_commit_free(notes_commits[1]);
658 }