diff mercurial/commands.py @ 28548:b7a31068cc80

templater: add debugtemplate command This is useful for debugging template parsing. Several tests are ported to this command.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 14 Feb 2016 01:06:12 +0900
parents e0d19d955608
children 6433da9c96a9
line wrap: on
line diff
--- a/mercurial/commands.py	Sat Feb 13 23:20:47 2016 +0900
+++ b/mercurial/commands.py	Sun Feb 14 01:06:12 2016 +0900
@@ -68,6 +68,7 @@
     sshserver,
     streamclone,
     templatekw,
+    templater,
     treediscovery,
     ui as uimod,
     util,
@@ -2757,7 +2758,6 @@
     fm.condwrite(err, 'extensionserror', " %s\n", err)
 
     # templates
-    from . import templater
     p = templater.templatepaths()
     fm.write('templatedirs', 'checking templates (%s)...\n', ' '.join(p))
     fm.condwrite(not p, '', _(" no template directories found\n"))
@@ -3592,6 +3592,54 @@
                     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:
+        tree = templater.parse(tmpl)
+        ui.note(templater.prettyformat(tree), '\n')
+
+    mapfile = None
+    if revs is None:
+        k = 'debugtemplate'
+        t = templater.templater(mapfile)
+        t.cache[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('debugwalk', walkopts, _('[OPTION]... [FILE]...'), inferrepo=True)
 def debugwalk(ui, repo, *pats, **opts):
     """show how files match on given patterns"""