Commit ba01547d4a8190d5e4df317d4b0d0767e160bc57

Edward Thomson 2021-09-20T21:45:10

Merge pull request #6061 from libgit2/ethomson/email Introduce `git_email_create`; deprecate `git_diff_format_email`

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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
diff --git a/include/git2.h b/include/git2.h
index f39d7fb..2961cc3 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -26,6 +26,7 @@
 #include "git2/deprecated.h"
 #include "git2/describe.h"
 #include "git2/diff.h"
+#include "git2/email.h"
 #include "git2/errors.h"
 #include "git2/filter.h"
 #include "git2/global.h"
diff --git a/include/git2/deprecated.h b/include/git2/deprecated.h
index a2b117f..6b268eb 100644
--- a/include/git2/deprecated.h
+++ b/include/git2/deprecated.h
@@ -294,6 +294,102 @@ typedef git_configmap git_cvar_map;
 
 /**@}*/
 
+/** @name Deprecated Diff Functions and Constants
+ *
+ * These functions and enumeration values are retained for backward
+ * compatibility.  The newer versions of these functions and values
+ * should be preferred in all new code.
+ *
+ * There is no plan to remove these backward compatibility values at
+ * this time.
+ */
+/**@{*/
+
+/**
+ * Formatting options for diff e-mail generation
+ */
+typedef enum {
+	/** Normal patch, the default */
+	GIT_DIFF_FORMAT_EMAIL_NONE = 0,
+
+	/** Don't insert "[PATCH]" in the subject header*/
+	GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER = (1 << 0),
+
+} git_diff_format_email_flags_t;
+
+/**
+ * Options for controlling the formatting of the generated e-mail.
+ */
+typedef struct {
+	unsigned int version;
+
+	/** see `git_diff_format_email_flags_t` above */
+	uint32_t flags;
+
+	/** This patch number */
+	size_t patch_no;
+
+	/** Total number of patches in this series */
+	size_t total_patches;
+
+	/** id to use for the commit */
+	const git_oid *id;
+
+	/** Summary of the change */
+	const char *summary;
+
+	/** Commit message's body */
+	const char *body;
+
+	/** Author of the change */
+	const git_signature *author;
+} git_diff_format_email_options;
+
+#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1
+#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL}
+
+/**
+ * Create an e-mail ready patch from a diff.
+ *
+ * @deprecated git_email_create_from_diff
+ * @see git_email_create_from_diff
+ */
+GIT_EXTERN(int) git_diff_format_email(
+	git_buf *out,
+	git_diff *diff,
+	const git_diff_format_email_options *opts);
+
+/**
+ * Create an e-mail ready patch for a commit.
+ *
+ * @deprecated git_email_create_from_commit
+ * @see git_email_create_from_commit
+ */
+GIT_EXTERN(int) git_diff_commit_as_email(
+	git_buf *out,
+	git_repository *repo,
+	git_commit *commit,
+	size_t patch_no,
+	size_t total_patches,
+	uint32_t flags,
+	const git_diff_options *diff_opts);
+
+/**
+ * Initialize git_diff_format_email_options structure
+ *
+ * Initializes a `git_diff_format_email_options` with default values. Equivalent
+ * to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT.
+ *
+ * @param opts The `git_blame_options` struct to initialize.
+ * @param version The struct version; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`.
+ * @return Zero on success; -1 on failure.
+ */
+GIT_EXTERN(int) git_diff_format_email_options_init(
+	git_diff_format_email_options *opts,
+	unsigned int version);
+
+/**@}*/
+
 /** @name Deprecated Error Functions and Constants
  *
  * These functions and enumeration values are retained for backward
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 9497793..a0a15e7 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -1377,99 +1377,6 @@ GIT_EXTERN(int) git_diff_stats_to_buf(
 GIT_EXTERN(void) git_diff_stats_free(git_diff_stats *stats);
 
 /**
- * Formatting options for diff e-mail generation
- */
-typedef enum {
-	/** Normal patch, the default */
-	GIT_DIFF_FORMAT_EMAIL_NONE = 0,
-
-	/** Don't insert "[PATCH]" in the subject header*/
-	GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER = (1 << 0),
-
-} git_diff_format_email_flags_t;
-
-/**
- * Options for controlling the formatting of the generated e-mail.
- */
-typedef struct {
-	unsigned int version;
-
-	/** see `git_diff_format_email_flags_t` above */
-	uint32_t flags;
-
-	/** This patch number */
-	size_t patch_no;
-
-	/** Total number of patches in this series */
-	size_t total_patches;
-
-	/** id to use for the commit */
-	const git_oid *id;
-
-	/** Summary of the change */
-	const char *summary;
-
-	/** Commit message's body */
-	const char *body;
-
-	/** Author of the change */
-	const git_signature *author;
-} git_diff_format_email_options;
-
-#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1
-#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL}
-
-/**
- * Create an e-mail ready patch from a diff.
- *
- * @param out buffer to store the e-mail patch in
- * @param diff containing the commit
- * @param opts structure with options to influence content and formatting.
- * @return 0 or an error code
- */
-GIT_EXTERN(int) git_diff_format_email(
-	git_buf *out,
-	git_diff *diff,
-	const git_diff_format_email_options *opts);
-
-/**
- * Create an e-mail ready patch for a commit.
- *
- * Does not support creating patches for merge commits (yet).
- *
- * @param out buffer to store the e-mail patch in
- * @param repo containing the commit
- * @param commit pointer to up commit
- * @param patch_no patch number of the commit
- * @param total_patches total number of patches in the patch set
- * @param flags determines the formatting of the e-mail
- * @param diff_opts structure with options to influence diff or NULL for defaults.
- * @return 0 or an error code
- */
-GIT_EXTERN(int) git_diff_commit_as_email(
-	git_buf *out,
-	git_repository *repo,
-	git_commit *commit,
-	size_t patch_no,
-	size_t total_patches,
-	uint32_t flags,
-	const git_diff_options *diff_opts);
-
-/**
- * Initialize git_diff_format_email_options structure
- *
- * Initializes a `git_diff_format_email_options` with default values. Equivalent
- * to creating an instance with GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT.
- *
- * @param opts The `git_blame_options` struct to initialize.
- * @param version The struct version; pass `GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION`.
- * @return Zero on success; -1 on failure.
- */
-GIT_EXTERN(int) git_diff_format_email_options_init(
-	git_diff_format_email_options *opts,
-	unsigned int version);
-
-/**
  * Patch ID options structure
  *
  * Initialize with `GIT_PATCHID_OPTIONS_INIT`. Alternatively, you can
diff --git a/include/git2/email.h b/include/git2/email.h
new file mode 100644
index 0000000..b56be5d
--- /dev/null
+++ b/include/git2/email.h
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_git_email_h__
+#define INCLUDE_git_email_h__
+
+#include "common.h"
+
+/**
+ * @file git2/email.h
+ * @brief Git email formatting and application routines.
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Formatting options for diff e-mail generation
+ */
+typedef enum {
+	/** Normal patch, the default */
+	GIT_EMAIL_CREATE_DEFAULT = 0,
+
+	/** Do not include patch numbers in the subject prefix. */
+	GIT_EMAIL_CREATE_OMIT_NUMBERS = (1u << 0),
+
+	/**
+	 * Include numbers in the subject prefix even when the
+	 * patch is for a single commit (1/1).
+	 */
+	GIT_EMAIL_CREATE_ALWAYS_NUMBER = (1u << 1),
+
+	/** Do not perform rename or similarity detection. */
+	GIT_EMAIL_CREATE_NO_RENAMES = (1u << 2),
+} git_email_create_flags_t;
+
+/**
+ * Options for controlling the formatting of the generated e-mail.
+ */
+typedef struct {
+	unsigned int version;
+
+	/** see `git_email_create_flags_t` above */
+	uint32_t flags;
+
+	/** Options to use when creating diffs */
+	git_diff_options diff_opts;
+
+	/** Options for finding similarities within diffs */
+	git_diff_find_options diff_find_opts;
+
+	/**
+	 * The subject prefix, by default "PATCH".  If set to an empty
+	 * string ("") then only the patch numbers will be shown in the
+	 * prefix.  If the subject_prefix is empty and patch numbers
+	 * are not being shown, the prefix will be omitted entirely.
+	 */
+	const char *subject_prefix;
+
+	/**
+	 * The starting patch number; this cannot be 0.  By default,
+	 * this is 1.
+	 */
+	size_t start_number;
+
+	/** The "re-roll" number.  By default, there is no re-roll. */
+	size_t reroll_number;
+} git_email_create_options;
+
+/*
+ * By default, our options include rename detection and binary
+ * diffs to match `git format-patch`.
+ */
+#define GIT_EMAIL_CREATE_OPTIONS_VERSION 1
+#define GIT_EMAIL_CREATE_OPTIONS_INIT \
+{ \
+	GIT_EMAIL_CREATE_OPTIONS_VERSION, \
+	GIT_EMAIL_CREATE_DEFAULT, \
+	{ GIT_DIFF_OPTIONS_VERSION, GIT_DIFF_SHOW_BINARY, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3 }, \
+	GIT_DIFF_FIND_OPTIONS_INIT \
+}
+
+/**
+ * Create a diff for a commit in mbox format for sending via email.
+ *
+ * @param out buffer to store the e-mail patch in
+ * @param diff the changes to include in the email
+ * @param patch_idx the patch index
+ * @param patch_count the total number of patches that will be included
+ * @param commit_id the commit id for this change
+ * @param summary the commit message for this change
+ * @param body optional text to include above the diffstat
+ * @param author the person who authored this commit
+ * @param opts email creation options
+ */
+GIT_EXTERN(int) git_email_create_from_diff(
+	git_buf *out,
+	git_diff *diff,
+	size_t patch_idx,
+	size_t patch_count,
+	const git_oid *commit_id,
+	const char *summary,
+	const char *body,
+	const git_signature *author,
+	const git_email_create_options *opts);
+
+/**
+ * Create a diff for a commit in mbox format for sending via email.
+ * The commit must not be a merge commit.
+ *
+ * @param out buffer to store the e-mail patch in
+ * @param commit commit to create a patch for
+ * @param opts email creation options
+ */
+GIT_EXTERN(int) git_email_create_from_commit(
+	git_buf *out,
+	git_commit *commit,
+	const git_email_create_options *opts);
+
+GIT_END_DECL
+
+/** @} */
+
+#endif
diff --git a/include/git2/sys/email.h b/include/git2/sys/email.h
new file mode 100644
index 0000000..6f4a286
--- /dev/null
+++ b/include/git2/sys/email.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_sys_git_email_h__
+#define INCLUDE_sys_git_email_h__
+
+/**
+ * @file git2/sys/email.h
+ * @brief Advanced git email creation routines
+ * @defgroup git_email Advanced git email creation routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Create a diff for a commit in mbox format for sending via email.
+ *
+ * @param out buffer to store the e-mail patch in
+ * @param diff the changes to include in the email
+ * @param patch_idx the patch index
+ * @param patch_count the total number of patches that will be included
+ * @param commit_id the commit id for this change
+ * @param summary the commit message for this change
+ * @param body optional text to include above the diffstat
+ * @param author the person who authored this commit
+ * @param opts email creation options
+ */
+GIT_EXTERN(int) git_email_create_from_diff(
+	git_buf *out,
+	git_diff *diff,
+	size_t patch_idx,
+	size_t patch_count,
+	const git_oid *commit_id,
+	const char *summary,
+	const char *body,
+	const git_signature *author,
+	const git_email_create_options *opts);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/buffer.c b/src/buffer.c
index ab2a613..a57df12 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -600,6 +600,13 @@ void git_buf_shorten(git_buf *buf, size_t amount)
 		git_buf_clear(buf);
 }
 
+void git_buf_truncate_at_char(git_buf *buf, char separator)
+{
+	ssize_t idx = git_buf_find(buf, separator);
+	if (idx >= 0)
+		git_buf_truncate(buf, (size_t)idx);
+}
+
 void git_buf_rtruncate_at_char(git_buf *buf, char separator)
 {
 	ssize_t idx = git_buf_rfind_next(buf, separator);
diff --git a/src/buffer.h b/src/buffer.h
index 7faa9fd..a356beb 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -171,6 +171,7 @@ void git_buf_consume_bytes(git_buf *buf, size_t len);
 void git_buf_consume(git_buf *buf, const char *end);
 void git_buf_truncate(git_buf *buf, size_t len);
 void git_buf_shorten(git_buf *buf, size_t amount);
+void git_buf_truncate_at_char(git_buf *buf, char separator);
 void git_buf_rtruncate_at_char(git_buf *path, char separator);
 
 /** General join with separator */
diff --git a/src/diff.c b/src/diff.c
index 4085c5f..30b9f64 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -7,11 +7,15 @@
 
 #include "diff.h"
 
-#include "git2/version.h"
-#include "diff_generate.h"
+#include "common.h"
 #include "patch.h"
+#include "email.h"
 #include "commit.h"
 #include "index.h"
+#include "diff_generate.h"
+
+#include "git2/version.h"
+#include "git2/email.h"
 
 struct patch_id_args {
 	git_hash_ctx ctx;
@@ -150,97 +154,14 @@ int git_diff_foreach(
 	return error;
 }
 
-static int diff_format_email_append_header_tobuf(
-	git_buf *out,
-	const git_oid *id,
-	const git_signature *author,
-	const char *summary,
-	const char *body,
-	size_t patch_no,
-	size_t total_patches,
-	bool exclude_patchno_marker)
-{
-	char idstr[GIT_OID_HEXSZ + 1];
-	char date_str[GIT_DATE_RFC2822_SZ];
-	int error = 0;
-
-	git_oid_fmt(idstr, id);
-	idstr[GIT_OID_HEXSZ] = '\0';
-
-	if ((error = git__date_rfc2822_fmt(date_str, sizeof(date_str),
-		&author->when)) < 0)
-		return error;
-
-	error = git_buf_printf(out,
-				"From %s Mon Sep 17 00:00:00 2001\n" \
-				"From: %s <%s>\n" \
-				"Date: %s\n" \
-				"Subject: ",
-				idstr,
-				author->name, author->email,
-				date_str);
-
-	if (error < 0)
-		return error;
-
-	if (!exclude_patchno_marker) {
-		if (total_patches == 1) {
-			error = git_buf_puts(out, "[PATCH] ");
-		} else {
-			error = git_buf_printf(out, "[PATCH %"PRIuZ"/%"PRIuZ"] ",
-				patch_no, total_patches);
-		}
-
-		if (error < 0)
-			return error;
-	}
-
-	error = git_buf_printf(out, "%s\n\n", summary);
-
-	if (body) {
-		git_buf_puts(out, body);
-
-		if (out->ptr[out->size - 1] != '\n')
-			git_buf_putc(out, '\n');
-	}
-
-	return error;
-}
-
-static int diff_format_email_append_patches_tobuf(
-	git_buf *out,
-	git_diff *diff)
-{
-	size_t i, deltas;
-	int error = 0;
-
-	deltas = git_diff_num_deltas(diff);
-
-	for (i = 0; i < deltas; ++i) {
-		git_patch *patch = NULL;
-
-		if ((error = git_patch_from_diff(&patch, diff, i)) >= 0)
-			error = git_patch_to_buf(out, patch);
-
-		git_patch_free(patch);
-
-		if (error < 0)
-			break;
-	}
-
-	return error;
-}
+#ifndef GIT_DEPRECATE_HARD
 
 int git_diff_format_email(
 	git_buf *out,
 	git_diff *diff,
 	const git_diff_format_email_options *opts)
 {
-	git_diff_stats *stats = NULL;
-	char *summary = NULL, *loc = NULL;
-	bool ignore_marker;
-	unsigned int format_flags = 0;
-	size_t allocsize;
+	git_email_create_options email_create_opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
 	int error;
 
 	GIT_ASSERT_ARG(out);
@@ -251,64 +172,13 @@ int git_diff_format_email(
 		GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION,
 		"git_format_email_options");
 
-	ignore_marker = (opts->flags &
-		GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0;
-
-	if (!ignore_marker) {
-		if (opts->patch_no > opts->total_patches) {
-			git_error_set(GIT_ERROR_INVALID,
-				"patch %"PRIuZ" out of range. max %"PRIuZ,
-				opts->patch_no, opts->total_patches);
-			return -1;
-		}
+	if ((opts->flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0)
+		email_create_opts.subject_prefix = "";
 
-		if (opts->patch_no == 0) {
-			git_error_set(GIT_ERROR_INVALID,
-				"invalid patch no %"PRIuZ". should be >0", opts->patch_no);
-			return -1;
-		}
-	}
 
-	/* the summary we receive may not be clean.
-	 * it could potentially contain new line characters
-	 * or not be set, sanitize, */
-	if ((loc = strpbrk(opts->summary, "\r\n")) != NULL) {
-		size_t offset = 0;
-
-		if ((offset = (loc - opts->summary)) == 0) {
-			git_error_set(GIT_ERROR_INVALID, "summary is empty");
-			error = -1;
-			goto on_error;
-		}
-
-		GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, offset, 1);
-		summary = git__calloc(allocsize, sizeof(char));
-		GIT_ERROR_CHECK_ALLOC(summary);
-
-		strncpy(summary, opts->summary, offset);
-	}
-
-	error = diff_format_email_append_header_tobuf(out,
-		opts->id, opts->author, summary == NULL ? opts->summary : summary,
-		opts->body, opts->patch_no, opts->total_patches, ignore_marker);
-
-	if (error < 0)
-		goto on_error;
-
-	format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY;
-
-	if ((error = git_buf_puts(out, "---\n")) < 0 ||
-		(error = git_diff_get_stats(&stats, diff)) < 0 ||
-		(error = git_diff_stats_to_buf(out, stats, format_flags, 0)) < 0 ||
-		(error = git_buf_putc(out, '\n')) < 0 ||
-		(error = diff_format_email_append_patches_tobuf(out, diff)) < 0)
-			goto on_error;
-
-	error = git_buf_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n");
-
-on_error:
-	git__free(summary);
-	git_diff_stats_free(stats);
+	error = git_email__append_from_diff(out, diff, opts->patch_no,
+		opts->total_patches, opts->id, opts->summary, opts->body,
+		opts->author, &email_create_opts);
 
 	return error;
 }
@@ -323,60 +193,43 @@ int git_diff_commit_as_email(
 	const git_diff_options *diff_opts)
 {
 	git_diff *diff = NULL;
-	git_diff_format_email_options opts =
-		GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+	const git_oid *commit_id;
+	const char *summary, *body;
+	const git_signature *author;
 	int error;
 
 	GIT_ASSERT_ARG(out);
 	GIT_ASSERT_ARG(repo);
 	GIT_ASSERT_ARG(commit);
 
-	opts.flags = flags;
-	opts.patch_no = patch_no;
-	opts.total_patches = total_patches;
-	opts.id = git_commit_id(commit);
-	opts.summary = git_commit_summary(commit);
-	opts.body = git_commit_body(commit);
-	opts.author = git_commit_author(commit);
+	commit_id = git_commit_id(commit);
+	summary = git_commit_summary(commit);
+	body = git_commit_body(commit);
+	author = git_commit_author(commit);
+
+	if ((flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0)
+		opts.subject_prefix = "";
 
 	if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
 		return error;
 
-	error = git_diff_format_email(out, diff, &opts);
+	error = git_email_create_from_diff(out, diff, patch_no, total_patches, commit_id, summary, body, author, &opts);
 
 	git_diff_free(diff);
 	return error;
 }
 
-int git_diff_options_init(git_diff_options *opts, unsigned int version)
-{
-	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
-		opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT);
-	return 0;
-}
-
-#ifndef GIT_DEPRECATE_HARD
 int git_diff_init_options(git_diff_options *opts, unsigned int version)
 {
 	return git_diff_options_init(opts, version);
 }
-#endif
 
-int git_diff_find_options_init(
-	git_diff_find_options *opts, unsigned int version)
-{
-	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
-		opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT);
-	return 0;
-}
-
-#ifndef GIT_DEPRECATE_HARD
 int git_diff_find_init_options(
 	git_diff_find_options *opts, unsigned int version)
 {
 	return git_diff_find_options_init(opts, version);
 }
-#endif
 
 int git_diff_format_email_options_init(
 	git_diff_format_email_options *opts, unsigned int version)
@@ -387,14 +240,29 @@ int git_diff_format_email_options_init(
 	return 0;
 }
 
-#ifndef GIT_DEPRECATE_HARD
 int git_diff_format_email_init_options(
 	git_diff_format_email_options *opts, unsigned int version)
 {
 	return git_diff_format_email_options_init(opts, version);
 }
+
 #endif
 
+int git_diff_options_init(git_diff_options *opts, unsigned int version)
+{
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT);
+	return 0;
+}
+
+int git_diff_find_options_init(
+	git_diff_find_options *opts, unsigned int version)
+{
+	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+		opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT);
+	return 0;
+}
+
 static int flush_hunk(git_oid *result, git_hash_ctx *ctx)
 {
 	git_oid hash;
diff --git a/src/email.c b/src/email.c
new file mode 100644
index 0000000..8957d9a
--- /dev/null
+++ b/src/email.c
@@ -0,0 +1,300 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "email.h"
+
+#include "buffer.h"
+#include "common.h"
+#include "diff_generate.h"
+
+#include "git2/email.h"
+#include "git2/patch.h"
+#include "git2/version.h"
+
+/*
+ * Git uses a "magic" timestamp to indicate that an email message
+ * is from `git format-patch` (or our equivalent).
+ */
+#define EMAIL_TIMESTAMP "Mon Sep 17 00:00:00 2001"
+
+GIT_INLINE(int) include_prefix(
+	size_t patch_count,
+	git_email_create_options *opts)
+{
+	return ((!opts->subject_prefix || *opts->subject_prefix) ||
+	        (opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 ||
+	        opts->reroll_number ||
+		(patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS)));
+}
+
+static int append_prefix(
+	git_buf *out,
+	size_t patch_idx,
+	size_t patch_count,
+	git_email_create_options *opts)
+{
+	const char *subject_prefix = opts->subject_prefix ?
+		opts->subject_prefix : "PATCH";
+
+	git_buf_putc(out, '[');
+
+	if (*subject_prefix)
+		git_buf_puts(out, subject_prefix);
+
+	if (opts->reroll_number) {
+		if (*subject_prefix)
+			git_buf_putc(out, ' ');
+
+		git_buf_printf(out, "v%" PRIuZ, opts->reroll_number);
+	}
+
+	if ((opts->flags & GIT_EMAIL_CREATE_ALWAYS_NUMBER) != 0 ||
+	    (patch_count > 1 && !(opts->flags & GIT_EMAIL_CREATE_OMIT_NUMBERS))) {
+		size_t start_number = opts->start_number ?
+			opts->start_number : 1;
+
+		if (*subject_prefix || opts->reroll_number)
+			git_buf_putc(out, ' ');
+
+		git_buf_printf(out, "%" PRIuZ "/%" PRIuZ,
+		               patch_idx + (start_number - 1),
+		               patch_count + (start_number - 1));
+	}
+
+	git_buf_puts(out, "]");
+
+	return git_buf_oom(out) ? -1 : 0;
+}
+
+static int append_subject(
+	git_buf *out,
+	size_t patch_idx,
+	size_t patch_count,
+	const char *summary,
+	git_email_create_options *opts)
+{
+	bool prefix = include_prefix(patch_count, opts);
+	size_t summary_len = summary ? strlen(summary) : 0;
+	int error;
+
+	if (summary_len) {
+		const char *nl = strchr(summary, '\n');
+
+		if (nl)
+			summary_len = (nl - summary);
+	}
+
+	if ((error = git_buf_puts(out, "Subject: ")) < 0)
+		return error;
+
+	if (prefix &&
+	    (error = append_prefix(out, patch_idx, patch_count, opts)) < 0)
+		return error;
+
+	if (prefix && summary_len && (error = git_buf_putc(out, ' ')) < 0)
+		return error;
+
+	if (summary_len &&
+	    (error = git_buf_put(out, summary, summary_len)) < 0)
+		return error;
+
+	return git_buf_putc(out, '\n');
+}
+
+static int append_header(
+	git_buf *out,
+	size_t patch_idx,
+	size_t patch_count,
+	const git_oid *commit_id,
+	const char *summary,
+	const git_signature *author,
+	git_email_create_options *opts)
+{
+	char id[GIT_OID_HEXSZ];
+	char date[GIT_DATE_RFC2822_SZ];
+	int error;
+
+	if ((error = git_oid_fmt(id, commit_id)) < 0 ||
+	    (error = git_buf_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 ||
+	    (error = git_buf_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 ||
+	    (error = git__date_rfc2822_fmt(date, sizeof(date), &author->when)) < 0 ||
+	    (error = git_buf_printf(out, "Date: %s\n", date)) < 0 ||
+	    (error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0)
+		return error;
+
+	if ((error = git_buf_putc(out, '\n')) < 0)
+		return error;
+
+	return 0;
+}
+
+static int append_body(git_buf *out, const char *body)
+{
+	size_t body_len;
+	int error;
+
+	if (!body)
+		return 0;
+
+	body_len = strlen(body);
+
+	if ((error = git_buf_puts(out, body)) < 0)
+		return error;
+
+	if (body_len && body[body_len - 1] != '\n')
+		error = git_buf_putc(out, '\n');
+
+	return error;
+}
+
+static int append_diffstat(git_buf *out, git_diff *diff)
+{
+	git_diff_stats *stats = NULL;
+	unsigned int format_flags;
+	int error;
+
+	format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY;
+
+	if ((error = git_diff_get_stats(&stats, diff)) == 0 &&
+	    (error = git_diff_stats_to_buf(out, stats, format_flags, 0)) == 0)
+		error = git_buf_putc(out, '\n');
+
+	git_diff_stats_free(stats);
+	return error;
+}
+
+static int append_patches(git_buf *out, git_diff *diff)
+{
+	size_t i, deltas;
+	int error = 0;
+
+	deltas = git_diff_num_deltas(diff);
+
+	for (i = 0; i < deltas; ++i) {
+		git_patch *patch = NULL;
+
+		if ((error = git_patch_from_diff(&patch, diff, i)) >= 0)
+			error = git_patch_to_buf(out, patch);
+
+		git_patch_free(patch);
+
+		if (error < 0)
+			break;
+	}
+
+	return error;
+}
+
+int git_email__append_from_diff(
+	git_buf *out,
+	git_diff *diff,
+	size_t patch_idx,
+	size_t patch_count,
+	const git_oid *commit_id,
+	const char *summary,
+	const char *body,
+	const git_signature *author,
+	const git_email_create_options *given_opts)
+{
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+	int error;
+
+	GIT_ASSERT_ARG(out);
+	GIT_ASSERT_ARG(diff);
+	GIT_ASSERT_ARG(!patch_idx || patch_idx <= patch_count);
+	GIT_ASSERT_ARG(commit_id);
+	GIT_ASSERT_ARG(author);
+
+	GIT_ERROR_CHECK_VERSION(given_opts,
+		GIT_EMAIL_CREATE_OPTIONS_VERSION,
+		"git_email_create_options");
+
+	if (given_opts)
+		memcpy(&opts, given_opts, sizeof(git_email_create_options));
+
+	git_buf_sanitize(out);
+	git_buf_clear(out);
+
+	if ((error = append_header(out, patch_idx, patch_count, commit_id, summary, author, &opts)) == 0 &&
+	    (error = append_body(out, body)) == 0 &&
+	    (error = git_buf_puts(out, "---\n")) == 0 &&
+	    (error = append_diffstat(out, diff)) == 0 &&
+	    (error = append_patches(out, diff)) == 0)
+		error = git_buf_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n");
+
+	return error;
+}
+
+int git_email_create_from_diff(
+	git_buf *out,
+	git_diff *diff,
+	size_t patch_idx,
+	size_t patch_count,
+	const git_oid *commit_id,
+	const char *summary,
+	const char *body,
+	const git_signature *author,
+	const git_email_create_options *given_opts)
+{
+	int error;
+
+	git_buf_sanitize(out);
+	git_buf_clear(out);
+
+	error = git_email__append_from_diff(out, diff, patch_idx,
+		patch_count, commit_id, summary, body, author,
+		given_opts);
+
+	return error;
+}
+
+int git_email_create_from_commit(
+	git_buf *out,
+	git_commit *commit,
+	const git_email_create_options *given_opts)
+{
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+	git_diff *diff = NULL;
+	git_repository *repo;
+	git_diff_options *diff_opts;
+	git_diff_find_options *find_opts;
+	const git_signature *author;
+	const char *summary, *body;
+	const git_oid *commit_id;
+	int error = -1;
+
+	GIT_ASSERT_ARG(out);
+	GIT_ASSERT_ARG(commit);
+
+	GIT_ERROR_CHECK_VERSION(given_opts,
+		GIT_EMAIL_CREATE_OPTIONS_VERSION,
+		"git_email_create_options");
+
+	if (given_opts)
+		memcpy(&opts, given_opts, sizeof(git_email_create_options));
+
+	repo = git_commit_owner(commit);
+	author = git_commit_author(commit);
+	summary = git_commit_summary(commit);
+	body = git_commit_body(commit);
+	commit_id = git_commit_id(commit);
+	diff_opts = &opts.diff_opts;
+	find_opts = &opts.diff_find_opts;
+
+	if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
+		goto done;
+
+	if ((opts.flags & GIT_EMAIL_CREATE_NO_RENAMES) == 0 &&
+	    (error = git_diff_find_similar(diff, find_opts)) < 0)
+		goto done;
+
+	error = git_email_create_from_diff(out, diff, 1, 1, commit_id, summary, body, author, &opts);
+
+done:
+	git_diff_free(diff);
+	return error;
+}
diff --git a/src/email.h b/src/email.h
new file mode 100644
index 0000000..7aeb462
--- /dev/null
+++ b/src/email.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_email_h__
+#define INCLUDE_email_h__
+
+#include "common.h"
+
+#include "git2/email.h"
+
+extern int git_email__append_from_diff(
+	git_buf *out,
+	git_diff *diff,
+	size_t patch_idx,
+	size_t patch_count,
+	const git_oid *commit_id,
+	const char *summary,
+	const char *body,
+	const git_signature *author,
+	const git_email_create_options *given_opts);
+
+#endif
diff --git a/tests/diff/format_email.c b/tests/diff/format_email.c
index bdfc4ca..ea7aa07 100644
--- a/tests/diff/format_email.c
+++ b/tests/diff/format_email.c
@@ -18,6 +18,7 @@ void test_diff_format_email__cleanup(void)
 	cl_git_sandbox_cleanup();
 }
 
+#ifndef GIT_DEPRECATE_HARD
 static void assert_email_match(
 	const char *expected,
 	const char *oidstr,
@@ -51,9 +52,11 @@ static void assert_email_match(
 	git_commit_free(commit);
 	git_buf_dispose(&buf);
 }
+#endif
 
 void test_diff_format_email__simple(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
 	const char *email =
 	"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
@@ -96,10 +99,12 @@ void test_diff_format_email__simple(void)
 
 	assert_email_match(
 		email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+#endif
 }
 
 void test_diff_format_email__with_message(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
 	const char *email = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
 	"From: Patrick Steinhardt <ps@pks.im>\n" \
@@ -136,11 +141,13 @@ void test_diff_format_email__with_message(void)
 
 	assert_email_match(
 		email, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270", &opts);
+#endif
 }
 
 
 void test_diff_format_email__multiple(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_oid oid;
 	git_commit *commit = NULL;
 	git_diff *diff = NULL;
@@ -256,10 +263,12 @@ void test_diff_format_email__multiple(void)
 	git_diff_free(diff);
 	git_commit_free(commit);
 	git_buf_dispose(&buf);
+#endif
 }
 
 void test_diff_format_email__exclude_marker(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
 	const char *email =
 	"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
@@ -304,10 +313,12 @@ void test_diff_format_email__exclude_marker(void)
 
 	assert_email_match(
 		email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+#endif
 }
 
 void test_diff_format_email__invalid_no(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_oid oid;
 	git_commit *commit = NULL;
 	git_diff *diff = NULL;
@@ -327,15 +338,16 @@ void test_diff_format_email__invalid_no(void)
 	cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
 	cl_git_fail(git_diff_format_email(&buf, diff, &opts));
 	cl_git_fail(git_diff_commit_as_email(&buf, repo, commit, 2, 1, 0, NULL));
-	cl_git_fail(git_diff_commit_as_email(&buf, repo, commit, 0, 0, 0, NULL));
 
 	git_diff_free(diff);
 	git_commit_free(commit);
 	git_buf_dispose(&buf);
+#endif
 }
 
 void test_diff_format_email__mode_change(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
 	const char *email =
 	"From 7ade76dd34bba4733cf9878079f9fd4a456a9189 Mon Sep 17 00:00:00 2001\n" \
@@ -357,10 +369,12 @@ void test_diff_format_email__mode_change(void)
 
 	assert_email_match(
 		email, "7ade76dd34bba4733cf9878079f9fd4a456a9189", &opts);
+#endif
 }
 
 void test_diff_format_email__rename_add_remove(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
 	const char *email =
 	"From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
@@ -427,10 +441,12 @@ void test_diff_format_email__rename_add_remove(void)
 
 	assert_email_match(
 		email, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
+#endif
 }
 
 void test_diff_format_email__multiline_summary(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
 	const char *email =
 	"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
@@ -475,10 +491,12 @@ void test_diff_format_email__multiline_summary(void)
 
 	assert_email_match(
 		email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+#endif
 }
 
 void test_diff_format_email__binary(void)
 {
+#ifndef GIT_DEPRECATE_HARD
 	git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
 	const char *email =
 	"From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
@@ -501,5 +519,6 @@ void test_diff_format_email__binary(void)
 
 	assert_email_match(
 		email, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
+#endif
 }
 
diff --git a/tests/email/create.c b/tests/email/create.c
new file mode 100644
index 0000000..ccf79c2
--- /dev/null
+++ b/tests/email/create.c
@@ -0,0 +1,363 @@
+#include "clar.h"
+#include "clar_libgit2.h"
+
+#include "buffer.h"
+#include "diff_generate.h"
+
+static git_repository *repo;
+
+void test_email_create__initialize(void)
+{
+	repo = cl_git_sandbox_init("diff_format_email");
+}
+
+void test_email_create__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
+static void email_for_commit(
+	git_buf *out,
+	const char *commit_id,
+	git_email_create_options *opts)
+{
+	git_oid oid;
+	git_commit *commit = NULL;
+	git_diff *diff = NULL;
+
+	git_oid_fromstr(&oid, commit_id);
+
+	cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+
+	cl_git_pass(git_email_create_from_commit(out, commit, opts));
+
+	git_diff_free(diff);
+	git_commit_free(commit);
+}
+
+static void assert_email_match(
+	const char *expected,
+	const char *commit_id,
+	git_email_create_options *opts)
+{
+	git_buf buf = GIT_BUF_INIT;
+
+	email_for_commit(&buf, commit_id, opts);
+	cl_assert_equal_s(expected, git_buf_cstr(&buf));
+
+	git_buf_dispose(&buf);
+}
+
+static void assert_subject_match(
+	const char *expected,
+	const char *commit_id,
+	git_email_create_options *opts)
+{
+	git_buf buf = GIT_BUF_INIT;
+	const char *loc;
+
+	email_for_commit(&buf, commit_id, opts);
+
+	cl_assert((loc = strstr(buf.ptr, "\nSubject: ")) != NULL);
+	git_buf_consume(&buf, (loc + 10));
+	git_buf_truncate_at_char(&buf, '\n');
+
+	cl_assert_equal_s(expected, git_buf_cstr(&buf));
+
+	git_buf_dispose(&buf);
+}
+
+void test_email_create__commit(void)
+{
+	const char *expected =
+	"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
+	"From: Jacques Germishuys <jacquesg@striata.com>\n" \
+	"Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
+	"Subject: [PATCH] Modify some content\n" \
+	"\n" \
+	"---\n" \
+	" file1.txt | 8 +++++---\n" \
+	" 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
+	"\n" \
+	"diff --git a/file1.txt b/file1.txt\n" \
+	"index 94aaae8..af8f41d 100644\n" \
+	"--- a/file1.txt\n" \
+	"+++ b/file1.txt\n" \
+	"@@ -1,15 +1,17 @@\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	"+_file1.txt_\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	"+\n" \
+	"+\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"+_file1.txt_\n" \
+	"+_file1.txt_\n" \
+	" file1.txt\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	assert_email_match(
+		expected, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL);
+}
+
+void test_email_create__rename(void)
+{
+	const char *expected =
+	"From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
+	"From: Jacques Germishuys <jacquesg@striata.com>\n" \
+	"Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
+	"Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
+	"\n" \
+	"---\n" \
+	" file1.txt => file1.txt.renamed | 4 ++--\n" \
+	" 1 file changed, 2 insertions(+), 2 deletions(-)\n" \
+	"\n" \
+	"diff --git a/file1.txt b/file1.txt.renamed\n" \
+	"similarity index 86%\n" \
+	"rename from file1.txt\n" \
+	"rename to file1.txt.renamed\n" \
+	"index af8f41d..a97157a 100644\n" \
+	"--- a/file1.txt\n" \
+	"+++ b/file1.txt.renamed\n" \
+	"@@ -3,13 +3,13 @@ file1.txt\n" \
+	" _file1.txt_\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	"-file1.txt\n" \
+	"+file1.txt_renamed\n" \
+	" file1.txt\n" \
+	" \n" \
+	" \n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	"-file1.txt\n" \
+	"+file1.txt_renamed\n" \
+	" file1.txt\n" \
+	" file1.txt\n" \
+	" _file1.txt_\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", NULL);
+}
+
+void test_email_create__rename_as_add_delete(void)
+{
+	const char *expected =
+	"From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
+	"From: Jacques Germishuys <jacquesg@striata.com>\n" \
+	"Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
+	"Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
+	"\n" \
+	"---\n" \
+	" file1.txt         | 17 -----------------\n" \
+	" file1.txt.renamed | 17 +++++++++++++++++\n" \
+	" 2 files changed, 17 insertions(+), 17 deletions(-)\n" \
+	" delete mode 100644 file1.txt\n" \
+	" create mode 100644 file1.txt.renamed\n" \
+	"\n" \
+	"diff --git a/file1.txt b/file1.txt\n" \
+	"deleted file mode 100644\n" \
+	"index af8f41d..0000000\n" \
+	"--- a/file1.txt\n" \
+	"+++ /dev/null\n" \
+	"@@ -1,17 +0,0 @@\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-_file1.txt_\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-\n" \
+	"-\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-file1.txt\n" \
+	"-_file1.txt_\n" \
+	"-_file1.txt_\n" \
+	"-file1.txt\n" \
+	"diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
+	"new file mode 100644\n" \
+	"index 0000000..a97157a\n" \
+	"--- /dev/null\n" \
+	"+++ b/file1.txt.renamed\n" \
+	"@@ -0,0 +1,17 @@\n" \
+	"+file1.txt\n" \
+	"+file1.txt\n" \
+	"+_file1.txt_\n" \
+	"+file1.txt\n" \
+	"+file1.txt\n" \
+	"+file1.txt_renamed\n" \
+	"+file1.txt\n" \
+	"+\n" \
+	"+\n" \
+	"+file1.txt\n" \
+	"+file1.txt\n" \
+	"+file1.txt_renamed\n" \
+	"+file1.txt\n" \
+	"+file1.txt\n" \
+	"+_file1.txt_\n" \
+	"+_file1.txt_\n" \
+	"+file1.txt\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+	opts.flags |= GIT_EMAIL_CREATE_NO_RENAMES;
+
+	assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
+}
+
+void test_email_create__binary(void)
+{
+	const char *expected =
+	"From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
+	"From: Jacques Germishuys <jacquesg@striata.com>\n" \
+	"Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
+	"Subject: [PATCH] Modified binary file\n" \
+	"\n" \
+	"---\n" \
+	" binary.bin | Bin 3 -> 5 bytes\n" \
+	" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
+	"\n" \
+	"diff --git a/binary.bin b/binary.bin\n" \
+	"index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \
+	"GIT binary patch\n" \
+	"literal 5\n" \
+	"Mc${NkU}WL~000&M4gdfE\n" \
+	"\n" \
+	"literal 3\n" \
+	"Kc${Nk-~s>u4FC%O\n" \
+	"\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", NULL);
+}
+
+void test_email_create__binary_not_included(void)
+{
+	const char *expected =
+	"From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
+	"From: Jacques Germishuys <jacquesg@striata.com>\n" \
+	"Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
+	"Subject: [PATCH] Modified binary file\n" \
+	"\n" \
+	"---\n" \
+	" binary.bin | Bin 3 -> 5 bytes\n" \
+	" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
+	"\n" \
+	"diff --git a/binary.bin b/binary.bin\n" \
+	"index bd474b2..9ac35ff 100644\n" \
+	"Binary files a/binary.bin and b/binary.bin differ\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+	opts.diff_opts.flags &= ~GIT_DIFF_SHOW_BINARY;
+
+	assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
+}
+
+void test_email_create__custom_summary_and_body(void)
+{
+	const char *expected = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
+	"From: Patrick Steinhardt <ps@pks.im>\n" \
+	"Date: Tue, 24 Nov 2015 13:34:39 +0100\n" \
+	"Subject: [PPPPPATCH 2/4] This is a subject\n" \
+	"\n" \
+	"Modify content of file3.txt by appending a new line. Make this\n" \
+	"commit message somewhat longer to test behavior with newlines\n" \
+	"embedded in the message body.\n" \
+	"\n" \
+	"Also test if new paragraphs are included correctly.\n" \
+	"---\n" \
+	" file3.txt | 1 +\n" \
+	" 1 file changed, 1 insertion(+)\n" \
+	"\n" \
+	"diff --git a/file3.txt b/file3.txt\n" \
+	"index 9a2d780..7309653 100644\n" \
+	"--- a/file3.txt\n" \
+	"+++ b/file3.txt\n" \
+	"@@ -3,3 +3,4 @@ file3!\n" \
+	" file3\n" \
+	" file3\n" \
+	" file3\n" \
+	"+file3\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	const char *summary = "This is a subject\nwith\nnewlines";
+	const char *body = "Modify content of file3.txt by appending a new line. Make this\n" \
+	"commit message somewhat longer to test behavior with newlines\n" \
+	"embedded in the message body.\n" \
+	"\n" \
+	"Also test if new paragraphs are included correctly.";
+
+	git_oid oid;
+	git_commit *commit = NULL;
+	git_diff *diff = NULL;
+	git_buf buf = GIT_BUF_INIT;
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+
+	opts.subject_prefix = "PPPPPATCH";
+
+	git_oid_fromstr(&oid, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270");
+	cl_git_pass(git_commit_lookup(&commit, repo, &oid));
+	cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
+	cl_git_pass(git_email_create_from_diff(&buf, diff, 2, 4, &oid, summary, body, git_commit_author(commit), &opts));
+
+	cl_assert_equal_s(expected, git_buf_cstr(&buf));
+
+	git_diff_free(diff);
+	git_commit_free(commit);
+	git_buf_dispose(&buf);
+}
+
+void test_email_create__commit_subjects(void)
+{
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+
+	assert_subject_match("[PATCH] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+
+	opts.reroll_number = 42;
+	assert_subject_match("[PATCH v42] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+
+	opts.flags |= GIT_EMAIL_CREATE_ALWAYS_NUMBER;
+	assert_subject_match("[PATCH v42 1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+
+	opts.start_number = 9;
+	assert_subject_match("[PATCH v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+
+	opts.subject_prefix = "";
+	assert_subject_match("[v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+
+	opts.reroll_number = 0;
+	assert_subject_match("[9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+
+	opts.start_number = 0;
+	assert_subject_match("[1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+
+	opts.flags = GIT_EMAIL_CREATE_OMIT_NUMBERS;
+	assert_subject_match("Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
+}