2 #include "clar_libgit2.h"
5 #include "diff_generate.h"
7 static git_repository *repo;
9 void test_email_create__initialize(void)
11 repo = cl_git_sandbox_init("diff_format_email");
14 void test_email_create__cleanup(void)
16 cl_git_sandbox_cleanup();
19 static void email_for_commit(
21 const char *commit_id,
22 git_email_create_options *opts)
25 git_commit *commit = NULL;
26 git_diff *diff = NULL;
28 git_oid_fromstr(&oid, commit_id);
30 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
32 cl_git_pass(git_email_create_from_commit(out, commit, opts));
35 git_commit_free(commit);
38 static void assert_email_match(
40 const char *commit_id,
41 git_email_create_options *opts)
43 git_buf buf = GIT_BUF_INIT;
45 email_for_commit(&buf, commit_id, opts);
46 cl_assert_equal_s(expected, git_buf_cstr(&buf));
48 git_buf_dispose(&buf);
51 static void assert_subject_match(
53 const char *commit_id,
54 git_email_create_options *opts)
56 git_buf buf = GIT_BUF_INIT;
59 email_for_commit(&buf, commit_id, opts);
61 cl_assert((loc = strstr(buf.ptr, "\nSubject: ")) != NULL);
62 git_buf_consume(&buf, (loc + 10));
63 git_buf_truncate_at_char(&buf, '\n');
65 cl_assert_equal_s(expected, git_buf_cstr(&buf));
67 git_buf_dispose(&buf);
70 void test_email_create__commit(void)
72 const char *expected =
73 "From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
74 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
75 "Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
76 "Subject: [PATCH] Modify some content\n" \
79 " file1.txt | 8 +++++---\n" \
80 " 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
82 "diff --git a/file1.txt b/file1.txt\n" \
83 "index 94aaae8..af8f41d 100644\n" \
86 "@@ -1,15 +1,17 @@\n" \
108 "libgit2 " LIBGIT2_VERSION "\n" \
112 expected, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL);
115 void test_email_create__custom_summary_and_body(void)
117 const char *expected = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
118 "From: Patrick Steinhardt <ps@pks.im>\n" \
119 "Date: Tue, 24 Nov 2015 13:34:39 +0100\n" \
120 "Subject: [PPPPPATCH 2/4] This is a subject\n" \
122 "Modify content of file3.txt by appending a new line. Make this\n" \
123 "commit message somewhat longer to test behavior with newlines\n" \
124 "embedded in the message body.\n" \
126 "Also test if new paragraphs are included correctly.\n" \
128 " file3.txt | 1 +\n" \
129 " 1 file changed, 1 insertion(+)\n" \
131 "diff --git a/file3.txt b/file3.txt\n" \
132 "index 9a2d780..7309653 100644\n" \
133 "--- a/file3.txt\n" \
134 "+++ b/file3.txt\n" \
135 "@@ -3,3 +3,4 @@ file3!\n" \
141 "libgit2 " LIBGIT2_VERSION "\n" \
144 const char *summary = "This is a subject\nwith\nnewlines";
145 const char *body = "Modify content of file3.txt by appending a new line. Make this\n" \
146 "commit message somewhat longer to test behavior with newlines\n" \
147 "embedded in the message body.\n" \
149 "Also test if new paragraphs are included correctly.";
152 git_commit *commit = NULL;
153 git_diff *diff = NULL;
154 git_buf buf = GIT_BUF_INIT;
155 git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
157 opts.subject_prefix = "PPPPPATCH";
159 git_oid_fromstr(&oid, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270");
160 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
161 cl_git_pass(git_diff__commit(&diff, repo, commit, NULL));
162 cl_git_pass(git_email_create_from_diff(&buf, diff, 2, 4, &oid, summary, body, git_commit_author(commit), &opts));
164 cl_assert_equal_s(expected, git_buf_cstr(&buf));
167 git_commit_free(commit);
168 git_buf_dispose(&buf);
171 void test_email_create__mode_change(void)
173 const char *expected =
174 "From 7ade76dd34bba4733cf9878079f9fd4a456a9189 Mon Sep 17 00:00:00 2001\n" \
175 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
176 "Date: Thu, 10 Apr 2014 10:05:03 +0200\n" \
177 "Subject: [PATCH] Update permissions\n" \
180 " file1.txt.renamed | 0\n" \
181 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
182 " mode change 100644 => 100755 file1.txt.renamed\n" \
184 "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
185 "old mode 100644\n" \
186 "new mode 100755\n" \
188 "libgit2 " LIBGIT2_VERSION "\n" \
191 assert_email_match(expected, "7ade76dd34bba4733cf9878079f9fd4a456a9189", NULL);
194 void test_email_create__rename(void)
196 const char *expected =
197 "From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
198 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
199 "Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
200 "Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
203 " file1.txt => file1.txt.renamed | 4 ++--\n" \
204 " 1 file changed, 2 insertions(+), 2 deletions(-)\n" \
206 "diff --git a/file1.txt b/file1.txt.renamed\n" \
207 "similarity index 86%\n" \
208 "rename from file1.txt\n" \
209 "rename to file1.txt.renamed\n" \
210 "index af8f41d..a97157a 100644\n" \
211 "--- a/file1.txt\n" \
212 "+++ b/file1.txt.renamed\n" \
213 "@@ -3,13 +3,13 @@ file1.txt\n" \
218 "+file1.txt_renamed\n" \
225 "+file1.txt_renamed\n" \
230 "libgit2 " LIBGIT2_VERSION "\n" \
233 assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", NULL);
236 void test_email_create__rename_as_add_delete(void)
238 const char *expected =
239 "From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
240 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
241 "Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
242 "Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
245 " file1.txt | 17 -----------------\n" \
246 " file1.txt.renamed | 17 +++++++++++++++++\n" \
247 " 2 files changed, 17 insertions(+), 17 deletions(-)\n" \
248 " delete mode 100644 file1.txt\n" \
249 " create mode 100644 file1.txt.renamed\n" \
251 "diff --git a/file1.txt b/file1.txt\n" \
252 "deleted file mode 100644\n" \
253 "index af8f41d..0000000\n" \
254 "--- a/file1.txt\n" \
256 "@@ -1,17 +0,0 @@\n" \
274 "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
275 "new file mode 100644\n" \
276 "index 0000000..a97157a\n" \
278 "+++ b/file1.txt.renamed\n" \
279 "@@ -0,0 +1,17 @@\n" \
285 "+file1.txt_renamed\n" \
291 "+file1.txt_renamed\n" \
298 "libgit2 " LIBGIT2_VERSION "\n" \
301 git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
302 opts.flags |= GIT_EMAIL_CREATE_NO_RENAMES;
304 assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
307 void test_email_create__binary(void)
309 const char *expected =
310 "From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
311 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
312 "Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
313 "Subject: [PATCH] Modified binary file\n" \
316 " binary.bin | Bin 3 -> 5 bytes\n" \
317 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
319 "diff --git a/binary.bin b/binary.bin\n" \
320 "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \
321 "GIT binary patch\n" \
323 "Mc${NkU}WL~000&M4gdfE\n" \
326 "Kc${Nk-~s>u4FC%O\n" \
329 "libgit2 " LIBGIT2_VERSION "\n" \
332 assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", NULL);
335 void test_email_create__binary_not_included(void)
337 const char *expected =
338 "From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
339 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
340 "Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
341 "Subject: [PATCH] Modified binary file\n" \
344 " binary.bin | Bin 3 -> 5 bytes\n" \
345 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
347 "diff --git a/binary.bin b/binary.bin\n" \
348 "index bd474b2..9ac35ff 100644\n" \
349 "Binary files a/binary.bin and b/binary.bin differ\n" \
351 "libgit2 " LIBGIT2_VERSION "\n" \
354 git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
355 opts.diff_opts.flags &= ~GIT_DIFF_SHOW_BINARY;
357 assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
360 void test_email_create__commit_subjects(void)
362 git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
364 assert_subject_match("[PATCH] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
366 opts.reroll_number = 42;
367 assert_subject_match("[PATCH v42] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
369 opts.flags |= GIT_EMAIL_CREATE_ALWAYS_NUMBER;
370 assert_subject_match("[PATCH v42 1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
372 opts.start_number = 9;
373 assert_subject_match("[PATCH v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
375 opts.subject_prefix = "";
376 assert_subject_match("[v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
378 opts.reroll_number = 0;
379 assert_subject_match("[9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
381 opts.start_number = 0;
382 assert_subject_match("[1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
384 opts.flags = GIT_EMAIL_CREATE_OMIT_NUMBERS;
385 assert_subject_match("Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);