[PATCH] mail: Take --encoding and HGENCODING into account

Gábor Stefanik gabor.stefanik at nng.com
Wed Oct 5 11:46:15 UTC 2016


# HG changeset patch
# User Gábor Stefanik <gabor.stefanik at nng.com>
# Date 1475667922 -7200
#      Wed Oct 05 13:45:22 2016 +0200
# Node ID 85a6a72d4868c20c066ae4add380b70d13c1c2df
# Parent  1779dde4c9ef97cb242f8d501655f236f66e5439
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 -r 1779dde4c9ef -r 85a6a72d4868 mercurial/mail.py
--- a/mercurial/mail.py	Sun Oct 02 22:34:40 2016 -0700
+++ b/mercurial/mail.py	Wed Oct 05 13:45:22 2016 +0200
@@ -205,9 +205,17 @@
 
 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 as us-ascii or utf-8, falling back to hg's
+    current encoding if neither of those works.
     Transfer encodings will be used if necessary.'''
 
+    def codec2iana(encoding):
+        encoding = email.charset.Charset(encoding).input_charset
+        
+        if encoding.startswith("iso") and not encoding.startswith("iso-"):
+            return "iso-" + encoding[3:]
+        return encoding
+    
     cs = 'us-ascii'
     if not display:
         try:
@@ -217,10 +225,10 @@
                 s.decode('utf-8')
                 cs = 'utf-8'
             except UnicodeDecodeError:
-                # We'll go with us-ascii as a fallback.
-                pass
+                # We'll go with HGENCODING as a fallback.
+                cs = encoding.encoding
 
-    return mimetextqp(s, subtype, cs)
+    return mimetextqp(s, subtype, codec2iana(cs))
 
 def mimetextqp(body, subtype, charset):
     '''Return MIME message.


More information about the Mercurial-devel mailing list