Mercurial > public > mercurial-scm > hg
comparison mercurial/dispatch.py @ 12536:208fc9ad6a48
alias: only allow global options before a shell alias, pass later ones through
This patch refactors the dispatch code to change how arguments to shell aliases
are handled.
A separate "pass" to determine whether a command is a shell alias has been
added. The rough steps dispatch now performs when a command is given are these:
* Parse all arguments up to the command name.
* If any arguments such as --repository or --cwd are given (which could change
the config file used, and therefore the definition of aliases), they are
taken into account.
* We determine whether the command is a shell alias.
* If so, execute the alias. The --repo and --cwd arguments are still in effect.
Any arguments *after* the command name are passed unchanged through to the
shell command (and interpolated as normal.
* If the command is *not* a shell alias, the dispatching is effectively "reset"
and reparsed as normal in its entirety.
The net effect of this patch is to make shell alias commands behave as you
would expect.
Any arguments you give to a shell alias *after* the alias name are passed
through unchanged. This lets you do something like the following:
[alias]
filereleased = !$HG log -r 'descendants(adds("$1")) and tagged()' -l1 $2 $3 $4 $5
$ hg filereleased hgext/bookmarks.py --style compact
Previously the `--style compact` part would fail because Mercurial would
interpret those arguments as arguments to the alias command itself (which
doesn't take any arguments).
Also: running something like `hg -R ~/src/hg-crew filereleased
hgext/bookmarks.py` when `filereleased` is only defined in that repo's config
will now work.
These global arguments can *only* be given to a shell alias *before* the alias
name. For example, this will *not* work in the above situation:
$ hg filereleased -R ~/src/hg-crew hgext/bookmarks.py
The reason for this is that you may want to pass arguments like --repository to
the alias (or, more likely, their short versions like -R):
[alias]
own = !chown $@ `$HG root`
$ hg own steve
$ hg own -R steve
author | Steve Losh <steve@stevelosh.com> |
---|---|
date | Tue, 24 Aug 2010 18:25:33 -0400 |
parents | 0c605364373c |
children | 301d7626e0ff |
comparison
equal
deleted
inserted
replaced
12535:975ec4ce961c | 12536:208fc9ad6a48 |
---|---|
216 self.badalias = True | 216 self.badalias = True |
217 | 217 |
218 return | 218 return |
219 | 219 |
220 if self.definition.startswith('!'): | 220 if self.definition.startswith('!'): |
221 self.shell = True | |
221 def fn(ui, *args): | 222 def fn(ui, *args): |
222 env = {'HG_ARGS': ' '.join((self.name,) + args)} | 223 env = {'HG_ARGS': ' '.join((self.name,) + args)} |
223 def _checkvar(m): | 224 def _checkvar(m): |
224 if int(m.groups()[0]) <= len(args): | 225 if int(m.groups()[0]) <= len(args): |
225 return m.group() | 226 return m.group() |
402 # run post-hook, passing command result | 403 # run post-hook, passing command result |
403 hook.hook(lui, repo, "post-%s" % cmd, False, args=" ".join(fullargs), | 404 hook.hook(lui, repo, "post-%s" % cmd, False, args=" ".join(fullargs), |
404 result=ret, pats=cmdpats, opts=cmdoptions) | 405 result=ret, pats=cmdpats, opts=cmdoptions) |
405 return ret | 406 return ret |
406 | 407 |
407 _loaded = set() | 408 def _getlocal(ui, rpath): |
408 def _dispatch(ui, args): | 409 """Return (path, local ui object) for the given target path. |
409 # read --config before doing anything else | 410 |
410 # (e.g. to change trust settings for reading .hg/hgrc) | 411 Takes paths in [cwd]/.hg/hgrc into account." |
411 _parseconfig(ui, _earlygetopt(['--config'], args)) | 412 """ |
412 | |
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 | |
419 try: | 413 try: |
420 wd = os.getcwd() | 414 wd = os.getcwd() |
421 except OSError, e: | 415 except OSError, e: |
422 raise util.Abort(_("error getting current working directory: %s") % | 416 raise util.Abort(_("error getting current working directory: %s") % |
423 e.strerror) | 417 e.strerror) |
429 lui = ui.copy() | 423 lui = ui.copy() |
430 lui.readconfig(os.path.join(path, ".hg", "hgrc")) | 424 lui.readconfig(os.path.join(path, ".hg", "hgrc")) |
431 except IOError: | 425 except IOError: |
432 pass | 426 pass |
433 | 427 |
434 # now we can expand paths, even ones in .hg/hgrc | |
435 rpath = _earlygetopt(["-R", "--repository", "--repo"], args) | |
436 if rpath: | 428 if rpath: |
437 path = lui.expandpath(rpath[-1]) | 429 path = lui.expandpath(rpath[-1]) |
438 lui = ui.copy() | 430 lui = ui.copy() |
439 lui.readconfig(os.path.join(path, ".hg", "hgrc")) | 431 lui.readconfig(os.path.join(path, ".hg", "hgrc")) |
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) | |
440 | 485 |
441 # Configure extensions in phases: uisetup, extsetup, cmdtable, and | 486 # Configure extensions in phases: uisetup, extsetup, cmdtable, and |
442 # reposetup. Programs like TortoiseHg will call _dispatch several | 487 # reposetup. Programs like TortoiseHg will call _dispatch several |
443 # times so we keep track of configured extensions in _loaded. | 488 # times so we keep track of configured extensions in _loaded. |
444 extensions.loadall(lui) | 489 extensions.loadall(lui) |