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