diff --git a/mercurial/configitems.py b/mercurial/configitems.py --- a/mercurial/configitems.py +++ b/mercurial/configitems.py @@ -32,12 +32,14 @@ class configitem(object): :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 = {} @@ -194,6 +196,9 @@ coreconfigitem('ui', 'interactive', coreconfigitem('ui', 'quiet', default=False, ) +coreconfigitem('ui', 'username', + alias=[('ui', 'user')] +) # Windows defaults to a limit of 512 open files. A buffer of 128 # should give us enough headway. coreconfigitem('worker', 'backgroundclosemaxqueue', diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -448,38 +448,39 @@ class ui(object): 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 @@ class ui(object): """ 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 --git a/tests/test-config.t b/tests/test-config.t --- a/tests/test-config.t +++ b/tests/test-config.t @@ -178,3 +178,36 @@ config affected by environment variables $ PAGER=p1 hg config --debug --config pager.pager=p2 | grep 'pager\.pager' --config: pager.pager=p2 + +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 ..