[PATCH 09 of 13 V3] ui: use scmutil.rccomponents to load configs (BC)
Augie Fackler
raf at durin42.com
Thu Mar 23 18:07:29 EDT 2017
On Thu, Mar 23, 2017 at 09:53:26AM -0700, Jun Wu wrote:
> Excerpts from Martin von Zweigbergk's message of 2017-03-22 22:34:47 -0700:
> > On Wed, Mar 22, 2017 at 10:23 AM, Jun Wu <quark at fb.com> wrote:
> > > # HG changeset patch
> > > # User Jun Wu <quark at fb.com>
> > > # Date 1489462886 25200
> > > # Mon Mar 13 20:41:26 2017 -0700
> > > # Node ID 6c04717d3b4958800a39fdf6e2c28e2caf6629bd
> > > # Parent c537d04829a8dc0b88fe03ec41e95a85638c696b
> > > # Available At https://bitbucket.org/quark-zju/hg-draft
> > > # hg pull https://bitbucket.org/quark-zju/hg-draft -r 6c04717d3b49
> > > ui: use scmutil.rccomponents to load configs (BC)
> > >
> > > This is BC because system configs won't be able to override $EDITOR, $PAGER.
> > > The new behavior is arguably more rational.
> > >
> > > The code added is somehow temporary. Once we have immutable config objects,
> > > this area will be cleaner.
> > >
> > > diff --git a/mercurial/ui.py b/mercurial/ui.py
> > > --- a/mercurial/ui.py
> > > +++ b/mercurial/ui.py
> > > @@ -212,6 +212,19 @@ class ui(object):
> > > u = cls()
> > > # we always trust global config files
> >
> > Do you know what this comment refers to? Should it be moved a few
> > lines down now (I'm thinking maybe just before u.readconfig())?
>
> I think it can be updated to "trust global config files and environment" so
> it says at the current position and explains the 'items' case.
That's what this comment is referring to (as a confirmation).
>
> >
> > > - for f in scmutil.rcpath():
> > > - u.readconfig(f, trust=True)
> > > + for (t, f) in scmutil.rccomponents():
> > > + if t == 'path':
> > > + u.readconfig(f, trust=True)
> > > + elif t == 'items':
> > > + sections = set()
> > > + for section, name, value, source in f:
> > > + # do not set ocfg
> > > + # XXX change this once we have immutable config objects
> > > + u._tcfg.set(section, name, value, source)
> > > + u._ucfg.set(section, name, value, source)
> > > + sections.add(section)
> > > + for section in sections:
> > > + u.fixconfig(section=section)
> > > + else:
> > > + raise error.ProgrammingError('unexpected rccomponent: %s' % t)
> > > return u
> > >
> > > diff --git a/tests/test-config-env.py b/tests/test-config-env.py
> > > new file mode 100644
> > > --- /dev/null
> > > +++ b/tests/test-config-env.py
> > > @@ -0,0 +1,48 @@
> > > +# Test the config layer generated by environment variables
> > > +
> > > +from __future__ import absolute_import, print_function
> > > +
> > > +import os
> > > +
> > > +from mercurial import (
> > > + encoding,
> > > + scmutil,
> > > + ui as uimod,
> > > +)
> > > +
> > > +testtmp = encoding.environ['TESTTMP']
> > > +
> > > +# prepare hgrc files
> > > +def join(name):
> > > + return os.path.join(testtmp, name)
> > > +
> > > +with open(join('sysrc'), 'w') as f:
> > > + f.write('[ui]\neditor=e0\n[pager]\npager=p0\n')
> > > +
> > > +with open(join('userrc'), 'w') as f:
> > > + f.write('[ui]\neditor=e1')
> > > +
> > > +# replace rcpath functions so they point to the files above
> > > +def systemrcpath():
> > > + return [join('sysrc')]
> > > +
> > > +def userrcpath():
> > > + return [join('userrc')]
> > > +
> > > +scmutil.systemrcpath = systemrcpath
> > > +scmutil.userrcpath = userrcpath
> > > +os.path.isdir = lambda x: False # hack: do not load default.d/*.rc
> > > +
> > > +# utility to print configs
> > > +def printconfigs(env):
> > > + encoding.environ = env
> > > + scmutil._rccomponents = None # reset cache
> > > + ui = uimod.ui.load()
> > > + for section, name, value in ui.walkconfig():
> > > + source = ui.configsource(section, name)
> > > + print('%s.%s=%s # %s' % (section, name, value, source))
> > > + print('')
> > > +
> > > +# environment variable overrides
> > > +printconfigs({})
> > > +printconfigs({'EDITOR': 'e2', 'PAGER': 'p2'})
> > > diff --git a/tests/test-config-env.py.out b/tests/test-config-env.py.out
> > > new file mode 100644
> > > --- /dev/null
> > > +++ b/tests/test-config-env.py.out
> > > @@ -0,0 +1,6 @@
> > > +pager.pager=p0 # $TESTTMP/sysrc:4
> > > +ui.editor=e1 # $TESTTMP/userrc:2
> > > +
> > > +pager.pager=p2 # $PAGER
> > > +ui.editor=e1 # $TESTTMP/userrc:2
> > > +
> > > diff --git a/tests/test-config.t b/tests/test-config.t
> > > --- a/tests/test-config.t
> > > +++ b/tests/test-config.t
> > > @@ -165,2 +165,16 @@ edit failure
> > > abort: edit failed: false exited with status 1
> > > [255]
> > > +
> > > +config affected by environment variables
> > > +
> > > + $ EDITOR=e1 VISUAL=e2 hg config --debug | grep 'ui\.editor'
> > > + $VISUAL: ui.editor=e2
> > > +
> > > + $ VISUAL=e2 hg config --debug --config ui.editor=e3 | grep 'ui\.editor'
> > > + --config: ui.editor=e3
> > > +
> > > + $ PAGER=p1 hg config --debug | grep 'pager\.pager'
> > > + $PAGER: pager.pager=p1
> > > +
> > > + $ PAGER=p1 hg config --debug --config pager.pager=p2 | grep 'pager\.pager'
> > > + --config: pager.pager=p2
> > > _______________________________________________
> > > Mercurial-devel mailing list
> > > Mercurial-devel at mercurial-scm.org
> > > https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
More information about the Mercurial-devel
mailing list