# HG changeset patch
# User Gábor Stefanik <gabor.stefa...@nng.com>
# Date 1475667922 -7200
#      Wed Oct 05 13:45:22 2016 +0200
# Node ID c03ad1ac8772ffe8cd626c70ea0f544298fdcf9c
# Parent  91a3c58ecf938ed675f5364b88f0d663f12b0047
mail: take --encoding and HGENCODING into account

Fall back to our encoding strategy for sending MIME text
that's neither ASCII nor UTF-8.

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -8,6 +8,7 @@
 from __future__ import absolute_import, print_function
 
 import email
+import email.charset
 import os
 import quopri
 import smtplib
@@ -203,24 +204,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, None)
+    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.
_______________________________________________
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Reply via email to