[PATCH 1 of 1] dispatch: provide more flexible ways to work with shell alias arguments

Matt Mackall mpm at selenic.com
Tue Aug 17 19:34:45 CDT 2010


On Tue, 2010-08-17 at 19:24 -0400, Steve Losh wrote:
>          if self.definition.startswith('!'):
>              def fn(ui, *args):
> -                cmd = '%s %s' % (self.definition[1:], ' '.join(args))
> -                return util.system(cmd)
> +                env = {'HG_ARGS': ' '.join((self.name,) + args)}
> +                definition = self.definition.replace('$@', ' '.join(args))
> +                definition = definition.replace('$0', self.name)
> +                for i, arg in enumerate(args):
> +                    definition = definition.replace('$%d' % (i + 1), arg)
> +                definition = re.sub(r'\$\d+', '', definition)
> +                return util.system(definition[1:], environ=env)

This almost works, but will get in trouble with something like this:

[aliases]
foo=!echo $@

$ hg foo $2 bar
bar bar

In other words, iteratively modifying the definition in place is
problematic. I've got a quick hack to do something like this in
filemerge.py:

        replace = dict(local=a, base=b, other=c, output=out)                                                                 
        args = re.sub("\$(local|base|other|output)",                                                                         
            lambda x: '"%s"' % util.localpath(replace[x.group()[1:]]), args)   

but this perhaps calls for a utility function:

 out = util.interpolate(prefix, mapping, in)

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list