22#include "repository.h"
33#include "git2/sys/repository.h"
44#include "mailmap_helpers.h"
5+ #include "buf_text.h"
56
67static git_repository * g_repo ;
78static git_mailmap * g_mailmap ;
9+ static git_config * g_config ;
810
911void test_mailmap_parsing__initialize (void )
1012{
1113 g_repo = NULL ;
1214 g_mailmap = NULL ;
15+ g_config = NULL ;
1316}
1417
1518void test_mailmap_parsing__cleanup (void )
1619{
1720 git_mailmap_free (g_mailmap );
21+ git_config_free (g_config );
1822 cl_git_sandbox_cleanup ();
1923}
2024
2125static void check_mailmap_entries (
2226 const git_mailmap * mailmap , const mailmap_entry * entries , size_t entries_size )
2327{
24- const git_mailmap_entry * parsed = NULL ;
28+ const git_mailmap_entry * parsed ;
2529 size_t idx ;
2630
27- /* Check that the parsed entries match */
28- cl_assert_equal_sz (entries_size , git_mailmap_entry_count (mailmap ));
31+ /* Check the correct # of entries were parsed */
32+ cl_assert_equal_sz (entries_size , git_vector_length (& mailmap -> entries ));
33+
34+ /* Make sure looking up each entry succeeds */
2935 for (idx = 0 ; idx < entries_size ; ++ idx ) {
30- parsed = git_mailmap_entry_byindex (mailmap , idx );
36+ parsed = git_mailmap_entry_lookup (
37+ mailmap , entries [idx ].replace_name , entries [idx ].replace_email );
38+
39+ cl_assert (parsed );
3140 cl_assert_equal_s (parsed -> real_name , entries [idx ].real_name );
3241 cl_assert_equal_s (parsed -> real_email , entries [idx ].real_email );
3342 cl_assert_equal_s (parsed -> replace_name , entries [idx ].replace_name );
@@ -52,6 +61,10 @@ static void check_mailmap_resolve(
5261 }
5362}
5463
64+ static const mailmap_entry resolved_untracked [] = {
65+ { "Untracked" , "untracked@company.xx" , "xx" , "untracked@company.xx" }
66+ };
67+
5568void test_mailmap_parsing__string (void )
5669{
5770 git_buf buf = GIT_BUF_INIT ;
@@ -77,7 +90,7 @@ void test_mailmap_parsing__windows_string(void)
7790 git_buf_text_lf_to_crlf (& winbuf , & unixbuf );
7891
7992 cl_git_pass (git_mailmap_from_buffer (& g_mailmap , & winbuf ));
80- git_buf_free (winbuf );
93+ git_buf_free (& winbuf );
8194
8295 /* We should have parsed all of the entries */
8396 check_mailmap_entries (g_mailmap , entries , ARRAY_SIZE (entries ));
@@ -93,7 +106,7 @@ void test_mailmap_parsing__fromrepo(void)
93106 g_repo = cl_git_sandbox_init ("mailmap" );
94107 cl_check (!git_repository_is_bare (g_repo ));
95108
96- cl_git_pass (git_mailmap_from_repo (& g_mailmap , g_repo ));
109+ cl_git_pass (git_mailmap_from_repository (& g_mailmap , g_repo ));
97110
98111 /* We should have parsed all of the entries */
99112 check_mailmap_entries (g_mailmap , entries , ARRAY_SIZE (entries ));
@@ -104,13 +117,17 @@ void test_mailmap_parsing__fromrepo(void)
104117 g_mailmap , resolved_untracked , ARRAY_SIZE (resolved_untracked ));
105118}
106119
120+ static const mailmap_entry resolved_bare [] = {
121+ { "xx" , "untracked@company.xx" , "xx" , "untracked@company.xx" }
122+ };
123+
107124void test_mailmap_parsing__frombare (void )
108125{
109126 g_repo = cl_git_sandbox_init ("mailmap/.gitted" );
110127 cl_git_pass (git_repository_set_bare (g_repo ));
111128 cl_check (git_repository_is_bare (g_repo ));
112129
113- cl_git_pass (git_mailmap_from_repo (& g_mailmap , g_repo ));
130+ cl_git_pass (git_mailmap_from_repository (& g_mailmap , g_repo ));
114131
115132 /* We should have parsed all of the entries, except for the untracked one */
116133 check_mailmap_entries (g_mailmap , entries , ARRAY_SIZE (entries ) - 1 );
@@ -120,3 +137,112 @@ void test_mailmap_parsing__frombare(void)
120137 check_mailmap_resolve (
121138 g_mailmap , resolved_bare , ARRAY_SIZE (resolved_bare ));
122139}
140+
141+ static const mailmap_entry resolved_with_file_override [] = {
142+ { "Brad" , "cto@company.xx" , "Brad" , "cto@coompany.xx" },
143+ { "Brad L" , "cto@company.xx" , "Brad L" , "cto@coompany.xx" },
144+ { "Some Dude" , "some@dude.xx" , "nick1" , "bugs@company.xx" },
145+ { "Other Author" , "other@author.xx" , "nick2" , "bugs@company.xx" },
146+ { "nick3" , "bugs@company.xx" , "nick3" , "bugs@company.xx" },
147+ { "Other Author" , "other@author.xx" , "Some Garbage" , "nick2@company.xx" },
148+ { "Joseph" , "joseph@company.xx" , "Joseph" , "bugs@company.xx" },
149+ { "Santa Claus" , "santa.claus@northpole.xx" , "Clause" , "me@company.xx" },
150+ { "Charles" , "charles@charles.xx" , "Charles" , "charles@charles.xx" },
151+
152+ /* This name is overridden by file_override */
153+ { "File Override" , "phil@company.xx" , "unknown" , "phil@company.xx" },
154+ { "Other Name" , "fileoverridename@company.xx" , "override" , "fileoverridename@company.xx" }
155+ };
156+
157+ void test_mailmap_parsing__file_config (void )
158+ {
159+ g_repo = cl_git_sandbox_init ("mailmap" );
160+ cl_git_pass (git_repository_config (& g_config , g_repo ));
161+
162+ cl_git_pass (git_config_set_string (
163+ g_config , "mailmap.file" , cl_fixture ("mailmap/file_override" )));
164+
165+ cl_git_pass (git_mailmap_from_repository (& g_mailmap , g_repo ));
166+
167+ /* Check we don't have duplicate entries */
168+ cl_assert_equal_sz (git_vector_length (& g_mailmap -> entries ), 9 );
169+
170+ /* Check that resolving the entries works */
171+ check_mailmap_resolve (
172+ g_mailmap , resolved_with_file_override ,
173+ ARRAY_SIZE (resolved_with_file_override ));
174+ }
175+
176+ static const mailmap_entry resolved_with_blob_override [] = {
177+ { "Brad" , "cto@company.xx" , "Brad" , "cto@coompany.xx" },
178+ { "Brad L" , "cto@company.xx" , "Brad L" , "cto@coompany.xx" },
179+ { "Some Dude" , "some@dude.xx" , "nick1" , "bugs@company.xx" },
180+ { "Other Author" , "other@author.xx" , "nick2" , "bugs@company.xx" },
181+ { "nick3" , "bugs@company.xx" , "nick3" , "bugs@company.xx" },
182+ { "Other Author" , "other@author.xx" , "Some Garbage" , "nick2@company.xx" },
183+ { "Joseph" , "joseph@company.xx" , "Joseph" , "bugs@company.xx" },
184+ { "Santa Claus" , "santa.claus@northpole.xx" , "Clause" , "me@company.xx" },
185+ { "Charles" , "charles@charles.xx" , "Charles" , "charles@charles.xx" },
186+
187+ /* This name is overridden by blob_override */
188+ { "Blob Override" , "phil@company.xx" , "unknown" , "phil@company.xx" },
189+ { "Other Name" , "bloboverridename@company.xx" , "override" , "bloboverridename@company.xx" }
190+ };
191+
192+ void test_mailmap_parsing__blob_config (void )
193+ {
194+ g_repo = cl_git_sandbox_init ("mailmap" );
195+ cl_git_pass (git_repository_config (& g_config , g_repo ));
196+
197+ cl_git_pass (git_config_set_string (
198+ g_config , "mailmap.blob" , "HEAD:blob_override" ));
199+
200+ cl_git_pass (git_mailmap_from_repository (& g_mailmap , g_repo ));
201+
202+ /* Check we don't have duplicate entries */
203+ cl_assert_equal_sz (git_vector_length (& g_mailmap -> entries ), 9 );
204+
205+ /* Check that resolving the entries works */
206+ check_mailmap_resolve (
207+ g_mailmap , resolved_with_blob_override ,
208+ ARRAY_SIZE (resolved_with_blob_override ));
209+ }
210+
211+ static const mailmap_entry bare_resolved_with_blob_override [] = {
212+ /* As mailmap.blob is set, we won't load HEAD:.mailmap */
213+ { "Brad" , "cto@coompany.xx" , "Brad" , "cto@coompany.xx" },
214+ { "Brad L" , "cto@coompany.xx" , "Brad L" , "cto@coompany.xx" },
215+ { "nick1" , "bugs@company.xx" , "nick1" , "bugs@company.xx" },
216+ { "nick2" , "bugs@company.xx" , "nick2" , "bugs@company.xx" },
217+ { "nick3" , "bugs@company.xx" , "nick3" , "bugs@company.xx" },
218+ { "Some Garbage" , "nick2@company.xx" , "Some Garbage" , "nick2@company.xx" },
219+ { "Joseph" , "bugs@company.xx" , "Joseph" , "bugs@company.xx" },
220+ { "Clause" , "me@company.xx" , "Clause" , "me@company.xx" },
221+ { "Charles" , "charles@charles.xx" , "Charles" , "charles@charles.xx" },
222+
223+ /* This name is overridden by blob_override */
224+ { "Blob Override" , "phil@company.xx" , "unknown" , "phil@company.xx" },
225+ { "Other Name" , "bloboverridename@company.xx" , "override" , "bloboverridename@company.xx" }
226+ };
227+
228+ void test_mailmap_parsing__bare_blob_config (void )
229+ {
230+ g_repo = cl_git_sandbox_init ("mailmap/.gitted" );
231+ cl_git_pass (git_repository_set_bare (g_repo ));
232+ cl_check (git_repository_is_bare (g_repo ));
233+
234+ cl_git_pass (git_repository_config (& g_config , g_repo ));
235+
236+ cl_git_pass (git_config_set_string (
237+ g_config , "mailmap.blob" , "HEAD:blob_override" ));
238+
239+ cl_git_pass (git_mailmap_from_repository (& g_mailmap , g_repo ));
240+
241+ /* Check that we only have the 2 entries */
242+ cl_assert_equal_sz (git_vector_length (& g_mailmap -> entries ), 2 );
243+
244+ /* Check that resolving the entries works */
245+ check_mailmap_resolve (
246+ g_mailmap , bare_resolved_with_blob_override ,
247+ ARRAY_SIZE (bare_resolved_with_blob_override ));
248+ }
0 commit comments