##// END OF EJS Templates
Move alias into core
Brendan Cully -
r8655:21688b8a default
parent child Browse files
Show More
@@ -100,6 +100,29 b' This section describes the different sec'
100 Mercurial "hgrc" file, the purpose of each section, its possible
100 Mercurial "hgrc" file, the purpose of each section, its possible
101 keys, and their possible values.
101 keys, and their possible values.
102
102
103 [[alias]]
104 alias::
105 Defines command aliases.
106 Aliases allow you to define your own commands in terms of other
107 commands (or aliases), optionally including arguments.
108 +
109 Alias definitions consist of lines of the form:
110 +
111 <alias> = <command> [<argument]...
112 +
113 For example, this definition:
114 +
115 latest = log --limit 5
116 +
117 creates a new command `latest` that shows only the five most recent
118 changesets. You can define subsequent aliases using earlier ones:
119 +
120 stable5 = latest -b stable
121 +
122 *Note*: It is possible to create aliases with the same names as
123 existing commands, which will then override the original
124 definitions. This is almost always a bad idea!
125
103 [[auth]]
126 [[auth]]
104 auth::
127 auth::
105 Authentication credentials for HTTP authentication.
128 Authentication credentials for HTTP authentication.
@@ -161,6 +161,74 b' def _findrepo(p):'
161
161
162 return p
162 return p
163
163
164 def aliasargs(fn):
165 if hasattr(fn, 'args'):
166 return fn.args
167 return []
168
169 class cmdalias(object):
170 def __init__(self, name, definition, cmdtable):
171 self.name = name
172 self.definition = definition
173 self.args = []
174 self.opts = []
175 self.help = ''
176 self.norepo = True
177
178 try:
179 cmdutil.findcmd(self.name, cmdtable, True)
180 self.shadows = True
181 except error.UnknownCommand:
182 self.shadows = False
183
184 if not self.definition:
185 def fn(ui, *args):
186 ui.warn(_("no definition for alias '%s'\n") % self.name)
187 return 1
188 self.fn = fn
189
190 return
191
192 args = shlex.split(self.definition)
193 cmd = args.pop(0)
194 opts = []
195 help = ''
196
197 try:
198 self.fn, self.opts, self.help = cmdutil.findcmd(cmd, cmdtable, False)[1]
199 self.args = aliasargs(self.fn) + args
200 if cmd not in commands.norepo.split(' '):
201 self.norepo = False
202 except error.UnknownCommand:
203 def fn(ui, *args):
204 ui.warn(_("alias '%s' resolves to unknown command '%s'\n") \
205 % (self.name, cmd))
206 return 1
207 self.fn = fn
208 except error.AmbiguousCommand:
209 def fn(ui, *args):
210 ui.warn(_("alias '%s' resolves to ambiguous command '%s'\n") \
211 % (self.name, cmd))
212 return 1
213 self.fn = fn
214
215 def __call__(self, ui, *args, **opts):
216 if self.shadows:
217 ui.debug(_("alias '%s' shadows command\n") % self.name)
218
219 return self.fn(ui, *args, **opts)
220
221 def addaliases(ui, cmdtable):
222 # aliases are processed after extensions have been loaded, so they
223 # may use extension commands. Aliases can also use other alias definitions,
224 # but only if they have been defined prior to the current definition.
225 for alias, definition in ui.configitems('alias'):
226 aliasdef = cmdalias(alias, definition, cmdtable)
227
228 cmdtable[alias] = (aliasdef, aliasdef.opts, aliasdef.help)
229 if aliasdef.norepo:
230 commands.norepo += ' %s' % alias
231
164 def _parse(ui, args):
232 def _parse(ui, args):
165 options = {}
233 options = {}
166 cmdoptions = {}
234 cmdoptions = {}
@@ -175,6 +243,7 b' def _parse(ui, args):'
175 aliases, i = cmdutil.findcmd(cmd, commands.table,
243 aliases, i = cmdutil.findcmd(cmd, commands.table,
176 ui.config("ui", "strict"))
244 ui.config("ui", "strict"))
177 cmd = aliases[0]
245 cmd = aliases[0]
246 args = aliasargs(i[0]) + args
178 defaults = ui.config("defaults", cmd)
247 defaults = ui.config("defaults", cmd)
179 if defaults:
248 if defaults:
180 args = shlex.split(defaults) + args
249 args = shlex.split(defaults) + args
@@ -301,6 +370,9 b' def _dispatch(ui, args):'
301 % (name, " ".join(overrides)))
370 % (name, " ".join(overrides)))
302 commands.table.update(cmdtable)
371 commands.table.update(cmdtable)
303 _loaded.add(name)
372 _loaded.add(name)
373
374 addaliases(lui, commands.table)
375
304 # check for fallback encoding
376 # check for fallback encoding
305 fallback = lui.config('ui', 'fallbackencoding')
377 fallback = lui.config('ui', 'fallbackencoding')
306 if fallback:
378 if fallback:
@@ -1,18 +1,17 b''
1 #!/bin/sh
1 #!/bin/sh
2
2
3 cat >> $HGRCPATH <<EOF
3 cat >> $HGRCPATH <<EOF
4 [extensions]
5 alias=
6
7 [alias]
4 [alias]
8 myinit = init
5 myinit = init
9 cleanstatus = status -c
6 cleanstatus = status -c
10 unknown = bargle
7 unknown = bargle
11 ambiguous = s
8 ambiguous = s
12 recursive = recursive
9 recursive = recursive
10 nodefinition =
13 mylog = log
11 mylog = log
14 lognull = log -r null
12 lognull = log -r null
15 shortlog = log --template '{rev} {node|short} | {date|isodate}\n'
13 shortlog = log --template '{rev} {node|short} | {date|isodate}\n'
14 dln = lognull --debug
16
15
17 [defaults]
16 [defaults]
18 mylog = -q
17 mylog = -q
@@ -32,6 +31,9 b' hg ambiguous'
32 echo '% recursive'
31 echo '% recursive'
33 hg recursive
32 hg recursive
34
33
34 echo '% no definition'
35 hg nodef
36
35 cd alias
37 cd alias
36 echo foo > foo
38 echo foo > foo
37 hg ci -Amfoo
39 hg ci -Amfoo
@@ -45,3 +47,6 b' hg shortlog'
45 echo '% interaction with defaults'
47 echo '% interaction with defaults'
46 hg mylog
48 hg mylog
47 hg lognull
49 hg lognull
50
51 echo '% properly recursive'
52 hg dln
@@ -1,10 +1,12 b''
1 % basic
1 % basic
2 % unknown
2 % unknown
3 *** [alias] unknown: command bargle is unknown
3 alias 'unknown' resolves to unknown command 'bargle'
4 % ambiguous
4 % ambiguous
5 *** [alias] ambiguous: command s is ambiguous
5 alias 'ambiguous' resolves to ambiguous command 's'
6 % recursive
6 % recursive
7 *** [alias] recursive: circular dependency on recursive
7 alias 'recursive' resolves to unknown command 'recursive'
8 % no definition
9 no definition for alias 'nodefinition'
8 adding foo
10 adding foo
9 % with opts
11 % with opts
10 C foo
12 C foo
@@ -13,3 +15,12 b' 0 e63c23eaa88a | 1970-01-01 00:00 +0000'
13 % interaction with defaults
15 % interaction with defaults
14 0:e63c23eaa88a
16 0:e63c23eaa88a
15 -1:000000000000
17 -1:000000000000
18 % properly recursive
19 changeset: -1:0000000000000000000000000000000000000000
20 parent: -1:0000000000000000000000000000000000000000
21 parent: -1:0000000000000000000000000000000000000000
22 manifest: -1:0000000000000000000000000000000000000000
23 user:
24 date: Thu Jan 01 00:00:00 1970 +0000
25 extra: branch=default
26
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now