[PATCH] Move alias into core
Brendan Cully
brendan at kublai.com
Wed May 20 02:24:09 CDT 2009
On Monday, 18 May 2009 at 19:49, Brendan Cully wrote:
> # HG changeset patch
> # User Brendan Cully <brendan at kublai.com>
> # Date 1242701320 25200
> # Node ID ff2c73f240604842f214e44e595726357c22f2ee
> # Parent 252232621165917755727729c7f0b9a1f1263668
> Move alias into core.
Here's an improved version with better error reporting.
-------------- next part --------------
# HG changeset patch
# User Brendan Cully <brendan at kublai.com>
# Date 1242804128 25200
# Node ID 115592444afb38a47eddab78ed78d7e1ede95536
# Parent 4ddffb793d183f5933e1412246a7cbb31f1c563d
Move alias into core.
Alias errors are now resolved eagerly instead of lazily. Otherwise,
this is functionally equivalent to the extension.
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
-
-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 = target.split(' ', 1)
- tcmd = args.pop(0)
- if args:
- args = args[0]
- defaults = ui.config('defaults', cmd)
- if defaults:
- args = ' '.join((args, defaults))
- ui.setconfig('defaults', cmd, 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,45 @@
return p
+class cmdalias(object):
+ def __init__(self, cmd, args):
+ if isinstance(cmd, cmdalias):
+ self.cmd = cmd.cmd
+ self.args = cmd.args + args
+ else:
+ self.cmd = cmd
+ self.args = args
+
+def resolvealias(cmddef, args):
+ if not isinstance(cmddef, cmdalias):
+ return cmddef, args
+ return cmddef.cmd, cmddef.args + args
+
+def addaliases(ui, cmdtable):
+ # aliases are processed after extensions have been loaded, so they
+ # may use extension commands. Aliases can also use other alias definitions,
+ # but only if they have been defined prior to the current definition.
+ for alias, definition in ui.configitems('alias'):
+ if not definition:
+ ui.warn(_("*** no definition for alias '%s'\n") % alias)
+ continue
+ if alias in cmdtable:
+ ui.warn(_("*** alias '%s' shadows command\n") % alias)
+ args = shlex.split(definition)
+ cmd = args.pop(0)
+ try:
+ cmddef, opts, help = cmdutil.findcmd(cmd, cmdtable, False)[1]
+ cmdtable[alias] = (cmdalias(cmddef, args), opts, help)
+ if cmd in commands.norepo.split(' '):
+ commands.norepo += ' %s' % alias
+ except error.UnknownCommand:
+ ui.warn(_("*** alias '%s' resolves to unknown command '%s'\n") \
+ % (alias, cmd))
+ continue
+ except error.AmbiguousCommand:
+ ui.warn(_("*** alias '%s' resolves to ambiguous command '%s'\n") \
+ % (alias, cmd))
+
def _parse(ui, args):
options = {}
cmdoptions = {}
@@ -175,12 +214,14 @@
aliases, i = cmdutil.findcmd(cmd, commands.table,
ui.config("ui", "strict"))
cmd = aliases[0]
+ cmdfun, args = resolvealias(i[0], args)
defaults = ui.config("defaults", cmd)
if defaults:
args = shlex.split(defaults) + args
c = list(i[1])
else:
cmd = None
+ cmdfun = None
c = []
# combine global options into local
@@ -198,7 +239,7 @@
options[n] = cmdoptions[n]
del cmdoptions[n]
- return (cmd, cmd and i[0] or None, args, options, cmdoptions)
+ return (cmd, cmdfun, args, options, cmdoptions)
def _parseconfig(ui, config):
"""parse the --config options from the command line"""
@@ -301,6 +342,9 @@
% (name, " ".join(overrides)))
commands.table.update(cmdtable)
_loaded.add(name)
+
+ addaliases(lui, commands.table)
+
# check for fallback encoding
fallback = lui.config('ui', 'fallbackencoding')
if fallback:
diff --git a/tests/test-alias b/tests/test-alias
--- a/tests/test-alias
+++ b/tests/test-alias
@@ -1,43 +1,28 @@
#!/bin/sh
cat >> $HGRCPATH <<EOF
-[extensions]
-alias=
+[defaults]
+mylog = -q
+lognull = -q
+log = -v
[alias]
myinit = init
cleanstatus = status -c
-unknown = bargle
-ambiguous = s
-recursive = recursive
mylog = log
lognull = log -r null
shortlog = log --template '{rev} {node|short} | {date|isodate}\n'
-
-[defaults]
-mylog = -q
-lognull = -q
-log = -v
EOF
echo '% basic'
hg myinit alias
-echo '% unknown'
-hg unknown
-
-echo '% ambiguous'
-hg ambiguous
-
-echo '% recursive'
-hg recursive
-
cd alias
echo foo > foo
hg ci -Amfoo
echo '% with opts'
-hg cleanst
+hg cleanstatus
echo '% with opts and whitespace'
hg shortlog
@@ -45,3 +30,14 @@
echo '% interaction with defaults'
hg mylog
hg lognull
+
+cat >> $HGRCPATH <<EOF
+unknown = bargle
+ambiguous = s
+recursive = recursive
+nodefinition =
+heads = status
+EOF
+
+echo '% bad aliases'
+hg heads
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,4 @@
% basic
-% unknown
-*** [alias] unknown: command bargle is unknown
-% ambiguous
-*** [alias] ambiguous: command s is ambiguous
-% recursive
-*** [alias] recursive: circular dependency on recursive
adding foo
% with opts
C foo
@@ -13,3 +7,9 @@
% interaction with defaults
0:e63c23eaa88a
-1:000000000000
+% bad aliases
+*** alias 'unknown' resolves to unknown command 'bargle'
+*** alias 'ambiguous' resolves to ambiguous command 's'
+*** alias 'recursive' resolves to unknown command 'recursive'
+*** no definition for alias 'nodefinition'
+*** alias 'heads' shadows command
More information about the Mercurial-devel
mailing list