[PATCH] Move alias into core

Brendan Cully brendan at kublai.com
Mon May 18 21:49:26 CDT 2009


# HG changeset patch
# User Brendan Cully <brendan at kublai.com>
# Date 1242701320 25200
# Node ID ff2c73f240604842f214e44e595726357c22f2ee
# Parent  252232621165917755727729c7f0b9a1f1263668
Move alias into core.

Error reporting for aliases that are ambiguous, recursive or do not
resolve to a real command is slightly different (and slightly less
informative). This version doesn't accept abbreviations for aliases
either. Otherwise, it is equivalent in power to the extension. Calling
_dispatch recursively would allow for more powerful features, like
setting --config options in an alias definition.

diff --git a/hgext/alias.py b/hgext/alias.py
deleted file mode 100644
--- a/hgext/alias.py
+++ /dev/null
@@ -1,80 +0,0 @@
-# Copyright (C) 2007 Brendan Cully <brendan at kublai.com>
-# This file is published under the GNU GPL.
-
-'''allow user-defined command aliases
-
-To use, create entries in your hgrc of the form
-
-[alias]
-mycmd = cmd --args
-'''
-
-from mercurial.i18n import _
-from mercurial import commands, cmdutil, error
-import shlex
-
-cmdtable = {}
-
-class RecursiveCommand(Exception): pass
-
-class lazycommand(object):
-    '''defer command lookup until needed, so that extensions loaded
-    after alias can be aliased'''
-    def __init__(self, ui, name, target):
-        self._ui = ui
-        self._name = name
-        self._target = target
-        self._cmd = None
-
-    def __len__(self):
-        self._resolve()
-        return len(self._cmd)
-
-    def __getitem__(self, key):
-        self._resolve()
-        return self._cmd[key]
-
-    def __iter__(self):
-        self._resolve()
-        return self._cmd.__iter__()
-
-    def _resolve(self):
-        if self._cmd is not None:
-            return
-
-        try:
-            self._cmd = cmdutil.findcmd(self._target, commands.table, False)[1]
-            if self._cmd == self:
-                raise RecursiveCommand()
-            if self._target in commands.norepo.split(' '):
-                commands.norepo += ' %s' % self._name
-            return
-        except error.UnknownCommand:
-            msg = _('*** [alias] %s: command %s is unknown') % \
-                  (self._name, self._target)
-        except error.AmbiguousCommand:
-            msg = _('*** [alias] %s: command %s is ambiguous') % \
-                  (self._name, self._target)
-        except RecursiveCommand:
-            msg = _('*** [alias] %s: circular dependency on %s') % \
-                  (self._name, self._target)
-        def nocmd(*args, **opts):
-            self._ui.warn(msg + '\n')
-            return 1
-        nocmd.__doc__ = msg
-        self._cmd = (nocmd, [], '')
-        commands.norepo += ' %s' % self._name
-
-def uisetup(ui):
-    for cmd, target in ui.configitems('alias'):
-        if not target:
-            ui.warn(_('*** [alias] %s: no definition\n') % cmd)
-            continue
-        args = shlex.split(target)
-        tcmd = args.pop(0)
-        if args:
-            defaults = ui.config('defaults', cmd)
-            if defaults:
-                args = shlex.split(defaults) + args
-            ui.setconfig('defaults', cmd, ' '.join(args))
-        cmdtable[cmd] = lazycommand(ui, cmd, tcmd)
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -161,6 +161,21 @@
 
     return p
 
+def resolvealias(ui, args):
+    """Return real command if cmd is an alias definition.
+    Does not currently support recursive aliases"""
+    cmd, args = args[0], args[1:]
+    aliasdef = ui.config("alias", cmd)
+    if not aliasdef:
+        return cmd, args
+    aliasdefaults = ui.config("defaults", cmd)
+    aliasargs = shlex.split(aliasdef)
+    cmd = aliasargs[0]
+    args = aliasargs[1:] + args
+    if aliasdefaults:
+        args = shlex.split(aliasdefaults) + args
+    return cmd, args
+
 def _parse(ui, args):
     options = {}
     cmdoptions = {}
@@ -171,7 +186,7 @@
         raise error.ParseError(None, inst)
 
     if args:
-        cmd, args = args[0], args[1:]
+        cmd, args = resolvealias(ui, args)
         aliases, i = cmdutil.findcmd(cmd, commands.table,
                                      ui.config("ui", "strict"))
         cmd = aliases[0]
diff --git a/tests/test-alias b/tests/test-alias
--- a/tests/test-alias
+++ b/tests/test-alias
@@ -1,9 +1,6 @@
 #!/bin/sh
 
 cat >> $HGRCPATH <<EOF
-[extensions]
-alias=
-
 [alias]
 myinit = init
 cleanstatus = status -c
@@ -16,27 +13,26 @@
 [defaults]
 mylog = -q
 lognull = -q
-log = -v
 EOF
 
 echo '% basic'
 hg myinit alias
 
 echo '% unknown'
-hg unknown
+hg unknown > /dev/null
 
 echo '% ambiguous'
 hg ambiguous
 
 echo '% recursive'
-hg recursive
+hg recursive > /dev/null
 
 cd alias
 echo foo > foo
 hg ci -Amfoo
 
 echo '% with opts'
-hg cleanst
+hg cleanstatus
 
 echo '% interaction with defaults'
 hg mylog
diff --git a/tests/test-alias.out b/tests/test-alias.out
--- a/tests/test-alias.out
+++ b/tests/test-alias.out
@@ -1,10 +1,11 @@
 % basic
 % unknown
-*** [alias] unknown: command bargle is unknown
+hg: unknown command 'bargle'
 % ambiguous
-*** [alias] ambiguous: command s is ambiguous
+hg: command 's' is ambiguous:
+    serve showconfig status
 % recursive
-*** [alias] recursive: circular dependency on recursive
+hg: unknown command 'recursive'
 adding foo
 % with opts
 C foo


More information about the Mercurial-devel mailing list