Mercurial > hg-stable
changeset 35473:f1c54d003327
templater: move repo, ui and cache to per-engine resources
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 21 Dec 2017 22:05:30 +0900 |
parents | 32c278eb876f |
children | a33be093ec62 |
files | hgext/show.py mercurial/cmdutil.py mercurial/debugcommands.py mercurial/filemerge.py mercurial/formatter.py tests/test-command-template.t |
diffstat | 6 files changed, 33 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/hgext/show.py Thu Dec 21 21:29:06 2017 +0900 +++ b/hgext/show.py Thu Dec 21 22:05:30 2017 +0900 @@ -252,7 +252,9 @@ # our simplicity and the customizations required. # TODO use proper graph symbols from graphmod - shortesttmpl = formatter.maketemplater(ui, '{shortest(node, %d)}' % nodelen) + tres = formatter.templateresources(ui, repo) + shortesttmpl = formatter.maketemplater(ui, '{shortest(node, %d)}' % nodelen, + resources=tres) def shortest(ctx): return shortesttmpl.render({'ctx': ctx, 'node': ctx.hex()}) @@ -438,7 +440,10 @@ If we fail to do this, a value of e.g. ``10023`` could mean either revision 10023 or node ``10023abc...``. """ - tmpl = formatter.maketemplater(repo.ui, '{shortest(node, %d)}' % minlen) + tres = formatter.templateresources(repo.ui, repo) + tmpl = formatter.maketemplater(repo.ui, '{shortest(node, %d)}' % minlen, + resources=tres) + lens = [minlen] for rev in revs: ctx = repo[rev]
--- a/mercurial/cmdutil.py Thu Dec 21 21:29:06 2017 +0900 +++ b/mercurial/cmdutil.py Thu Dec 21 22:05:30 2017 +0900 @@ -1843,10 +1843,11 @@ diffopts = diffopts or {} changeset_printer.__init__(self, ui, repo, matchfn, diffopts, buffered) - self.t = formatter.loadtemplater(ui, tmplspec, + tres = formatter.templateresources(ui, repo) + self.t = formatter.loadtemplater(ui, tmplspec, resources=tres, cache=templatekw.defaulttempl) self._counter = itertools.count() - self.cache = {} + self.cache = tres['cache'] # shared with _graphnodeformatter() self._tref = tmplspec.ref self._parts = {'header': '', 'footer': '', @@ -1887,11 +1888,8 @@ props = props.copy() props.update(templatekw.keywords) props['ctx'] = ctx - props['repo'] = self.repo - props['ui'] = self.repo.ui props['index'] = index = next(self._counter) props['revcache'] = {'copies': copies} - props['cache'] = self.cache props = pycompat.strkwargs(props) # write separator, which wouldn't work well with the header part below @@ -2657,16 +2655,14 @@ return templatekw.showgraphnode # fast path for "{graphnode}" spec = templater.unquotestring(spec) - templ = formatter.maketemplater(ui, spec) - cache = {} + tres = formatter.templateresources(ui) if isinstance(displayer, changeset_templater): - cache = displayer.cache # reuse cache of slow templates + tres['cache'] = displayer.cache # reuse cache of slow templates + templ = formatter.maketemplater(ui, spec, resources=tres) props = templatekw.keywords.copy() - props['cache'] = cache def formatnode(repo, ctx): props['ctx'] = ctx props['repo'] = repo - props['ui'] = repo.ui props['revcache'] = {} return templ.render(props) return formatnode
--- a/mercurial/debugcommands.py Thu Dec 21 21:29:06 2017 +0900 +++ b/mercurial/debugcommands.py Thu Dec 21 22:05:30 2017 +0900 @@ -2361,8 +2361,8 @@ ui.note(("* expanded:\n"), templater.prettyformat(newtree), '\n') if revs is None: - t = formatter.maketemplater(ui, tmpl) - props['ui'] = ui + tres = formatter.templateresources(ui, repo) + t = formatter.maketemplater(ui, tmpl, resources=tres) ui.write(t.render(props)) else: displayer = cmdutil.makelogtemplater(ui, repo, tmpl)
--- a/mercurial/filemerge.py Thu Dec 21 21:29:06 2017 +0900 +++ b/mercurial/filemerge.py Thu Dec 21 22:05:30 2017 +0900 @@ -581,7 +581,8 @@ ui = repo.ui template = ui.config('ui', 'mergemarkertemplate') template = templater.unquotestring(template) - tmpl = formatter.maketemplater(ui, template) + tres = formatter.templateresources(ui, repo) + tmpl = formatter.maketemplater(ui, template, resources=tres) pad = max(len(l) for l in labels)
--- a/mercurial/formatter.py Thu Dec 21 21:29:06 2017 +0900 +++ b/mercurial/formatter.py Thu Dec 21 22:05:30 2017 +0900 @@ -363,11 +363,11 @@ self._out = out spec = lookuptemplate(ui, topic, opts.get('template', '')) self._tref = spec.ref - self._t = loadtemplater(ui, spec, cache=templatekw.defaulttempl) + self._t = loadtemplater(ui, spec, resources=templateresources(ui), + cache=templatekw.defaulttempl) self._parts = templatepartsmap(spec, self._t, ['docheader', 'docfooter', 'separator']) self._counter = itertools.count() - self._cache = {} # for templatekw/funcs to store reusable data self._renderitem('docheader', {}) def _showitem(self): @@ -395,7 +395,7 @@ props['repo'] = props['ctx'].repo() props['revcache'] = {} props = pycompat.strkwargs(props) - g = self._t(ref, ui=self._ui, cache=self._cache, **props) + g = self._t(ref, **props) self._out.write(templater.stringify(g)) def end(self): @@ -486,6 +486,15 @@ t.cache[''] = tmpl return t +def templateresources(ui, repo=None): + """Create a dict of template resources designed for the default templatekw + and function""" + return { + 'cache': {}, # for templatekw/funcs to store reusable data + 'repo': repo, + 'ui': ui, + } + def formatter(ui, out, topic, opts): template = opts.get("template", "") if template == "json":
--- a/tests/test-command-template.t Thu Dec 21 21:29:06 2017 +0900 +++ b/tests/test-command-template.t Thu Dec 21 22:05:30 2017 +0900 @@ -204,6 +204,10 @@ $ hg log -r 'wdir()' -T '{manifest}\n' +Internal resources shouldn't be exposed (issue5699): + + $ hg log -r. -T '{cache}{repo}{templ}{ui}' + Quoting for ui.logtemplate $ hg tip --config "ui.logtemplate={rev}\n"