configwarn: new extension to warn about unsupported configs

We have seen issues caused by user config file (`~/.hgrc`) having
unsupported extensions enabled. This new extension would allow us to warn
users about those problematic configs.

Differential Revision: https://phab.mercurial-scm.org/D154
This commit is contained in:
Jun Wu 2017-08-07 13:16:09 -07:00
parent 760abb3739
commit ba4766820a
2 changed files with 107 additions and 0 deletions

53
hgext3rd/configwarn.py Normal file
View File

@ -0,0 +1,53 @@
# configwarn.py - warn unsupported user configs
#
# Copyright 2017 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""warn unsupported user configs
Config::
[configwarn]
# Config names that are supposed to be set by system config and not
# overrided by user config.
systemconfigs = diff.git, extensions.hggit
"""
from __future__ import absolute_import
from mercurial.i18n import _
from mercurial import (
rcutil,
registrar,
)
configtable = {}
configitem = registrar.configitem(configtable)
configitem('configwarn', 'systemconfigs', default=[])
def reposetup(ui, repo):
# use reposetup, not uisetup to work better with chg and it checks reporc.
if not repo.local():
return
nonsystempaths = set(rcutil.userrcpath() + [repo.vfs.join('hgrc')])
systemconfigs = ui.configlist('configwarn', 'systemconfigs')
for configname in systemconfigs:
if '.' not in configname:
continue
section, name = configname.split('.', 1)
source = ui.configsource(section, name)
if ':' not in source:
continue
path, lineno = source.split(':', 1)
if path in nonsystempaths and lineno.isdigit():
ui.warn(_('warning: overriding config %s is unsupported (hint: '
'remove line %s from %s to resolve this issue)\n')
% (configname, lineno, path))

54
tests/test-configwarn.t Normal file
View File

@ -0,0 +1,54 @@
$ cat >> $HGRCPATH <<EOF
> [extensions]
> configwarn=$TESTDIR/../hgext3rd/configwarn.py
> [configwarn]
> systemconfigs=diff.git, phases.publish
> [alias]
> noop=log -r null -T '{files}'
> EOF
Need to override rcutil.userrcpath to test properly without side-effects
$ cat >> $TESTTMP/rcpath.py <<EOF
> from __future__ import absolute_import
> from mercurial import encoding, rcutil
> def userrcpath():
> return [encoding.environ[b'USERHGRC']]
> rcutil.userrcpath = userrcpath
> EOF
$ cat >> $HGRCPATH <<EOF
> [extensions]
> rcpath=$TESTTMP/rcpath.py
> EOF
$ USERHGRC="$TESTTMP/userhgrc"
$ HGRCPATH="$HGRCPATH:$USERHGRC"
$ export USERHGRC
Config set by system config files or command line flags are fine
$ hg init
$ hg noop
$ hg noop --config diff.git=1
Config set by user config will generate warnings
$ cat >> $USERHGRC <<EOF
> [diff]
> git=0
> EOF
$ hg noop
warning: overriding config diff.git is unsupported (hint: remove line 2 from $TESTTMP/userhgrc to resolve this issue)
Config set by repo will generate warnings
$ cat >> .hg/hgrc << EOF
> [phases]
> publish=1
> EOF
$ hg noop
warning: overriding config diff.git is unsupported (hint: remove line 2 from $TESTTMP/userhgrc to resolve this issue)
warning: overriding config phases.publish is unsupported (hint: remove line 2 from $TESTTMP/.hg/hgrc to resolve this issue)