mercurial/debugcommands.py
changeset 30977 14794735faa8
parent 30976 db30c6bfeb70
child 30978 df73368c87c3
--- a/mercurial/debugcommands.py	Thu Feb 02 10:05:22 2017 +0100
+++ b/mercurial/debugcommands.py	Thu Feb 02 10:06:01 2017 +0100
@@ -40,6 +40,7 @@
     exchange,
     extensions,
     fileset,
+    formatter,
     hg,
     localrepo,
     lock as lockmod,
@@ -1949,6 +1950,57 @@
                     ui.write(node2str(node))
             ui.write('\n')
 
+@command('debugtemplate',
+    [('r', 'rev', [], _('apply template on changesets'), _('REV')),
+     ('D', 'define', [], _('define template keyword'), _('KEY=VALUE'))],
+    _('[-r REV]... [-D KEY=VALUE]... TEMPLATE'),
+    optionalrepo=True)
+def debugtemplate(ui, repo, tmpl, **opts):
+    """parse and apply a template
+
+    If -r/--rev is given, the template is processed as a log template and
+    applied to the given changesets. Otherwise, it is processed as a generic
+    template.
+
+    Use --verbose to print the parsed tree.
+    """
+    revs = None
+    if opts['rev']:
+        if repo is None:
+            raise error.RepoError(_('there is no Mercurial repository here '
+                                    '(.hg not found)'))
+        revs = scmutil.revrange(repo, opts['rev'])
+
+    props = {}
+    for d in opts['define']:
+        try:
+            k, v = (e.strip() for e in d.split('=', 1))
+            if not k:
+                raise ValueError
+            props[k] = v
+        except ValueError:
+            raise error.Abort(_('malformed keyword definition: %s') % d)
+
+    if ui.verbose:
+        aliases = ui.configitems('templatealias')
+        tree = templater.parse(tmpl)
+        ui.note(templater.prettyformat(tree), '\n')
+        newtree = templater.expandaliases(tree, aliases)
+        if newtree != tree:
+            ui.note(("* expanded:\n"), templater.prettyformat(newtree), '\n')
+
+    mapfile = None
+    if revs is None:
+        k = 'debugtemplate'
+        t = formatter.maketemplater(ui, k, tmpl)
+        ui.write(templater.stringify(t(k, **props)))
+    else:
+        displayer = cmdutil.changeset_templater(ui, repo, None, opts, tmpl,
+                                                mapfile, buffered=False)
+        for r in revs:
+            displayer.show(repo[r], **props)
+        displayer.close()
+
 @command('debugupgraderepo', [
     ('o', 'optimize', [], _('extra optimization to perform'), _('NAME')),
     ('', 'run', False, _('performs an upgrade')),