# HG changeset patch # User David Demelier <demelier.da...@gmail.com> # Date 1498646676 -7200 # Wed Jun 28 12:44:36 2017 +0200 # Node ID 9a80ff1ee41ce0c87cf1f7f5219b6337f3c04941 # Parent 247bae545061374a683d8d9369a4869aa6ae4537 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. diff -r 247bae545061 -r 9a80ff1ee41c mercurial/configitems.py --- a/mercurial/configitems.py Tue Jun 27 23:50:22 2017 +0900 +++ b/mercurial/configitems.py Wed Jun 28 12:44:36 2017 +0200 @@ -19,9 +19,10 @@ :default: default value for this item, """ - def __init__(self, section, name, default=None): + def __init__(self, section, name, alias=None, default=None): self.section = section self.name = name + self.alias = alias self.default = default coreitems = {} @@ -48,3 +49,6 @@ coreconfigitem('ui', 'quiet', default=False, ) +coreconfigitem('ui', 'username', + alias=[('ui', 'user')] +) diff -r 247bae545061 -r 9a80ff1ee41c mercurial/ui.py --- a/mercurial/ui.py Tue Jun 27 23:50:22 2017 +0900 +++ b/mercurial/ui.py Wed Jun 28 12:44:36 2017 +0200 @@ -448,36 +448,37 @@ 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 - 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') + item = self._knownconfig.get(section, {}).get(name) + alternates = [(section, name)] + + if item is not None and item.alias is not None: + alternates = alternates + item.alias - alternates = [name] + if default is _unset: + if item is None: + value = 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') - 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): @@ -742,7 +743,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 247bae545061 -r 9a80ff1ee41c tests/test-config-alias.t --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-config-alias.t Wed Jun 28 12:44:36 2017 +0200 @@ -0,0 +1,14 @@ +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 _______________________________________________ Mercurial-devel mailing list Mercurial-devel@mercurial-scm.org https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel