changeset 28239:7279e0132347

templatekw: workaround for utf-8 round-trip of {desc} Though our encoding strategy is best effort, {desc} is a primitive keyword that should be worth enough to try hard to preserve UTF-8 bytes.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 27 Dec 2015 20:21:37 +0900
parents b57d45ec33b2
children 1ac8ce137377
files mercurial/templatekw.py tests/test-command-template.t
diffstat 2 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templatekw.py	Thu Feb 25 10:34:31 2016 +0100
+++ b/mercurial/templatekw.py	Sun Dec 27 20:21:37 2015 +0900
@@ -9,6 +9,7 @@
 
 from .node import hex, nullid
 from . import (
+    encoding,
     error,
     hbisect,
     patch,
@@ -257,7 +258,12 @@
 
 def showdescription(repo, ctx, templ, **args):
     """:desc: String. The text of the changeset description."""
-    return ctx.description().strip()
+    s = ctx.description()
+    if isinstance(s, encoding.localstr):
+        # try hard to preserve utf-8 bytes
+        return encoding.tolocal(encoding.fromlocal(s).strip())
+    else:
+        return s.strip()
 
 def showdiffstat(repo, ctx, templ, **args):
     """:diffstat: String. Statistics of changes with the following format:
--- a/tests/test-command-template.t	Thu Feb 25 10:34:31 2016 +0100
+++ b/tests/test-command-template.t	Sun Dec 27 20:21:37 2015 +0900
@@ -3556,12 +3556,14 @@
   > open('utf-8', 'w').write('\xc3\xa9')
   > EOF
   $ HGENCODING=utf-8 hg branch -q `cat utf-8`
-  $ HGENCODING=utf-8 hg ci -qAm 'non-ascii branch' utf-8
+  $ HGENCODING=utf-8 hg ci -qAm "non-ascii branch: `cat utf-8`" utf-8
 
 json filter should try round-trip conversion to utf-8:
 
   $ HGENCODING=ascii hg log -T "{branch|json}\n" -r0
   "\u00e9"
+  $ HGENCODING=ascii hg log -T "{desc|json}\n" -r0
+  "non-ascii branch: \u00e9"
 
 json filter takes input as utf-8b: