mail: take --encoding and HGENCODING into account
Fall back to our encoding strategy for sending MIME text
that's neither ASCII nor UTF-8.
--- a/mercurial/mail.py Sat Oct 08 02:26:48 2016 -0700
+++ b/mercurial/mail.py Wed Oct 05 13:45:22 2016 +0200
@@ -8,6 +8,7 @@
from __future__ import absolute_import, print_function
import email
+import email.charset
import email.header
import os
import quopri
@@ -204,24 +205,33 @@
raise error.Abort(_('%r specified as email transport, '
'but not in PATH') % method)
+def codec2iana(cs):
+ ''''''
+ cs = email.charset.Charset(cs).input_charset.lower()
+
+ # "latin1" normalizes to "iso8859-1", standard calls for "iso-8859-1"
+ if cs.startswith("iso") and not cs.startswith("iso-"):
+ return "iso-" + cs[3:]
+ return cs
+
def mimetextpatch(s, subtype='plain', display=False):
'''Return MIME message suitable for a patch.
- Charset will be detected as utf-8 or (possibly fake) us-ascii.
+ Charset will be detected by first trying to decode as us-ascii, then utf-8,
+ and finally the global encodings. If all those fail, fall back to
+ ISO-8859-1, an encoding with that allows all byte sequences.
Transfer encodings will be used if necessary.'''
- cs = 'us-ascii'
- if not display:
+ cs = ['us-ascii', 'utf-8', encoding.encoding, encoding.fallbackencoding]
+ if display:
+ return mimetextqp(s, subtype, 'us-ascii')
+ for charset in cs:
try:
- s.decode('us-ascii')
+ s.decode(charset)
+ return mimetextqp(s, subtype, codec2iana(charset))
except UnicodeDecodeError:
- try:
- s.decode('utf-8')
- cs = 'utf-8'
- except UnicodeDecodeError:
- # We'll go with us-ascii as a fallback.
- pass
+ pass
- return mimetextqp(s, subtype, cs)
+ return mimetextqp(s, subtype, "iso-8859-1")
def mimetextqp(body, subtype, charset):
'''Return MIME message.
--- a/tests/test-patchbomb.t Sat Oct 08 02:26:48 2016 -0700
+++ b/tests/test-patchbomb.t Wed Oct 05 13:45:22 2016 +0200
@@ -632,7 +632,7 @@
$ hg commit -A -d '5 0' -m 'isolatin 8-bit encoding'
adding isolatin
-fake ascii mbox:
+iso-8859-1 mbox:
$ hg email --date '1970-1-1 0:5' -f quux -t foo -c bar -r tip -m mbox
this patch series consists of 1 patches.
@@ -640,9 +640,9 @@
sending [PATCH] isolatin 8-bit encoding ...
$ cat mbox
From quux ... ... .. ..:..:.. .... (re)
- Content-Type: text/plain; charset="us-ascii"
+ Content-Type: text/plain; charset="iso-8859-1"
MIME-Version: 1.0
- Content-Transfer-Encoding: 8bit
+ Content-Transfer-Encoding: quoted-printable
Subject: [PATCH] isolatin 8-bit encoding
X-Mercurial-Node: 240fb913fc1b7ff15ddb9f33e73d82bf5277c720
X-Mercurial-Series-Index: 1
@@ -667,7 +667,7 @@
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/isolatin Thu Jan 01 00:00:05 1970 +0000
@@ -0,0 +1,1 @@
- +h\xf6mma! (esc)
+ +h=F6mma!