# HG changeset patch # User Matt Mackall # Date 2013-04-16 14:44:29 # Node ID 3cdb6f2f6789f1f8a1767eb3b5cff2d8c440e290 # Parent 257afe5489d4a6ebca6a9e13bba1b69a9289b89d templatekw: add default styles for hybrid types (issue3887) This allows elements like file_copies to be printed as 'name (source)' when used with join. diff --git a/mercurial/templatekw.py b/mercurial/templatekw.py --- a/mercurial/templatekw.py +++ b/mercurial/templatekw.py @@ -14,9 +14,13 @@ import hbisect # "{files % '{file}\n'}" (hgweb-style with inlining and function support) class _hybrid(object): - def __init__(self, gen, values): + def __init__(self, gen, values, joinfmt=None): self.gen = gen self.values = values + if joinfmt: + self.joinfmt = joinfmt + else: + self.joinfmt = lambda x: x.values()[0] def __iter__(self): return self.gen def __call__(self): @@ -245,7 +249,7 @@ def showfilecopies(**args): c = [{'name': x[0], 'source': x[1]} for x in copies] f = _showlist('file_copy', c, plural='file_copies', **args) - return _hybrid(f, c) + return _hybrid(f, c, lambda x: '%s (%s)' % (x['name'], x['source'])) # showfilecopiesswitch() displays file copies only if copy records are # provided before calling the templater, usually with a --copies @@ -257,7 +261,7 @@ def showfilecopiesswitch(**args): copies = args['revcache'].get('copies') or [] c = [{'name': x[0], 'source': x[1]} for x in copies] f = _showlist('file_copy', c, plural='file_copies', **args) - return _hybrid(f, c) + return _hybrid(f, c, lambda x: '%s (%s)' % (x['name'], x['source'])) def showfiledels(**args): """:file_dels: List of strings. Files removed by this changeset.""" diff --git a/mercurial/templater.py b/mercurial/templater.py --- a/mercurial/templater.py +++ b/mercurial/templater.py @@ -228,7 +228,8 @@ def join(context, mapping, args): joinset = args[0][0](context, mapping, args[0][1]) if util.safehasattr(joinset, '__call__'): - joinset = [x.values()[0] for x in joinset()] + jf = joinset.joinfmt + joinset = [jf(x) for x in joinset()] joiner = " " if len(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 @@ -43,6 +43,8 @@ Second branch starting at nullrev: $ hg mv second fourth $ hg commit -m third -d "2020-01-01 10:01" + $ hg log --template '{join(file_copies, ",\n")}\n' -r . + fourth (second) $ hg log --template '{file_copies % "{source} -> {name}\n"}' -r . second -> fourth