@@ -73,6 +73,128 @@ static int note_list_cb(
7373 return 0 ;
7474}
7575
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+
76198/*
77199 * $ git notes --ref i-can-see-dead-notes add -m "I decorate a65f" a65fedf39aefe402d3bb6e24df4d4f5fe4547750
78200 * $ git notes --ref i-can-see-dead-notes add -m "I decorate c478" c47800c7266a2be04c571c04d5a6614691ea99bd
0 commit comments