author | Bryan O'Sullivan <bos@serpentine.com> |
Fri, 29 May 2009 21:31:33 -0700 | |
changeset 8652 | 64614c7e4bd9 |
parent 8519 | 5fbee915ea5d |
permissions | -rw-r--r-- |
4801 | 1 |
# Copyright (C) 2007 Brendan Cully <brendan@kublai.com> |
2 |
# This file is published under the GNU GPL. |
|
3 |
||
4 |
'''allow user-defined command aliases |
|
5 |
||
6 |
To use, create entries in your hgrc of the form |
|
7 |
||
8 |
[alias] |
|
9 |
mycmd = cmd --args |
|
10 |
''' |
|
11 |
||
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
12 |
from mercurial.i18n import _ |
7643
9a1ea6587557
error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
13 |
from mercurial import commands, cmdutil, error |
4801 | 14 |
|
15 |
cmdtable = {} |
|
16 |
||
17 |
class RecursiveCommand(Exception): pass |
|
18 |
||
19 |
class lazycommand(object): |
|
20 |
'''defer command lookup until needed, so that extensions loaded |
|
21 |
after alias can be aliased''' |
|
22 |
def __init__(self, ui, name, target): |
|
23 |
self._ui = ui |
|
24 |
self._name = name |
|
25 |
self._target = target |
|
26 |
self._cmd = None |
|
27 |
||
28 |
def __len__(self): |
|
29 |
self._resolve() |
|
30 |
return len(self._cmd) |
|
31 |
||
32 |
def __getitem__(self, key): |
|
33 |
self._resolve() |
|
34 |
return self._cmd[key] |
|
35 |
||
36 |
def __iter__(self): |
|
37 |
self._resolve() |
|
38 |
return self._cmd.__iter__() |
|
39 |
||
40 |
def _resolve(self): |
|
41 |
if self._cmd is not None: |
|
42 |
return |
|
43 |
||
44 |
try: |
|
7643
9a1ea6587557
error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
45 |
self._cmd = cmdutil.findcmd(self._target, commands.table, False)[1] |
4801 | 46 |
if self._cmd == self: |
47 |
raise RecursiveCommand() |
|
48 |
if self._target in commands.norepo.split(' '): |
|
49 |
commands.norepo += ' %s' % self._name |
|
50 |
return |
|
7643
9a1ea6587557
error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
51 |
except error.UnknownCommand: |
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
52 |
msg = _('*** [alias] %s: command %s is unknown') % \ |
4801 | 53 |
(self._name, self._target) |
7643
9a1ea6587557
error: move UnknownCommand and AmbiguousCommand
Matt Mackall <mpm@selenic.com>
parents:
7213
diff
changeset
|
54 |
except error.AmbiguousCommand: |
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
55 |
msg = _('*** [alias] %s: command %s is ambiguous') % \ |
4801 | 56 |
(self._name, self._target) |
57 |
except RecursiveCommand: |
|
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
58 |
msg = _('*** [alias] %s: circular dependency on %s') % \ |
4801 | 59 |
(self._name, self._target) |
60 |
def nocmd(*args, **opts): |
|
61 |
self._ui.warn(msg + '\n') |
|
62 |
return 1 |
|
63 |
nocmd.__doc__ = msg |
|
64 |
self._cmd = (nocmd, [], '') |
|
65 |
commands.norepo += ' %s' % self._name |
|
66 |
||
67 |
def uisetup(ui): |
|
68 |
for cmd, target in ui.configitems('alias'): |
|
69 |
if not target: |
|
6954
f7d545a866e8
i18n: mark strings for translation in alias extension
Martin Geisler <mg@daimi.au.dk>
parents:
5178
diff
changeset
|
70 |
ui.warn(_('*** [alias] %s: no definition\n') % cmd) |
4801 | 71 |
continue |
8519
5fbee915ea5d
alias: a0104303f400 did not correctly handle whitespace in the args
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8477
diff
changeset
|
72 |
args = target.split(' ', 1) |
4801 | 73 |
tcmd = args.pop(0) |
74 |
if args: |
|
8519
5fbee915ea5d
alias: a0104303f400 did not correctly handle whitespace in the args
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8477
diff
changeset
|
75 |
args = args[0] |
8477
a0104303f400
alias: honor the [defaults] section, fix issue1642
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7643
diff
changeset
|
76 |
defaults = ui.config('defaults', cmd) |
a0104303f400
alias: honor the [defaults] section, fix issue1642
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
7643
diff
changeset
|
77 |
if defaults: |
8519
5fbee915ea5d
alias: a0104303f400 did not correctly handle whitespace in the args
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8477
diff
changeset
|
78 |
args = ' '.join((args, defaults)) |
5fbee915ea5d
alias: a0104303f400 did not correctly handle whitespace in the args
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
8477
diff
changeset
|
79 |
ui.setconfig('defaults', cmd, args) |
4801 | 80 |
cmdtable[cmd] = lazycommand(ui, cmd, tcmd) |