# HG changeset patch # User Alexander Plavin # Date 2013-09-22 09:52:18 # Node ID 0361163efbaf8b96fac8db712a72b9dfa916079e # Parent 186f54d40fdd59224c2893534a5715f843cb5f67 templater: support using templates with non-standard names from map file Allow to add arbitrarily-named entries to a template map file and then reference them, to make it possible to deduplicate and simplify templates code. diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -139,7 +139,12 @@ def runstring(context, mapping, data): def runsymbol(context, mapping, key): v = mapping.get(key) if v is None: - v = context._defaults.get(key, '') + v = context._defaults.get(key) + if v is None: + try: + v = context.process(key, mapping) + except TemplateNotFound: + v = '' if util.safehasattr(v, '__call__'): return v(**mapping) if isinstance(v, types.GeneratorType): @@ -449,6 +454,9 @@ def stylelist(): stylelist.append(split[1]) return ", ".join(sorted(stylelist)) +class TemplateNotFound(util.Abort): + pass + class templater(object): def __init__(self, mapfile, filters={}, defaults={}, cache={}, @@ -500,7 +508,8 @@ class templater(object): try: self.cache[t] = util.readfile(self.map[t][1]) except KeyError, inst: - raise util.Abort(_('"%s" not in template map') % inst.args[0]) + raise TemplateNotFound(_('"%s" not in template map') % + inst.args[0]) except IOError, inst: raise IOError(inst.args[0], _('template file %s: %s') % (self.map[t][1], inst.args[1])) diff --git a/tests/test-command-template.t b/tests/test-command-template.t --- a/tests/test-command-template.t +++ b/tests/test-command-template.t @@ -500,6 +500,28 @@ Include works: 1 0 +Missing non-standard names give no error (backward compatibility): + + $ echo "changeset = '{c}'" > t + $ hg log --style ./t + +Defining non-standard name works: + + $ cat < t + > changeset = '{c}' + > c = q + > EOF + $ hg log --style ./t + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 + ui.style works: $ echo '[ui]' > .hg/hgrc