Mercurial > public > mercurial-scm > hg
comparison mercurial/ui.py @ 33329:e714159860fd
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.
author | David Demelier <demelier.david@gmail.com> |
---|---|
date | Fri, 07 Jul 2017 08:33:10 +0200 |
parents | 77e666f943a6 |
children | d74141ccfd8b |
comparison
equal
deleted
inserted
replaced
33328:c8f212cb0c83 | 33329:e714159860fd |
---|---|
446 return None | 446 return None |
447 return value | 447 return value |
448 | 448 |
449 def _config(self, section, name, default=_unset, untrusted=False): | 449 def _config(self, section, name, default=_unset, untrusted=False): |
450 value = default | 450 value = default |
451 if isinstance(name, list): | 451 item = self._knownconfig.get(section, {}).get(name) |
452 alternates = name | 452 alternates = [(section, name)] |
453 else: | 453 |
454 item = self._knownconfig.get(section, {}).get(name) | 454 if item is not None: |
455 if default is _unset: | 455 alternates.extend(item.alias) |
456 if item is None: | 456 |
457 value = default | 457 if default is _unset: |
458 elif callable(item.default): | 458 if item is None: |
459 value = default | |
460 elif callable(item.default): | |
459 value = item.default() | 461 value = item.default() |
460 else: | 462 else: |
461 value = item.default | 463 value = item.default |
462 elif item is not None: | 464 elif item is not None: |
463 msg = ("specifying a default value for a registered " | 465 msg = ("specifying a default value for a registered " |
464 "config item: '%s.%s' '%s'") | 466 "config item: '%s.%s' '%s'") |
465 msg %= (section, name, default) | 467 msg %= (section, name, default) |
466 self.develwarn(msg, 2, 'warn-config-default') | 468 self.develwarn(msg, 2, 'warn-config-default') |
467 | 469 |
468 alternates = [name] | 470 for s, n in alternates: |
469 | 471 candidate = self._data(untrusted).get(s, n, None) |
470 for n in alternates: | |
471 candidate = self._data(untrusted).get(section, n, None) | |
472 if candidate is not None: | 472 if candidate is not None: |
473 value = candidate | 473 value = candidate |
474 section = s | |
474 name = n | 475 name = n |
475 break | 476 break |
476 | 477 |
477 if self.debugflag and not untrusted and self._reportuntrusted: | 478 if self.debugflag and not untrusted and self._reportuntrusted: |
478 for n in alternates: | 479 for s, n in alternates: |
479 uvalue = self._ucfg.get(section, n) | 480 uvalue = self._ucfg.get(s, n) |
480 if uvalue is not None and uvalue != value: | 481 if uvalue is not None and uvalue != value: |
481 self.debug("ignoring untrusted configuration option " | 482 self.debug("ignoring untrusted configuration option " |
482 "%s.%s = %s\n" % (section, n, uvalue)) | 483 "%s.%s = %s\n" % (s, n, uvalue)) |
483 return value | 484 return value |
484 | 485 |
485 def configsuboptions(self, section, name, default=_unset, untrusted=False): | 486 def configsuboptions(self, section, name, default=_unset, untrusted=False): |
486 """Get a config option and all sub-options. | 487 """Get a config option and all sub-options. |
487 | 488 |
742 If not found and ui.askusername is True, ask the user, else use | 743 If not found and ui.askusername is True, ask the user, else use |
743 ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". | 744 ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". |
744 """ | 745 """ |
745 user = encoding.environ.get("HGUSER") | 746 user = encoding.environ.get("HGUSER") |
746 if user is None: | 747 if user is None: |
747 user = self.config("ui", ["username", "user"]) | 748 user = self.config("ui", "username") |
748 if user is not None: | 749 if user is not None: |
749 user = os.path.expandvars(user) | 750 user = os.path.expandvars(user) |
750 if user is None: | 751 if user is None: |
751 user = encoding.environ.get("EMAIL") | 752 user = encoding.environ.get("EMAIL") |
752 if user is None and self.configbool("ui", "askusername"): | 753 if user is None and self.configbool("ui", "askusername"): |