[PATCH] dispatch: add shell aliases

Steve Losh steve at stevelosh.com
Wed Jul 7 07:02:36 CDT 2010


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1278504144 14400
# Node ID 771b5150d555f3b4f0d13dcf7a3e05c01c78482a
# Parent  4a73a09801941c11bb91d15c291a970cfd281382
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`"

Running these new aliases would look like:

    $ hg echo foo
    foo
    $ hg qempty
    # the current MQ patch is emptied
    $

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:


More information about the Mercurial-devel mailing list