[PATCH 2 of 2 stable] util: fix ellipsis() not to break multi-byte sequence (issue2564)

Yuya Nishihara yuya at tcha.org
Thu Dec 23 10:37:13 CST 2010


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1293121040 -32400
# Branch stable
# Node ID 2ab92b58076868e42c632828b2487cabe2823e8e
# Parent  0ed736fe75b467ad9191f2ef52129992381659e5
util: fix ellipsis() not to break multi-byte sequence (issue2564)

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1202,10 +1202,13 @@ def email(author):
 
 def ellipsis(text, maxlength=400):
     """Trim string to at most maxlength (default: 400) characters."""
-    if len(text) <= maxlength:
+    utext = encoding.fromlocal(text).decode('utf-8')
+    if len(utext) <= maxlength:
         return text
     else:
-        return "%s..." % (text[:maxlength - 3])
+        # use unicode not to split at intermediate multi-byte sequence
+        utext = utext[:maxlength - 3]
+        return "%s..." % encoding.tolocal(utext.encode('utf-8'))
 
 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
     '''yield every hg repository under path, recursively.'''
diff --git a/tests/test-notify.t b/tests/test-notify.t
--- a/tests/test-notify.t
+++ b/tests/test-notify.t
@@ -302,3 +302,49 @@ test merge
   changeset 22c88b85aa27 in b
   description: merge
   (run 'hg update' to get a working copy)
+
+truncate multi-byte subject
+
+  $ cat <<EOF >> $HGRCPATH
+  > [notify]
+  > maxsubject = 4
+  > EOF
+  $ echo a >> a/a
+  $ HGENCODING=utf-8 hg --cwd a commit -A -d '0 0' \
+  >   -m `python -c 'print "\xc3\xa0\xc3\xa1\xc3\xa2\xc3\xa3\xc3\xa4"'`
+  $ HGENCODING=utf-8 hg --traceback --cwd b pull ../a | \
+  >   python -c 'import sys,re; print re.sub("\n\t", " ", sys.stdin.read()),'
+  pulling from ../a
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+  Content-Type: text/plain; charset="us-ascii"
+  MIME-Version: 1.0
+  Content-Transfer-Encoding: 8bit
+  X-Test: foo
+  Date: * (glob)
+  Subject: \xc3\xa0... (esc)
+  From: test at test.com
+  X-Hg-Notification: changeset 4a47f01c1356
+  Message-Id: <*> (glob)
+  To: baz at test.com, foo at bar
+  
+  changeset 4a47f01c1356 in b
+  description: \xc3\xa0\xc3\xa1\xc3\xa2\xc3\xa3\xc3\xa4 (esc)
+  diffstat:
+  
+   a |  1 +
+   1 files changed, 1 insertions(+), 0 deletions(-)
+  
+  diffs (7 lines):
+  
+  diff -r 22c88b85aa27 -r 4a47f01c1356 a
+  --- a/a	Thu Jan 01 00:00:03 1970 +0000
+  +++ b/a	Thu Jan 01 00:00:00 1970 +0000
+  @@ -1,2 +1,3 @@
+   a
+   a
+  +a
+  (run 'hg update' to get a working copy)


More information about the Mercurial-devel mailing list