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