comparison mercurial/templatekw.py @ 34581:ee0d74083a22

templater: store revisions as ints so min/max won't compare them as strings Because a template value has no explicit type (like ancient PHP), ifcontains() has to coerce the type of the needle. Before, it was always converted to a string, which meant any container type should be a list/dict of strings. This no longer works since we've introduced min/max functions. In order to work around the untyped nature of templater, this patch adds a type specifier to hybrid dict/list. It isn't named as "valuetype" since the _hybrid class can also wrap a dict.
author Yuya Nishihara <yuya@tcha.org>
date Tue, 19 Sep 2017 23:13:46 +0900
parents 0a0a72c043ac
children eb7fffdc6e5b
comparison
equal deleted inserted replaced
34580:7259f0ddfc0f 34581:ee0d74083a22
35 - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}" 35 - "{ifcontains(file, files, ...)}", "{ifcontains(key, extras, ...)}"
36 - "{get(extras, key)}" 36 - "{get(extras, key)}"
37 - "{files|json}" 37 - "{files|json}"
38 """ 38 """
39 39
40 def __init__(self, gen, values, makemap, joinfmt): 40 def __init__(self, gen, values, makemap, joinfmt, keytype=None):
41 if gen is not None: 41 if gen is not None:
42 self.gen = gen # generator or function returning generator 42 self.gen = gen # generator or function returning generator
43 self._values = values 43 self._values = values
44 self._makemap = makemap 44 self._makemap = makemap
45 self.joinfmt = joinfmt 45 self.joinfmt = joinfmt
46 self.keytype = keytype # hint for 'x in y' where type(x) is unresolved
46 def gen(self): 47 def gen(self):
47 """Default generator to stringify this as {join(self, ' ')}""" 48 """Default generator to stringify this as {join(self, ' ')}"""
48 for i, x in enumerate(self._values): 49 for i, x in enumerate(self._values):
49 if i > 0: 50 if i > 0:
50 yield ' ' 51 yield ' '
786 revision) nothing is shown.""" 787 revision) nothing is shown."""
787 args = pycompat.byteskwargs(args) 788 args = pycompat.byteskwargs(args)
788 repo = args['repo'] 789 repo = args['repo']
789 ctx = args['ctx'] 790 ctx = args['ctx']
790 pctxs = scmutil.meaningfulparents(repo, ctx) 791 pctxs = scmutil.meaningfulparents(repo, ctx)
791 # ifcontains() needs a list of str 792 prevs = [p.rev() for p in pctxs]
792 prevs = ["%d" % p.rev() for p in pctxs]
793 parents = [[('rev', p.rev()), 793 parents = [[('rev', p.rev()),
794 ('node', p.hex()), 794 ('node', p.hex()),
795 ('phase', p.phasestr())] 795 ('phase', p.phasestr())]
796 for p in pctxs] 796 for p in pctxs]
797 f = _showlist('parent', parents, args) 797 f = _showlist('parent', parents, args)
798 return _hybrid(f, prevs, lambda x: {'ctx': repo[int(x)], 'revcache': {}}, 798 return _hybrid(f, prevs, lambda x: {'ctx': repo[x], 'revcache': {}},
799 lambda x: scmutil.formatchangeid(repo[int(x)])) 799 lambda x: scmutil.formatchangeid(repo[x]), keytype=int)
800 800
801 @templatekeyword('phase') 801 @templatekeyword('phase')
802 def showphase(repo, ctx, templ, **args): 802 def showphase(repo, ctx, templ, **args):
803 """String. The changeset phase name.""" 803 """String. The changeset phase name."""
804 return ctx.phasestr() 804 return ctx.phasestr()
816 def showrevslist(name, revs, **args): 816 def showrevslist(name, revs, **args):
817 """helper to generate a list of revisions in which a mapped template will 817 """helper to generate a list of revisions in which a mapped template will
818 be evaluated""" 818 be evaluated"""
819 args = pycompat.byteskwargs(args) 819 args = pycompat.byteskwargs(args)
820 repo = args['ctx'].repo() 820 repo = args['ctx'].repo()
821 # ifcontains() needs a list of str 821 f = _showlist(name, ['%d' % r for r in revs], args)
822 revs = ["%d" % r for r in revs]
823 f = _showlist(name, revs, args)
824 return _hybrid(f, revs, 822 return _hybrid(f, revs,
825 lambda x: {name: x, 'ctx': repo[int(x)], 'revcache': {}}, 823 lambda x: {name: x, 'ctx': repo[x], 'revcache': {}},
826 pycompat.identity) 824 pycompat.identity, keytype=int)
827 825
828 @templatekeyword('subrepos') 826 @templatekeyword('subrepos')
829 def showsubrepos(**args): 827 def showsubrepos(**args):
830 """List of strings. Updated subrepositories in the changeset.""" 828 """List of strings. Updated subrepositories in the changeset."""
831 args = pycompat.byteskwargs(args) 829 args = pycompat.byteskwargs(args)