[PATCH 1 of 2 RFC] dispatch: add shell aliases

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


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1278542983 14400
# Node ID 1b8db360ae3f5430a6a41c05f26c58c5475e4c81
# Parent  8bbe396883dbca71f8134a5f6f997febe8797ae6
dispatch: add shell aliases

This patch adds git-style "shell aliases" to Mercurial.

Any alias with a definition beginning with a '!' will be treated as a shell
alias. For example:

    [alias]
    echo = !echo
    qempty = !hg qrefresh -X "`hg root`" ; echo Emptied patch "`hg qtop`"

    $ hg echo foo
    foo
    $ hg qempty
    Emptied patch foo
    $

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1864,11 +1864,14 @@
         # description
         doc = gettext(entry[0].__doc__)
         if not doc:
             doc = _("(no help text available)")
         if hasattr(entry[0], 'definition'):  # aliased command
-            doc = _('alias for: hg %s\n\n%s') % (entry[0].definition, doc)
+            if entry[0].definition.startswith('!'):  # shell alias
+                doc = _('shell alias for::\n\n    %s') % entry[0].definition[1:]
+            else:
+                doc = _('alias for: hg %s\n\n%s') % (entry[0].definition, doc)
         if ui.quiet:
             doc = doc.splitlines()[0]
         keep = ui.verbose and ['verbose'] or []
         formatted, pruned = minirst.format(doc, textwidth, keep=keep)
         ui.write("\n%s\n" % formatted)
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -206,10 +206,17 @@
             self.fn = fn
             self.badalias = True
 
             return
 
+        if self.definition.startswith('!'):
+            def fn(ui, *args):
+                cmd = '%s %s' % (self.definition[1:], ' '.join(args))
+                return util.system(cmd)
+            self.fn = fn
+            return
+
         args = shlex.split(self.definition)
         cmd = args.pop(0)
         args = map(util.expandpath, args)
 
         try:
diff --git a/tests/test-alias b/tests/test-alias
--- a/tests/test-alias
+++ b/tests/test-alias
@@ -12,10 +12,11 @@
 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
 
 [defaults]
 mylog = -q
 lognull = -q
 log = -v
@@ -62,5 +63,8 @@
 hg dln
 
 echo '% path expanding'
 FOO=`pwd` hg put
 cat 0.diff
+
+echo '% shell aliases'
+hg echo foo
diff --git a/tests/test-alias.out b/tests/test-alias.out
--- a/tests/test-alias.out
+++ b/tests/test-alias.out
@@ -41,5 +41,7 @@
 diff -r 000000000000 -r e63c23eaa88a foo
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
 +++ b/foo	Thu Jan 01 00:00:00 1970 +0000
 @@ -0,0 +1,1 @@
 +foo
+% shell aliases
+foo


More information about the Mercurial-devel mailing list