Bug 4200 - shell aliases should support interpolating "$@" the way POSIX shells do
Summary: shell aliases should support interpolating "$@" the way POSIX shells do
Status: RESOLVED FIXED
Alias: None
Product: Mercurial
Classification: Unclassified
Component: Mercurial (show other bugs)
Version: unspecified
Hardware: All All
: normal feature
Assignee: Bugzilla
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-20 20:21 UTC by Siddharth Agarwal
Modified: 2014-09-04 13:48 UTC (History)
2 users (show)

See Also:
Python Version: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Siddharth Agarwal 2014-03-20 20:21 UTC
The "$@" parameter in Bourne shells (at least) interpolates arguments while splitting tokens in the way they were provided. In other words,  "$@" expands to "$1" "$2"...

It is currently not possible to achieve this in Mercurial.

In a POSIX shell:

$ cat echotokens
#!/bin/sh
printf "%s\n" "$@"

$ ./echotokens foo 'bar $1 baz'
foo
bar $1 baz

In Mercurial, the closest equivalents are $@ (without quotes) and '$@' (with single quotes). Neither of those are exact matches, however.

The following tests are with hg @ 43054dc84abd.

$ hg showconfig alias.echotokens
!printf "%s\n" $@
$ hg echotokens foo 'bar $1 baz'
foo
bar
baz

$ hg showconfig alias.echotokens
!printf "%s\n" '$@'
$ hg echotokens foo 'bar $1 baz'
foo bar $1 baz

$ hg showconfig alias.echotokens
!printf "%s\n" "$@"
$ hg echotokens foo 'bar $1 baz'
foo bar  baz
Comment 1 HG Bot 2014-08-14 16:30 UTC
Fixed by http://selenic.com/repo/hg/rev/bc2132dfc0a4
Siddharth Agarwal <sid0@fb.com>
alias: expand "$@" as list of parameters quoted individually (BC) (issue4200)

Before this patch, there was no way to pass in all the positional parameters as
separate words down to another command.

(1) $@ (without quotes) would expand to all the parameters separated by a space.
    This would work fine for arguments without spaces, but arguments with spaces
    in them would be split up by POSIX shells into separate words.
(2) '$@' (in single quotes) would expand to all the parameters within a pair of
    single quotes. POSIX shells would then treat the entire list of arguments
    as one word.
(3) "$@" (in double quotes) would expand similarly to (2).

With this patch, we expand "$@" (in double quotes) as all positional
parameters, quoted individually with util.shellquote, and separated by spaces.
Under standard field-splitting conditions, POSIX shells will tokenize each
argument into exactly one word.

This is a backwards-incompatible change, but the old behavior was arguably a
bug: Bourne-derived shells have expanded "$@" as a tokenized list of positional
parameters for a very long time. I could find this behavior specified in IEEE
Std 1003.1-2001, and this probably goes back to much further before that.

(please test the fix)