[PATCH] add positional arguments to non-shell aliases

Alexander Solovyov alexander at solovyov.net
Sun May 1 07:37:34 CDT 2011


# HG changeset patch
# User Alexander Solovyov <alexander at solovyov.net>
# Date 1304245772 -7200
# Node ID 55349a878c79c8893500fe0b9560265ea06fd659
# Parent  0386b51dd749f83e3f759a5b799ec364924e3b82
add positional arguments to non-shell aliases

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -5,6 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import pipes
 from i18n import _
 import os, sys, atexit, signal, pdb, socket, errno, shlex, time, traceback, re
 import util, commands, hg, fancyopts, extensions, hook, error
@@ -182,10 +183,21 @@ def _runcatch(ui, args):
 
     return -1
 
-def aliasargs(fn):
-    if hasattr(fn, 'args'):
-        return fn.args
-    return []
+def aliasargs(fn, givenargs):
+    args = getattr(fn, 'args', [])
+    if args and givenargs:
+        cmd = ' '.join(map(pipes.quote, args))
+
+        nums = []
+        def replacer(m):
+            num = int(m.group(1)) - 1
+            nums.append(num)
+            return givenargs[num]
+        cmd = re.sub(r'\$(\d+|\$)', replacer, cmd)
+        givenargs = [x for i, x in enumerate(givenargs)
+                     if i not in nums]
+        args = shlex.split(cmd)
+    return args + givenargs
 
 class cmdalias(object):
     def __init__(self, name, definition, cmdtable):
@@ -263,7 +275,7 @@ class cmdalias(object):
             else:
                 self.fn, self.opts = tableentry
 
-            self.args = aliasargs(self.fn) + args
+            self.args = aliasargs(self.fn, args)
             if cmd not in commands.norepo.split(' '):
                 self.norepo = False
             if self.help.startswith("hg " + cmd):
@@ -330,7 +342,7 @@ def _parse(ui, args):
         aliases, entry = cmdutil.findcmd(cmd, commands.table,
                                      ui.config("ui", "strict"))
         cmd = aliases[0]
-        args = aliasargs(entry[0]) + args
+        args = aliasargs(entry[0], args)
         defaults = ui.config("defaults", cmd)
         if defaults:
             args = map(util.expandpath, shlex.split(defaults)) + args
diff --git a/tests/test-alias.t b/tests/test-alias.t
--- a/tests/test-alias.t
+++ b/tests/test-alias.t
@@ -17,6 +17,7 @@
   > mylog = log
   > lognull = log -r null
   > shortlog = log --template '{rev} {node|short} | {date|isodate}\n'
+  > positional = log --template '{\$2} {\$1} | {date|isodate}\n'
   > dln = lognull --debug
   > nousage = rollback
   > put = export -r 0 -o "\$FOO/%R.diff"
@@ -127,6 +128,10 @@ with opts and whitespace
   $ hg shortlog
   0 e63c23eaa88a | 1970-01-01 00:00 +0000
 
+positional arguments
+
+  $ hg positional 'node|short' rev
+  0 e63c23eaa88a | 1970-01-01 00:00 +0000
 
 interaction with defaults
 


More information about the Mercurial-devel mailing list