[PATCH] dispatch: support for $ escaping in shell-alias definition

Roman Sokolov sokolov.r.v at gmail.com
Thu Feb 10 18:33:36 CST 2011


# HG changeset patch
# User Roman Sokolov <sokolov.r.v at gmail.com>
# Date 1297384360 -10800
# Node ID 44c18b981cc1fa229407cdda77cb0325242704fa
# Parent  8f5c865b7b4a947b6eb2089ea31ebcc91132cc7f
dispatch: support for $ escaping in shell-alias definition

Sigils in shell-alias can be escaped by doubling them.

diff -r 8f5c865b7b4a -r 44c18b981cc1 mercurial/dispatch.py
--- a/mercurial/dispatch.py	Tue Feb 01 17:53:50 2011 -0600
+++ b/mercurial/dispatch.py	Fri Feb 11 03:32:40 2011 +0300
@@ -221,15 +221,17 @@
             def fn(ui, *args):
                 env = {'HG_ARGS': ' '.join((self.name,) + args)}
                 def _checkvar(m):
-                    if int(m.groups()[0]) <= len(args):
+                    if m.groups()[0] == '$':
+                        return m.group()
+                    elif int(m.groups()[0]) <= len(args):
                         return m.group()
                     else:
                         return ''
-                cmd = re.sub(r'\$(\d+)', _checkvar, self.definition[1:])
+                cmd = re.sub(r'\$(\d+|\$)', _checkvar, self.definition[1:])
                 replace = dict((str(i + 1), arg) for i, arg in enumerate(args))
                 replace['0'] = self.name
                 replace['@'] = ' '.join(args)
-                cmd = util.interpolate(r'\$', replace, cmd)
+                cmd = util.interpolate(r'\$', replace, cmd, escape_prefix=True)
                 return util.system(cmd, environ=env)
             self.fn = fn
             return
diff -r 8f5c865b7b4a -r 44c18b981cc1 mercurial/util.py
--- a/mercurial/util.py	Tue Feb 01 17:53:50 2011 -0600
+++ b/mercurial/util.py	Fri Feb 11 03:32:40 2011 +0300
@@ -1512,7 +1512,7 @@
                 return False
         return True
 
-def interpolate(prefix, mapping, s, fn=None):
+def interpolate(prefix, mapping, s, fn=None, escape_prefix=False):
     """Return the result of interpolating items in the mapping into string s.
 
     prefix is a single character string, or a two character string with
@@ -1521,9 +1521,20 @@
 
     fn is an optional function that will be applied to the replacement text
     just before replacement.
+
+    escape_prefix is an optional flag that allows using doubled prefix for
+    its escaping.
     """
     fn = fn or (lambda s: s)
-    r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys())))
+    patterns = '|'.join(mapping.keys())
+    if escape_prefix:
+        patterns += '|' + prefix
+        if len(prefix) > 1:
+            prefix_char = prefix[1:]
+        else:
+            prefix_char = prefix
+        mapping[prefix_char] = prefix_char
+    r = re.compile(r'%s(%s)' % (prefix, patterns))
     return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
 
 def getport(port):


More information about the Mercurial-devel mailing list