mercurial/cmdutil.py
changeset 37774 d6970628b95f
parent 37763 b54404d66f7e
child 37778 f10cb49951e1
--- a/mercurial/cmdutil.py	Fri Apr 13 23:07:12 2018 +0900
+++ b/mercurial/cmdutil.py	Sat Apr 14 00:30:39 2018 +0900
@@ -904,6 +904,33 @@
     else:
         return commiteditor
 
+def _escapecommandtemplate(tmpl):
+    parts = []
+    for typ, start, end in templater.scantemplate(tmpl, raw=True):
+        if typ == b'string':
+            parts.append(stringutil.escapestr(tmpl[start:end]))
+        else:
+            parts.append(tmpl[start:end])
+    return b''.join(parts)
+
+def rendercommandtemplate(ui, tmpl, props):
+    r"""Expand a literal template 'tmpl' in a way suitable for command line
+
+    '\' in outermost string is not taken as an escape character because it
+    is a directory separator on Windows.
+
+    >>> from . import ui as uimod
+    >>> ui = uimod.ui()
+    >>> rendercommandtemplate(ui, b'c:\\{path}', {b'path': b'foo'})
+    'c:\\foo'
+    >>> rendercommandtemplate(ui, b'{"c:\\{path}"}', {'path': b'foo'})
+    'c:{path}'
+    """
+    if not tmpl:
+        return tmpl
+    t = formatter.maketemplater(ui, _escapecommandtemplate(tmpl))
+    return t.renderdefault(props)
+
 def rendertemplate(ctx, tmpl, props=None):
     """Expand a literal template 'tmpl' byte-string against one changeset