commit-graph: when incompatible with graphs, indicate why
[git] / t / t9001-send-email.sh
1 #!/bin/sh
2
3 test_description='git send-email'
4 . ./test-lib.sh
5
6 # May be altered later in the test
7 PREREQ="PERL"
8
9 replace_variable_fields () {
10         sed     -e "s/^\(Date:\).*/\1 DATE-STRING/" \
11                 -e "s/^\(Message-Id:\).*/\1 MESSAGE-ID-STRING/" \
12                 -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/"
13 }
14
15 test_expect_success $PREREQ 'prepare reference tree' '
16         echo "1A quick brown fox jumps over the" >file &&
17         echo "lazy dog" >>file &&
18         git add file &&
19         GIT_AUTHOR_NAME="A" git commit -a -m "Initial."
20 '
21
22 test_expect_success $PREREQ 'Setup helper tool' '
23         write_script fake.sendmail <<-\EOF &&
24         shift
25         output=1
26         while test -f commandline$output
27         do
28                 output=$(($output+1))
29         done
30         for a
31         do
32                 echo "!$a!"
33         done >commandline$output
34         cat >"msgtxt$output"
35         EOF
36         git add fake.sendmail &&
37         GIT_AUTHOR_NAME="A" git commit -a -m "Second."
38 '
39
40 clean_fake_sendmail () {
41         rm -f commandline* msgtxt*
42 }
43
44 test_expect_success $PREREQ 'Extract patches' '
45         patches=$(git format-patch -s --cc="One <one@example.com>" --cc=two@example.com -n HEAD^1) &&
46         threaded_patches=$(git format-patch -o threaded -s --in-reply-to="format" HEAD^1)
47 '
48
49 # Test no confirm early to ensure remaining tests will not hang
50 test_no_confirm () {
51         rm -f no_confirm_okay
52         echo n | \
53                 GIT_SEND_EMAIL_NOTTY=1 \
54                 git send-email \
55                 --from="Example <from@example.com>" \
56                 --to=nobody@example.com \
57                 --smtp-server="$(pwd)/fake.sendmail" \
58                 $@ \
59                 $patches >stdout &&
60                 ! grep "Send this email" stdout &&
61                 >no_confirm_okay
62 }
63
64 # Exit immediately to prevent hang if a no-confirm test fails
65 check_no_confirm () {
66         if ! test -f no_confirm_okay
67         then
68                 say 'confirm test failed; skipping remaining tests to prevent hanging'
69                 PREREQ="$PREREQ,CHECK_NO_CONFIRM"
70         fi
71         return 0
72 }
73
74 test_expect_success $PREREQ 'No confirm with --suppress-cc' '
75         test_no_confirm --suppress-cc=sob &&
76         check_no_confirm
77 '
78
79
80 test_expect_success $PREREQ 'No confirm with --confirm=never' '
81         test_no_confirm --confirm=never &&
82         check_no_confirm
83 '
84
85 # leave sendemail.confirm set to never after this so that none of the
86 # remaining tests prompt unintentionally.
87 test_expect_success $PREREQ 'No confirm with sendemail.confirm=never' '
88         git config sendemail.confirm never &&
89         test_no_confirm --compose --subject=foo &&
90         check_no_confirm
91 '
92
93 test_expect_success $PREREQ 'Send patches' '
94         git send-email --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
95 '
96
97 test_expect_success $PREREQ 'setup expect' '
98         cat >expected <<-\EOF
99         !nobody@example.com!
100         !author@example.com!
101         !one@example.com!
102         !two@example.com!
103         EOF
104 '
105
106 test_expect_success $PREREQ 'Verify commandline' '
107         test_cmp expected commandline1
108 '
109
110 test_expect_success $PREREQ 'Send patches with --envelope-sender' '
111         clean_fake_sendmail &&
112         git send-email --envelope-sender="Patch Contributor <patch@example.com>" --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
113 '
114
115 test_expect_success $PREREQ 'setup expect' '
116         cat >expected <<-\EOF
117         !patch@example.com!
118         !-i!
119         !nobody@example.com!
120         !author@example.com!
121         !one@example.com!
122         !two@example.com!
123         EOF
124 '
125
126 test_expect_success $PREREQ 'Verify commandline' '
127         test_cmp expected commandline1
128 '
129
130 test_expect_success $PREREQ 'Send patches with --envelope-sender=auto' '
131         clean_fake_sendmail &&
132         git send-email --envelope-sender=auto --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
133 '
134
135 test_expect_success $PREREQ 'setup expect' '
136         cat >expected <<-\EOF
137         !nobody@example.com!
138         !-i!
139         !nobody@example.com!
140         !author@example.com!
141         !one@example.com!
142         !two@example.com!
143         EOF
144 '
145
146 test_expect_success $PREREQ 'Verify commandline' '
147         test_cmp expected commandline1
148 '
149
150 test_expect_success $PREREQ 'setup expect for cc trailer' "
151 cat >expected-cc <<\EOF
152 !recipient@example.com!
153 !author@example.com!
154 !one@example.com!
155 !two@example.com!
156 !three@example.com!
157 !four@example.com!
158 !five@example.com!
159 !six@example.com!
160 EOF
161 "
162
163 test_expect_success $PREREQ 'cc trailer with various syntax' '
164         test_commit cc-trailer &&
165         test_when_finished "git reset --hard HEAD^" &&
166         git commit --amend -F - <<-EOF &&
167         Test Cc: trailers.
168
169         Cc: one@example.com
170         Cc: <two@example.com> # trailing comments are ignored
171         Cc: <three@example.com>, <not.four@example.com> one address per line
172         Cc: "Some # Body" <four@example.com> [ <also.a.comment> ]
173         Cc: five@example.com # not.six@example.com
174         Cc: six@example.com, not.seven@example.com
175         EOF
176         clean_fake_sendmail &&
177         git send-email -1 --to=recipient@example.com \
178                 --smtp-server="$(pwd)/fake.sendmail" &&
179         test_cmp expected-cc commandline1
180 '
181
182 test_expect_success $PREREQ 'setup fake get_maintainer.pl script for cc trailer' "
183         write_script expected-cc-script.sh <<-EOF
184         echo 'One Person <one@example.com> (supporter:THIS (FOO/bar))'
185         echo 'Two Person <two@example.com> (maintainer:THIS THING)'
186         echo 'Third List <three@example.com> (moderated list:THIS THING (FOO/bar))'
187         echo '<four@example.com> (moderated list:FOR THING)'
188         echo 'five@example.com (open list:FOR THING (FOO/bar))'
189         echo 'six@example.com (open list)'
190         EOF
191 "
192
193 test_expect_success $PREREQ 'cc trailer with get_maintainer.pl output' '
194         clean_fake_sendmail &&
195         git send-email -1 --to=recipient@example.com \
196                 --cc-cmd=./expected-cc-script.sh \
197                 --smtp-server="$(pwd)/fake.sendmail" &&
198         test_cmp expected-cc commandline1
199 '
200
201 test_expect_success $PREREQ 'setup expect' "
202 cat >expected-show-all-headers <<\EOF
203 0001-Second.patch
204 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
205 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
206 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
207 Dry-OK. Log says:
208 Server: relay.example.com
209 MAIL FROM:<from@example.com>
210 RCPT TO:<to@example.com>
211 RCPT TO:<cc@example.com>
212 RCPT TO:<author@example.com>
213 RCPT TO:<one@example.com>
214 RCPT TO:<two@example.com>
215 RCPT TO:<bcc@example.com>
216 From: Example <from@example.com>
217 To: to@example.com
218 Cc: cc@example.com,
219         A <author@example.com>,
220         One <one@example.com>,
221         two@example.com
222 Subject: [PATCH 1/1] Second.
223 Date: DATE-STRING
224 Message-Id: MESSAGE-ID-STRING
225 X-Mailer: X-MAILER-STRING
226 In-Reply-To: <unique-message-id@example.com>
227 References: <unique-message-id@example.com>
228 Reply-To: Reply <reply@example.com>
229 MIME-Version: 1.0
230 Content-Transfer-Encoding: 8bit
231
232 Result: OK
233 EOF
234 "
235
236 test_suppress_self () {
237         test_commit $3 &&
238         test_when_finished "git reset --hard HEAD^" &&
239
240         write_script cccmd-sed <<-EOF &&
241                 sed -n -e s/^cccmd--//p "\$1"
242         EOF
243
244         git commit --amend --author="$1 <$2>" -F - &&
245         clean_fake_sendmail &&
246         git format-patch --stdout -1 >"suppress-self-$3.patch" &&
247
248         git send-email --from="$1 <$2>" \
249                 --to=nobody@example.com \
250                 --cc-cmd=./cccmd-sed \
251                 --suppress-cc=self \
252                 --smtp-server="$(pwd)/fake.sendmail" \
253                 suppress-self-$3.patch &&
254
255         mv msgtxt1 msgtxt1-$3 &&
256         sed -e '/^$/q' msgtxt1-$3 >"msghdr1-$3" &&
257
258         (grep '^Cc:' msghdr1-$3 >"actual-no-cc-$3";
259          test_must_be_empty actual-no-cc-$3)
260 }
261
262 test_suppress_self_unquoted () {
263         test_suppress_self "$1" "$2" "unquoted-$3" <<-EOF
264                 test suppress-cc.self unquoted-$3 with name $1 email $2
265
266                 unquoted-$3
267
268                 cccmd--$1 <$2>
269
270                 Cc: $1 <$2>
271                 Signed-off-by: $1 <$2>
272         EOF
273 }
274
275 test_suppress_self_quoted () {
276         test_suppress_self "$1" "$2" "quoted-$3" <<-EOF
277                 test suppress-cc.self quoted-$3 with name $1 email $2
278
279                 quoted-$3
280
281                 cccmd--"$1" <$2>
282
283                 Cc: $1 <$2>
284                 Cc: "$1" <$2>
285                 Signed-off-by: $1 <$2>
286                 Signed-off-by: "$1" <$2>
287         EOF
288 }
289
290 test_expect_success $PREREQ 'self name is suppressed' "
291         test_suppress_self_unquoted 'A U Thor' 'author@example.com' \
292                 'self_name_suppressed'
293 "
294
295 test_expect_success $PREREQ 'self name with dot is suppressed' "
296         test_suppress_self_quoted 'A U. Thor' 'author@example.com' \
297                 'self_name_dot_suppressed'
298 "
299
300 test_expect_success $PREREQ 'non-ascii self name is suppressed' "
301         test_suppress_self_quoted 'Füñný Nâmé' 'odd_?=mail@example.com' \
302                 'non_ascii_self_suppressed'
303 "
304
305 # This name is long enough to force format-patch to split it into multiple
306 # encoded-words, assuming it uses UTF-8 with the "Q" encoding.
307 test_expect_success $PREREQ 'long non-ascii self name is suppressed' "
308         test_suppress_self_quoted 'Ƒüñníęř €. Nâṁé' 'odd_?=mail@example.com' \
309                 'long_non_ascii_self_suppressed'
310 "
311
312 test_expect_success $PREREQ 'sanitized self name is suppressed' "
313         test_suppress_self_unquoted '\"A U. Thor\"' 'author@example.com' \
314                 'self_name_sanitized_suppressed'
315 "
316
317 test_expect_success $PREREQ 'Show all headers' '
318         git send-email \
319                 --dry-run \
320                 --suppress-cc=sob \
321                 --from="Example <from@example.com>" \
322                 --reply-to="Reply <reply@example.com>" \
323                 --to=to@example.com \
324                 --cc=cc@example.com \
325                 --bcc=bcc@example.com \
326                 --in-reply-to="<unique-message-id@example.com>" \
327                 --smtp-server relay.example.com \
328                 $patches | replace_variable_fields \
329                 >actual-show-all-headers &&
330         test_cmp expected-show-all-headers actual-show-all-headers
331 '
332
333 test_expect_success $PREREQ 'Prompting works' '
334         clean_fake_sendmail &&
335         (echo "to@example.com" &&
336          echo ""
337         ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
338                 --smtp-server="$(pwd)/fake.sendmail" \
339                 $patches \
340                 2>errors &&
341                 grep "^From: A U Thor <author@example.com>\$" msgtxt1 &&
342                 grep "^To: to@example.com\$" msgtxt1
343 '
344
345 test_expect_success $PREREQ,AUTOIDENT 'implicit ident is allowed' '
346         clean_fake_sendmail &&
347         (sane_unset GIT_AUTHOR_NAME &&
348         sane_unset GIT_AUTHOR_EMAIL &&
349         sane_unset GIT_COMMITTER_NAME &&
350         sane_unset GIT_COMMITTER_EMAIL &&
351         GIT_SEND_EMAIL_NOTTY=1 git send-email \
352                 --smtp-server="$(pwd)/fake.sendmail" \
353                 --to=to@example.com \
354                 $patches </dev/null 2>errors
355         )
356 '
357
358 test_expect_success $PREREQ,!AUTOIDENT 'broken implicit ident aborts send-email' '
359         clean_fake_sendmail &&
360         (sane_unset GIT_AUTHOR_NAME &&
361         sane_unset GIT_AUTHOR_EMAIL &&
362         sane_unset GIT_COMMITTER_NAME &&
363         sane_unset GIT_COMMITTER_EMAIL &&
364         GIT_SEND_EMAIL_NOTTY=1 && export GIT_SEND_EMAIL_NOTTY &&
365         test_must_fail git send-email \
366                 --smtp-server="$(pwd)/fake.sendmail" \
367                 --to=to@example.com \
368                 $patches </dev/null 2>errors &&
369         test_i18ngrep "tell me who you are" errors
370         )
371 '
372
373 test_expect_success $PREREQ 'setup tocmd and cccmd scripts' '
374         write_script tocmd-sed <<-\EOF &&
375         sed -n -e "s/^tocmd--//p" "$1"
376         EOF
377         write_script cccmd-sed <<-\EOF
378         sed -n -e "s/^cccmd--//p" "$1"
379         EOF
380 '
381
382 test_expect_success $PREREQ 'tocmd works' '
383         clean_fake_sendmail &&
384         cp $patches tocmd.patch &&
385         echo tocmd--tocmd@example.com >>tocmd.patch &&
386         git send-email \
387                 --from="Example <nobody@example.com>" \
388                 --to-cmd=./tocmd-sed \
389                 --smtp-server="$(pwd)/fake.sendmail" \
390                 tocmd.patch \
391                 &&
392         grep "^To: tocmd@example.com" msgtxt1
393 '
394
395 test_expect_success $PREREQ 'cccmd works' '
396         clean_fake_sendmail &&
397         cp $patches cccmd.patch &&
398         echo "cccmd--  cccmd@example.com" >>cccmd.patch &&
399         git send-email \
400                 --from="Example <nobody@example.com>" \
401                 --to=nobody@example.com \
402                 --cc-cmd=./cccmd-sed \
403                 --smtp-server="$(pwd)/fake.sendmail" \
404                 cccmd.patch \
405                 &&
406         grep "^ cccmd@example.com" msgtxt1
407 '
408
409 test_expect_success $PREREQ 'reject long lines' '
410         z8=zzzzzzzz &&
411         z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&
412         z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
413         clean_fake_sendmail &&
414         cp $patches longline.patch &&
415         echo $z512$z512 >>longline.patch &&
416         test_must_fail git send-email \
417                 --from="Example <nobody@example.com>" \
418                 --to=nobody@example.com \
419                 --smtp-server="$(pwd)/fake.sendmail" \
420                 --transfer-encoding=8bit \
421                 $patches longline.patch \
422                 2>errors &&
423         grep longline.patch errors
424 '
425
426 test_expect_success $PREREQ 'no patch was sent' '
427         ! test -e commandline1
428 '
429
430 test_expect_success $PREREQ 'Author From: in message body' '
431         clean_fake_sendmail &&
432         git send-email \
433                 --from="Example <nobody@example.com>" \
434                 --to=nobody@example.com \
435                 --smtp-server="$(pwd)/fake.sendmail" \
436                 $patches &&
437         sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
438         grep "From: A <author@example.com>" msgbody1
439 '
440
441 test_expect_success $PREREQ 'Author From: not in message body' '
442         clean_fake_sendmail &&
443         git send-email \
444                 --from="A <author@example.com>" \
445                 --to=nobody@example.com \
446                 --smtp-server="$(pwd)/fake.sendmail" \
447                 $patches &&
448         sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
449         ! grep "From: A <author@example.com>" msgbody1
450 '
451
452 test_expect_success $PREREQ 'allow long lines with --no-validate' '
453         git send-email \
454                 --from="Example <nobody@example.com>" \
455                 --to=nobody@example.com \
456                 --smtp-server="$(pwd)/fake.sendmail" \
457                 --no-validate \
458                 $patches longline.patch \
459                 2>errors
460 '
461
462 test_expect_success $PREREQ 'short lines with auto encoding are 8bit' '
463         clean_fake_sendmail &&
464         git send-email \
465                 --from="A <author@example.com>" \
466                 --to=nobody@example.com \
467                 --smtp-server="$(pwd)/fake.sendmail" \
468                 --transfer-encoding=auto \
469                 $patches &&
470         grep "Content-Transfer-Encoding: 8bit" msgtxt1
471 '
472
473 test_expect_success $PREREQ 'long lines with auto encoding are quoted-printable' '
474         clean_fake_sendmail &&
475         git send-email \
476                 --from="Example <nobody@example.com>" \
477                 --to=nobody@example.com \
478                 --smtp-server="$(pwd)/fake.sendmail" \
479                 --transfer-encoding=auto \
480                 --no-validate \
481                 longline.patch &&
482         grep "Content-Transfer-Encoding: quoted-printable" msgtxt1
483 '
484
485 test_expect_success $PREREQ 'carriage returns with auto encoding are quoted-printable' '
486         clean_fake_sendmail &&
487         cp $patches cr.patch &&
488         printf "this is a line\r\n" >>cr.patch &&
489         git send-email \
490                 --from="Example <nobody@example.com>" \
491                 --to=nobody@example.com \
492                 --smtp-server="$(pwd)/fake.sendmail" \
493                 --transfer-encoding=auto \
494                 --no-validate \
495                 cr.patch &&
496         grep "Content-Transfer-Encoding: quoted-printable" msgtxt1
497 '
498
499 for enc in auto quoted-printable base64
500 do
501         test_expect_success $PREREQ "--validate passes with encoding $enc" '
502                 git send-email \
503                         --from="Example <nobody@example.com>" \
504                         --to=nobody@example.com \
505                         --smtp-server="$(pwd)/fake.sendmail" \
506                         --transfer-encoding=$enc \
507                         --validate \
508                         $patches longline.patch
509         '
510
511 done
512
513 for enc in 7bit 8bit quoted-printable base64
514 do
515         test_expect_success $PREREQ "--transfer-encoding=$enc produces correct header" '
516                 clean_fake_sendmail &&
517                 git send-email \
518                         --from="Example <nobody@example.com>" \
519                         --to=nobody@example.com \
520                         --smtp-server="$(pwd)/fake.sendmail" \
521                         --transfer-encoding=$enc \
522                         $patches &&
523                 grep "Content-Transfer-Encoding: $enc" msgtxt1
524         '
525 done
526
527 test_expect_success $PREREQ 'Invalid In-Reply-To' '
528         clean_fake_sendmail &&
529         git send-email \
530                 --from="Example <nobody@example.com>" \
531                 --to=nobody@example.com \
532                 --in-reply-to=" " \
533                 --smtp-server="$(pwd)/fake.sendmail" \
534                 $patches \
535                 2>errors &&
536         ! grep "^In-Reply-To: < *>" msgtxt1
537 '
538
539 test_expect_success $PREREQ 'Valid In-Reply-To when prompting' '
540         clean_fake_sendmail &&
541         (echo "From Example <from@example.com>" &&
542          echo "To Example <to@example.com>" &&
543          echo ""
544         ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
545                 --smtp-server="$(pwd)/fake.sendmail" \
546                 $patches 2>errors &&
547         ! grep "^In-Reply-To: < *>" msgtxt1
548 '
549
550 test_expect_success $PREREQ 'In-Reply-To without --chain-reply-to' '
551         clean_fake_sendmail &&
552         echo "<unique-message-id@example.com>" >expect &&
553         git send-email \
554                 --from="Example <nobody@example.com>" \
555                 --to=nobody@example.com \
556                 --no-chain-reply-to \
557                 --in-reply-to="$(cat expect)" \
558                 --smtp-server="$(pwd)/fake.sendmail" \
559                 $patches $patches $patches \
560                 2>errors &&
561         # The first message is a reply to --in-reply-to
562         sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
563         test_cmp expect actual &&
564         # Second and subsequent messages are replies to the first one
565         sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
566         sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
567         test_cmp expect actual &&
568         sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
569         test_cmp expect actual
570 '
571
572 test_expect_success $PREREQ 'In-Reply-To with --chain-reply-to' '
573         clean_fake_sendmail &&
574         echo "<unique-message-id@example.com>" >expect &&
575         git send-email \
576                 --from="Example <nobody@example.com>" \
577                 --to=nobody@example.com \
578                 --chain-reply-to \
579                 --in-reply-to="$(cat expect)" \
580                 --smtp-server="$(pwd)/fake.sendmail" \
581                 $patches $patches $patches \
582                 2>errors &&
583         sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
584         test_cmp expect actual &&
585         sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt1 >expect &&
586         sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
587         test_cmp expect actual &&
588         sed -n -e "s/^Message-Id: *\(.*\)/\1/p" msgtxt2 >expect &&
589         sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
590         test_cmp expect actual
591 '
592
593 test_expect_success $PREREQ 'setup fake editor' '
594         write_script fake-editor <<-\EOF
595         echo fake edit >>"$1"
596         EOF
597 '
598
599 test_set_editor "$(pwd)/fake-editor"
600
601 test_expect_success $PREREQ '--compose works' '
602         clean_fake_sendmail &&
603         git send-email \
604         --compose --subject foo \
605         --from="Example <nobody@example.com>" \
606         --to=nobody@example.com \
607         --smtp-server="$(pwd)/fake.sendmail" \
608         $patches \
609         2>errors
610 '
611
612 test_expect_success $PREREQ 'first message is compose text' '
613         grep "^fake edit" msgtxt1
614 '
615
616 test_expect_success $PREREQ 'second message is patch' '
617         grep "Subject:.*Second" msgtxt2
618 '
619
620 test_expect_success $PREREQ 'setup expect' "
621 cat >expected-suppress-sob <<\EOF
622 0001-Second.patch
623 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
624 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
625 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
626 Dry-OK. Log says:
627 Server: relay.example.com
628 MAIL FROM:<from@example.com>
629 RCPT TO:<to@example.com>
630 RCPT TO:<cc@example.com>
631 RCPT TO:<author@example.com>
632 RCPT TO:<one@example.com>
633 RCPT TO:<two@example.com>
634 From: Example <from@example.com>
635 To: to@example.com
636 Cc: cc@example.com,
637         A <author@example.com>,
638         One <one@example.com>,
639         two@example.com
640 Subject: [PATCH 1/1] Second.
641 Date: DATE-STRING
642 Message-Id: MESSAGE-ID-STRING
643 X-Mailer: X-MAILER-STRING
644 MIME-Version: 1.0
645 Content-Transfer-Encoding: 8bit
646
647 Result: OK
648 EOF
649 "
650
651 test_suppression () {
652         git send-email \
653                 --dry-run \
654                 --suppress-cc=$1 ${2+"--suppress-cc=$2"} \
655                 --from="Example <from@example.com>" \
656                 --to=to@example.com \
657                 --smtp-server relay.example.com \
658                 $patches | replace_variable_fields \
659                 >actual-suppress-$1${2+"-$2"} &&
660         test_cmp expected-suppress-$1${2+"-$2"} actual-suppress-$1${2+"-$2"}
661 }
662
663 test_expect_success $PREREQ 'sendemail.cc set' '
664         git config sendemail.cc cc@example.com &&
665         test_suppression sob
666 '
667
668 test_expect_success $PREREQ 'setup expect' "
669 cat >expected-suppress-sob <<\EOF
670 0001-Second.patch
671 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
672 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
673 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
674 Dry-OK. Log says:
675 Server: relay.example.com
676 MAIL FROM:<from@example.com>
677 RCPT TO:<to@example.com>
678 RCPT TO:<author@example.com>
679 RCPT TO:<one@example.com>
680 RCPT TO:<two@example.com>
681 From: Example <from@example.com>
682 To: to@example.com
683 Cc: A <author@example.com>,
684         One <one@example.com>,
685         two@example.com
686 Subject: [PATCH 1/1] Second.
687 Date: DATE-STRING
688 Message-Id: MESSAGE-ID-STRING
689 X-Mailer: X-MAILER-STRING
690 MIME-Version: 1.0
691 Content-Transfer-Encoding: 8bit
692
693 Result: OK
694 EOF
695 "
696
697 test_expect_success $PREREQ 'sendemail.cc unset' '
698         git config --unset sendemail.cc &&
699         test_suppression sob
700 '
701
702 test_expect_success $PREREQ 'setup expect' "
703 cat >expected-suppress-cccmd <<\EOF
704 0001-Second.patch
705 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
706 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
707 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
708 (body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
709 Dry-OK. Log says:
710 Server: relay.example.com
711 MAIL FROM:<from@example.com>
712 RCPT TO:<to@example.com>
713 RCPT TO:<author@example.com>
714 RCPT TO:<one@example.com>
715 RCPT TO:<two@example.com>
716 RCPT TO:<committer@example.com>
717 From: Example <from@example.com>
718 To: to@example.com
719 Cc: A <author@example.com>,
720         One <one@example.com>,
721         two@example.com,
722         C O Mitter <committer@example.com>
723 Subject: [PATCH 1/1] Second.
724 Date: DATE-STRING
725 Message-Id: MESSAGE-ID-STRING
726 X-Mailer: X-MAILER-STRING
727 MIME-Version: 1.0
728 Content-Transfer-Encoding: 8bit
729
730 Result: OK
731 EOF
732 "
733
734 test_expect_success $PREREQ 'sendemail.cccmd' '
735         write_script cccmd <<-\EOF &&
736         echo cc-cmd@example.com
737         EOF
738         git config sendemail.cccmd ./cccmd &&
739         test_suppression cccmd
740 '
741
742 test_expect_success $PREREQ 'setup expect' '
743 cat >expected-suppress-all <<\EOF
744 0001-Second.patch
745 Dry-OK. Log says:
746 Server: relay.example.com
747 MAIL FROM:<from@example.com>
748 RCPT TO:<to@example.com>
749 From: Example <from@example.com>
750 To: to@example.com
751 Subject: [PATCH 1/1] Second.
752 Date: DATE-STRING
753 Message-Id: MESSAGE-ID-STRING
754 X-Mailer: X-MAILER-STRING
755 MIME-Version: 1.0
756 Content-Transfer-Encoding: 8bit
757
758 Result: OK
759 EOF
760 '
761
762 test_expect_success $PREREQ '--suppress-cc=all' '
763         test_suppression all
764 '
765
766 test_expect_success $PREREQ 'setup expect' "
767 cat >expected-suppress-body <<\EOF
768 0001-Second.patch
769 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
770 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
771 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
772 (cc-cmd) Adding cc: cc-cmd@example.com from: './cccmd'
773 Dry-OK. Log says:
774 Server: relay.example.com
775 MAIL FROM:<from@example.com>
776 RCPT TO:<to@example.com>
777 RCPT TO:<author@example.com>
778 RCPT TO:<one@example.com>
779 RCPT TO:<two@example.com>
780 RCPT TO:<cc-cmd@example.com>
781 From: Example <from@example.com>
782 To: to@example.com
783 Cc: A <author@example.com>,
784         One <one@example.com>,
785         two@example.com,
786         cc-cmd@example.com
787 Subject: [PATCH 1/1] Second.
788 Date: DATE-STRING
789 Message-Id: MESSAGE-ID-STRING
790 X-Mailer: X-MAILER-STRING
791 MIME-Version: 1.0
792 Content-Transfer-Encoding: 8bit
793
794 Result: OK
795 EOF
796 "
797
798 test_expect_success $PREREQ '--suppress-cc=body' '
799         test_suppression body
800 '
801
802 test_expect_success $PREREQ 'setup expect' "
803 cat >expected-suppress-body-cccmd <<\EOF
804 0001-Second.patch
805 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
806 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
807 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
808 Dry-OK. Log says:
809 Server: relay.example.com
810 MAIL FROM:<from@example.com>
811 RCPT TO:<to@example.com>
812 RCPT TO:<author@example.com>
813 RCPT TO:<one@example.com>
814 RCPT TO:<two@example.com>
815 From: Example <from@example.com>
816 To: to@example.com
817 Cc: A <author@example.com>,
818         One <one@example.com>,
819         two@example.com
820 Subject: [PATCH 1/1] Second.
821 Date: DATE-STRING
822 Message-Id: MESSAGE-ID-STRING
823 X-Mailer: X-MAILER-STRING
824 MIME-Version: 1.0
825 Content-Transfer-Encoding: 8bit
826
827 Result: OK
828 EOF
829 "
830
831 test_expect_success $PREREQ '--suppress-cc=body --suppress-cc=cccmd' '
832         test_suppression body cccmd
833 '
834
835 test_expect_success $PREREQ 'setup expect' "
836 cat >expected-suppress-sob <<\EOF
837 0001-Second.patch
838 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
839 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
840 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
841 Dry-OK. Log says:
842 Server: relay.example.com
843 MAIL FROM:<from@example.com>
844 RCPT TO:<to@example.com>
845 RCPT TO:<author@example.com>
846 RCPT TO:<one@example.com>
847 RCPT TO:<two@example.com>
848 From: Example <from@example.com>
849 To: to@example.com
850 Cc: A <author@example.com>,
851         One <one@example.com>,
852         two@example.com
853 Subject: [PATCH 1/1] Second.
854 Date: DATE-STRING
855 Message-Id: MESSAGE-ID-STRING
856 X-Mailer: X-MAILER-STRING
857 MIME-Version: 1.0
858 Content-Transfer-Encoding: 8bit
859
860 Result: OK
861 EOF
862 "
863
864 test_expect_success $PREREQ '--suppress-cc=sob' '
865         test_might_fail git config --unset sendemail.cccmd &&
866         test_suppression sob
867 '
868
869 test_expect_success $PREREQ 'setup expect' "
870 cat >expected-suppress-bodycc <<\EOF
871 0001-Second.patch
872 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
873 (mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
874 (mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
875 (body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
876 Dry-OK. Log says:
877 Server: relay.example.com
878 MAIL FROM:<from@example.com>
879 RCPT TO:<to@example.com>
880 RCPT TO:<author@example.com>
881 RCPT TO:<one@example.com>
882 RCPT TO:<two@example.com>
883 RCPT TO:<committer@example.com>
884 From: Example <from@example.com>
885 To: to@example.com
886 Cc: A <author@example.com>,
887         One <one@example.com>,
888         two@example.com,
889         C O Mitter <committer@example.com>
890 Subject: [PATCH 1/1] Second.
891 Date: DATE-STRING
892 Message-Id: MESSAGE-ID-STRING
893 X-Mailer: X-MAILER-STRING
894 MIME-Version: 1.0
895 Content-Transfer-Encoding: 8bit
896
897 Result: OK
898 EOF
899 "
900
901 test_expect_success $PREREQ '--suppress-cc=bodycc' '
902         test_suppression bodycc
903 '
904
905 test_expect_success $PREREQ 'setup expect' "
906 cat >expected-suppress-cc <<\EOF
907 0001-Second.patch
908 (mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
909 (body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
910 Dry-OK. Log says:
911 Server: relay.example.com
912 MAIL FROM:<from@example.com>
913 RCPT TO:<to@example.com>
914 RCPT TO:<author@example.com>
915 RCPT TO:<committer@example.com>
916 From: Example <from@example.com>
917 To: to@example.com
918 Cc: A <author@example.com>,
919         C O Mitter <committer@example.com>
920 Subject: [PATCH 1/1] Second.
921 Date: DATE-STRING
922 Message-Id: MESSAGE-ID-STRING
923 X-Mailer: X-MAILER-STRING
924 MIME-Version: 1.0
925 Content-Transfer-Encoding: 8bit
926
927 Result: OK
928 EOF
929 "
930
931 test_expect_success $PREREQ '--suppress-cc=cc' '
932         test_suppression cc
933 '
934
935 test_confirm () {
936         echo y | \
937                 GIT_SEND_EMAIL_NOTTY=1 \
938                 git send-email \
939                 --from="Example <nobody@example.com>" \
940                 --to=nobody@example.com \
941                 --smtp-server="$(pwd)/fake.sendmail" \
942                 $@ $patches >stdout &&
943         grep "Send this email" stdout
944 }
945
946 test_expect_success $PREREQ '--confirm=always' '
947         test_confirm --confirm=always --suppress-cc=all
948 '
949
950 test_expect_success $PREREQ '--confirm=auto' '
951         test_confirm --confirm=auto
952 '
953
954 test_expect_success $PREREQ '--confirm=cc' '
955         test_confirm --confirm=cc
956 '
957
958 test_expect_success $PREREQ '--confirm=compose' '
959         test_confirm --confirm=compose --compose
960 '
961
962 test_expect_success $PREREQ 'confirm by default (due to cc)' '
963         test_when_finished git config sendemail.confirm never &&
964         git config --unset sendemail.confirm &&
965         test_confirm
966 '
967
968 test_expect_success $PREREQ 'confirm by default (due to --compose)' '
969         test_when_finished git config sendemail.confirm never &&
970         git config --unset sendemail.confirm &&
971         test_confirm --suppress-cc=all --compose
972 '
973
974 test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
975         test_when_finished git config sendemail.confirm never &&
976         git config --unset sendemail.confirm &&
977         rm -fr outdir &&
978         git format-patch -2 -o outdir &&
979         GIT_SEND_EMAIL_NOTTY=1 \
980                 git send-email \
981                         --from="Example <nobody@example.com>" \
982                         --to=nobody@example.com \
983                         --smtp-server="$(pwd)/fake.sendmail" \
984                         outdir/*.patch </dev/null
985 '
986
987 test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
988         test_when_finished git config sendemail.confirm never &&
989         git config sendemail.confirm auto &&
990         GIT_SEND_EMAIL_NOTTY=1 &&
991         export GIT_SEND_EMAIL_NOTTY &&
992                 test_must_fail git send-email \
993                         --from="Example <nobody@example.com>" \
994                         --to=nobody@example.com \
995                         --smtp-server="$(pwd)/fake.sendmail" \
996                         $patches </dev/null
997 '
998
999 test_expect_success $PREREQ 'confirm does not loop forever' '
1000         test_when_finished git config sendemail.confirm never &&
1001         git config sendemail.confirm auto &&
1002         GIT_SEND_EMAIL_NOTTY=1 &&
1003         export GIT_SEND_EMAIL_NOTTY &&
1004                 yes "bogus" | test_must_fail git send-email \
1005                         --from="Example <nobody@example.com>" \
1006                         --to=nobody@example.com \
1007                         --smtp-server="$(pwd)/fake.sendmail" \
1008                         $patches
1009 '
1010
1011 test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '
1012         clean_fake_sendmail &&
1013         rm -fr outdir &&
1014         git format-patch -1 -o outdir --cc="àéìöú <utf8@example.com>" &&
1015         git send-email \
1016         --from="Example <nobody@example.com>" \
1017         --to=nobody@example.com \
1018         --smtp-server="$(pwd)/fake.sendmail" \
1019         outdir/*.patch &&
1020         grep "^ " msgtxt1 |
1021         grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <utf8@example.com>"
1022 '
1023
1024 test_expect_success $PREREQ '--compose adds MIME for utf8 body' '
1025         clean_fake_sendmail &&
1026         write_script fake-editor-utf8 <<-\EOF &&
1027         echo "utf8 body: àéìöú" >>"$1"
1028         EOF
1029         GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1030         git send-email \
1031                 --compose --subject foo \
1032                 --from="Example <nobody@example.com>" \
1033                 --to=nobody@example.com \
1034                 --smtp-server="$(pwd)/fake.sendmail" \
1035                 $patches &&
1036         grep "^utf8 body" msgtxt1 &&
1037         grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
1038 '
1039
1040 test_expect_success $PREREQ '--compose respects user mime type' '
1041         clean_fake_sendmail &&
1042         write_script fake-editor-utf8-mime <<-\EOF &&
1043         cat >"$1" <<-\EOM
1044         MIME-Version: 1.0
1045         Content-Type: text/plain; charset=iso-8859-1
1046         Content-Transfer-Encoding: 8bit
1047         Subject: foo
1048
1049         utf8 body: àéìöú
1050         EOM
1051         EOF
1052         GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
1053         git send-email \
1054                 --compose --subject foo \
1055                 --from="Example <nobody@example.com>" \
1056                 --to=nobody@example.com \
1057                 --smtp-server="$(pwd)/fake.sendmail" \
1058                 $patches &&
1059         grep "^utf8 body" msgtxt1 &&
1060         grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
1061         ! grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
1062 '
1063
1064 test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
1065         clean_fake_sendmail &&
1066         GIT_EDITOR="\"$(pwd)/fake-editor\"" \
1067         git send-email \
1068                 --compose --subject utf8-sübjëct \
1069                 --from="Example <nobody@example.com>" \
1070                 --to=nobody@example.com \
1071                 --smtp-server="$(pwd)/fake.sendmail" \
1072                 $patches &&
1073         grep "^fake edit" msgtxt1 &&
1074         grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
1075 '
1076
1077 test_expect_success $PREREQ 'utf8 author is correctly passed on' '
1078         clean_fake_sendmail &&
1079         test_commit weird_author &&
1080         test_when_finished "git reset --hard HEAD^" &&
1081         git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
1082         git format-patch --stdout -1 >funny_name.patch &&
1083         git send-email --from="Example <nobody@example.com>" \
1084                 --to=nobody@example.com \
1085                 --smtp-server="$(pwd)/fake.sendmail" \
1086                 funny_name.patch &&
1087         grep "^From: Füñný Nâmé <odd_?=mail@example.com>" msgtxt1
1088 '
1089
1090 test_expect_success $PREREQ 'utf8 sender is not duplicated' '
1091         clean_fake_sendmail &&
1092         test_commit weird_sender &&
1093         test_when_finished "git reset --hard HEAD^" &&
1094         git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
1095         git format-patch --stdout -1 >funny_name.patch &&
1096         git send-email --from="Füñný Nâmé <odd_?=mail@example.com>" \
1097                 --to=nobody@example.com \
1098                 --smtp-server="$(pwd)/fake.sendmail" \
1099                 funny_name.patch &&
1100         grep "^From: " msgtxt1 >msgfrom &&
1101         test_line_count = 1 msgfrom
1102 '
1103
1104 test_expect_success $PREREQ 'sendemail.composeencoding works' '
1105         clean_fake_sendmail &&
1106         git config sendemail.composeencoding iso-8859-1 &&
1107         write_script fake-editor-utf8 <<-\EOF &&
1108         echo "utf8 body: àéìöú" >>"$1"
1109         EOF
1110         GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1111         git send-email \
1112                 --compose --subject foo \
1113                 --from="Example <nobody@example.com>" \
1114                 --to=nobody@example.com \
1115                 --smtp-server="$(pwd)/fake.sendmail" \
1116                 $patches &&
1117         grep "^utf8 body" msgtxt1 &&
1118         grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
1119 '
1120
1121 test_expect_success $PREREQ '--compose-encoding works' '
1122         clean_fake_sendmail &&
1123         write_script fake-editor-utf8 <<-\EOF &&
1124         echo "utf8 body: àéìöú" >>"$1"
1125         EOF
1126         GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1127         git send-email \
1128                 --compose-encoding iso-8859-1 \
1129                 --compose --subject foo \
1130                 --from="Example <nobody@example.com>" \
1131                 --to=nobody@example.com \
1132                 --smtp-server="$(pwd)/fake.sendmail" \
1133                 $patches &&
1134         grep "^utf8 body" msgtxt1 &&
1135         grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
1136 '
1137
1138 test_expect_success $PREREQ '--compose-encoding overrides sendemail.composeencoding' '
1139         clean_fake_sendmail &&
1140         git config sendemail.composeencoding iso-8859-1 &&
1141         write_script fake-editor-utf8 <<-\EOF &&
1142         echo "utf8 body: àéìöú" >>"$1"
1143         EOF
1144         GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
1145         git send-email \
1146                 --compose-encoding iso-8859-2 \
1147                 --compose --subject foo \
1148                 --from="Example <nobody@example.com>" \
1149                 --to=nobody@example.com \
1150                 --smtp-server="$(pwd)/fake.sendmail" \
1151                 $patches &&
1152         grep "^utf8 body" msgtxt1 &&
1153         grep "^Content-Type: text/plain; charset=iso-8859-2" msgtxt1
1154 '
1155
1156 test_expect_success $PREREQ '--compose-encoding adds correct MIME for subject' '
1157         clean_fake_sendmail &&
1158         GIT_EDITOR="\"$(pwd)/fake-editor\"" \
1159         git send-email \
1160                 --compose-encoding iso-8859-2 \
1161                 --compose --subject utf8-sübjëct \
1162                 --from="Example <nobody@example.com>" \
1163                 --to=nobody@example.com \
1164                 --smtp-server="$(pwd)/fake.sendmail" \
1165                 $patches &&
1166         grep "^fake edit" msgtxt1 &&
1167         grep "^Subject: =?iso-8859-2?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
1168 '
1169
1170 test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
1171         echo master >master &&
1172         git add master &&
1173         git commit -m"add master" &&
1174         test_must_fail git send-email --dry-run master 2>errors &&
1175         grep disambiguate errors
1176 '
1177
1178 test_expect_success $PREREQ 'feed two files' '
1179         rm -fr outdir &&
1180         git format-patch -2 -o outdir &&
1181         git send-email \
1182                 --dry-run \
1183                 --from="Example <nobody@example.com>" \
1184                 --to=nobody@example.com \
1185                 outdir/000?-*.patch 2>errors >out &&
1186         grep "^Subject: " out >subjects &&
1187         test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
1188         test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add master"
1189 '
1190
1191 test_expect_success $PREREQ 'in-reply-to but no threading' '
1192         git send-email \
1193                 --dry-run \
1194                 --from="Example <nobody@example.com>" \
1195                 --to=nobody@example.com \
1196                 --in-reply-to="<in-reply-id@example.com>" \
1197                 --no-thread \
1198                 $patches >out &&
1199         grep "In-Reply-To: <in-reply-id@example.com>" out
1200 '
1201
1202 test_expect_success $PREREQ 'no in-reply-to and no threading' '
1203         git send-email \
1204                 --dry-run \
1205                 --from="Example <nobody@example.com>" \
1206                 --to=nobody@example.com \
1207                 --no-thread \
1208                 $patches >stdout &&
1209         ! grep "In-Reply-To: " stdout
1210 '
1211
1212 test_expect_success $PREREQ 'threading but no chain-reply-to' '
1213         git send-email \
1214                 --dry-run \
1215                 --from="Example <nobody@example.com>" \
1216                 --to=nobody@example.com \
1217                 --thread \
1218                 --no-chain-reply-to \
1219                 $patches $patches >stdout &&
1220         grep "In-Reply-To: " stdout
1221 '
1222
1223 test_expect_success $PREREQ 'override in-reply-to if no threading' '
1224         git send-email \
1225                 --dry-run \
1226                 --from="Example <nobody@example.com>" \
1227                 --to=nobody@example.com \
1228                 --no-thread \
1229                 --in-reply-to="override" \
1230                 $threaded_patches >stdout &&
1231         grep "In-Reply-To: <override>" stdout
1232 '
1233
1234 test_expect_success $PREREQ 'sendemail.to works' '
1235         git config --replace-all sendemail.to "Somebody <somebody@ex.com>" &&
1236         git send-email \
1237                 --dry-run \
1238                 --from="Example <nobody@example.com>" \
1239                 $patches >stdout &&
1240         grep "To: Somebody <somebody@ex.com>" stdout
1241 '
1242
1243 test_expect_success $PREREQ 'setup sendemail.identity' '
1244         git config --replace-all sendemail.to "default@example.com" &&
1245         git config --replace-all sendemail.isp.to "isp@example.com" &&
1246         git config --replace-all sendemail.cloud.to "cloud@example.com"
1247 '
1248
1249 test_expect_success $PREREQ 'sendemail.identity: reads the correct identity config' '
1250         git -c sendemail.identity=cloud send-email \
1251                 --dry-run \
1252                 --from="nobody@example.com" \
1253                 $patches >stdout &&
1254         grep "To: cloud@example.com" stdout
1255 '
1256
1257 test_expect_success $PREREQ 'sendemail.identity: identity overrides sendemail.identity' '
1258         git -c sendemail.identity=cloud send-email \
1259                 --identity=isp \
1260                 --dry-run \
1261                 --from="nobody@example.com" \
1262                 $patches >stdout &&
1263         grep "To: isp@example.com" stdout
1264 '
1265
1266 test_expect_success $PREREQ 'sendemail.identity: --no-identity clears previous identity' '
1267         git -c sendemail.identity=cloud send-email \
1268                 --no-identity \
1269                 --dry-run \
1270                 --from="nobody@example.com" \
1271                 $patches >stdout &&
1272         grep "To: default@example.com" stdout
1273 '
1274
1275 test_expect_success $PREREQ 'sendemail.identity: bool identity variable existence overrides' '
1276         git -c sendemail.identity=cloud \
1277                 -c sendemail.xmailer=true \
1278                 -c sendemail.cloud.xmailer=false \
1279                 send-email \
1280                 --dry-run \
1281                 --from="nobody@example.com" \
1282                 $patches >stdout &&
1283         grep "To: cloud@example.com" stdout &&
1284         ! grep "X-Mailer" stdout
1285 '
1286
1287 test_expect_success $PREREQ 'sendemail.identity: bool variable fallback' '
1288         git -c sendemail.identity=cloud \
1289                 -c sendemail.xmailer=false \
1290                 send-email \
1291                 --dry-run \
1292                 --from="nobody@example.com" \
1293                 $patches >stdout &&
1294         grep "To: cloud@example.com" stdout &&
1295         ! grep "X-Mailer" stdout
1296 '
1297
1298 test_expect_success $PREREQ '--no-to overrides sendemail.to' '
1299         git send-email \
1300                 --dry-run \
1301                 --from="Example <nobody@example.com>" \
1302                 --no-to \
1303                 --to=nobody@example.com \
1304                 $patches >stdout &&
1305         grep "To: nobody@example.com" stdout &&
1306         ! grep "To: Somebody <somebody@ex.com>" stdout
1307 '
1308
1309 test_expect_success $PREREQ 'sendemail.cc works' '
1310         git config --replace-all sendemail.cc "Somebody <somebody@ex.com>" &&
1311         git send-email \
1312                 --dry-run \
1313                 --from="Example <nobody@example.com>" \
1314                 --to=nobody@example.com \
1315                 $patches >stdout &&
1316         grep "Cc: Somebody <somebody@ex.com>" stdout
1317 '
1318
1319 test_expect_success $PREREQ '--no-cc overrides sendemail.cc' '
1320         git send-email \
1321                 --dry-run \
1322                 --from="Example <nobody@example.com>" \
1323                 --no-cc \
1324                 --cc=bodies@example.com \
1325                 --to=nobody@example.com \
1326                 $patches >stdout &&
1327         grep "Cc: bodies@example.com" stdout &&
1328         ! grep "Cc: Somebody <somebody@ex.com>" stdout
1329 '
1330
1331 test_expect_success $PREREQ 'sendemail.bcc works' '
1332         git config --replace-all sendemail.bcc "Other <other@ex.com>" &&
1333         git send-email \
1334                 --dry-run \
1335                 --from="Example <nobody@example.com>" \
1336                 --to=nobody@example.com \
1337                 --smtp-server relay.example.com \
1338                 $patches >stdout &&
1339         grep "RCPT TO:<other@ex.com>" stdout
1340 '
1341
1342 test_expect_success $PREREQ '--no-bcc overrides sendemail.bcc' '
1343         git send-email \
1344                 --dry-run \
1345                 --from="Example <nobody@example.com>" \
1346                 --no-bcc \
1347                 --bcc=bodies@example.com \
1348                 --to=nobody@example.com \
1349                 --smtp-server relay.example.com \
1350                 $patches >stdout &&
1351         grep "RCPT TO:<bodies@example.com>" stdout &&
1352         ! grep "RCPT TO:<other@ex.com>" stdout
1353 '
1354
1355 test_expect_success $PREREQ 'patches To headers are used by default' '
1356         patch=$(git format-patch -1 --to="bodies@example.com") &&
1357         test_when_finished "rm $patch" &&
1358         git send-email \
1359                 --dry-run \
1360                 --from="Example <nobody@example.com>" \
1361                 --smtp-server relay.example.com \
1362                 $patch >stdout &&
1363         grep "RCPT TO:<bodies@example.com>" stdout
1364 '
1365
1366 test_expect_success $PREREQ 'patches To headers are appended to' '
1367         patch=$(git format-patch -1 --to="bodies@example.com") &&
1368         test_when_finished "rm $patch" &&
1369         git send-email \
1370                 --dry-run \
1371                 --from="Example <nobody@example.com>" \
1372                 --to=nobody@example.com \
1373                 --smtp-server relay.example.com \
1374                 $patch >stdout &&
1375         grep "RCPT TO:<bodies@example.com>" stdout &&
1376         grep "RCPT TO:<nobody@example.com>" stdout
1377 '
1378
1379 test_expect_success $PREREQ 'To headers from files reset each patch' '
1380         patch1=$(git format-patch -1 --to="bodies@example.com") &&
1381         patch2=$(git format-patch -1 --to="other@example.com" HEAD~) &&
1382         test_when_finished "rm $patch1 && rm $patch2" &&
1383         git send-email \
1384                 --dry-run \
1385                 --from="Example <nobody@example.com>" \
1386                 --to="nobody@example.com" \
1387                 --smtp-server relay.example.com \
1388                 $patch1 $patch2 >stdout &&
1389         test $(grep -c "RCPT TO:<bodies@example.com>" stdout) = 1 &&
1390         test $(grep -c "RCPT TO:<nobody@example.com>" stdout) = 2 &&
1391         test $(grep -c "RCPT TO:<other@example.com>" stdout) = 1
1392 '
1393
1394 test_expect_success $PREREQ 'setup expect' '
1395 cat >email-using-8bit <<\EOF
1396 From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1397 Message-Id: <bogus-message-id@example.com>
1398 From: author@example.com
1399 Date: Sat, 12 Jun 2010 15:53:58 +0200
1400 Subject: subject goes here
1401
1402 Dieser deutsche Text enthält einen Umlaut!
1403 EOF
1404 '
1405
1406 test_expect_success $PREREQ 'setup expect' '
1407         echo "Subject: subject goes here" >expected
1408 '
1409
1410 test_expect_success $PREREQ 'ASCII subject is not RFC2047 quoted' '
1411         clean_fake_sendmail &&
1412         echo bogus |
1413         git send-email --from=author@example.com --to=nobody@example.com \
1414                         --smtp-server="$(pwd)/fake.sendmail" \
1415                         --8bit-encoding=UTF-8 \
1416                         email-using-8bit >stdout &&
1417         grep "Subject" msgtxt1 >actual &&
1418         test_cmp expected actual
1419 '
1420
1421 test_expect_success $PREREQ 'setup expect' '
1422         cat >content-type-decl <<-\EOF
1423         MIME-Version: 1.0
1424         Content-Type: text/plain; charset=UTF-8
1425         Content-Transfer-Encoding: 8bit
1426         EOF
1427 '
1428
1429 test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
1430         clean_fake_sendmail &&
1431         echo |
1432         git send-email --from=author@example.com --to=nobody@example.com \
1433                         --smtp-server="$(pwd)/fake.sendmail" \
1434                         email-using-8bit >stdout &&
1435         grep "do not declare a Content-Transfer-Encoding" stdout &&
1436         grep email-using-8bit stdout &&
1437         grep "Which 8bit encoding" stdout &&
1438         egrep "Content|MIME" msgtxt1 >actual &&
1439         test_cmp content-type-decl actual
1440 '
1441
1442 test_expect_success $PREREQ 'sendemail.8bitEncoding works' '
1443         clean_fake_sendmail &&
1444         git config sendemail.assume8bitEncoding UTF-8 &&
1445         echo bogus |
1446         git send-email --from=author@example.com --to=nobody@example.com \
1447                         --smtp-server="$(pwd)/fake.sendmail" \
1448                         email-using-8bit >stdout &&
1449         egrep "Content|MIME" msgtxt1 >actual &&
1450         test_cmp content-type-decl actual
1451 '
1452
1453 test_expect_success $PREREQ '--8bit-encoding overrides sendemail.8bitEncoding' '
1454         clean_fake_sendmail &&
1455         git config sendemail.assume8bitEncoding "bogus too" &&
1456         echo bogus |
1457         git send-email --from=author@example.com --to=nobody@example.com \
1458                         --smtp-server="$(pwd)/fake.sendmail" \
1459                         --8bit-encoding=UTF-8 \
1460                         email-using-8bit >stdout &&
1461         egrep "Content|MIME" msgtxt1 >actual &&
1462         test_cmp content-type-decl actual
1463 '
1464
1465 test_expect_success $PREREQ 'setup expect' '
1466         cat >email-using-8bit <<-\EOF
1467         From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1468         Message-Id: <bogus-message-id@example.com>
1469         From: author@example.com
1470         Date: Sat, 12 Jun 2010 15:53:58 +0200
1471         Subject: Dieser Betreff enthält auch einen Umlaut!
1472
1473         Nothing to see here.
1474         EOF
1475 '
1476
1477 test_expect_success $PREREQ 'setup expect' '
1478         cat >expected <<-\EOF
1479         Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
1480         EOF
1481 '
1482
1483 test_expect_success $PREREQ '--8bit-encoding also treats subject' '
1484         clean_fake_sendmail &&
1485         echo bogus |
1486         git send-email --from=author@example.com --to=nobody@example.com \
1487                         --smtp-server="$(pwd)/fake.sendmail" \
1488                         --8bit-encoding=UTF-8 \
1489                         email-using-8bit >stdout &&
1490         grep "Subject" msgtxt1 >actual &&
1491         test_cmp expected actual
1492 '
1493
1494 test_expect_success $PREREQ 'setup expect' '
1495         cat >email-using-8bit <<-\EOF
1496         From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1497         Message-Id: <bogus-message-id@example.com>
1498         From: A U Thor <author@example.com>
1499         Date: Sat, 12 Jun 2010 15:53:58 +0200
1500         Content-Type: text/plain; charset=UTF-8
1501         Subject: Nothing to see here.
1502
1503         Dieser Betreff enthält auch einen Umlaut!
1504         EOF
1505 '
1506
1507 test_expect_success $PREREQ '--transfer-encoding overrides sendemail.transferEncoding' '
1508         clean_fake_sendmail &&
1509         test_must_fail git -c sendemail.transferEncoding=8bit \
1510                 send-email \
1511                 --transfer-encoding=7bit \
1512                 --smtp-server="$(pwd)/fake.sendmail" \
1513                 email-using-8bit \
1514                 2>errors >out &&
1515         grep "cannot send message as 7bit" errors &&
1516         test -z "$(ls msgtxt*)"
1517 '
1518
1519 test_expect_success $PREREQ 'sendemail.transferEncoding via config' '
1520         clean_fake_sendmail &&
1521         test_must_fail git -c sendemail.transferEncoding=7bit \
1522                 send-email \
1523                 --smtp-server="$(pwd)/fake.sendmail" \
1524                 email-using-8bit \
1525                 2>errors >out &&
1526         grep "cannot send message as 7bit" errors &&
1527         test -z "$(ls msgtxt*)"
1528 '
1529
1530 test_expect_success $PREREQ 'sendemail.transferEncoding via cli' '
1531         clean_fake_sendmail &&
1532         test_must_fail git send-email \
1533                 --transfer-encoding=7bit \
1534                 --smtp-server="$(pwd)/fake.sendmail" \
1535                 email-using-8bit \
1536                 2>errors >out &&
1537         grep "cannot send message as 7bit" errors &&
1538         test -z "$(ls msgtxt*)"
1539 '
1540
1541 test_expect_success $PREREQ 'setup expect' '
1542         cat >expected <<-\EOF
1543         Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1544         EOF
1545 '
1546
1547 test_expect_success $PREREQ '8-bit and sendemail.transferencoding=quoted-printable' '
1548         clean_fake_sendmail &&
1549         git send-email \
1550                 --transfer-encoding=quoted-printable \
1551                 --smtp-server="$(pwd)/fake.sendmail" \
1552                 email-using-8bit \
1553                 2>errors >out &&
1554         sed "1,/^$/d" msgtxt1 >actual &&
1555         test_cmp expected actual
1556 '
1557
1558 test_expect_success $PREREQ 'setup expect' '
1559         cat >expected <<-\EOF
1560         RGllc2VyIEJldHJlZmYgZW50aMOkbHQgYXVjaCBlaW5lbiBVbWxhdXQhCg==
1561         EOF
1562 '
1563
1564 test_expect_success $PREREQ '8-bit and sendemail.transferencoding=base64' '
1565         clean_fake_sendmail &&
1566         git send-email \
1567                 --transfer-encoding=base64 \
1568                 --smtp-server="$(pwd)/fake.sendmail" \
1569                 email-using-8bit \
1570                 2>errors >out &&
1571         sed "1,/^$/d" msgtxt1 >actual &&
1572         test_cmp expected actual
1573 '
1574
1575 test_expect_success $PREREQ 'setup expect' '
1576         cat >email-using-qp <<-\EOF
1577         From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1578         Message-Id: <bogus-message-id@example.com>
1579         From: A U Thor <author@example.com>
1580         Date: Sat, 12 Jun 2010 15:53:58 +0200
1581         MIME-Version: 1.0
1582         Content-Transfer-Encoding: quoted-printable
1583         Content-Type: text/plain; charset=UTF-8
1584         Subject: Nothing to see here.
1585
1586         Dieser Betreff enth=C3=A4lt auch einen Umlaut!
1587         EOF
1588 '
1589
1590 test_expect_success $PREREQ 'convert from quoted-printable to base64' '
1591         clean_fake_sendmail &&
1592         git send-email \
1593                 --transfer-encoding=base64 \
1594                 --smtp-server="$(pwd)/fake.sendmail" \
1595                 email-using-qp \
1596                 2>errors >out &&
1597         sed "1,/^$/d" msgtxt1 >actual &&
1598         test_cmp expected actual
1599 '
1600
1601 test_expect_success $PREREQ 'setup expect' "
1602 tr -d '\\015' | tr '%' '\\015' >email-using-crlf <<EOF
1603 From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
1604 Message-Id: <bogus-message-id@example.com>
1605 From: A U Thor <author@example.com>
1606 Date: Sat, 12 Jun 2010 15:53:58 +0200
1607 Content-Type: text/plain; charset=UTF-8
1608 Subject: Nothing to see here.
1609
1610 Look, I have a CRLF and an = sign!%
1611 EOF
1612 "
1613
1614 test_expect_success $PREREQ 'setup expect' '
1615         cat >expected <<-\EOF
1616         Look, I have a CRLF and an =3D sign!=0D
1617         EOF
1618 '
1619
1620 test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=quoted-printable' '
1621         clean_fake_sendmail &&
1622         git send-email \
1623                 --transfer-encoding=quoted-printable \
1624                 --smtp-server="$(pwd)/fake.sendmail" \
1625                 email-using-crlf \
1626                 2>errors >out &&
1627         sed "1,/^$/d" msgtxt1 >actual &&
1628         test_cmp expected actual
1629 '
1630
1631 test_expect_success $PREREQ 'setup expect' '
1632         cat >expected <<-\EOF
1633         TG9vaywgSSBoYXZlIGEgQ1JMRiBhbmQgYW4gPSBzaWduIQ0K
1634         EOF
1635 '
1636
1637 test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=base64' '
1638         clean_fake_sendmail &&
1639         git send-email \
1640                 --transfer-encoding=base64 \
1641                 --smtp-server="$(pwd)/fake.sendmail" \
1642                 email-using-crlf \
1643                 2>errors >out &&
1644         sed "1,/^$/d" msgtxt1 >actual &&
1645         test_cmp expected actual
1646 '
1647
1648
1649 # Note that the patches in this test are deliberately out of order; we
1650 # want to make sure it works even if the cover-letter is not in the
1651 # first mail.
1652 test_expect_success $PREREQ 'refusing to send cover letter template' '
1653         clean_fake_sendmail &&
1654         rm -fr outdir &&
1655         git format-patch --cover-letter -2 -o outdir &&
1656         test_must_fail git send-email \
1657                 --from="Example <nobody@example.com>" \
1658                 --to=nobody@example.com \
1659                 --smtp-server="$(pwd)/fake.sendmail" \
1660                 outdir/0002-*.patch \
1661                 outdir/0000-*.patch \
1662                 outdir/0001-*.patch \
1663                 2>errors >out &&
1664         grep "SUBJECT HERE" errors &&
1665         test -z "$(ls msgtxt*)"
1666 '
1667
1668 test_expect_success $PREREQ '--force sends cover letter template anyway' '
1669         clean_fake_sendmail &&
1670         rm -fr outdir &&
1671         git format-patch --cover-letter -2 -o outdir &&
1672         git send-email \
1673                 --force \
1674                 --from="Example <nobody@example.com>" \
1675                 --to=nobody@example.com \
1676                 --smtp-server="$(pwd)/fake.sendmail" \
1677                 outdir/0002-*.patch \
1678                 outdir/0000-*.patch \
1679                 outdir/0001-*.patch \
1680                 2>errors >out &&
1681         ! grep "SUBJECT HERE" errors &&
1682         test -n "$(ls msgtxt*)"
1683 '
1684
1685 test_cover_addresses () {
1686         header="$1"
1687         shift
1688         clean_fake_sendmail &&
1689         rm -fr outdir &&
1690         git format-patch --cover-letter -2 -o outdir &&
1691         cover=$(echo outdir/0000-*.patch) &&
1692         mv $cover cover-to-edit.patch &&
1693         perl -pe "s/^From:/$header: extra\@address.com\nFrom:/" cover-to-edit.patch >"$cover" &&
1694         git send-email \
1695                 --force \
1696                 --from="Example <nobody@example.com>" \
1697                 --no-to --no-cc \
1698                 "$@" \
1699                 --smtp-server="$(pwd)/fake.sendmail" \
1700                 outdir/0000-*.patch \
1701                 outdir/0001-*.patch \
1702                 outdir/0002-*.patch \
1703                 2>errors >out &&
1704         grep "^$header: extra@address.com" msgtxt1 >to1 &&
1705         grep "^$header: extra@address.com" msgtxt2 >to2 &&
1706         grep "^$header: extra@address.com" msgtxt3 >to3 &&
1707         test_line_count = 1 to1 &&
1708         test_line_count = 1 to2 &&
1709         test_line_count = 1 to3
1710 }
1711
1712 test_expect_success $PREREQ 'to-cover adds To to all mail' '
1713         test_cover_addresses "To" --to-cover
1714 '
1715
1716 test_expect_success $PREREQ 'cc-cover adds Cc to all mail' '
1717         test_cover_addresses "Cc" --cc-cover
1718 '
1719
1720 test_expect_success $PREREQ 'tocover adds To to all mail' '
1721         test_config sendemail.tocover true &&
1722         test_cover_addresses "To"
1723 '
1724
1725 test_expect_success $PREREQ 'cccover adds Cc to all mail' '
1726         test_config sendemail.cccover true &&
1727         test_cover_addresses "Cc"
1728 '
1729
1730 test_expect_success $PREREQ 'escaped quotes in sendemail.aliasfiletype=mutt' '
1731         clean_fake_sendmail &&
1732         echo "alias sbd \\\"Dot U. Sir\\\" <somebody@example.org>" >.mutt &&
1733         git config --replace-all sendemail.aliasesfile "$(pwd)/.mutt" &&
1734         git config sendemail.aliasfiletype mutt &&
1735         git send-email \
1736                 --from="Example <nobody@example.com>" \
1737                 --to=sbd \
1738                 --smtp-server="$(pwd)/fake.sendmail" \
1739                 outdir/0001-*.patch \
1740                 2>errors >out &&
1741         grep "^!somebody@example\.org!$" commandline1 &&
1742         grep -F "To: \"Dot U. Sir\" <somebody@example.org>" out
1743 '
1744
1745 test_expect_success $PREREQ 'sendemail.aliasfiletype=mailrc' '
1746         clean_fake_sendmail &&
1747         echo "alias sbd  somebody@example.org" >.mailrc &&
1748         git config --replace-all sendemail.aliasesfile "$(pwd)/.mailrc" &&
1749         git config sendemail.aliasfiletype mailrc &&
1750         git send-email \
1751                 --from="Example <nobody@example.com>" \
1752                 --to=sbd \
1753                 --smtp-server="$(pwd)/fake.sendmail" \
1754                 outdir/0001-*.patch \
1755                 2>errors >out &&
1756         grep "^!somebody@example\.org!$" commandline1
1757 '
1758
1759 test_expect_success $PREREQ 'sendemail.aliasfile=~/.mailrc' '
1760         clean_fake_sendmail &&
1761         echo "alias sbd  someone@example.org" >"$HOME/.mailrc" &&
1762         git config --replace-all sendemail.aliasesfile "~/.mailrc" &&
1763         git config sendemail.aliasfiletype mailrc &&
1764         git send-email \
1765                 --from="Example <nobody@example.com>" \
1766                 --to=sbd \
1767                 --smtp-server="$(pwd)/fake.sendmail" \
1768                 outdir/0001-*.patch \
1769                 2>errors >out &&
1770         grep "^!someone@example\.org!$" commandline1
1771 '
1772
1773 test_dump_aliases () {
1774         msg="$1" && shift &&
1775         filetype="$1" && shift &&
1776         printf '%s\n' "$@" >expect &&
1777         cat >.tmp-email-aliases &&
1778
1779         test_expect_success $PREREQ "$msg" '
1780                 clean_fake_sendmail && rm -fr outdir &&
1781                 git config --replace-all sendemail.aliasesfile \
1782                         "$(pwd)/.tmp-email-aliases" &&
1783                 git config sendemail.aliasfiletype "$filetype" &&
1784                 git send-email --dump-aliases 2>errors >actual &&
1785                 test_cmp expect actual
1786         '
1787 }
1788
1789 test_dump_aliases '--dump-aliases sendmail format' \
1790         'sendmail' \
1791         'abgroup' \
1792         'alice' \
1793         'bcgrp' \
1794         'bob' \
1795         'chloe' <<-\EOF
1796         alice: Alice W Land <awol@example.com>
1797         bob: Robert Bobbyton <bob@example.com>
1798         chloe: chloe@example.com
1799         abgroup: alice, bob
1800         bcgrp: bob, chloe, Other <o@example.com>
1801         EOF
1802
1803 test_dump_aliases '--dump-aliases mutt format' \
1804         'mutt' \
1805         'alice' \
1806         'bob' \
1807         'chloe' \
1808         'donald' <<-\EOF
1809         alias alice Alice W Land <awol@example.com>
1810         alias donald Donald C Carlton <donc@example.com>
1811         alias bob Robert Bobbyton <bob@example.com>
1812         alias chloe chloe@example.com
1813         EOF
1814
1815 test_dump_aliases '--dump-aliases mailrc format' \
1816         'mailrc' \
1817         'alice' \
1818         'bob' \
1819         'chloe' \
1820         'eve' <<-\EOF
1821         alias alice   Alice W Land <awol@example.com>
1822         alias eve     Eve <eve@example.com>
1823         alias bob     Robert Bobbyton <bob@example.com>
1824         alias chloe   chloe@example.com
1825         EOF
1826
1827 test_dump_aliases '--dump-aliases pine format' \
1828         'pine' \
1829         'alice' \
1830         'bob' \
1831         'chloe' \
1832         'eve' <<-\EOF
1833         alice   Alice W Land    <awol@example.com>
1834         eve     Eve     <eve@example.com>
1835         bob     Robert  Bobbyton <bob@example.com>
1836         chloe           chloe@example.com
1837         EOF
1838
1839 test_dump_aliases '--dump-aliases gnus format' \
1840         'gnus' \
1841         'alice' \
1842         'bob' \
1843         'chloe' \
1844         'eve' <<-\EOF
1845         (define-mail-alias "alice" "awol@example.com")
1846         (define-mail-alias "eve" "eve@example.com")
1847         (define-mail-alias "bob" "bob@example.com")
1848         (define-mail-alias "chloe" "chloe@example.com")
1849         EOF
1850
1851 test_expect_success '--dump-aliases must be used alone' '
1852         test_must_fail git send-email --dump-aliases --to=janice@example.com -1 refs/heads/accounting
1853 '
1854
1855 test_expect_success $PREREQ 'aliases and sendemail.identity' '
1856         test_must_fail git \
1857                 -c sendemail.identity=cloud \
1858                 -c sendemail.aliasesfile=default-aliases \
1859                 -c sendemail.cloud.aliasesfile=cloud-aliases \
1860                 send-email -1 2>stderr &&
1861         test_i18ngrep "cloud-aliases" stderr
1862 '
1863
1864 test_sendmail_aliases () {
1865         msg="$1" && shift &&
1866         expect="$@" &&
1867         cat >.tmp-email-aliases &&
1868
1869         test_expect_success $PREREQ "$msg" '
1870                 clean_fake_sendmail && rm -fr outdir &&
1871                 git format-patch -1 -o outdir &&
1872                 git config --replace-all sendemail.aliasesfile \
1873                         "$(pwd)/.tmp-email-aliases" &&
1874                 git config sendemail.aliasfiletype sendmail &&
1875                 git send-email \
1876                         --from="Example <nobody@example.com>" \
1877                         --to=alice --to=bcgrp \
1878                         --smtp-server="$(pwd)/fake.sendmail" \
1879                         outdir/0001-*.patch \
1880                         2>errors >out &&
1881                 for i in $expect
1882                 do
1883                         grep "^!$i!$" commandline1 || return 1
1884                 done
1885         '
1886 }
1887
1888 test_sendmail_aliases 'sendemail.aliasfiletype=sendmail' \
1889         'awol@example\.com' \
1890         'bob@example\.com' \
1891         'chloe@example\.com' \
1892         'o@example\.com' <<-\EOF
1893         alice: Alice W Land <awol@example.com>
1894         bob: Robert Bobbyton <bob@example.com>
1895         # this is a comment
1896            # this is also a comment
1897         chloe: chloe@example.com
1898         abgroup: alice, bob
1899         bcgrp: bob, chloe, Other <o@example.com>
1900         EOF
1901
1902 test_sendmail_aliases 'sendmail aliases line folding' \
1903         alice1 \
1904         bob1 bob2 \
1905         chuck1 chuck2 \
1906         darla1 darla2 darla3 \
1907         elton1 elton2 elton3 \
1908         fred1 fred2 \
1909         greg1 <<-\EOF
1910         alice: alice1
1911         bob: bob1,\
1912         bob2
1913         chuck: chuck1,
1914             chuck2
1915         darla: darla1,\
1916         darla2,
1917             darla3
1918         elton: elton1,
1919             elton2,\
1920         elton3
1921         fred: fred1,\
1922             fred2
1923         greg: greg1
1924         bcgrp: bob, chuck, darla, elton, fred, greg
1925         EOF
1926
1927 test_sendmail_aliases 'sendmail aliases tolerate bogus line folding' \
1928         alice1 bob1 <<-\EOF
1929             alice: alice1
1930         bcgrp: bob1\
1931         EOF
1932
1933 test_sendmail_aliases 'sendmail aliases empty' alice bcgrp <<-\EOF
1934         EOF
1935
1936 test_expect_success $PREREQ 'alias support in To header' '
1937         clean_fake_sendmail &&
1938         echo "alias sbd  someone@example.org" >.mailrc &&
1939         test_config sendemail.aliasesfile ".mailrc" &&
1940         test_config sendemail.aliasfiletype mailrc &&
1941         git format-patch --stdout -1 --to=sbd >aliased.patch &&
1942         git send-email \
1943                 --from="Example <nobody@example.com>" \
1944                 --smtp-server="$(pwd)/fake.sendmail" \
1945                 aliased.patch \
1946                 2>errors >out &&
1947         grep "^!someone@example\.org!$" commandline1
1948 '
1949
1950 test_expect_success $PREREQ 'alias support in Cc header' '
1951         clean_fake_sendmail &&
1952         echo "alias sbd  someone@example.org" >.mailrc &&
1953         test_config sendemail.aliasesfile ".mailrc" &&
1954         test_config sendemail.aliasfiletype mailrc &&
1955         git format-patch --stdout -1 --cc=sbd >aliased.patch &&
1956         git send-email \
1957                 --from="Example <nobody@example.com>" \
1958                 --smtp-server="$(pwd)/fake.sendmail" \
1959                 aliased.patch \
1960                 2>errors >out &&
1961         grep "^!someone@example\.org!$" commandline1
1962 '
1963
1964 test_expect_success $PREREQ 'tocmd works with aliases' '
1965         clean_fake_sendmail &&
1966         echo "alias sbd  someone@example.org" >.mailrc &&
1967         test_config sendemail.aliasesfile ".mailrc" &&
1968         test_config sendemail.aliasfiletype mailrc &&
1969         git format-patch --stdout -1 >tocmd.patch &&
1970         echo tocmd--sbd >>tocmd.patch &&
1971         git send-email \
1972                 --from="Example <nobody@example.com>" \
1973                 --to-cmd=./tocmd-sed \
1974                 --smtp-server="$(pwd)/fake.sendmail" \
1975                 tocmd.patch \
1976                 2>errors >out &&
1977         grep "^!someone@example\.org!$" commandline1
1978 '
1979
1980 test_expect_success $PREREQ 'cccmd works with aliases' '
1981         clean_fake_sendmail &&
1982         echo "alias sbd  someone@example.org" >.mailrc &&
1983         test_config sendemail.aliasesfile ".mailrc" &&
1984         test_config sendemail.aliasfiletype mailrc &&
1985         git format-patch --stdout -1 >cccmd.patch &&
1986         echo cccmd--sbd >>cccmd.patch &&
1987         git send-email \
1988                 --from="Example <nobody@example.com>" \
1989                 --cc-cmd=./cccmd-sed \
1990                 --smtp-server="$(pwd)/fake.sendmail" \
1991                 cccmd.patch \
1992                 2>errors >out &&
1993         grep "^!someone@example\.org!$" commandline1
1994 '
1995
1996 do_xmailer_test () {
1997         expected=$1 params=$2 &&
1998         git format-patch -1 &&
1999         git send-email \
2000                 --from="Example <nobody@example.com>" \
2001                 --to=someone@example.com \
2002                 --smtp-server="$(pwd)/fake.sendmail" \
2003                 $params \
2004                 0001-*.patch \
2005                 2>errors >out &&
2006         { grep '^X-Mailer:' out || :; } >mailer &&
2007         test_line_count = $expected mailer
2008 }
2009
2010 test_expect_success $PREREQ '--[no-]xmailer without any configuration' '
2011         do_xmailer_test 1 "--xmailer" &&
2012         do_xmailer_test 0 "--no-xmailer"
2013 '
2014
2015 test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=true' '
2016         test_config sendemail.xmailer true &&
2017         do_xmailer_test 1 "" &&
2018         do_xmailer_test 0 "--no-xmailer" &&
2019         do_xmailer_test 1 "--xmailer"
2020 '
2021
2022 test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=false' '
2023         test_config sendemail.xmailer false &&
2024         do_xmailer_test 0 "" &&
2025         do_xmailer_test 0 "--no-xmailer" &&
2026         do_xmailer_test 1 "--xmailer"
2027 '
2028
2029 test_expect_success $PREREQ 'setup expected-list' '
2030         git send-email \
2031         --dry-run \
2032         --from="Example <from@example.com>" \
2033         --to="To 1 <to1@example.com>" \
2034         --to="to2@example.com" \
2035         --to="to3@example.com" \
2036         --cc="Cc 1 <cc1@example.com>" \
2037         --cc="Cc2 <cc2@example.com>" \
2038         --bcc="bcc1@example.com" \
2039         --bcc="bcc2@example.com" \
2040         0001-add-master.patch | replace_variable_fields \
2041         >expected-list
2042 '
2043
2044 test_expect_success $PREREQ 'use email list in --cc --to and --bcc' '
2045         git send-email \
2046         --dry-run \
2047         --from="Example <from@example.com>" \
2048         --to="To 1 <to1@example.com>, to2@example.com" \
2049         --to="to3@example.com" \
2050         --cc="Cc 1 <cc1@example.com>, Cc2 <cc2@example.com>" \
2051         --bcc="bcc1@example.com, bcc2@example.com" \
2052         0001-add-master.patch | replace_variable_fields \
2053         >actual-list &&
2054         test_cmp expected-list actual-list
2055 '
2056
2057 test_expect_success $PREREQ 'aliases work with email list' '
2058         echo "alias to2 to2@example.com" >.mutt &&
2059         echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
2060         test_config sendemail.aliasesfile ".mutt" &&
2061         test_config sendemail.aliasfiletype mutt &&
2062         git send-email \
2063         --dry-run \
2064         --from="Example <from@example.com>" \
2065         --to="To 1 <to1@example.com>, to2, to3@example.com" \
2066         --cc="cc1, Cc2 <cc2@example.com>" \
2067         --bcc="bcc1@example.com, bcc2@example.com" \
2068         0001-add-master.patch | replace_variable_fields \
2069         >actual-list &&
2070         test_cmp expected-list actual-list
2071 '
2072
2073 test_expect_success $PREREQ 'leading and trailing whitespaces are removed' '
2074         echo "alias to2 to2@example.com" >.mutt &&
2075         echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
2076         test_config sendemail.aliasesfile ".mutt" &&
2077         test_config sendemail.aliasfiletype mutt &&
2078         TO1=$(echo "QTo 1 <to1@example.com>" | q_to_tab) &&
2079         TO2=$(echo "QZto2" | qz_to_tab_space) &&
2080         CC1=$(echo "cc1" | append_cr) &&
2081         BCC1=$(echo " bcc1@example.com Q" | q_to_nul) &&
2082         git send-email \
2083         --dry-run \
2084         --from="        Example <from@example.com>" \
2085         --to="$TO1" \
2086         --to="$TO2" \
2087         --to="  to3@example.com   " \
2088         --cc="$CC1" \
2089         --cc="Cc2 <cc2@example.com>" \
2090         --bcc="$BCC1" \
2091         --bcc="bcc2@example.com" \
2092         0001-add-master.patch | replace_variable_fields \
2093         >actual-list &&
2094         test_cmp expected-list actual-list
2095 '
2096
2097 test_expect_success $PREREQ 'invoke hook' '
2098         mkdir -p .git/hooks &&
2099
2100         write_script .git/hooks/sendemail-validate <<-\EOF &&
2101         # test that we have the correct environment variable, pwd, and
2102         # argument
2103         case "$GIT_DIR" in
2104         *.git)
2105                 true
2106                 ;;
2107         *)
2108                 false
2109                 ;;
2110         esac &&
2111         test -f 0001-add-master.patch &&
2112         grep "add master" "$1"
2113         EOF
2114
2115         mkdir subdir &&
2116         (
2117                 # Test that it works even if we are not at the root of the
2118                 # working tree
2119                 cd subdir &&
2120                 git send-email \
2121                         --from="Example <nobody@example.com>" \
2122                         --to=nobody@example.com \
2123                         --smtp-server="$(pwd)/../fake.sendmail" \
2124                         ../0001-add-master.patch &&
2125
2126                 # Verify error message when a patch is rejected by the hook
2127                 sed -e "s/add master/x/" ../0001-add-master.patch >../another.patch &&
2128                 test_must_fail git send-email \
2129                         --from="Example <nobody@example.com>" \
2130                         --to=nobody@example.com \
2131                         --smtp-server="$(pwd)/../fake.sendmail" \
2132                         ../another.patch 2>err &&
2133                 test_i18ngrep "rejected by sendemail-validate hook" err
2134         )
2135 '
2136
2137 test_expect_success $PREREQ 'test that send-email works outside a repo' '
2138         nongit git send-email \
2139                 --from="Example <nobody@example.com>" \
2140                 --to=nobody@example.com \
2141                 --smtp-server="$(pwd)/fake.sendmail" \
2142                 "$(pwd)/0001-add-master.patch"
2143 '
2144
2145 test_expect_success $PREREQ 'test that sendmail config is rejected' '
2146         test_config sendmail.program sendmail &&
2147         test_must_fail git send-email \
2148                 --from="Example <nobody@example.com>" \
2149                 --to=nobody@example.com \
2150                 --smtp-server="$(pwd)/fake.sendmail" \
2151                 HEAD^ 2>err &&
2152         test_i18ngrep "found configuration options for '"'"sendmail"'"'" err
2153 '
2154
2155 test_expect_success $PREREQ 'test that sendmail config rejection is specific' '
2156         test_config resendmail.program sendmail &&
2157         git send-email \
2158                 --from="Example <nobody@example.com>" \
2159                 --to=nobody@example.com \
2160                 --smtp-server="$(pwd)/fake.sendmail" \
2161                 HEAD^
2162 '
2163
2164 test_expect_success $PREREQ 'test forbidSendmailVariables behavior override' '
2165         test_config sendmail.program sendmail &&
2166         test_config sendemail.forbidSendmailVariables false &&
2167         git send-email \
2168                 --from="Example <nobody@example.com>" \
2169                 --to=nobody@example.com \
2170                 --smtp-server="$(pwd)/fake.sendmail" \
2171                 HEAD^
2172 '
2173
2174 test_done