##// END OF EJS Templates
alias: only allow global options before a shell alias, pass later ones through...
Steve Losh -
r12536:208fc9ad default
parent child Browse files
Show More
@@ -218,6 +218,7 b' class cmdalias(object):'
218 218 return
219 219
220 220 if self.definition.startswith('!'):
221 self.shell = True
221 222 def fn(ui, *args):
222 223 env = {'HG_ARGS': ' '.join((self.name,) + args)}
223 224 def _checkvar(m):
@@ -404,18 +405,11 b' def runcommand(lui, repo, cmd, fullargs,'
404 405 result=ret, pats=cmdpats, opts=cmdoptions)
405 406 return ret
406 407
407 _loaded = set()
408 def _dispatch(ui, args):
409 # read --config before doing anything else
410 # (e.g. to change trust settings for reading .hg/hgrc)
411 _parseconfig(ui, _earlygetopt(['--config'], args))
408 def _getlocal(ui, rpath):
409 """Return (path, local ui object) for the given target path.
412 410
413 # check for cwd
414 cwd = _earlygetopt(['--cwd'], args)
415 if cwd:
416 os.chdir(cwd[-1])
417
418 # read the local repository .hgrc into a local ui object
411 Takes paths in [cwd]/.hg/hgrc into account."
412 """
419 413 try:
420 414 wd = os.getcwd()
421 415 except OSError, e:
@@ -431,13 +425,64 b' def _dispatch(ui, args):'
431 425 except IOError:
432 426 pass
433 427
434 # now we can expand paths, even ones in .hg/hgrc
435 rpath = _earlygetopt(["-R", "--repository", "--repo"], args)
436 428 if rpath:
437 429 path = lui.expandpath(rpath[-1])
438 430 lui = ui.copy()
439 431 lui.readconfig(os.path.join(path, ".hg", "hgrc"))
440 432
433 return path, lui
434
435 def _checkshellalias(ui, args):
436 cwd = os.getcwd()
437 options = {}
438 args = fancyopts.fancyopts(args, commands.globalopts, options)
439
440 if not args:
441 return
442
443 _parseconfig(ui, options['config'])
444 if options['cwd']:
445 os.chdir(options['cwd'])
446
447 path, lui = _getlocal(ui, [options['repository']])
448
449 cmdtable = commands.table.copy()
450 addaliases(lui, cmdtable)
451
452 cmd = args[0]
453 try:
454 aliases, entry = cmdutil.findcmd(cmd, cmdtable, lui.config("ui", "strict"))
455 except error.UnknownCommand:
456 os.chdir(cwd)
457 return
458
459 cmd = aliases[0]
460 fn = entry[0]
461
462 if cmd and hasattr(fn, 'shell'):
463 d = lambda: fn(ui, *args[1:])
464 return lambda: runcommand(lui, None, cmd, args[:1], ui, options, d, [], {})
465
466 os.chdir(cwd)
467
468 _loaded = set()
469 def _dispatch(ui, args):
470 shellaliasfn = _checkshellalias(ui, args)
471 if shellaliasfn:
472 return shellaliasfn()
473
474 # read --config before doing anything else
475 # (e.g. to change trust settings for reading .hg/hgrc)
476 _parseconfig(ui, _earlygetopt(['--config'], args))
477
478 # check for cwd
479 cwd = _earlygetopt(['--cwd'], args)
480 if cwd:
481 os.chdir(cwd[-1])
482
483 rpath = _earlygetopt(["-R", "--repository", "--repo"], args)
484 path, lui = _getlocal(ui, rpath)
485
441 486 # Configure extensions in phases: uisetup, extsetup, cmdtable, and
442 487 # reposetup. Programs like TortoiseHg will call _dispatch several
443 488 # times so we keep track of configured extensions in _loaded.
@@ -23,6 +23,7 b''
23 23 > echo2 = !echo '\$2'
24 24 > echo13 = !echo '\$1' '\$3'
25 25 > count = !hg log -r '\$@' --template='.' | wc -c | sed -e 's/ //g'
26 > mcount = !hg log \$@ --template='.' | wc -c | sed -e 's/ //g'
26 27 > rt = root
27 28 >
28 29 > [defaults]
@@ -158,10 +159,10 b' simple shell aliases'
158 159
159 160 $ hg blank foo
160 161
162 $ hg self
163 self
161 164 $ hg echo
162 165
163 $ hg self
164 self
165 166 $ hg echo foo
166 167 foo
167 168 $ hg echo 'test $2' foo
@@ -180,6 +181,59 b' simple shell aliases'
180 181 1
181 182 $ hg count 'branch(default)'
182 183 2
184 $ hg mcount -r '"branch(default)"'
185 2
186
187
188 shell aliases with global options
189
190 $ hg init sub
191 $ cd sub
192 $ hg count 'branch(default)'
193 0
194 $ hg -v count 'branch(default)'
195 0
196 $ hg -R .. count 'branch(default)'
197 0
198 $ hg --cwd .. count 'branch(default)'
199 2
200 $ hg echo --cwd ..
201 --cwd ..
202
203
204 repo specific shell aliases
205
206 $ cat >> .hg/hgrc <<EOF
207 > [alias]
208 > subalias = !echo sub \$@
209 > EOF
210 $ cat >> ../.hg/hgrc <<EOF
211 > [alias]
212 > mainalias = !echo main \$@
213 > EOF
214
215
216 shell alias defined in current repo
217
218 $ hg subalias
219 sub
220 $ hg --cwd .. subalias > /dev/null
221 hg: unknown command 'subalias'
222 [255]
223 $ hg -R .. subalias > /dev/null
224 hg: unknown command 'subalias'
225 [255]
226
227
228 shell alias defined in other repo
229
230 $ hg mainalias > /dev/null
231 hg: unknown command 'mainalias'
232 [255]
233 $ hg -R .. mainalias
234 main
235 $ hg --cwd .. mainalias
236 main
183 237
184 238
185 239 invalid arguments
General Comments 0
You need to be logged in to leave comments. Login now