Patchwork templater: do not strip non-quote characters from template config

login
register
mail settings
Submitter Yuya Nishihara
Date March 29, 2016, 3:26 p.m.
Message ID <b106317fea11feb11986.1459265208@mimosa>
Download mbox | patch
Permalink /patch/14138/
State Accepted
Headers show

Comments

Yuya Nishihara - March 29, 2016, 3:26 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1459068139 -32400
#      Sun Mar 27 17:42:19 2016 +0900
# Node ID b106317fea11feb11986aefadfbedc0f98efdf25
# Parent  c4b727795d6a5770ec523c1965b464baed81097f
templater: do not strip non-quote characters from template config

Before this patch, the first and last characters were stripped from
ui.logtemplate and template.* if they were the same. It could lead to a
strange result as quotes are optional. See the test for example.
Sean Farley - March 29, 2016, 8:09 p.m.
Yuya Nishihara <yuya@tcha.org> writes:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1459068139 -32400
> #      Sun Mar 27 17:42:19 2016 +0900
> # Node ID b106317fea11feb11986aefadfbedc0f98efdf25
> # Parent  c4b727795d6a5770ec523c1965b464baed81097f
> templater: do not strip non-quote characters from template config
>
> Before this patch, the first and last characters were stripped from
> ui.logtemplate and template.* if they were the same. It could lead to a
> strange result as quotes are optional. See the test for example.

Looks good to me.
Pierre-Yves David - March 30, 2016, 12:37 a.m.
On 03/29/2016 08:26 AM, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1459068139 -32400
> #      Sun Mar 27 17:42:19 2016 +0900
> # Node ID b106317fea11feb11986aefadfbedc0f98efdf25
> # Parent  c4b727795d6a5770ec523c1965b464baed81097f
> templater: do not strip non-quote characters from template config
>
> Before this patch, the first and last characters were stripped from
> ui.logtemplate and template.* if they were the same. It could lead to a
> strange result as quotes are optional. See the test for example.

What a cute little funny bug.

Pushed, much thanks.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -868,7 +868,7 @@  def _flatten(thing):
 
 def unquotestring(s):
     '''unwrap quotes if any; otherwise returns unmodified string'''
-    if len(s) < 2 or s[0] != s[-1]:
+    if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]:
         return s
     return s[1:-1]
 
diff --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -71,6 +71,8 @@  Quoting for ui.logtemplate
   8
   $ hg tip --config 'ui.logtemplate="{rev}\n"'
   8
+  $ hg tip --config 'ui.logtemplate=n{rev}\n'
+  n8
 
 Make sure user/global hgrc does not affect tests