Patchwork [2,of,2] config: use the alternative with highest level

login
register
mail settings
Submitter Boris Feld
Date July 9, 2018, 10:12 a.m.
Message ID <2eac4bcf6e050eeb55f6.1531131142@FB-lair>
Download mbox | patch
Permalink /patch/32697/
State Accepted
Headers show

Comments

Boris Feld - July 9, 2018, 10:12 a.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1530895697 -7200
#      Fri Jul 06 18:48:17 2018 +0200
# Node ID 2eac4bcf6e050eeb55f6872b816ed3a328818614
# Parent  1019d8a4f6b810aaa63651ed56b29668650f590e
# EXP-Topic config-order
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 2eac4bcf6e05
config: use the alternative with highest level

With this change, an alias defined in an higher level configuration file will
take precedence over values from other alternatives.

Change the case from 'r' to 'R' to make sure the sorting is made over the
configuration file priority and not on the values themselves.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -552,13 +552,17 @@  class ui(object):
             msg %= (section, name, pycompat.bytestr(default))
             self.develwarn(msg, 2, 'warn-config-default')
 
-        for s, n in alternates:
+        found = []
+        for idx, (s, n) in enumerate(alternates):
             candidate = self._data(untrusted).get(s, n, None)
             if candidate is not None:
                 value = candidate
                 section = s
                 name = n
-                break
+                level = self._data(untrusted).level(s, n)
+                found.append((level, -idx, value))
+        if found:
+            value = max(found)[-1]
 
         if self.debugflag and not untrusted and self._reportuntrusted:
             for s, n in alternates:
diff --git a/tests/test-config.t b/tests/test-config.t
--- a/tests/test-config.t
+++ b/tests/test-config.t
@@ -202,12 +202,36 @@  alias has lower priority
   $ cat > .hg/hgrc << EOF
   > [ui]
   > user = alias user
-  > username = repo user
+  > username = Repo user
   > EOF
   $ touch index
   $ unset HGUSER
   $ hg ci -Am test
   adding index
   $ hg log --template '{author}\n'
-  repo user
+  Repo user
   $ cd ..
+
+Unless alias are specified at an higher level
+
+  $ hg init aliaspriority-level
+  $ cd aliaspriority-level
+  $ cat >> $HGRCPATH << EOF
+  > [ui]
+  > username = repo user
+  > EOF
+  $ cat > .hg/hgrc << EOF
+  > [ui]
+  > user = alias user
+  > EOF
+  $ touch index
+  $ unset HGUSER
+  $ hg ci -Am test
+  adding index
+  $ hg log --template '{author}\n'
+  alias user
+  $ echo foo >> index
+  $ hg ci -m test --config ui.username=cmd-user
+  $ hg log --template '{author}\n' -r .
+  cmd-user
+  $ cd ..