[PATCH] dispatch: provide an HG_ARGS environment variable to shell aliases

Steve Losh steve at stevelosh.com
Wed Jul 28 22:52:34 CDT 2010


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1280375531 14400
# Node ID eb11d2fb96d16d5b8e55621260ea0e1d49d45aae
# Parent  84fb29f5e0d29bc95006947a62133032499e3fa3
dispatch: provide an HG_ARGS environment variable to shell aliases

This patch changes the functionality of shell aliases to pass the extra command
line arguments as an HG_ARGS environment variable, instead of simply appending
them to the command.

This allows for more flexible shell aliases:

    [alias]
    echo = !echo $HG_ARGS
    count = !hg log -r "$HG_ARGS" --template='.' | wc -c | sed -e 's/ //g'

In action:

    $ hg echo foo
    foo

    $ hg count 'branch(default)'
    901

    $ hg count 'branch(stable) and keyword(fixes)'
    102

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -212,12 +212,12 @@
 
             return
 
         if self.definition.startswith('!'):
             def fn(ui, *args):
-                cmd = '%s %s' % (self.definition[1:], ' '.join(args))
-                return util.system(cmd)
+                env = {'HG_ARGS': ' '.join(args)}
+                return util.system(self.definition[1:], environ=env)
             self.fn = fn
             return
 
         args = shlex.split(self.definition)
         cmd = args.pop(0)
diff --git a/tests/test-alias b/tests/test-alias
--- a/tests/test-alias
+++ b/tests/test-alias
@@ -12,11 +12,12 @@
 lognull = log -r null
 shortlog = log --template '{rev} {node|short} | {date|isodate}\n'
 dln = lognull --debug
 nousage = rollback
 put = export -r 0 -o "\$FOO/%R.diff"
-echo = !echo
+echo = !echo \$HG_ARGS
+count = !hg log -r "\$HG_ARGS" --template='.' | wc -c | sed -e 's/ //g'
 
 [defaults]
 mylog = -q
 lognull = -q
 log = -v
@@ -66,5 +67,11 @@
 FOO=`pwd` hg put
 cat 0.diff
 
 echo '% shell aliases'
 hg echo foo
+
+echo bar > bar
+hg ci -qAmbar
+
+hg count .
+hg count '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,7 @@
 +++ b/foo	Thu Jan 01 00:00:00 1970 +0000
 @@ -0,0 +1,1 @@
 +foo
 % shell aliases
 foo
+1
+2


More information about the Mercurial-devel mailing list