diff --git a/hgext/show.py b/hgext/show.py index 32c278eb876f826b50457887daad72123ddace03_aGdleHQvc2hvdy5weQ==..f1c54d0033272bba950eb86c63c2896def29270a_aGdleHQvc2hvdy5weQ== 100644 --- a/hgext/show.py +++ b/hgext/show.py @@ -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] diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py index 32c278eb876f826b50457887daad72123ddace03_bWVyY3VyaWFsL2NtZHV0aWwucHk=..f1c54d0033272bba950eb86c63c2896def29270a_bWVyY3VyaWFsL2NtZHV0aWwucHk= 100644 --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -1843,6 +1843,7 @@ 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() @@ -1847,6 +1848,6 @@ 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,7 +1888,5 @@ 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} @@ -1892,6 +1891,5 @@ 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,6 +2655,5 @@ 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): @@ -2662,3 +2659,4 @@ 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() @@ -2664,5 +2662,4 @@ props = templatekw.keywords.copy() - props['cache'] = cache def formatnode(repo, ctx): props['ctx'] = ctx props['repo'] = repo @@ -2666,7 +2663,6 @@ def formatnode(repo, ctx): props['ctx'] = ctx props['repo'] = repo - props['ui'] = repo.ui props['revcache'] = {} return templ.render(props) return formatnode diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py index 32c278eb876f826b50457887daad72123ddace03_bWVyY3VyaWFsL2RlYnVnY29tbWFuZHMucHk=..f1c54d0033272bba950eb86c63c2896def29270a_bWVyY3VyaWFsL2RlYnVnY29tbWFuZHMucHk= 100644 --- a/mercurial/debugcommands.py +++ b/mercurial/debugcommands.py @@ -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) diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py index 32c278eb876f826b50457887daad72123ddace03_bWVyY3VyaWFsL2ZpbGVtZXJnZS5weQ==..f1c54d0033272bba950eb86c63c2896def29270a_bWVyY3VyaWFsL2ZpbGVtZXJnZS5weQ== 100644 --- a/mercurial/filemerge.py +++ b/mercurial/filemerge.py @@ -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) diff --git a/mercurial/formatter.py b/mercurial/formatter.py index 32c278eb876f826b50457887daad72123ddace03_bWVyY3VyaWFsL2Zvcm1hdHRlci5weQ==..f1c54d0033272bba950eb86c63c2896def29270a_bWVyY3VyaWFsL2Zvcm1hdHRlci5weQ== 100644 --- a/mercurial/formatter.py +++ b/mercurial/formatter.py @@ -363,7 +363,8 @@ 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() @@ -367,7 +368,6 @@ 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": diff --git a/tests/test-command-template.t b/tests/test-command-template.t index 32c278eb876f826b50457887daad72123ddace03_dGVzdHMvdGVzdC1jb21tYW5kLXRlbXBsYXRlLnQ=..f1c54d0033272bba950eb86c63c2896def29270a_dGVzdHMvdGVzdC1jb21tYW5kLXRlbXBsYXRlLnQ= 100644 --- a/tests/test-command-template.t +++ b/tests/test-command-template.t @@ -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"