Commit 56303e1ade453648230115cdaaba8244273f3315

Nika Layzell 2018-05-07T11:59:00

mailmap: API and style cleanup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
diff --git a/include/git2/mailmap.h b/include/git2/mailmap.h
index 761db67..b22a00c 100644
--- a/include/git2/mailmap.h
+++ b/include/git2/mailmap.h
@@ -58,9 +58,11 @@ GIT_EXTERN(int) git_mailmap_add_entry(
  *
  * @param mm mailmap to add the entries to
  * @param buf the buffer to read the mailmap file from
+ * @param len the length of the input buffer [optional: 0 defaults to 'strlen']
  * @return 0 on success, or an error code
  */
-GIT_EXTERN(int) git_mailmap_add_buffer(git_mailmap *mm, const git_buf *buf);
+GIT_EXTERN(int) git_mailmap_add_buffer(
+	git_mailmap *mm, const char *buf, size_t len);
 
 /**
  * Create a new mailmap instance containing a single mailmap file
@@ -72,9 +74,11 @@ GIT_EXTERN(int) git_mailmap_add_buffer(git_mailmap *mm, const git_buf *buf);
  *
  * @param out pointer to store the new mailmap
  * @param buf buffer to parse the mailmap from
+ * @param len the length of the input buffer [optional: 0 defaults to 'strlen']
  * @return 0 on success, or an error code
  */
-GIT_EXTERN(int) git_mailmap_from_buffer(git_mailmap **out, const git_buf *buf);
+GIT_EXTERN(int) git_mailmap_from_buffer(
+	git_mailmap **out, const char *buf, size_t len);
 
 /**
  * Create a new mailmap instance from a repository, loading mailmap files based
@@ -109,6 +113,19 @@ GIT_EXTERN(int) git_mailmap_resolve(
 	const char **real_name, const char **real_email,
 	const git_mailmap *mm, const char *name, const char *email);
 
+/**
+ * Resolve a signature to use real names and emails with a mailmap.
+ *
+ * Call `git_signature_free()` to free the data.
+ *
+ * @param out new signature
+ * @param mm mailmap to resolve with
+ * @param sig signature to resolve
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_mailmap_resolve_signature(
+	git_signature **out, const git_mailmap *mm, const git_signature *sig);
+
 /** @} */
 GIT_END_DECL
 #endif
diff --git a/include/git2/signature.h b/include/git2/signature.h
index 981a4f1..7a2a023 100644
--- a/include/git2/signature.h
+++ b/include/git2/signature.h
@@ -76,18 +76,6 @@ GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo)
 GIT_EXTERN(int) git_signature_from_buffer(git_signature **out, const char *buf);
 
 /**
- * Create a signature with names updated respecting the mailmap.
- *
- * Call `git_signature_free()` to free the data.
- *
- * @param out new signature
- * @param sig signature to resolve
- * @param mailmap mailmap to resolve with
- * @return 0 or an error code
- */
-GIT_EXTERN(int) git_signature_with_mailmap(git_signature **out, const git_signature *sig, const git_mailmap *mailmap);
-
-/**
  * Create a copy of an existing signature.  All internal strings are also
  * duplicated.
  *
diff --git a/include/git2/types.h b/include/git2/types.h
index 943a7e9..607a62a 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -434,7 +434,7 @@ struct git_writestream {
 	void (*free)(git_writestream *stream);
 };
 
-/** A parsed representation of a .mailmap file. */
+/** Representation of .mailmap file state. */
 typedef struct git_mailmap git_mailmap;
 
 /** @} */
diff --git a/src/commit.c b/src/commit.c
index 03ecea8..e0ba51d 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -11,6 +11,7 @@
 #include "git2/object.h"
 #include "git2/repository.h"
 #include "git2/signature.h"
+#include "git2/mailmap.h"
 #include "git2/sys/commit.h"
 
 #include "odb.h"
@@ -893,11 +894,11 @@ cleanup:
 int git_commit_committer_with_mailmap(
 	git_signature **out, const git_commit *commit, const git_mailmap *mailmap)
 {
-	return git_signature_with_mailmap(out, commit->committer, mailmap);
+	return git_mailmap_resolve_signature(out, mailmap, commit->committer);
 }
 
 int git_commit_author_with_mailmap(
 	git_signature **out, const git_commit *commit, const git_mailmap *mailmap)
 {
-	return git_signature_with_mailmap(out, commit->author, mailmap);
+	return git_mailmap_resolve_signature(out, mailmap, commit->author);
 }
diff --git a/src/mailmap.c b/src/mailmap.c
index 3da46a8..bfee489 100644
--- a/src/mailmap.c
+++ b/src/mailmap.c
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "path.h"
 #include "repository.h"
+#include "signature.h"
 #include "git2/config.h"
 #include "git2/revparse.h"
 #include "blob.h"
@@ -171,9 +172,12 @@ void git_mailmap_free(git_mailmap *mm)
 	git__free(mm);
 }
 
-int git_mailmap_add_entry(
-	git_mailmap *mm, const char *real_name, const char *real_email,
-	const char *replace_name, const char *replace_email)
+static int mailmap_add_entry_unterminated(
+	git_mailmap *mm,
+	const char *real_name, size_t real_name_size,
+	const char *real_email, size_t real_email_size,
+	const char *replace_name, size_t replace_name_size,
+	const char *replace_email, size_t replace_email_size)
 {
 	int error;
 	git_mailmap_entry *entry = git__calloc(1, sizeof(git_mailmap_entry));
@@ -181,19 +185,19 @@ int git_mailmap_add_entry(
 
 	assert(mm && replace_email && *replace_email);
 
-	if (real_name && *real_name) {
-		entry->real_name = git__strdup(real_name);
+	if (real_name_size > 0) {
+		entry->real_name = git__substrdup(real_name, real_name_size);
 		GITERR_CHECK_ALLOC(entry->real_name);
 	}
-	if (real_email && *real_email) {
-		entry->real_email = git__strdup(real_email);
+	if (real_email_size > 0) {
+		entry->real_email = git__substrdup(real_email, real_email_size);
 		GITERR_CHECK_ALLOC(entry->real_email);
 	}
-	if (replace_name && *replace_name) {
-		entry->replace_name = git__strdup(replace_name);
+	if (replace_name_size > 0) {
+		entry->replace_name = git__substrdup(replace_name, replace_name_size);
 		GITERR_CHECK_ALLOC(entry->replace_name);
 	}
-	entry->replace_email = git__strdup(replace_email);
+	entry->replace_email = git__substrdup(replace_email, replace_email_size);
 	GITERR_CHECK_ALLOC(entry->replace_email);
 
 	error = git_vector_insert_sorted(&mm->entries, entry, mailmap_entry_replace);
@@ -203,10 +207,21 @@ int git_mailmap_add_entry(
 	return error;
 }
 
-int git_mailmap_add_buffer(git_mailmap *mm, const git_buf *buf)
+int git_mailmap_add_entry(
+	git_mailmap *mm, const char *real_name, const char *real_email,
+	const char *replace_name, const char *replace_email)
+{
+	return mailmap_add_entry_unterminated(
+		mm,
+		real_name, real_name ? strlen(real_name) : 0,
+		real_email, real_email ? strlen(real_email) : 0,
+		replace_name, replace_name ? strlen(replace_name) : 0,
+		replace_email, strlen(replace_email));
+}
+
+int git_mailmap_add_buffer(git_mailmap *mm, const char *buf, size_t len)
 {
 	int error;
-	git_mailmap_entry *entry = NULL;
 	git_parse_ctx ctx;
 
 	/* Scratch buffers containing the real parsed names & emails */
@@ -215,10 +230,15 @@ int git_mailmap_add_buffer(git_mailmap *mm, const git_buf *buf)
 	git_buf replace_name = GIT_BUF_INIT;
 	git_buf replace_email = GIT_BUF_INIT;
 
-	if (git_buf_contains_nul(buf))
+	/* If `len` is passed as 0, use strlen to get the real length */
+	if (buf && len == 0)
+		len = strlen(buf);
+
+	/* Buffers may not contain '\0's. */
+	if (memchr(buf, '\0', len) != NULL)
 		return -1;
 
-	git_parse_ctx_init(&ctx, buf->ptr, buf->size);
+	git_parse_ctx_init(&ctx, buf, len);
 
 	/* Run the parser */
 	while (ctx.remain_len > 0) {
@@ -230,37 +250,17 @@ int git_mailmap_add_buffer(git_mailmap *mm, const git_buf *buf)
 			continue; /* TODO: warn */
 		}
 
-		entry = git__calloc(1, sizeof(git_mailmap_entry));
-		GITERR_CHECK_ALLOC(entry);
-
-		if (real_name.size > 0) {
-			entry->real_name = git__substrdup(real_name.ptr, real_name.size);
-			GITERR_CHECK_ALLOC(entry->real_name);
-		}
-		if (real_email.size > 0) {
-			entry->real_email = git__substrdup(real_email.ptr, real_email.size);
-			GITERR_CHECK_ALLOC(entry->real_email);
-		}
-		if (replace_name.size > 0) {
-			entry->replace_name = git__substrdup(replace_name.ptr, replace_name.size);
-			GITERR_CHECK_ALLOC(entry->replace_name);
-		}
-		entry->replace_email = git__substrdup(replace_email.ptr, replace_email.size);
-		GITERR_CHECK_ALLOC(entry->replace_email);
-
-		error = git_vector_insert_sorted(
-			&mm->entries, entry, mailmap_entry_replace);
+		/* NOTE: Can't use add_entry(...) as our buffers aren't terminated */
+		error = mailmap_add_entry_unterminated(
+			mm, real_name.ptr, real_name.size, real_email.ptr, real_email.size,
+			replace_name.ptr, replace_name.size, replace_email.ptr, replace_email.size);
 		if (error < 0 && error != GIT_EEXISTS)
 			goto cleanup;
 
-		entry = NULL;
 		error = 0;
 	}
 
 cleanup:
-	mailmap_entry_free(entry);
-
-	/* We never allocate data in these buffers, but better safe than sorry */
 	git_buf_free(&real_name);
 	git_buf_free(&real_email);
 	git_buf_free(&replace_name);
@@ -268,13 +268,13 @@ cleanup:
 	return error;
 }
 
-int git_mailmap_from_buffer(git_mailmap **out, const git_buf *buffer)
+int git_mailmap_from_buffer(git_mailmap **out, const char *data, size_t len)
 {
 	int error = git_mailmap_new(out);
 	if (error < 0)
 		return error;
 
-	error = git_mailmap_add_buffer(*out, buffer);
+	error = git_mailmap_add_buffer(*out, data, len);
 	if (error < 0) {
 		git_mailmap_free(*out);
 		*out = NULL;
@@ -283,7 +283,7 @@ int git_mailmap_from_buffer(git_mailmap **out, const git_buf *buffer)
 }
 
 static int mailmap_add_blob(
-	git_mailmap *mm, git_repository *repo, const char *spec)
+	git_mailmap *mm, git_repository *repo, const char *rev)
 {
 	git_object *object = NULL;
 	git_blob *blob = NULL;
@@ -292,7 +292,7 @@ static int mailmap_add_blob(
 
 	assert(mm && repo);
 
-	error = git_revparse_single(&object, repo, spec);
+	error = git_revparse_single(&object, repo, rev);
 	if (error < 0)
 		goto cleanup;
 
@@ -304,7 +304,7 @@ static int mailmap_add_blob(
 	if (error < 0)
 		goto cleanup;
 
-	error = git_mailmap_add_buffer(mm, &content);
+	error = git_mailmap_add_buffer(mm, content.ptr, content.size);
 	if (error < 0)
 		goto cleanup;
 
@@ -331,7 +331,7 @@ static int mailmap_add_file_ondisk(
 	if (error < 0)
 		goto cleanup;
 
-	error = git_mailmap_add_buffer(mm, &content);
+	error = git_mailmap_add_buffer(mm, content.ptr, content.size);
 	if (error < 0)
 		goto cleanup;
 
@@ -345,21 +345,21 @@ cleanup:
 static void mailmap_add_from_repository(git_mailmap *mm, git_repository *repo)
 {
 	git_config *config = NULL;
-	git_buf spec_buf = GIT_BUF_INIT;
+	git_buf rev_buf = GIT_BUF_INIT;
 	git_buf path_buf = GIT_BUF_INIT;
-	const char *spec = NULL;
+	const char *rev = NULL;
 	const char *path = NULL;
 
 	assert(mm && repo);
 
 	/* If we're in a bare repo, default blob to 'HEAD:.mailmap' */
 	if (repo->is_bare)
-		spec = MM_BLOB_DEFAULT;
+		rev = MM_BLOB_DEFAULT;
 
 	/* Try to load 'mailmap.file' and 'mailmap.blob' cfgs from the repo */
 	if (git_repository_config(&config, repo) == 0) {
-		if (git_config_get_string_buf(&spec_buf, config, MM_BLOB_CONFIG) == 0)
-			spec = spec_buf.ptr;
+		if (git_config_get_string_buf(&rev_buf, config, MM_BLOB_CONFIG) == 0)
+			rev = rev_buf.ptr;
 		if (git_config_get_path(&path_buf, config, MM_FILE_CONFIG) == 0)
 			path = path_buf.ptr;
 	}
@@ -377,12 +377,12 @@ static void mailmap_add_from_repository(git_mailmap *mm, git_repository *repo)
 	 */
 	if (!repo->is_bare)
 		mailmap_add_file_ondisk(mm, MM_FILE, repo);
-	if (spec != NULL)
-		mailmap_add_blob(mm, repo, spec);
+	if (rev != NULL)
+		mailmap_add_blob(mm, repo, rev);
 	if (path != NULL)
 		mailmap_add_file_ondisk(mm, path, repo);
 
-	git_buf_free(&spec_buf);
+	git_buf_free(&rev_buf);
 	git_buf_free(&path_buf);
 	git_config_free(config);
 }
@@ -403,7 +403,10 @@ const git_mailmap_entry *git_mailmap_entry_lookup(
 	ssize_t fallback = -1;
 	size_t idx;
 	git_mailmap_entry *entry;
-	git_mailmap_entry needle = { NULL, NULL, NULL, (char *)email };
+
+	/* The lookup needle we want to use only sets the replace_email. */
+	git_mailmap_entry needle = { NULL };
+	needle.replace_email = (char *)email;
 
 	assert(email);
 
@@ -457,3 +460,26 @@ int git_mailmap_resolve(
 	}
 	return 0;
 }
+
+int git_mailmap_resolve_signature(
+	git_signature **out, const git_mailmap *mailmap, const git_signature *sig)
+{
+	const char *name = NULL;
+	const char *email = NULL;
+	int error;
+
+	if (!sig)
+		return 0;
+
+	error = git_mailmap_resolve(&name, &email, mailmap, sig->name, sig->email);
+	if (error < 0)
+		return error;
+
+	error = git_signature_new(out, name, email, sig->when.time, sig->when.offset);
+	if (error < 0)
+		return error;
+
+	/* Copy over the sign, as git_signature_new doesn't let you pass it. */
+	(*out)->when.sign = sig->when.sign;
+	return 0;
+}
diff --git a/src/signature.c b/src/signature.c
index a8177e0..cd68523 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -9,7 +9,6 @@
 
 #include "repository.h"
 #include "git2/common.h"
-#include "git2/mailmap.h"
 #include "posix.h"
 
 void git_signature_free(git_signature *sig)
@@ -99,35 +98,18 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema
 
 int git_signature_dup(git_signature **dest, const git_signature *source)
 {
-	/* If mailmap is NULL, this method just copies the signature */
-	return git_signature_with_mailmap(dest, source, NULL);
-}
-
-int git_signature_with_mailmap(
-	git_signature **dest,
-	const git_signature *source,
-	const git_mailmap *mailmap)
-{
-	git_signature *signature = NULL;
-	const char *name = NULL;
-	const char *email = NULL;
-	int error;
+	git_signature *signature;
 
 	if (source == NULL)
 		return 0;
 
-	error = git_mailmap_resolve(
-		&name, &email, mailmap, source->name, source->email);
-	if (error < 0)
-		return error;
-
 	signature = git__calloc(1, sizeof(git_signature));
 	GITERR_CHECK_ALLOC(signature);
 
-	signature->name = git__strdup(name);
+	signature->name = git__strdup(source->name);
 	GITERR_CHECK_ALLOC(signature->name);
 
-	signature->email = git__strdup(email);
+	signature->email = git__strdup(source->email);
 	GITERR_CHECK_ALLOC(signature->email);
 
 	signature->when.time = source->when.time;
diff --git a/tests/mailmap/basic.c b/tests/mailmap/basic.c
index b8a9132..20fa8be 100644
--- a/tests/mailmap/basic.c
+++ b/tests/mailmap/basic.c
@@ -27,10 +27,7 @@ struct {
 
 void test_mailmap_basic__initialize(void)
 {
-	git_buf buf = GIT_BUF_INIT;
-	git_buf_attach_notowned(&buf, TEST_MAILMAP, strlen(TEST_MAILMAP));
-
-	cl_git_pass(git_mailmap_from_buffer(&mailmap, &buf));
+	cl_git_pass(git_mailmap_from_buffer(&mailmap, TEST_MAILMAP, 0));
 }
 
 void test_mailmap_basic__cleanup(void)
diff --git a/tests/mailmap/blame.c b/tests/mailmap/blame.c
index 328f4bc..e6bc1a4 100644
--- a/tests/mailmap/blame.c
+++ b/tests/mailmap/blame.c
@@ -2,7 +2,7 @@
 #include "git2/repository.h"
 #include "git2/blame.h"
 #include "mailmap.h"
-#include "mailmap_helpers.h"
+#include "mailmap_testdata.h"
 
 static git_repository *g_repo;
 static git_blame *g_blame;
@@ -30,8 +30,7 @@ void test_mailmap_blame__hunks(void)
 	opts.flags |= GIT_BLAME_USE_MAILMAP;
 
 	cl_git_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts));
-	if (!g_blame)
-		return;
+	cl_assert(g_blame);
 
 	for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) {
 		hunk = git_blame_get_hunk_byline(g_blame, idx + 1);
@@ -52,8 +51,7 @@ void test_mailmap_blame__hunks_no_mailmap(void)
 	g_repo = cl_git_sandbox_init("mailmap");
 
 	cl_git_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts));
-	if (!g_blame)
-		return;
+	cl_assert(g_blame);
 
 	for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) {
 		hunk = git_blame_get_hunk_byline(g_blame, idx + 1);
diff --git a/tests/mailmap/mailmap_helpers.h b/tests/mailmap/mailmap_helpers.h
deleted file mode 100644
index 173536d..0000000
--- a/tests/mailmap/mailmap_helpers.h
+++ /dev/null
@@ -1,44 +0,0 @@
-#include "mailmap.h"
-
-typedef struct mailmap_entry {
-	const char *real_name;
-	const char *real_email;
-	const char *replace_name;
-	const char *replace_email;
-} mailmap_entry;
-
-static const char string_mailmap[] =
-	"# Simple Comment line\n"
-	"<cto@company.xx>                       <cto@coompany.xx>\n"
-	"Some Dude <some@dude.xx>         nick1 <bugs@company.xx>\n"
-	"Other Author <other@author.xx>   nick2 <bugs@company.xx>\n"
-	"Other Author <other@author.xx>         <nick2@company.xx>\n"
-	"Phil Hill <phil@company.xx>  # Comment at end of line\n"
-	"<joseph@company.xx>             Joseph <bugs@company.xx>\n"
-	"Santa Claus <santa.claus@northpole.xx> <me@company.xx>\n"
-	"Untracked <untracked@company.xx>";
-
-static const mailmap_entry entries[] = {
-	{ NULL, "cto@company.xx", NULL, "cto@coompany.xx" },
-	{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
-	{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
-	{ "Other Author", "other@author.xx", NULL, "nick2@company.xx" },
-	{ "Phil Hill", NULL, NULL, "phil@company.xx" },
-	{ NULL, "joseph@company.xx", "Joseph", "bugs@company.xx" },
-	{ "Santa Claus", "santa.claus@northpole.xx", NULL, "me@company.xx" },
-	/* This entry isn't in the bare repository */
-	{ "Untracked", NULL, NULL, "untracked@company.xx" }
-};
-
-static const mailmap_entry resolved[] = {
-	{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
-	{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
-	{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
-	{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
-	{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
-	{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
-	{ "Phil Hill", "phil@company.xx", "unknown", "phil@company.xx" },
-	{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
-	{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
-	{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" }
-};
diff --git a/tests/mailmap/mailmap_testdata.h b/tests/mailmap/mailmap_testdata.h
new file mode 100644
index 0000000..173536d
--- /dev/null
+++ b/tests/mailmap/mailmap_testdata.h
@@ -0,0 +1,44 @@
+#include "mailmap.h"
+
+typedef struct mailmap_entry {
+	const char *real_name;
+	const char *real_email;
+	const char *replace_name;
+	const char *replace_email;
+} mailmap_entry;
+
+static const char string_mailmap[] =
+	"# Simple Comment line\n"
+	"<cto@company.xx>                       <cto@coompany.xx>\n"
+	"Some Dude <some@dude.xx>         nick1 <bugs@company.xx>\n"
+	"Other Author <other@author.xx>   nick2 <bugs@company.xx>\n"
+	"Other Author <other@author.xx>         <nick2@company.xx>\n"
+	"Phil Hill <phil@company.xx>  # Comment at end of line\n"
+	"<joseph@company.xx>             Joseph <bugs@company.xx>\n"
+	"Santa Claus <santa.claus@northpole.xx> <me@company.xx>\n"
+	"Untracked <untracked@company.xx>";
+
+static const mailmap_entry entries[] = {
+	{ NULL, "cto@company.xx", NULL, "cto@coompany.xx" },
+	{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", NULL, "nick2@company.xx" },
+	{ "Phil Hill", NULL, NULL, "phil@company.xx" },
+	{ NULL, "joseph@company.xx", "Joseph", "bugs@company.xx" },
+	{ "Santa Claus", "santa.claus@northpole.xx", NULL, "me@company.xx" },
+	/* This entry isn't in the bare repository */
+	{ "Untracked", NULL, NULL, "untracked@company.xx" }
+};
+
+static const mailmap_entry resolved[] = {
+	{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
+	{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
+	{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
+	{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
+	{ "Phil Hill", "phil@company.xx", "unknown", "phil@company.xx" },
+	{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
+	{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
+	{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" }
+};
diff --git a/tests/mailmap/parsing.c b/tests/mailmap/parsing.c
index 70110c0..2cb8304 100644
--- a/tests/mailmap/parsing.c
+++ b/tests/mailmap/parsing.c
@@ -1,7 +1,7 @@
 #include "clar_libgit2.h"
 #include "repository.h"
 #include "git2/sys/repository.h"
-#include "mailmap_helpers.h"
+#include "mailmap_testdata.h"
 #include "buf_text.h"
 
 static git_repository *g_repo;
@@ -67,9 +67,7 @@ static const mailmap_entry resolved_untracked[] = {
 
 void test_mailmap_parsing__string(void)
 {
-	git_buf buf = GIT_BUF_INIT;
-	git_buf_attach_notowned(&buf, string_mailmap, strlen(string_mailmap));
-	cl_git_pass(git_mailmap_from_buffer(&g_mailmap, &buf));
+	cl_git_pass(git_mailmap_from_buffer(&g_mailmap, string_mailmap, 0));
 
 	/* We should have parsed all of the entries */
 	check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries));
@@ -89,7 +87,7 @@ void test_mailmap_parsing__windows_string(void)
 	git_buf_attach_notowned(&unixbuf, string_mailmap, strlen(string_mailmap));
 	git_buf_text_lf_to_crlf(&winbuf, &unixbuf);
 
-	cl_git_pass(git_mailmap_from_buffer(&g_mailmap, &winbuf));
+	cl_git_pass(git_mailmap_from_buffer(&g_mailmap, winbuf.ptr, winbuf.size));
 	git_buf_free(&winbuf);
 
 	/* We should have parsed all of the entries */