Skip to content

Commit 0706bd1

Browse files
peffgitster
authored andcommitted
send-email: specify content-type of --compose body
If the compose message contains non-ascii characters, then we assume it is in utf-8 and include the appropriate MIME headers. If the user has already included a MIME-Version header, then we assume they know what they are doing and don't add any headers. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4d594c commit 0706bd1

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

git-send-email.perl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,22 @@ sub expand_aliases {
518518
open(C,"<",$compose_filename)
519519
or die "Failed to open $compose_filename : " . $!;
520520

521+
my $need_8bit_cte = file_has_nonascii($compose_filename);
522+
my $in_body = 0;
521523
while(<C>) {
522524
next if m/^GIT: /;
525+
if (!$in_body && /^\n$/) {
526+
$in_body = 1;
527+
if ($need_8bit_cte) {
528+
print C2 "MIME-Version: 1.0\n",
529+
"Content-Type: text/plain; ",
530+
"charset=utf-8\n",
531+
"Content-Transfer-Encoding: 8bit\n";
532+
}
533+
}
534+
if (!$in_body && /^MIME-Version:/i) {
535+
$need_8bit_cte = 0;
536+
}
523537
print C2 $_;
524538
}
525539
close(C);
@@ -956,3 +970,13 @@ sub validate_patch {
956970
}
957971
return undef;
958972
}
973+
974+
sub file_has_nonascii {
975+
my $fn = shift;
976+
open(my $fh, '<', $fn)
977+
or die "unable to open $fn: $!\n";
978+
while (my $line = <$fh>) {
979+
return 1 if $line =~ /[^[:ascii:]]/;
980+
}
981+
return 0;
982+
}

t/t9001-send-email.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,48 @@ test_expect_success 'second message is patch' '
166166
grep "Subject:.*Second" msgtxt2
167167
'
168168

169+
test_expect_success '--compose adds MIME for utf8 body' '
170+
clean_fake_sendmail &&
171+
(echo "#!/bin/sh" &&
172+
echo "echo utf8 body: àéìöú >>\$1"
173+
) >fake-editor-utf8 &&
174+
chmod +x fake-editor-utf8 &&
175+
echo y | \
176+
GIT_EDITOR=$(pwd)/fake-editor-utf8 \
177+
GIT_SEND_EMAIL_NOTTY=1 \
178+
git send-email \
179+
--compose --subject foo \
180+
--from="Example <[email protected]>" \
181+
182+
--smtp-server="$(pwd)/fake.sendmail" \
183+
$patches &&
184+
grep "^utf8 body" msgtxt1 &&
185+
grep "^Content-Type: text/plain; charset=utf-8" msgtxt1
186+
'
187+
188+
test_expect_success '--compose respects user mime type' '
189+
clean_fake_sendmail &&
190+
(echo "#!/bin/sh" &&
191+
echo "(echo MIME-Version: 1.0"
192+
echo " echo Content-Type: text/plain\\; charset=iso-8859-1"
193+
echo " echo Content-Transfer-Encoding: 8bit"
194+
echo " echo Subject: foo"
195+
echo " echo "
196+
echo " echo utf8 body: àéìöú) >\$1"
197+
) >fake-editor-utf8-mime &&
198+
chmod +x fake-editor-utf8-mime &&
199+
echo y | \
200+
GIT_EDITOR=$(pwd)/fake-editor-utf8-mime \
201+
GIT_SEND_EMAIL_NOTTY=1 \
202+
git send-email \
203+
--compose --subject foo \
204+
--from="Example <[email protected]>" \
205+
206+
--smtp-server="$(pwd)/fake.sendmail" \
207+
$patches &&
208+
grep "^utf8 body" msgtxt1 &&
209+
grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
210+
! grep "^Content-Type: text/plain; charset=utf-8" msgtxt1
211+
'
212+
169213
test_done

0 commit comments

Comments
 (0)