Patchwork [v3] configitems: add alias support in config

login
register
mail settings
Submitter David Demelier
Date July 5, 2017, 11:56 a.m.
Message ID <92546f63e0bee8fa71fd.1499255815@fedy>
Download mbox | patch
Permalink /patch/22010/
State Changes Requested
Headers show

Comments

David Demelier - July 5, 2017, 11:56 a.m.
# HG changeset patch
# User David Demelier <demelier.david@gmail.com>
# Date 1499079875 -7200
#      Mon Jul 03 13:04:35 2017 +0200
# Node ID 92546f63e0bee8fa71fd46485e0970b335e438e6
# Parent  5c9ad50fd62fbc2057ef7b44f921f22e7359af32
configitems: add alias support in config

Aliases define optional alternatives to existing options. For example the old
option ui.user was deprecated and replaced by ui.username. With this mechanism,
it's even possible to create an alias to an option in a different section.

Add ui.user as alias to ui.username as an example of this concept.

The old alternates principle in ui.config is removed as it was used only for
this option.

Update hgweb methods to use ui._unset.
Yuya Nishihara - July 6, 2017, 1:18 p.m.
On Wed, 05 Jul 2017 13:56:55 +0200, David Demelier wrote:
> # HG changeset patch
> # User David Demelier <demelier.david@gmail.com>
> # Date 1499079875 -7200
> #      Mon Jul 03 13:04:35 2017 +0200
> # Node ID 92546f63e0bee8fa71fd46485e0970b335e438e6
> # Parent  5c9ad50fd62fbc2057ef7b44f921f22e7359af32
> configitems: add alias support in config
> 
> Aliases define optional alternatives to existing options. For example the old
> option ui.user was deprecated and replaced by ui.username. With this mechanism,
> it's even possible to create an alias to an option in a different section.
> 
> Add ui.user as alias to ui.username as an example of this concept.
> 
> The old alternates principle in ui.config is removed as it was used only for
> this option.
> 
> Update hgweb methods to use ui._unset.

Looks good, but any reason why hgweb changes are folded into one patch? I
think the previous series was better in the way of how patches were organized.

> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/tests/test-config-alias.t	Mon Jul 03 13:04:35 2017 +0200

Both this and test-config.t are small. If you don't have a strong reason
to create new test file, please add new test to test-config.t.

Patch

diff -r 5c9ad50fd62f -r 92546f63e0be mercurial/configitems.py
--- a/mercurial/configitems.py	Wed Jun 28 13:32:36 2017 +0200
+++ b/mercurial/configitems.py	Mon Jul 03 13:04:35 2017 +0200
@@ -32,12 +32,14 @@ 
     :section: the official config section where to find this item,
        :name: the official name within the section,
     :default: default value for this item,
+    :alias: optional list of tuples as alternatives.
     """
 
-    def __init__(self, section, name, default=None):
+    def __init__(self, section, name, default=None, alias=()):
         self.section = section
         self.name = name
         self.default = default
+        self.alias = list(alias)
 
 coreitems = {}
 
@@ -95,3 +97,6 @@ 
 coreconfigitem('ui', 'quiet',
     default=False,
 )
+coreconfigitem('ui', 'username',
+    alias=[('ui', 'user')]
+)
diff -r 5c9ad50fd62f -r 92546f63e0be mercurial/hgweb/hgweb_mod.py
--- a/mercurial/hgweb/hgweb_mod.py	Wed Jun 28 13:32:36 2017 +0200
+++ b/mercurial/hgweb/hgweb_mod.py	Mon Jul 03 13:04:35 2017 +0200
@@ -119,19 +119,19 @@ 
         self.csp, self.nonce = cspvalues(self.repo.ui)
 
     # Trust the settings from the .hg/hgrc files by default.
-    def config(self, section, name, default=None, untrusted=True):
+    def config(self, section, name, default=uimod._unset, untrusted=True):
         return self.repo.ui.config(section, name, default,
                                    untrusted=untrusted)
 
-    def configbool(self, section, name, default=False, untrusted=True):
+    def configbool(self, section, name, default=uimod._unset, untrusted=True):
         return self.repo.ui.configbool(section, name, default,
                                        untrusted=untrusted)
 
-    def configint(self, section, name, default=None, untrusted=True):
+    def configint(self, section, name, default=uimod._unset, untrusted=True):
         return self.repo.ui.configint(section, name, default,
                                       untrusted=untrusted)
 
-    def configlist(self, section, name, default=None, untrusted=True):
+    def configlist(self, section, name, default=uimod._unset, untrusted=True):
         return self.repo.ui.configlist(section, name, default,
                                        untrusted=untrusted)
 
diff -r 5c9ad50fd62f -r 92546f63e0be mercurial/hgweb/hgwebdir_mod.py
--- a/mercurial/hgweb/hgwebdir_mod.py	Wed Jun 28 13:32:36 2017 +0200
+++ b/mercurial/hgweb/hgwebdir_mod.py	Mon Jul 03 13:04:35 2017 +0200
@@ -404,7 +404,7 @@ 
                 except Exception as e:
                     u.warn(_('error reading %s/.hg/hgrc: %s\n') % (path, e))
                     continue
-                def get(section, name, default=None):
+                def get(section, name, default=uimod._unset):
                     return u.config(section, name, default, untrusted=True)
 
                 if u.configbool("web", "hidden", untrusted=True):
diff -r 5c9ad50fd62f -r 92546f63e0be mercurial/ui.py
--- a/mercurial/ui.py	Wed Jun 28 13:32:36 2017 +0200
+++ b/mercurial/ui.py	Mon Jul 03 13:04:35 2017 +0200
@@ -448,38 +448,39 @@ 
 
     def _config(self, section, name, default=_unset, untrusted=False):
         value = default
-        if isinstance(name, list):
-            alternates = name
-        else:
-            item = self._knownconfig.get(section, {}).get(name)
-            if default is _unset:
-                if item is None:
-                    value = default
-                elif callable(item.default):
+        item = self._knownconfig.get(section, {}).get(name)
+        alternates = [(section, name)]
+
+        if item is not None:
+            alternates.extend(item.alias)
+
+        if default is _unset:
+            if item is None:
+                value = default
+            elif callable(item.default):
                     value = item.default()
-                else:
-                    value = item.default
-            elif item is not None:
-                msg = ("specifying a default value for a registered "
-                       "config item: '%s.%s' '%s'")
-                msg %= (section, name, default)
-                self.develwarn(msg, 2, 'warn-config-default')
+            else:
+                value = item.default
+        elif item is not None:
+            msg = ("specifying a default value for a registered "
+                   "config item: '%s.%s' '%s'")
+            msg %= (section, name, default)
+            self.develwarn(msg, 2, 'warn-config-default')
 
-            alternates = [name]
-
-        for n in alternates:
-            candidate = self._data(untrusted).get(section, n, None)
+        for s, n in alternates:
+            candidate = self._data(untrusted).get(s, n, None)
             if candidate is not None:
                 value = candidate
+                section = s
                 name = n
                 break
 
         if self.debugflag and not untrusted and self._reportuntrusted:
-            for n in alternates:
-                uvalue = self._ucfg.get(section, n)
+            for s, n in alternates:
+                uvalue = self._ucfg.get(s, n)
                 if uvalue is not None and uvalue != value:
                     self.debug("ignoring untrusted configuration option "
-                               "%s.%s = %s\n" % (section, n, uvalue))
+                               "%s.%s = %s\n" % (s, n, uvalue))
         return value
 
     def configsuboptions(self, section, name, default=_unset, untrusted=False):
@@ -744,7 +745,7 @@ 
         """
         user = encoding.environ.get("HGUSER")
         if user is None:
-            user = self.config("ui", ["username", "user"])
+            user = self.config("ui", "username")
             if user is not None:
                 user = os.path.expandvars(user)
         if user is None:
diff -r 5c9ad50fd62f -r 92546f63e0be tests/test-config-alias.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-config-alias.t	Mon Jul 03 13:04:35 2017 +0200
@@ -0,0 +1,32 @@ 
+verify that aliases are evaluated as well
+
+  $ hg init aliastest
+  $ cd aliastest
+  $ cat > .hg/hgrc << EOF
+  > [ui]
+  > user = repo user
+  > EOF
+  $ touch index
+  $ unset HGUSER
+  $ hg ci -Am test
+  adding index
+  $ hg log --template '{author}\n'
+  repo user
+  $ cd ..
+
+alias has lower priority
+
+  $ hg init aliaspriority
+  $ cd aliaspriority
+  $ cat > .hg/hgrc << EOF
+  > [ui]
+  > user = alias user
+  > username = repo user
+  > EOF
+  $ touch index
+  $ unset HGUSER
+  $ hg ci -Am test
+  adding index
+  $ hg log --template '{author}\n'
+  repo user
+  $ cd ..