]> git.proxmox.com Git - libgit2.git/blob - tests/diff/format_email.c
612804c4288bb2f84272934cd76b48f80c8c071b
[libgit2.git] / tests / diff / format_email.c
1 #include "clar.h"
2 #include "clar_libgit2.h"
3
4 #include "commit.h"
5 #include "diff.h"
6 #include "diff_generate.h"
7
8 static git_repository *repo;
9
10 void test_diff_format_email__initialize(void)
11 {
12 repo = cl_git_sandbox_init("diff_format_email");
13 }
14
15 void test_diff_format_email__cleanup(void)
16 {
17 cl_git_sandbox_cleanup();
18 }
19
20 #ifndef GIT_DEPRECATE_HARD
21 static void assert_email_match(
22 const char *expected,
23 const char *oidstr,
24 git_diff_format_email_options *opts)
25 {
26 git_oid oid;
27 git_commit *commit = NULL;
28 git_diff *diff = NULL;
29 git_buf buf = GIT_BUF_INIT;
30
31 git_oid_fromstr(&oid, oidstr);
32
33 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
34
35 opts->id = git_commit_id(commit);
36 opts->author = git_commit_author(commit);
37 if (!opts->summary)
38 opts->summary = git_commit_summary(commit);
39
40 cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
41 cl_git_pass(git_diff_format_email(&buf, diff, opts));
42
43 cl_assert_equal_s(expected, buf.ptr);
44 git_buf_dispose(&buf);
45
46 cl_git_pass(git_diff_commit_as_email(
47 &buf, repo, commit, 1, 1, opts->flags, NULL));
48 cl_assert_equal_s(expected, buf.ptr);
49
50 git_diff_free(diff);
51 git_commit_free(commit);
52 git_buf_dispose(&buf);
53 }
54 #endif
55
56 void test_diff_format_email__simple(void)
57 {
58 #ifndef GIT_DEPRECATE_HARD
59 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
60 const char *email =
61 "From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
62 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
63 "Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
64 "Subject: [PATCH] Modify some content\n" \
65 "\n" \
66 "---\n" \
67 " file1.txt | 8 +++++---\n" \
68 " 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
69 "\n" \
70 "diff --git a/file1.txt b/file1.txt\n" \
71 "index 94aaae8..af8f41d 100644\n" \
72 "--- a/file1.txt\n" \
73 "+++ b/file1.txt\n" \
74 "@@ -1,15 +1,17 @@\n" \
75 " file1.txt\n" \
76 " file1.txt\n" \
77 "+_file1.txt_\n" \
78 " file1.txt\n" \
79 " file1.txt\n" \
80 " file1.txt\n" \
81 " file1.txt\n" \
82 "+\n" \
83 "+\n" \
84 " file1.txt\n" \
85 " file1.txt\n" \
86 " file1.txt\n" \
87 " file1.txt\n" \
88 " file1.txt\n" \
89 "-file1.txt\n" \
90 "-file1.txt\n" \
91 "-file1.txt\n" \
92 "+_file1.txt_\n" \
93 "+_file1.txt_\n" \
94 " file1.txt\n" \
95 "--\n" \
96 "libgit2 " LIBGIT2_VERSION "\n" \
97 "\n";
98
99 assert_email_match(
100 email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
101 #endif
102 }
103
104 void test_diff_format_email__with_message(void)
105 {
106 #ifndef GIT_DEPRECATE_HARD
107 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
108 const char *email = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
109 "From: Patrick Steinhardt <ps@pks.im>\n" \
110 "Date: Tue, 24 Nov 2015 13:34:39 +0100\n" \
111 "Subject: [PATCH] Modify content with message\n" \
112 "\n" \
113 "Modify content of file3.txt by appending a new line. Make this\n" \
114 "commit message somewhat longer to test behavior with newlines\n" \
115 "embedded in the message body.\n" \
116 "\n" \
117 "Also test if new paragraphs are included correctly.\n" \
118 "---\n" \
119 " file3.txt | 1 +\n" \
120 " 1 file changed, 1 insertion(+)\n" \
121 "\n" \
122 "diff --git a/file3.txt b/file3.txt\n" \
123 "index 9a2d780..7309653 100644\n" \
124 "--- a/file3.txt\n" \
125 "+++ b/file3.txt\n" \
126 "@@ -3,3 +3,4 @@ file3!\n" \
127 " file3\n" \
128 " file3\n" \
129 " file3\n" \
130 "+file3\n" \
131 "--\n" \
132 "libgit2 " LIBGIT2_VERSION "\n" \
133 "\n";
134
135 opts.body = "Modify content of file3.txt by appending a new line. Make this\n" \
136 "commit message somewhat longer to test behavior with newlines\n" \
137 "embedded in the message body.\n" \
138 "\n" \
139 "Also test if new paragraphs are included correctly.";
140
141 assert_email_match(
142 email, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270", &opts);
143 #endif
144 }
145
146
147 void test_diff_format_email__multiple(void)
148 {
149 #ifndef GIT_DEPRECATE_HARD
150 git_oid oid;
151 git_commit *commit = NULL;
152 git_diff *diff = NULL;
153 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
154 git_buf buf = GIT_BUF_INIT;
155
156 const char *email =
157 "From 10808fe9c9be5a190c0ba68d1a002233fb363508 Mon Sep 17 00:00:00 2001\n" \
158 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
159 "Date: Thu, 10 Apr 2014 19:37:05 +0200\n" \
160 "Subject: [PATCH 1/2] Added file2.txt file3.txt\n" \
161 "\n" \
162 "---\n" \
163 " file2.txt | 5 +++++\n" \
164 " file3.txt | 5 +++++\n" \
165 " 2 files changed, 10 insertions(+)\n" \
166 " create mode 100644 file2.txt\n" \
167 " create mode 100644 file3.txt\n" \
168 "\n" \
169 "diff --git a/file2.txt b/file2.txt\n" \
170 "new file mode 100644\n" \
171 "index 0000000..e909123\n" \
172 "--- /dev/null\n" \
173 "+++ b/file2.txt\n" \
174 "@@ -0,0 +1,5 @@\n" \
175 "+file2\n" \
176 "+file2\n" \
177 "+file2\n" \
178 "+file2\n" \
179 "+file2\n" \
180 "diff --git a/file3.txt b/file3.txt\n" \
181 "new file mode 100644\n" \
182 "index 0000000..9435022\n" \
183 "--- /dev/null\n" \
184 "+++ b/file3.txt\n" \
185 "@@ -0,0 +1,5 @@\n" \
186 "+file3\n" \
187 "+file3\n" \
188 "+file3\n" \
189 "+file3\n" \
190 "+file3\n" \
191 "--\n" \
192 "libgit2 " LIBGIT2_VERSION "\n" \
193 "\n" \
194 "From 873806f6f27e631eb0b23e4b56bea2bfac14a373 Mon Sep 17 00:00:00 2001\n" \
195 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
196 "Date: Thu, 10 Apr 2014 19:37:36 +0200\n" \
197 "Subject: [PATCH 2/2] Modified file2.txt, file3.txt\n" \
198 "\n" \
199 "---\n" \
200 " file2.txt | 2 +-\n" \
201 " file3.txt | 2 +-\n" \
202 " 2 files changed, 2 insertions(+), 2 deletions(-)\n" \
203 "\n" \
204 "diff --git a/file2.txt b/file2.txt\n" \
205 "index e909123..7aff11d 100644\n" \
206 "--- a/file2.txt\n" \
207 "+++ b/file2.txt\n" \
208 "@@ -1,5 +1,5 @@\n" \
209 " file2\n" \
210 " file2\n" \
211 " file2\n" \
212 "-file2\n" \
213 "+file2!\n" \
214 " file2\n" \
215 "diff --git a/file3.txt b/file3.txt\n" \
216 "index 9435022..9a2d780 100644\n" \
217 "--- a/file3.txt\n" \
218 "+++ b/file3.txt\n" \
219 "@@ -1,5 +1,5 @@\n" \
220 " file3\n" \
221 "-file3\n" \
222 "+file3!\n" \
223 " file3\n" \
224 " file3\n" \
225 " file3\n" \
226 "--\n" \
227 "libgit2 " LIBGIT2_VERSION "\n" \
228 "\n";
229
230
231 git_oid_fromstr(&oid, "10808fe9c9be5a190c0ba68d1a002233fb363508");
232 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
233
234 opts.id = git_commit_id(commit);
235 opts.author = git_commit_author(commit);
236 opts.summary = git_commit_summary(commit);
237 opts.patch_no = 1;
238 opts.total_patches = 2;
239
240 cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
241 cl_git_pass(git_diff_format_email(&buf, diff, &opts));
242
243 git_diff_free(diff);
244 git_commit_free(commit);
245 diff = NULL;
246 commit = NULL;
247
248 git_oid_fromstr(&oid, "873806f6f27e631eb0b23e4b56bea2bfac14a373");
249 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
250
251 opts.id = git_commit_id(commit);
252 opts.author = git_commit_author(commit);
253 opts.summary = git_commit_summary(commit);
254 opts.patch_no = 2;
255 opts.total_patches = 2;
256
257 cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
258 cl_git_pass(git_diff_format_email(&buf, diff, &opts));
259
260 cl_assert_equal_s(email, buf.ptr);
261
262 git_diff_free(diff);
263 git_commit_free(commit);
264 git_buf_dispose(&buf);
265 #endif
266 }
267
268 void test_diff_format_email__exclude_marker(void)
269 {
270 #ifndef GIT_DEPRECATE_HARD
271 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
272 const char *email =
273 "From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
274 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
275 "Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
276 "Subject: Modify some content\n" \
277 "\n" \
278 "---\n" \
279 " file1.txt | 8 +++++---\n" \
280 " 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
281 "\n" \
282 "diff --git a/file1.txt b/file1.txt\n" \
283 "index 94aaae8..af8f41d 100644\n" \
284 "--- a/file1.txt\n" \
285 "+++ b/file1.txt\n" \
286 "@@ -1,15 +1,17 @@\n" \
287 " file1.txt\n" \
288 " file1.txt\n" \
289 "+_file1.txt_\n" \
290 " file1.txt\n" \
291 " file1.txt\n" \
292 " file1.txt\n" \
293 " file1.txt\n" \
294 "+\n" \
295 "+\n" \
296 " file1.txt\n" \
297 " file1.txt\n" \
298 " file1.txt\n" \
299 " file1.txt\n" \
300 " file1.txt\n" \
301 "-file1.txt\n" \
302 "-file1.txt\n" \
303 "-file1.txt\n" \
304 "+_file1.txt_\n" \
305 "+_file1.txt_\n" \
306 " file1.txt\n" \
307 "--\n" \
308 "libgit2 " LIBGIT2_VERSION "\n" \
309 "\n";
310
311 opts.flags |= GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER;
312
313 assert_email_match(
314 email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
315 #endif
316 }
317
318 void test_diff_format_email__invalid_no(void)
319 {
320 #ifndef GIT_DEPRECATE_HARD
321 git_oid oid;
322 git_commit *commit = NULL;
323 git_diff *diff = NULL;
324 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
325 git_buf buf = GIT_BUF_INIT;
326
327 git_oid_fromstr(&oid, "9264b96c6d104d0e07ae33d3007b6a48246c6f92");
328
329 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
330
331 opts.id = git_commit_id(commit);
332 opts.author = git_commit_author(commit);
333 opts.summary = git_commit_summary(commit);
334 opts.patch_no = 2;
335 opts.total_patches = 1;
336
337 cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
338 cl_git_fail(git_diff_format_email(&buf, diff, &opts));
339 cl_git_fail(git_diff_commit_as_email(&buf, repo, commit, 2, 1, 0, NULL));
340
341 git_diff_free(diff);
342 git_commit_free(commit);
343 git_buf_dispose(&buf);
344 #endif
345 }
346
347 void test_diff_format_email__mode_change(void)
348 {
349 #ifndef GIT_DEPRECATE_HARD
350 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
351 const char *email =
352 "From 7ade76dd34bba4733cf9878079f9fd4a456a9189 Mon Sep 17 00:00:00 2001\n" \
353 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
354 "Date: Thu, 10 Apr 2014 10:05:03 +0200\n" \
355 "Subject: [PATCH] Update permissions\n" \
356 "\n" \
357 "---\n" \
358 " file1.txt.renamed | 0\n" \
359 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
360 " mode change 100644 => 100755 file1.txt.renamed\n" \
361 "\n" \
362 "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
363 "old mode 100644\n" \
364 "new mode 100755\n" \
365 "--\n" \
366 "libgit2 " LIBGIT2_VERSION "\n" \
367 "\n";
368
369 assert_email_match(
370 email, "7ade76dd34bba4733cf9878079f9fd4a456a9189", &opts);
371 #endif
372 }
373
374 void test_diff_format_email__rename_add_remove(void)
375 {
376 #ifndef GIT_DEPRECATE_HARD
377 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
378 const char *email =
379 "From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
380 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
381 "Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
382 "Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
383 "\n" \
384 "---\n" \
385 " file1.txt | 17 -----------------\n" \
386 " file1.txt.renamed | 17 +++++++++++++++++\n" \
387 " 2 files changed, 17 insertions(+), 17 deletions(-)\n" \
388 " delete mode 100644 file1.txt\n" \
389 " create mode 100644 file1.txt.renamed\n" \
390 "\n" \
391 "diff --git a/file1.txt b/file1.txt\n" \
392 "deleted file mode 100644\n" \
393 "index af8f41d..0000000\n" \
394 "--- a/file1.txt\n" \
395 "+++ /dev/null\n" \
396 "@@ -1,17 +0,0 @@\n" \
397 "-file1.txt\n" \
398 "-file1.txt\n" \
399 "-_file1.txt_\n" \
400 "-file1.txt\n" \
401 "-file1.txt\n" \
402 "-file1.txt\n" \
403 "-file1.txt\n" \
404 "-\n" \
405 "-\n" \
406 "-file1.txt\n" \
407 "-file1.txt\n" \
408 "-file1.txt\n" \
409 "-file1.txt\n" \
410 "-file1.txt\n" \
411 "-_file1.txt_\n" \
412 "-_file1.txt_\n" \
413 "-file1.txt\n" \
414 "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
415 "new file mode 100644\n" \
416 "index 0000000..a97157a\n" \
417 "--- /dev/null\n" \
418 "+++ b/file1.txt.renamed\n" \
419 "@@ -0,0 +1,17 @@\n" \
420 "+file1.txt\n" \
421 "+file1.txt\n" \
422 "+_file1.txt_\n" \
423 "+file1.txt\n" \
424 "+file1.txt\n" \
425 "+file1.txt_renamed\n" \
426 "+file1.txt\n" \
427 "+\n" \
428 "+\n" \
429 "+file1.txt\n" \
430 "+file1.txt\n" \
431 "+file1.txt_renamed\n" \
432 "+file1.txt\n" \
433 "+file1.txt\n" \
434 "+_file1.txt_\n" \
435 "+_file1.txt_\n" \
436 "+file1.txt\n" \
437 "--\n" \
438 "libgit2 " LIBGIT2_VERSION "\n" \
439 "\n";
440
441 assert_email_match(
442 email, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
443 #endif
444 }
445
446 void test_diff_format_email__multiline_summary(void)
447 {
448 #ifndef GIT_DEPRECATE_HARD
449 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
450 const char *email =
451 "From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
452 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
453 "Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
454 "Subject: [PATCH] Modify some content\n" \
455 "\n" \
456 "---\n" \
457 " file1.txt | 8 +++++---\n" \
458 " 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
459 "\n" \
460 "diff --git a/file1.txt b/file1.txt\n" \
461 "index 94aaae8..af8f41d 100644\n" \
462 "--- a/file1.txt\n" \
463 "+++ b/file1.txt\n" \
464 "@@ -1,15 +1,17 @@\n" \
465 " file1.txt\n" \
466 " file1.txt\n" \
467 "+_file1.txt_\n" \
468 " file1.txt\n" \
469 " file1.txt\n" \
470 " file1.txt\n" \
471 " file1.txt\n" \
472 "+\n" \
473 "+\n" \
474 " file1.txt\n" \
475 " file1.txt\n" \
476 " file1.txt\n" \
477 " file1.txt\n" \
478 " file1.txt\n" \
479 "-file1.txt\n" \
480 "-file1.txt\n" \
481 "-file1.txt\n" \
482 "+_file1.txt_\n" \
483 "+_file1.txt_\n" \
484 " file1.txt\n" \
485 "--\n" \
486 "libgit2 " LIBGIT2_VERSION "\n" \
487 "\n";
488
489 opts.summary = "Modify some content\nSome extra stuff here";
490
491 assert_email_match(
492 email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
493 #endif
494 }
495
496 void test_diff_format_email__binary(void)
497 {
498 #ifndef GIT_DEPRECATE_HARD
499 git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
500 const char *email =
501 "From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
502 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
503 "Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
504 "Subject: [PATCH] Modified binary file\n" \
505 "\n" \
506 "---\n" \
507 " binary.bin | Bin 3 -> 5 bytes\n" \
508 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
509 "\n" \
510 "diff --git a/binary.bin b/binary.bin\n" \
511 "index bd474b2..9ac35ff 100644\n" \
512 "Binary files a/binary.bin and b/binary.bin differ\n" \
513 "--\n" \
514 "libgit2 " LIBGIT2_VERSION "\n" \
515 "\n";
516
517 opts.summary = "Modified binary file";
518
519 assert_email_match(
520 email, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
521 #endif
522 }
523