##// END OF EJS Templates
Add alias extension
Brendan Cully -
r4801:6aa1fae4 default
parent child Browse files
Show More
@@ -0,0 +1,78
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
12 from mercurial.cmdutil import findcmd, UnknownCommand, AmbiguousCommand
13 from mercurial import commands
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:
45 self._loading = True
46 self._cmd = findcmd(self._ui, self._target)[1]
47 if self._cmd == self:
48 raise RecursiveCommand()
49 if self._target in commands.norepo.split(' '):
50 commands.norepo += ' %s' % self._name
51 return
52 except UnknownCommand:
53 msg = '*** [alias] %s: command %s is unknown' % \
54 (self._name, self._target)
55 except AmbiguousCommand:
56 msg = '*** [alias] %s: command %s is ambiguous' % \
57 (self._name, self._target)
58 except RecursiveCommand:
59 msg = '*** [alias] %s: circular dependency on %s' % \
60 (self._name, self._target)
61 def nocmd(*args, **opts):
62 self._ui.warn(msg + '\n')
63 return 1
64 nocmd.__doc__ = msg
65 self._cmd = (nocmd, [], '')
66 commands.norepo += ' %s' % self._name
67
68 def uisetup(ui):
69 for cmd, target in ui.configitems('alias'):
70 if not target:
71 ui.warn('*** [alias] %s: no definition\n' % cmd)
72 continue
73 args = target.split(' ')
74 tcmd = args.pop(0)
75 if args:
76 pui = ui.parentui or ui
77 pui.setconfig('defaults', cmd, ' '.join(args))
78 cmdtable[cmd] = lazycommand(ui, cmd, tcmd)
@@ -0,0 +1,32
1 #!/bin/sh
2
3 cat > $HGRCPATH <<EOF
4 [extensions]
5 alias=
6
7 [alias]
8 myinit = init
9 cleanstatus = status -c
10 unknown = bargle
11 ambiguous = s
12 recursive = recursive
13 EOF
14
15 echo '% basic'
16 hg myinit alias
17
18 echo '% unknown'
19 hg unknown
20
21 echo '% ambiguous'
22 hg ambiguous
23
24 echo '% recursive'
25 hg recursive
26
27 cd alias
28 echo foo > foo
29 hg ci -Amfoo
30
31 echo '% with opts'
32 hg cleanst
@@ -0,0 +1,10
1 % basic
2 % unknown
3 *** [alias] unknown: command bargle is unknown
4 % ambiguous
5 *** [alias] ambiguous: command s is ambiguous
6 % recursive
7 *** [alias] recursive: circular dependency on recursive
8 adding foo
9 % with opts
10 C foo
General Comments 0
You need to be logged in to leave comments. Login now