11
11
#include "git2/email.h"
12
12
#include "diff_generate.h"
13
13
#include "patch.h"
14
+ #include "email.h"
14
15
#include "commit.h"
15
16
#include "index.h"
16
17
@@ -151,97 +152,12 @@ int git_diff_foreach(
151
152
return error ;
152
153
}
153
154
154
- static int diff_format_email_append_header_tobuf (
155
- git_buf * out ,
156
- const git_oid * id ,
157
- const git_signature * author ,
158
- const char * summary ,
159
- const char * body ,
160
- size_t patch_no ,
161
- size_t total_patches ,
162
- bool exclude_patchno_marker )
163
- {
164
- char idstr [GIT_OID_HEXSZ + 1 ];
165
- char date_str [GIT_DATE_RFC2822_SZ ];
166
- int error = 0 ;
167
-
168
- git_oid_fmt (idstr , id );
169
- idstr [GIT_OID_HEXSZ ] = '\0' ;
170
-
171
- if ((error = git__date_rfc2822_fmt (date_str , sizeof (date_str ),
172
- & author -> when )) < 0 )
173
- return error ;
174
-
175
- error = git_buf_printf (out ,
176
- "From %s Mon Sep 17 00:00:00 2001\n" \
177
- "From: %s <%s>\n" \
178
- "Date: %s\n" \
179
- "Subject: " ,
180
- idstr ,
181
- author -> name , author -> email ,
182
- date_str );
183
-
184
- if (error < 0 )
185
- return error ;
186
-
187
- if (!exclude_patchno_marker ) {
188
- if (total_patches == 1 ) {
189
- error = git_buf_puts (out , "[PATCH] " );
190
- } else {
191
- error = git_buf_printf (out , "[PATCH %" PRIuZ "/%" PRIuZ "] " ,
192
- patch_no , total_patches );
193
- }
194
-
195
- if (error < 0 )
196
- return error ;
197
- }
198
-
199
- error = git_buf_printf (out , "%s\n\n" , summary );
200
-
201
- if (body ) {
202
- git_buf_puts (out , body );
203
-
204
- if (out -> ptr [out -> size - 1 ] != '\n' )
205
- git_buf_putc (out , '\n' );
206
- }
207
-
208
- return error ;
209
- }
210
-
211
- static int diff_format_email_append_patches_tobuf (
212
- git_buf * out ,
213
- git_diff * diff )
214
- {
215
- size_t i , deltas ;
216
- int error = 0 ;
217
-
218
- deltas = git_diff_num_deltas (diff );
219
-
220
- for (i = 0 ; i < deltas ; ++ i ) {
221
- git_patch * patch = NULL ;
222
-
223
- if ((error = git_patch_from_diff (& patch , diff , i )) >= 0 )
224
- error = git_patch_to_buf (out , patch );
225
-
226
- git_patch_free (patch );
227
-
228
- if (error < 0 )
229
- break ;
230
- }
231
-
232
- return error ;
233
- }
234
-
235
155
int git_diff_format_email (
236
156
git_buf * out ,
237
157
git_diff * diff ,
238
158
const git_diff_format_email_options * opts )
239
159
{
240
- git_diff_stats * stats = NULL ;
241
- char * summary = NULL , * loc = NULL ;
242
- bool ignore_marker ;
243
- unsigned int format_flags = 0 ;
244
- size_t allocsize ;
160
+ git_email_create_options email_create_opts = GIT_EMAIL_CREATE_OPTIONS_INIT ;
245
161
int error ;
246
162
247
163
GIT_ASSERT_ARG (out );
@@ -252,64 +168,13 @@ int git_diff_format_email(
252
168
GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION ,
253
169
"git_format_email_options" );
254
170
255
- ignore_marker = (opts -> flags &
256
- GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER ) != 0 ;
257
-
258
- if (!ignore_marker ) {
259
- if (opts -> patch_no > opts -> total_patches ) {
260
- git_error_set (GIT_ERROR_INVALID ,
261
- "patch %" PRIuZ " out of range. max %" PRIuZ ,
262
- opts -> patch_no , opts -> total_patches );
263
- return -1 ;
264
- }
265
-
266
- if (opts -> patch_no == 0 ) {
267
- git_error_set (GIT_ERROR_INVALID ,
268
- "invalid patch no %" PRIuZ ". should be >0" , opts -> patch_no );
269
- return -1 ;
270
- }
271
- }
272
-
273
- /* the summary we receive may not be clean.
274
- * it could potentially contain new line characters
275
- * or not be set, sanitize, */
276
- if ((loc = strpbrk (opts -> summary , "\r\n" )) != NULL ) {
277
- size_t offset = 0 ;
278
-
279
- if ((offset = (loc - opts -> summary )) == 0 ) {
280
- git_error_set (GIT_ERROR_INVALID , "summary is empty" );
281
- error = -1 ;
282
- goto on_error ;
283
- }
284
-
285
- GIT_ERROR_CHECK_ALLOC_ADD (& allocsize , offset , 1 );
286
- summary = git__calloc (allocsize , sizeof (char ));
287
- GIT_ERROR_CHECK_ALLOC (summary );
288
-
289
- strncpy (summary , opts -> summary , offset );
290
- }
291
-
292
- error = diff_format_email_append_header_tobuf (out ,
293
- opts -> id , opts -> author , summary == NULL ? opts -> summary : summary ,
294
- opts -> body , opts -> patch_no , opts -> total_patches , ignore_marker );
295
-
296
- if (error < 0 )
297
- goto on_error ;
298
-
299
- format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY ;
300
-
301
- if ((error = git_buf_puts (out , "---\n" )) < 0 ||
302
- (error = git_diff_get_stats (& stats , diff )) < 0 ||
303
- (error = git_diff_stats_to_buf (out , stats , format_flags , 0 )) < 0 ||
304
- (error = git_buf_putc (out , '\n' )) < 0 ||
305
- (error = diff_format_email_append_patches_tobuf (out , diff )) < 0 )
306
- goto on_error ;
171
+ if ((opts -> flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER ) != 0 )
172
+ email_create_opts .subject_prefix = "" ;
307
173
308
- error = git_buf_puts (out , "--\nlibgit2 " LIBGIT2_VERSION "\n\n" );
309
174
310
- on_error :
311
- git__free ( summary );
312
- git_diff_stats_free ( stats );
175
+ error = git_email__append_from_diff ( out , diff , opts -> patch_no ,
176
+ opts -> total_patches , opts -> id , opts -> summary , opts -> body ,
177
+ opts -> author , & email_create_opts );
313
178
314
179
return error ;
315
180
}
0 commit comments