Commit Graph

7 Commits

Author SHA1 Message Date
Steve Losh
0224e99d28 alias: fail gracefully when invalid global options are given (issue2442)
This patch modifies the check for shell aliases to prevent crashing when an invalid
global option is given.

When an invalid global option is given the check will simply return and let the
normal error handling for this case happen.
2010-10-17 13:24:37 -04:00
Adrian Buehlmann
9614229975 tests: add testcase for 2933824cb30c to test-alias.t
The testsuite lacks a testcase for the bug introduced in 2933824cb30c.

This patch amends 91db5130b446 (which fixed 2933824cb30c) by adding a
testcase for that bug.

With 2933824cb30c, test-alias.t (as modified by this patch) fails
with "hg tglog: invalid arguments".
2010-10-09 11:16:35 +02:00
Steve Losh
0ec78824a3 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
2010-08-24 18:25:33 -04:00
Matt Mackall
bf9417da11 tests: exit 0 no longer needed on unified tests 2010-09-17 17:03:04 -05:00
Matt Mackall
08439e0f2d tests: add exit codes to unified tests 2010-09-16 17:51:32 -05:00
Steve Losh
51f95d13a2 aliases: provide more flexible ways to work with shell alias arguments
This patch changes the functionality of shell aliases to add more powerful
options for working with shell alias arguments.

First: the alias name + arguments to a shell alias are set as an HG_ARGS
environment variable, delimited by spaces. This matches the behavior of hooks.

Second: any occurrences of "$@" (without quotes) are replaced with the
arguments, separated by spaces. This happens *before* the alias gets to the shell.

Third: any positive numeric variables ("$1", "$2", etc) are replaced with the
appropriate argument, indexed from 1. "$0" is replaced with the name of the
alias. Any "extra" numeric variables are replaced with an empty string.  This
happens *before* the alias gets to the shell.

These changes allow for more flexible shell aliases:

    [alias]
    echo = !echo $@
    count = !hg log -r "$@" --template='.' | wc -c | sed -e 's/ //g'
    qqueuemv = !mv "`hg root`/.hg/patches-$1" "`hg root`/.hg/patches-$2"

In action:

    $ hg echo foo
    foo

    $ hg count 'branch(default)'
    901

    $ hg count 'branch(stable) and keyword(fixes)'
    102

    $ hg qqueuemv myfeature somefeature
2010-08-18 18:56:44 -04:00
Nicolas Dumazet
051d3b42b9 tests: unify test-alias 2010-08-12 23:46:56 +09:00