2 #include "clar_libgit2.h"
4 #include "diff_generate.h"
6 static git_repository
*repo
;
8 void test_email_create__initialize(void)
10 repo
= cl_git_sandbox_init("diff_format_email");
13 void test_email_create__cleanup(void)
15 cl_git_sandbox_cleanup();
18 static void email_for_commit(
20 const char *commit_id
,
21 git_email_create_options
*opts
)
24 git_commit
*commit
= NULL
;
25 git_diff
*diff
= NULL
;
27 git_oid_fromstr(&oid
, commit_id
);
29 cl_git_pass(git_commit_lookup(&commit
, repo
, &oid
));
31 cl_git_pass(git_email_create_from_commit(out
, commit
, opts
));
34 git_commit_free(commit
);
37 static void assert_email_match(
39 const char *commit_id
,
40 git_email_create_options
*opts
)
42 git_buf buf
= GIT_BUF_INIT
;
44 email_for_commit(&buf
, commit_id
, opts
);
45 cl_assert_equal_s(expected
, buf
.ptr
);
47 git_buf_dispose(&buf
);
50 static void assert_subject_match(
52 const char *commit_id
,
53 git_email_create_options
*opts
)
55 git_buf buf
= GIT_BUF_INIT
;
58 email_for_commit(&buf
, commit_id
, opts
);
60 cl_assert((subject
= strstr(buf
.ptr
, "\nSubject: ")) != NULL
);
63 if ((nl
= strchr(subject
, '\n')) != NULL
)
66 cl_assert_equal_s(expected
, subject
);
68 git_buf_dispose(&buf
);
71 void test_email_create__commit(void)
73 const char *expected
=
74 "From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
75 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
76 "Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
77 "Subject: [PATCH] Modify some content\n" \
80 " file1.txt | 8 +++++---\n" \
81 " 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
83 "diff --git a/file1.txt b/file1.txt\n" \
84 "index 94aaae8..af8f41d 100644\n" \
87 "@@ -1,15 +1,17 @@\n" \
109 "libgit2 " LIBGIT2_VERSION
"\n" \
113 expected
, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL
);
116 void test_email_create__rename(void)
118 const char *expected
=
119 "From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
120 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
121 "Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
122 "Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
125 " file1.txt => file1.txt.renamed | 4 ++--\n" \
126 " 1 file changed, 2 insertions(+), 2 deletions(-)\n" \
128 "diff --git a/file1.txt b/file1.txt.renamed\n" \
129 "similarity index 86%\n" \
130 "rename from file1.txt\n" \
131 "rename to file1.txt.renamed\n" \
132 "index af8f41d..a97157a 100644\n" \
133 "--- a/file1.txt\n" \
134 "+++ b/file1.txt.renamed\n" \
135 "@@ -3,13 +3,13 @@ file1.txt\n" \
140 "+file1.txt_renamed\n" \
147 "+file1.txt_renamed\n" \
152 "libgit2 " LIBGIT2_VERSION
"\n" \
155 assert_email_match(expected
, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", NULL
);
158 void test_email_create__rename_as_add_delete(void)
160 const char *expected
=
161 "From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
162 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
163 "Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
164 "Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
167 " file1.txt | 17 -----------------\n" \
168 " file1.txt.renamed | 17 +++++++++++++++++\n" \
169 " 2 files changed, 17 insertions(+), 17 deletions(-)\n" \
170 " delete mode 100644 file1.txt\n" \
171 " create mode 100644 file1.txt.renamed\n" \
173 "diff --git a/file1.txt b/file1.txt\n" \
174 "deleted file mode 100644\n" \
175 "index af8f41d..0000000\n" \
176 "--- a/file1.txt\n" \
178 "@@ -1,17 +0,0 @@\n" \
196 "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
197 "new file mode 100644\n" \
198 "index 0000000..a97157a\n" \
200 "+++ b/file1.txt.renamed\n" \
201 "@@ -0,0 +1,17 @@\n" \
207 "+file1.txt_renamed\n" \
213 "+file1.txt_renamed\n" \
220 "libgit2 " LIBGIT2_VERSION
"\n" \
223 git_email_create_options opts
= GIT_EMAIL_CREATE_OPTIONS_INIT
;
224 opts
.flags
|= GIT_EMAIL_CREATE_NO_RENAMES
;
226 assert_email_match(expected
, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts
);
229 void test_email_create__binary(void)
231 const char *expected
=
232 "From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
233 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
234 "Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
235 "Subject: [PATCH] Modified binary file\n" \
238 " binary.bin | Bin 3 -> 5 bytes\n" \
239 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
241 "diff --git a/binary.bin b/binary.bin\n" \
242 "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \
243 "GIT binary patch\n" \
245 "Mc${NkU}WL~000&M4gdfE\n" \
248 "Kc${Nk-~s>u4FC%O\n" \
251 "libgit2 " LIBGIT2_VERSION
"\n" \
254 assert_email_match(expected
, "8d7523f6fcb2404257889abe0d96f093d9f524f9", NULL
);
257 void test_email_create__binary_not_included(void)
259 const char *expected
=
260 "From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
261 "From: Jacques Germishuys <jacquesg@striata.com>\n" \
262 "Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
263 "Subject: [PATCH] Modified binary file\n" \
266 " binary.bin | Bin 3 -> 5 bytes\n" \
267 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
269 "diff --git a/binary.bin b/binary.bin\n" \
270 "index bd474b2..9ac35ff 100644\n" \
271 "Binary files a/binary.bin and b/binary.bin differ\n" \
273 "libgit2 " LIBGIT2_VERSION
"\n" \
276 git_email_create_options opts
= GIT_EMAIL_CREATE_OPTIONS_INIT
;
277 opts
.diff_opts
.flags
&= ~GIT_DIFF_SHOW_BINARY
;
279 assert_email_match(expected
, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts
);
282 void test_email_create__custom_summary_and_body(void)
284 const char *expected
= "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
285 "From: Patrick Steinhardt <ps@pks.im>\n" \
286 "Date: Tue, 24 Nov 2015 13:34:39 +0100\n" \
287 "Subject: [PPPPPATCH 2/4] This is a subject\n" \
289 "Modify content of file3.txt by appending a new line. Make this\n" \
290 "commit message somewhat longer to test behavior with newlines\n" \
291 "embedded in the message body.\n" \
293 "Also test if new paragraphs are included correctly.\n" \
295 " file3.txt | 1 +\n" \
296 " 1 file changed, 1 insertion(+)\n" \
298 "diff --git a/file3.txt b/file3.txt\n" \
299 "index 9a2d780..7309653 100644\n" \
300 "--- a/file3.txt\n" \
301 "+++ b/file3.txt\n" \
302 "@@ -3,3 +3,4 @@ file3!\n" \
308 "libgit2 " LIBGIT2_VERSION
"\n" \
311 const char *summary
= "This is a subject\nwith\nnewlines";
312 const char *body
= "Modify content of file3.txt by appending a new line. Make this\n" \
313 "commit message somewhat longer to test behavior with newlines\n" \
314 "embedded in the message body.\n" \
316 "Also test if new paragraphs are included correctly.";
319 git_commit
*commit
= NULL
;
320 git_diff
*diff
= NULL
;
321 git_buf buf
= GIT_BUF_INIT
;
322 git_email_create_options opts
= GIT_EMAIL_CREATE_OPTIONS_INIT
;
324 opts
.subject_prefix
= "PPPPPATCH";
326 git_oid_fromstr(&oid
, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270");
327 cl_git_pass(git_commit_lookup(&commit
, repo
, &oid
));
328 cl_git_pass(git_diff__commit(&diff
, repo
, commit
, NULL
));
329 cl_git_pass(git_email_create_from_diff(&buf
, diff
, 2, 4, &oid
, summary
, body
, git_commit_author(commit
), &opts
));
331 cl_assert_equal_s(expected
, buf
.ptr
);
334 git_commit_free(commit
);
335 git_buf_dispose(&buf
);
338 void test_email_create__commit_subjects(void)
340 git_email_create_options opts
= GIT_EMAIL_CREATE_OPTIONS_INIT
;
342 assert_subject_match("[PATCH] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);
344 opts
.reroll_number
= 42;
345 assert_subject_match("[PATCH v42] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);
347 opts
.flags
|= GIT_EMAIL_CREATE_ALWAYS_NUMBER
;
348 assert_subject_match("[PATCH v42 1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);
350 opts
.start_number
= 9;
351 assert_subject_match("[PATCH v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);
353 opts
.subject_prefix
= "";
354 assert_subject_match("[v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);
356 opts
.reroll_number
= 0;
357 assert_subject_match("[9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);
359 opts
.start_number
= 0;
360 assert_subject_match("[1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);
362 opts
.flags
= GIT_EMAIL_CREATE_OMIT_NUMBERS
;
363 assert_subject_match("Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts
);