Show More
@@ -331,6 +331,27 b' def aliasargs(fn, givenargs):' | |||
|
331 | 331 | args = shlex.split(cmd) |
|
332 | 332 | return args + givenargs |
|
333 | 333 | |
|
334 | def aliasinterpolate(name, args, cmd): | |
|
335 | '''interpolate args into cmd for shell aliases | |
|
336 | ||
|
337 | This also handles $0, $@ and "$@". | |
|
338 | ''' | |
|
339 | # util.interpolate can't deal with "$@" (with quotes) because it's only | |
|
340 | # built to match prefix + patterns. | |
|
341 | replacemap = dict(('$%d' % (i + 1), arg) for i, arg in enumerate(args)) | |
|
342 | replacemap['$0'] = name | |
|
343 | replacemap['$$'] = '$' | |
|
344 | replacemap['$@'] = ' '.join(args) | |
|
345 | # Typical Unix shells interpolate "$@" (with quotes) as all the positional | |
|
346 | # parameters, separated out into words. Emulate the same behavior here by | |
|
347 | # quoting the arguments individually. POSIX shells will then typically | |
|
348 | # tokenize each argument into exactly one word. | |
|
349 | replacemap['"$@"'] = ' '.join(util.shellquote(arg) for arg in args) | |
|
350 | # escape '\$' for regex | |
|
351 | regex = '|'.join(replacemap.keys()).replace('$', r'\$') | |
|
352 | r = re.compile(regex) | |
|
353 | return r.sub(lambda x: replacemap[x.group()], cmd) | |
|
354 | ||
|
334 | 355 | class cmdalias(object): |
|
335 | 356 | def __init__(self, name, definition, cmdtable): |
|
336 | 357 | self.name = self.cmd = name |
@@ -376,10 +397,7 b' class cmdalias(object):' | |||
|
376 | 397 | % (int(m.groups()[0]), self.name)) |
|
377 | 398 | return '' |
|
378 | 399 | cmd = re.sub(r'\$(\d+|\$)', _checkvar, self.definition[1:]) |
|
379 | replace = dict((str(i + 1), arg) for i, arg in enumerate(args)) | |
|
380 | replace['0'] = self.name | |
|
381 | replace['@'] = ' '.join(args) | |
|
382 | cmd = util.interpolate(r'\$', replace, cmd, escape_prefix=True) | |
|
400 | cmd = aliasinterpolate(self.name, args, cmd) | |
|
383 | 401 | return util.system(cmd, environ=env, out=ui.fout) |
|
384 | 402 | self.fn = fn |
|
385 | 403 | return |
@@ -229,8 +229,9 b' repository in the same manner as the pur' | |||
|
229 | 229 | Positional arguments like ``$1``, ``$2``, etc. in the alias definition |
|
230 | 230 | expand to the command arguments. Unmatched arguments are |
|
231 | 231 | removed. ``$0`` expands to the alias name and ``$@`` expands to all |
|
232 |
arguments separated by a space. |
|
|
233 | command is passed to the shell. | |
|
232 | arguments separated by a space. ``"$@"`` (with quotes) expands to all | |
|
233 | arguments quoted individually and separated by a space. These expansions | |
|
234 | happen before the command is passed to the shell. | |
|
234 | 235 | |
|
235 | 236 | Shell aliases are executed in an environment where ``$HG`` expands to |
|
236 | 237 | the path of the Mercurial that was used to execute the alias. This is |
@@ -30,6 +30,7 b'' | |||
|
30 | 30 | > echo1 = !printf '\$1\n' |
|
31 | 31 | > echo2 = !printf '\$2\n' |
|
32 | 32 | > echo13 = !printf '\$1 \$3\n' |
|
33 | > echotokens = !printf "%s\n" "\$@" | |
|
33 | 34 | > count = !hg log -r "\$@" --template=. | wc -c | sed -e 's/ //g' |
|
34 | 35 | > mcount = !hg log \$@ --template=. | wc -c | sed -e 's/ //g' |
|
35 | 36 | > rt = root |
@@ -241,6 +242,22 b' simple shell aliases' | |||
|
241 | 242 | foo baz |
|
242 | 243 | $ hg echo2 foo |
|
243 | 244 | |
|
245 | $ hg echotokens | |
|
246 | ||
|
247 | $ hg echotokens foo 'bar $1 baz' | |
|
248 | foo | |
|
249 | bar $1 baz | |
|
250 | $ hg echotokens 'test $2' foo | |
|
251 | test $2 | |
|
252 | foo | |
|
253 | $ hg echotokens 'test $@' foo '$@' | |
|
254 | test $@ | |
|
255 | foo | |
|
256 | $@ | |
|
257 | $ hg echotokens 'test "$@"' foo '"$@"' | |
|
258 | test "$@" | |
|
259 | foo | |
|
260 | "$@" | |
|
244 | 261 | $ echo bar > bar |
|
245 | 262 | $ hg commit -qA -m bar |
|
246 | 263 | $ hg count . |
General Comments 0
You need to be logged in to leave comments.
Login now