[PATCH 2 of 2 RFC] dispatch: add $@ expansion in shell aliases

Steve Losh steve at stevelosh.com
Thu Jul 8 21:23:03 CDT 2010


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1278641324 14400
# Node ID 6b673acfd0ab1b37b9ca744002b6eb1e616ee705
# Parent  1b8db360ae3f5430a6a41c05f26c58c5475e4c81
dispatch: add $@ expansion in shell aliases

If there are any instances of $@ in a shell alias they will be replaced with
the arguments given after the command (separated by spaces). If not, the
arguments will be appended to the end of the command instead:

    [alias]
    echo = !echo
    count = !hg log --template='.\n' -r '$@' | wc -l
    countv = !echo 'Revisions in "$@":' ; hg log --template='.\n' -r '$@' | wc -l

    $ hg echo foo
    foo
    $ hg count 'branch(default)'
           1203
    $ hg countv 'branch(default)'
    Revisions in "branch(default)":
           1203
    $

Note: *ALL* occurrences of "$@" in the definition are replaced, not just the
first one, which is why the "countv" example works.

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -208,11 +208,14 @@
 
             return
 
         if self.definition.startswith('!'):
             def fn(ui, *args):
-                cmd = '%s %s' % (self.definition[1:], ' '.join(args))
+                if '$@' in self.definition:
+                    cmd = self.definition[1:].replace('$@', ' '.join(args))
+                else:
+                    cmd = '%s %s' % (self.definition[1:], ' '.join(args))
                 return util.system(cmd)
             self.fn = fn
             return
 
         args = shlex.split(self.definition)
diff --git a/tests/test-alias b/tests/test-alias
--- a/tests/test-alias
+++ b/tests/test-alias
@@ -13,10 +13,12 @@
 shortlog = log --template '{rev} {node|short} | {date|isodate}\n'
 dln = lognull --debug
 nousage = rollback
 put = export -r 0 -o "\$FOO/%R.diff"
 echo = !echo
+count = !hg log -r '\$@' --template='.\n' | wc -l
+countv = !echo Counting revisions in '"\$@"' ; hg count '\$@'
 
 [defaults]
 mylog = -q
 lognull = -q
 log = -v
@@ -66,5 +68,8 @@
 FOO=`pwd` hg put
 cat 0.diff
 
 echo '% shell aliases'
 hg echo foo
+hg count .
+hg count 'branch(default)'
+hg countv 'branch(default)'
diff --git a/tests/test-alias.out b/tests/test-alias.out
--- a/tests/test-alias.out
+++ b/tests/test-alias.out
@@ -43,5 +43,9 @@
 +++ b/foo	Thu Jan 01 00:00:00 1970 +0000
 @@ -0,0 +1,1 @@
 +foo
 % shell aliases
 foo
+       1
+       1
+Counting revisions in "branch(default)"
+       1


More information about the Mercurial-devel mailing list