[PATCH] mail: encode long unicode lines in emails properly (issue5687)

Ippolitov Igor iippolitov at gmail.com
Thu Sep 21 14:59:52 UTC 2017


# HG changeset patch
# User Igor Ippolitov <iippolitov at gmail.com>
# Date 1505982498 0
#      Thu Sep 21 08:28:18 2017 +0000
# Node ID 474d0bfc9809032668864cea16026f9b53d24a6d
# Parent  05131c963767faaac6a66b2c658659bfbb4db29b
mail: encode long unicode lines in emails properly (issue5687)

3e544c074459 introduced a bug: emails Content-Transfer-Encoding
is silently replaced with 'quoted-printable' while any other
encoding could be used by underlying code. The problem is revealed
when a long unicode line is encoded.

The patch implements proper check which works for any text and
encoding.
test-notify.t changed so that it would fail on unpatched code
test-patchbomb.t changed due to email headers order change

The patch won't work for python 3.6 as it lacks email.set_charset()

diff -r 05131c963767 -r 474d0bfc9809 mercurial/mail.py
--- a/mercurial/mail.py	Wed Sep 20 09:35:45 2017 -0700
+++ b/mercurial/mail.py	Thu Sep 21 08:28:18 2017 +0000
@@ -217,16 +217,22 @@
     Quoted-printable transfer encoding will be used if necessary.
     '''
     enc = None
+    cs = email.charset.Charset(charset)
+    msg = email.MIMEText.MIMEText('', subtype)
+    del msg['Content-Transfer-Encoding']
     for line in body.splitlines():
         if len(line) > 950:
             body = quopri.encodestring(body)
-            enc = "quoted-printable"
+            cs.body_encoding = email.charset.QP
+            enc = True
             break
+    if enc:
+        msg.set_charset(cs)
+        msg.set_payload(body)
+    else:
+        msg.set_payload(body)
+        msg.set_charset(cs)
 
-    msg = email.MIMEText.MIMEText(body, subtype, charset)
-    if enc:
-        del msg['Content-Transfer-Encoding']
-        msg['Content-Transfer-Encoding'] = enc
     return msg
 
 def _charsets(ui):
diff -r 05131c963767 -r 474d0bfc9809 tests/test-notify.t
--- a/tests/test-notify.t	Wed Sep 20 09:35:45 2017 -0700
+++ b/tests/test-notify.t	Thu Sep 21 08:28:18 2017 +0000
@@ -416,7 +416,7 @@
   > test = False
   > mbox = mbox
   > EOF
-  $ $PYTHON -c 'file("a/a", "ab").write("no" * 500 + "\n")'
+  $ $PYTHON -c 'file("a/a", "ab").write("no" * 500 + "\xd1\x84" + "\n")'
   $ hg --cwd a commit -A -m "long line"
   $ hg --traceback --cwd b pull ../a
   pulling from ../a
@@ -429,25 +429,25 @@
   (run 'hg update' to get a working copy)
   $ $PYTHON $TESTTMP/filter.py < b/mbox
   From test at test.com ... ... .. ..:..:.. .... (re)
-  Content-Type: text/plain; charset="us-ascii"
   MIME-Version: 1.0
+  Content-Type: text/plain; charset="*" (glob)
   Content-Transfer-Encoding: quoted-printable
   X-Test: foo
   Date: * (glob)
   Subject: long line
   From: test at test.com
-  X-Hg-Notification: changeset e0be44cf638b
-  Message-Id: <hg.e0be44cf638b.*.*@*> (glob)
+  X-Hg-Notification: changeset a323cae54f6e
+  Message-Id: <hg.a323cae54f6e.*.*@*> (glob)
   To: baz at test.com, foo at bar
   
-  changeset e0be44cf638b in b
+  changeset a323cae54f6e in b
   description: long line
   diffstat:
    a |  1 + 1 files changed, 1 insertions(+), 0 deletions(-)
   
   diffs (8 lines):
   
-  diff -r 7ea05ad269dc -r e0be44cf638b a
+  diff -r 7ea05ad269dc -r a323cae54f6e a
   --- a/a	Thu Jan 01 00:00:00 1970 +0000
   +++ b/a	Thu Jan 01 00:00:00 1970 +0000
   @@ -1,3 +1,4 @@ a a a
@@ -464,7 +464,7 @@
   ononononononononononononononononononononononononononononononononononononono=
   nononononononononononononononononononononononononononononononononononononon=
   ononononononononononononononononononononononononononononononononononononono=
-  nonononononononononononono
+  nonononononononononononono=D1=84
   
  revset selection: send to address that matches branch and repo
 
@@ -501,11 +501,11 @@
   Date: * (glob)
   Subject: test
   From: test at test.com
-  X-Hg-Notification: changeset fbbcbc516f2f
-  Message-Id: <hg.fbbcbc516f2f.*.*@*> (glob)
+  X-Hg-Notification: changeset b7cf10b2bdec
+  Message-Id: <hg.b7cf10b2bdec.*.*@*> (glob)
   To: baz at test.com, foo at bar, notify at example.com
   
-  changeset fbbcbc516f2f in b
+  changeset b7cf10b2bdec in b
   description: test
   (run 'hg update' to get a working copy)
 
@@ -530,11 +530,11 @@
   Date: * (glob)
   Subject: test
   From: test at test.com
-  X-Hg-Notification: changeset 38b42fa092de
-  Message-Id: <hg.38b42fa092de.*.*@*> (glob)
+  X-Hg-Notification: changeset 5a07df312a79
+  Message-Id: <hg.5a07df312a79.*.*@*> (glob)
   To: baz at test.com, foo at bar
   
-  changeset 38b42fa092de in b
+  changeset 5a07df312a79 in b
   description: test
   (run 'hg heads' to see heads)
 
@@ -551,12 +551,12 @@
   Date: * (glob)
   Subject: changeset in b: default template
   From: test at test.com
-  X-Hg-Notification: changeset 3548c9e294b6
-  Message-Id: <hg.3548c9e294b6.*.*@*> (glob)
+  X-Hg-Notification: changeset f5e8ec95bf59
+  Message-Id: <hg.f5e8ec95bf59.*.*@*> (glob)
   To: baz at test.com, foo at bar
   
-  changeset 3548c9e294b6 in $TESTTMP/b (glob)
-  details: http://test/b?cmd=changeset;node=3548c9e294b6
+  changeset f5e8ec95bf59 in $TESTTMP/b (glob)
+  details: http://test/b?cmd=changeset;node=f5e8ec95bf59
   description: default template
 
 with style:
@@ -580,11 +580,11 @@
   Date: * (glob)
   Subject: with style
   From: test at test.com
-  X-Hg-Notification: changeset e917dbd961d3
-  Message-Id: <hg.e917dbd961d3.*.*@*> (glob)
+  X-Hg-Notification: changeset 9e2c3a8e9c43
+  Message-Id: <hg.9e2c3a8e9c43.*.*@*> (glob)
   To: baz at test.com, foo at bar
   
-  changeset e917dbd961d3
+  changeset 9e2c3a8e9c43
 
 with template (overrides style):
 
@@ -601,10 +601,10 @@
   MIME-Version: 1.0
   Content-Transfer-Encoding: 7bit
   Date: * (glob)
-  Subject: a09743fd3edd: with template
+  Subject: e2cbf5bf18a7: with template
   From: test at test.com
-  X-Hg-Notification: changeset a09743fd3edd
-  Message-Id: <hg.a09743fd3edd.*.*@*> (glob)
+  X-Hg-Notification: changeset e2cbf5bf18a7
+  Message-Id: <hg.e2cbf5bf18a7.*.*@*> (glob)
   To: baz at test.com, foo at bar
   
   with template
diff -r 05131c963767 -r 474d0bfc9809 tests/test-patchbomb.t
--- a/tests/test-patchbomb.t	Wed Sep 20 09:35:45 2017 -0700
+++ b/tests/test-patchbomb.t	Thu Sep 21 08:28:18 2017 +0000
@@ -460,8 +460,8 @@
 
   $ cat mbox
   From quux ... ... .. ..:..:.. .... (re)
+  MIME-Version: 1.0
   Content-Type: text/plain; charset="utf-8"
-  MIME-Version: 1.0
   Content-Transfer-Encoding: base64
   Subject: [PATCH] utf-8 content
   X-Mercurial-Node: 909a00e13e9d78b575aeee23dddbada46d5a143f
@@ -638,8 +638,8 @@
   sending [PATCH] isolatin 8-bit encoding ...
   $ cat mbox
   From quux ... ... .. ..:..:.. .... (re)
+  MIME-Version: 1.0
   Content-Type: text/plain; charset="iso-8859-1"
-  MIME-Version: 1.0
   Content-Transfer-Encoding: quoted-printable
   Subject: [PATCH] isolatin 8-bit encoding
   X-Mercurial-Node: 240fb913fc1b7ff15ddb9f33e73d82bf5277c720


More information about the Mercurial-devel mailing list