Patchwork [1,of,2] dispatch: defer environment variable resolution in alias commands (BC)

login
register
mail settings
Submitter Jun Wu
Date May 6, 2016, 11:22 p.m.
Message ID <24dc9e5deec415bfa635.1462576968@x1c>
Download mbox | patch
Permalink /patch/14958/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - May 6, 2016, 11:22 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1462576618 -3600
#      Sat May 07 00:16:58 2016 +0100
# Node ID 24dc9e5deec415bfa635fb80c7767ef5146c2775
# Parent  b473b7bd02b3bad724c0178ec08ae49019aed51c
dispatch: defer environment variable resolution in alias commands (BC)

Before this patch, if there are environment variables in an alias command,
they will be expanded immediately when we first see the alias.

This will cause issues with chg, because environment variable updates will
not propagate to expanded arguments.

This patch makes "args" of "cmdalias" a property that will be calculated
every time when accessed.
Yuya Nishihara - May 7, 2016, 9:36 a.m.
On Sat, 7 May 2016 00:22:48 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1462576618 -3600
> #      Sat May 07 00:16:58 2016 +0100
> # Node ID 24dc9e5deec415bfa635fb80c7767ef5146c2775
> # Parent  b473b7bd02b3bad724c0178ec08ae49019aed51c
> dispatch: defer environment variable resolution in alias commands (BC)

Awesome! Pushed to the committed repo, thanks.

> +environment variable changes in alias commands
> +
> +  $ cat > $TESTTMP/setcount.py <<EOF
> +  > import os
> +  > def uisetup(ui):
> +  >     os.environ['COUNT'] = '2'
> +  > EOF
> +
> +  $ cat >> $HGRCPATH <<'EOF'
> +  > [extensions]
> +  > setcount = $TESTTMP/setcount.py
> +  > [alias]
> +  > showcount = log -T "$COUNT\n" -r .
> +  > EOF
> +
> +  $ COUNT=1 hg showcount
> +  2

This doesn't pass with --chg due to uisetup() hack. Can you fix it by follow-up
patch?
Jun Wu - May 7, 2016, 9:52 a.m.
On 05/07/2016 10:36 AM, Yuya Nishihara wrote:
> This doesn't pass with --chg due to uisetup() hack. Can you fix it by
> follow-up patch?

Ah, I see. I didn't run --chg tests because I want to fix/skip all chg tests
in a series later. For this one, I will try to find another way to change the
environment variable.

Patch

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -384,7 +384,7 @@ 
         self.cmdname = ''
         self.definition = definition
         self.fn = None
-        self.args = []
+        self.givenargs = []
         self.opts = []
         self.help = ''
         self.badalias = None
@@ -432,7 +432,7 @@ 
                              % (self.name, inst))
             return
         self.cmdname = cmd = args.pop(0)
-        args = map(util.expandpath, args)
+        self.givenargs = args
 
         for invalidarg in ("--cwd", "-R", "--repository", "--repo", "--config"):
             if _earlygetopt([invalidarg], args):
@@ -448,7 +448,6 @@ 
             else:
                 self.fn, self.opts = tableentry
 
-            self.args = aliasargs(self.fn, args)
             if self.help.startswith("hg " + cmd):
                 # drop prefix in old-style help lines so hg shows the alias
                 self.help = self.help[4 + len(cmd):]
@@ -462,6 +461,11 @@ 
             self.badalias = (_("alias '%s' resolves to ambiguous command '%s'")
                              % (self.name, cmd))
 
+    @property
+    def args(self):
+        args = map(util.expandpath, self.givenargs)
+        return aliasargs(self.fn, args)
+
     def __getattr__(self, name):
         adefaults = {'norepo': True, 'optionalrepo': False, 'inferrepo': False}
         if name not in adefaults:
diff --git a/tests/test-alias.t b/tests/test-alias.t
--- a/tests/test-alias.t
+++ b/tests/test-alias.t
@@ -525,6 +525,24 @@ 
   (use "hg help" for the full list of commands or "hg -v" for details)
   [255]
 
+environment variable changes in alias commands
+
+  $ cat > $TESTTMP/setcount.py <<EOF
+  > import os
+  > def uisetup(ui):
+  >     os.environ['COUNT'] = '2'
+  > EOF
+
+  $ cat >> $HGRCPATH <<'EOF'
+  > [extensions]
+  > setcount = $TESTTMP/setcount.py
+  > [alias]
+  > showcount = log -T "$COUNT\n" -r .
+  > EOF
+
+  $ COUNT=1 hg showcount
+  2
+
 This should show id:
 
   $ hg --config alias.log='id' log