]>
Commit | Line | Data |
---|---|---|
e579e0f7 MB |
1 | #include "clar.h" |
2 | #include "clar_libgit2.h" | |
3 | ||
4 | #include "buffer.h" | |
5 | #include "diff_generate.h" | |
6 | ||
7 | static git_repository *repo; | |
8 | ||
9 | void test_email_create__initialize(void) | |
10 | { | |
11 | repo = cl_git_sandbox_init("diff_format_email"); | |
12 | } | |
13 | ||
14 | void test_email_create__cleanup(void) | |
15 | { | |
16 | cl_git_sandbox_cleanup(); | |
17 | } | |
18 | ||
19 | static void email_for_commit( | |
20 | git_buf *out, | |
21 | const char *commit_id, | |
22 | git_email_create_options *opts) | |
23 | { | |
24 | git_oid oid; | |
25 | git_commit *commit = NULL; | |
26 | git_diff *diff = NULL; | |
27 | ||
28 | git_oid_fromstr(&oid, commit_id); | |
29 | ||
30 | cl_git_pass(git_commit_lookup(&commit, repo, &oid)); | |
31 | ||
32 | cl_git_pass(git_email_create_from_commit(out, commit, opts)); | |
33 | ||
34 | git_diff_free(diff); | |
35 | git_commit_free(commit); | |
36 | } | |
37 | ||
38 | static void assert_email_match( | |
39 | const char *expected, | |
40 | const char *commit_id, | |
41 | git_email_create_options *opts) | |
42 | { | |
43 | git_buf buf = GIT_BUF_INIT; | |
44 | ||
45 | email_for_commit(&buf, commit_id, opts); | |
46 | cl_assert_equal_s(expected, git_buf_cstr(&buf)); | |
47 | ||
48 | git_buf_dispose(&buf); | |
49 | } | |
50 | ||
51 | static void assert_subject_match( | |
52 | const char *expected, | |
53 | const char *commit_id, | |
54 | git_email_create_options *opts) | |
55 | { | |
56 | git_buf buf = GIT_BUF_INIT; | |
57 | const char *loc; | |
58 | ||
59 | email_for_commit(&buf, commit_id, opts); | |
60 | ||
61 | cl_assert((loc = strstr(buf.ptr, "\nSubject: ")) != NULL); | |
62 | git_buf_consume(&buf, (loc + 10)); | |
63 | git_buf_truncate_at_char(&buf, '\n'); | |
64 | ||
65 | cl_assert_equal_s(expected, git_buf_cstr(&buf)); | |
66 | ||
67 | git_buf_dispose(&buf); | |
68 | } | |
69 | ||
70 | void test_email_create__commit(void) | |
71 | { | |
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" \ | |
77 | "\n" \ | |
78 | "---\n" \ | |
79 | " file1.txt | 8 +++++---\n" \ | |
80 | " 1 file changed, 5 insertions(+), 3 deletions(-)\n" \ | |
81 | "\n" \ | |
82 | "diff --git a/file1.txt b/file1.txt\n" \ | |
83 | "index 94aaae8..af8f41d 100644\n" \ | |
84 | "--- a/file1.txt\n" \ | |
85 | "+++ b/file1.txt\n" \ | |
86 | "@@ -1,15 +1,17 @@\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 | "+\n" \ | |
95 | "+\n" \ | |
96 | " file1.txt\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 | "--\n" \ | |
108 | "libgit2 " LIBGIT2_VERSION "\n" \ | |
109 | "\n"; | |
110 | ||
111 | assert_email_match( | |
112 | expected, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL); | |
113 | } | |
114 | ||
115 | void test_email_create__custom_summary_and_body(void) | |
116 | { | |
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" \ | |
121 | "\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" \ | |
125 | "\n" \ | |
126 | "Also test if new paragraphs are included correctly.\n" \ | |
127 | "---\n" \ | |
128 | " file3.txt | 1 +\n" \ | |
129 | " 1 file changed, 1 insertion(+)\n" \ | |
130 | "\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" \ | |
136 | " file3\n" \ | |
137 | " file3\n" \ | |
138 | " file3\n" \ | |
139 | "+file3\n" \ | |
140 | "--\n" \ | |
141 | "libgit2 " LIBGIT2_VERSION "\n" \ | |
142 | "\n"; | |
143 | ||
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" \ | |
148 | "\n" \ | |
149 | "Also test if new paragraphs are included correctly."; | |
150 | ||
151 | git_oid oid; | |
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; | |
156 | ||
157 | opts.subject_prefix = "PPPPPATCH"; | |
158 | ||
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)); | |
163 | ||
164 | cl_assert_equal_s(expected, git_buf_cstr(&buf)); | |
165 | ||
166 | git_diff_free(diff); | |
167 | git_commit_free(commit); | |
168 | git_buf_dispose(&buf); | |
169 | } | |
170 | ||
171 | void test_email_create__mode_change(void) | |
172 | { | |
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" \ | |
178 | "\n" \ | |
179 | "---\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" \ | |
183 | "\n" \ | |
184 | "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \ | |
185 | "old mode 100644\n" \ | |
186 | "new mode 100755\n" \ | |
187 | "--\n" \ | |
188 | "libgit2 " LIBGIT2_VERSION "\n" \ | |
189 | "\n"; | |
190 | ||
191 | assert_email_match(expected, "7ade76dd34bba4733cf9878079f9fd4a456a9189", NULL); | |
192 | } | |
193 | ||
194 | void test_email_create__rename(void) | |
195 | { | |
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" \ | |
201 | "\n" \ | |
202 | "---\n" \ | |
203 | " file1.txt => file1.txt.renamed | 4 ++--\n" \ | |
204 | " 1 file changed, 2 insertions(+), 2 deletions(-)\n" \ | |
205 | "\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" \ | |
214 | " _file1.txt_\n" \ | |
215 | " file1.txt\n" \ | |
216 | " file1.txt\n" \ | |
217 | "-file1.txt\n" \ | |
218 | "+file1.txt_renamed\n" \ | |
219 | " file1.txt\n" \ | |
220 | " \n" \ | |
221 | " \n" \ | |
222 | " file1.txt\n" \ | |
223 | " file1.txt\n" \ | |
224 | "-file1.txt\n" \ | |
225 | "+file1.txt_renamed\n" \ | |
226 | " file1.txt\n" \ | |
227 | " file1.txt\n" \ | |
228 | " _file1.txt_\n" \ | |
229 | "--\n" \ | |
230 | "libgit2 " LIBGIT2_VERSION "\n" \ | |
231 | "\n"; | |
232 | ||
233 | assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", NULL); | |
234 | } | |
235 | ||
236 | void test_email_create__rename_as_add_delete(void) | |
237 | { | |
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" \ | |
243 | "\n" \ | |
244 | "---\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" \ | |
250 | "\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" \ | |
255 | "+++ /dev/null\n" \ | |
256 | "@@ -1,17 +0,0 @@\n" \ | |
257 | "-file1.txt\n" \ | |
258 | "-file1.txt\n" \ | |
259 | "-_file1.txt_\n" \ | |
260 | "-file1.txt\n" \ | |
261 | "-file1.txt\n" \ | |
262 | "-file1.txt\n" \ | |
263 | "-file1.txt\n" \ | |
264 | "-\n" \ | |
265 | "-\n" \ | |
266 | "-file1.txt\n" \ | |
267 | "-file1.txt\n" \ | |
268 | "-file1.txt\n" \ | |
269 | "-file1.txt\n" \ | |
270 | "-file1.txt\n" \ | |
271 | "-_file1.txt_\n" \ | |
272 | "-_file1.txt_\n" \ | |
273 | "-file1.txt\n" \ | |
274 | "diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \ | |
275 | "new file mode 100644\n" \ | |
276 | "index 0000000..a97157a\n" \ | |
277 | "--- /dev/null\n" \ | |
278 | "+++ b/file1.txt.renamed\n" \ | |
279 | "@@ -0,0 +1,17 @@\n" \ | |
280 | "+file1.txt\n" \ | |
281 | "+file1.txt\n" \ | |
282 | "+_file1.txt_\n" \ | |
283 | "+file1.txt\n" \ | |
284 | "+file1.txt\n" \ | |
285 | "+file1.txt_renamed\n" \ | |
286 | "+file1.txt\n" \ | |
287 | "+\n" \ | |
288 | "+\n" \ | |
289 | "+file1.txt\n" \ | |
290 | "+file1.txt\n" \ | |
291 | "+file1.txt_renamed\n" \ | |
292 | "+file1.txt\n" \ | |
293 | "+file1.txt\n" \ | |
294 | "+_file1.txt_\n" \ | |
295 | "+_file1.txt_\n" \ | |
296 | "+file1.txt\n" \ | |
297 | "--\n" \ | |
298 | "libgit2 " LIBGIT2_VERSION "\n" \ | |
299 | "\n"; | |
300 | ||
301 | git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; | |
302 | opts.flags |= GIT_EMAIL_CREATE_NO_RENAMES; | |
303 | ||
304 | assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts); | |
305 | } | |
306 | ||
307 | void test_email_create__binary(void) | |
308 | { | |
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" \ | |
314 | "\n" \ | |
315 | "---\n" \ | |
316 | " binary.bin | Bin 3 -> 5 bytes\n" \ | |
317 | " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \ | |
318 | "\n" \ | |
319 | "diff --git a/binary.bin b/binary.bin\n" \ | |
320 | "index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \ | |
321 | "GIT binary patch\n" \ | |
322 | "literal 5\n" \ | |
323 | "Mc${NkU}WL~000&M4gdfE\n" \ | |
324 | "\n" \ | |
325 | "literal 3\n" \ | |
326 | "Kc${Nk-~s>u4FC%O\n" \ | |
327 | "\n" \ | |
328 | "--\n" \ | |
329 | "libgit2 " LIBGIT2_VERSION "\n" \ | |
330 | "\n"; | |
331 | ||
332 | assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", NULL); | |
333 | } | |
334 | ||
335 | void test_email_create__binary_not_included(void) | |
336 | { | |
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" \ | |
342 | "\n" \ | |
343 | "---\n" \ | |
344 | " binary.bin | Bin 3 -> 5 bytes\n" \ | |
345 | " 1 file changed, 0 insertions(+), 0 deletions(-)\n" \ | |
346 | "\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" \ | |
350 | "--\n" \ | |
351 | "libgit2 " LIBGIT2_VERSION "\n" \ | |
352 | "\n"; | |
353 | ||
354 | git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; | |
355 | opts.diff_opts.flags &= ~GIT_DIFF_SHOW_BINARY; | |
356 | ||
357 | assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts); | |
358 | } | |
359 | ||
360 | void test_email_create__commit_subjects(void) | |
361 | { | |
362 | git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT; | |
363 | ||
364 | assert_subject_match("[PATCH] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
365 | ||
366 | opts.reroll_number = 42; | |
367 | assert_subject_match("[PATCH v42] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
368 | ||
369 | opts.flags |= GIT_EMAIL_CREATE_ALWAYS_NUMBER; | |
370 | assert_subject_match("[PATCH v42 1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
371 | ||
372 | opts.start_number = 9; | |
373 | assert_subject_match("[PATCH v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
374 | ||
375 | opts.subject_prefix = ""; | |
376 | assert_subject_match("[v42 9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
377 | ||
378 | opts.reroll_number = 0; | |
379 | assert_subject_match("[9/9] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
380 | ||
381 | opts.start_number = 0; | |
382 | assert_subject_match("[1/1] Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
383 | ||
384 | opts.flags = GIT_EMAIL_CREATE_OMIT_NUMBERS; | |
385 | assert_subject_match("Modify some content", "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); | |
386 | } |