]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/email/create.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / email / create.c
CommitLineData
c25aa7cd
PP
1#include "clar.h"
2#include "clar_libgit2.h"
3
c25aa7cd
PP
4#include "diff_generate.h"
5
6static git_repository *repo;
7
8void test_email_create__initialize(void)
9{
10 repo = cl_git_sandbox_init("diff_format_email");
11}
12
13void test_email_create__cleanup(void)
14{
15 cl_git_sandbox_cleanup();
16}
17
18static void email_for_commit(
19 git_buf *out,
20 const char *commit_id,
21 git_email_create_options *opts)
22{
23 git_oid oid;
24 git_commit *commit = NULL;
25 git_diff *diff = NULL;
26
27 git_oid_fromstr(&oid, commit_id);
28
29 cl_git_pass(git_commit_lookup(&commit, repo, &oid));
30
31 cl_git_pass(git_email_create_from_commit(out, commit, opts));
32
33 git_diff_free(diff);
34 git_commit_free(commit);
35}
36
37static void assert_email_match(
38 const char *expected,
39 const char *commit_id,
40 git_email_create_options *opts)
41{
42 git_buf buf = GIT_BUF_INIT;
43
44 email_for_commit(&buf, commit_id, opts);
e579e0f7 45 cl_assert_equal_s(expected, buf.ptr);
c25aa7cd
PP
46
47 git_buf_dispose(&buf);
48}
49
50static void assert_subject_match(
51 const char *expected,
52 const char *commit_id,
53 git_email_create_options *opts)
54{
55 git_buf buf = GIT_BUF_INIT;
e579e0f7 56 char *subject, *nl;
c25aa7cd
PP
57
58 email_for_commit(&buf, commit_id, opts);
59
e579e0f7
MB
60 cl_assert((subject = strstr(buf.ptr, "\nSubject: ")) != NULL);
61 subject += 10;
c25aa7cd 62
e579e0f7
MB
63 if ((nl = strchr(subject, '\n')) != NULL)
64 *nl = '\0';
65
66 cl_assert_equal_s(expected, subject);
c25aa7cd
PP
67
68 git_buf_dispose(&buf);
69}
70
71void test_email_create__commit(void)
72{
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" \
78 "\n" \
79 "---\n" \
80 " file1.txt | 8 +++++---\n" \
81 " 1 file changed, 5 insertions(+), 3 deletions(-)\n" \
82 "\n" \
83 "diff --git a/file1.txt b/file1.txt\n" \
84 "index 94aaae8..af8f41d 100644\n" \
85 "--- a/file1.txt\n" \
86 "+++ b/file1.txt\n" \
87 "@@ -1,15 +1,17 @@\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 "+\n" \
97 " file1.txt\n" \
98 " file1.txt\n" \
99 " file1.txt\n" \
100 " file1.txt\n" \
101 " file1.txt\n" \
102 "-file1.txt\n" \
103 "-file1.txt\n" \
104 "-file1.txt\n" \
105 "+_file1.txt_\n" \
106 "+_file1.txt_\n" \
107 " file1.txt\n" \
108 "--\n" \
109 "libgit2 " LIBGIT2_VERSION "\n" \
110 "\n";
111
112 assert_email_match(
113 expected, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL);
114}
115
116void test_email_create__rename(void)
117{
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" \
123 "\n" \
124 "---\n" \
125 " file1.txt => file1.txt.renamed | 4 ++--\n" \
126 " 1 file changed, 2 insertions(+), 2 deletions(-)\n" \
127 "\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" \
136 " _file1.txt_\n" \
137 " file1.txt\n" \
138 " file1.txt\n" \
139 "-file1.txt\n" \
140 "+file1.txt_renamed\n" \
141 " file1.txt\n" \
142 " \n" \
143 " \n" \
144 " file1.txt\n" \
145 " file1.txt\n" \
146 "-file1.txt\n" \
147 "+file1.txt_renamed\n" \
148 " file1.txt\n" \
149 " file1.txt\n" \
150 " _file1.txt_\n" \
151 "--\n" \
152 "libgit2 " LIBGIT2_VERSION "\n" \
153 "\n";
154
155 assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", NULL);
156}
157
158void test_email_create__rename_as_add_delete(void)
159{
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" \
165 "\n" \
166 "---\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" \
172 "\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" \
177 "+++ /dev/null\n" \
178 "@@ -1,17 +0,0 @@\n" \
179 "-file1.txt\n" \
180 "-file1.txt\n" \
181 "-_file1.txt_\n" \
182 "-file1.txt\n" \
183 "-file1.txt\n" \
184 "-file1.txt\n" \
185 "-file1.txt\n" \
186 "-\n" \
187 "-\n" \
188 "-file1.txt\n" \
189 "-file1.txt\n" \
190 "-file1.txt\n" \
191 "-file1.txt\n" \
192 "-file1.txt\n" \
193 "-_file1.txt_\n" \
194 "-_file1.txt_\n" \
195 "-file1.txt\n" \
196 "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
197 "new file mode 100644\n" \
198 "index 0000000..a97157a\n" \
199 "--- /dev/null\n" \
200 "+++ b/file1.txt.renamed\n" \
201 "@@ -0,0 +1,17 @@\n" \
202 "+file1.txt\n" \
203 "+file1.txt\n" \
204 "+_file1.txt_\n" \
205 "+file1.txt\n" \
206 "+file1.txt\n" \
207 "+file1.txt_renamed\n" \
208 "+file1.txt\n" \
209 "+\n" \
210 "+\n" \
211 "+file1.txt\n" \
212 "+file1.txt\n" \
213 "+file1.txt_renamed\n" \
214 "+file1.txt\n" \
215 "+file1.txt\n" \
216 "+_file1.txt_\n" \
217 "+_file1.txt_\n" \
218 "+file1.txt\n" \
219 "--\n" \
220 "libgit2 " LIBGIT2_VERSION "\n" \
221 "\n";
222
223 git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
224 opts.flags |= GIT_EMAIL_CREATE_NO_RENAMES;
225
226 assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
227}
228
229void test_email_create__binary(void)
230{
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" \
236 "\n" \
237 "---\n" \
238 " binary.bin | Bin 3 -> 5 bytes\n" \
239 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
240 "\n" \
241 "diff --git a/binary.bin b/binary.bin\n" \
242 "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \
243 "GIT binary patch\n" \
244 "literal 5\n" \
245 "Mc${NkU}WL~000&M4gdfE\n" \
246 "\n" \
247 "literal 3\n" \
248 "Kc${Nk-~s>u4FC%O\n" \
249 "\n" \
250 "--\n" \
251 "libgit2 " LIBGIT2_VERSION "\n" \
252 "\n";
253
254 assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", NULL);
255}
256
257void test_email_create__binary_not_included(void)
258{
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" \
264 "\n" \
265 "---\n" \
266 " binary.bin | Bin 3 -> 5 bytes\n" \
267 " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
268 "\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" \
272 "--\n" \
273 "libgit2 " LIBGIT2_VERSION "\n" \
274 "\n";
275
276 git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
277 opts.diff_opts.flags &= ~GIT_DIFF_SHOW_BINARY;
278
279 assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
280}
281
282void test_email_create__custom_summary_and_body(void)
283{
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" \
288 "\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" \
292 "\n" \
293 "Also test if new paragraphs are included correctly.\n" \
294 "---\n" \
295 " file3.txt | 1 +\n" \
296 " 1 file changed, 1 insertion(+)\n" \
297 "\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" \
303 " file3\n" \
304 " file3\n" \
305 " file3\n" \
306 "+file3\n" \
307 "--\n" \
308 "libgit2 " LIBGIT2_VERSION "\n" \
309 "\n";
310
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" \
315 "\n" \
316 "Also test if new paragraphs are included correctly.";
317
318 git_oid oid;
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;
323
324 opts.subject_prefix = "PPPPPATCH";
325
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));
330
e579e0f7 331 cl_assert_equal_s(expected, buf.ptr);
c25aa7cd
PP
332
333 git_diff_free(diff);
334 git_commit_free(commit);
335 git_buf_dispose(&buf);
336}
337
338void test_email_create__commit_subjects(void)
339{
340 git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
341
342 assert_subject_match("[PATCH] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
343
344 opts.reroll_number = 42;
345 assert_subject_match("[PATCH v42] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
346
347 opts.flags |= GIT_EMAIL_CREATE_ALWAYS_NUMBER;
348 assert_subject_match("[PATCH v42 1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
349
350 opts.start_number = 9;
351 assert_subject_match("[PATCH v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
352
353 opts.subject_prefix = "";
354 assert_subject_match("[v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
355
356 opts.reroll_number = 0;
357 assert_subject_match("[9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
358
359 opts.start_number = 0;
360 assert_subject_match("[1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
361
362 opts.flags = GIT_EMAIL_CREATE_OMIT_NUMBERS;
363 assert_subject_match("Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
364}