Patchwork [2,of,6] show: construct changeset templater during dispatch

login
register
mail settings
Submitter Gregory Szorc
Date June 24, 2017, 10:13 p.m.
Message ID <9aad139f6e8778630347.1498342428@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/21678/
State Accepted
Headers show

Comments

Gregory Szorc - June 24, 2017, 10:13 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1498333645 25200
#      Sat Jun 24 12:47:25 2017 -0700
# Node ID 9aad139f6e8778630347ac660ae93b9ad2cd2fe7
# Parent  a9a61dd1c43ba19856bd6e4943676663d56a7f00
show: construct changeset templater during dispatch

Previously, we constructed a formatter from a specific template
topic. Then from show() we reached into the internals of the
formatter to resolve a template string to be used to construct
a changeset templater.

A downside to this approach was it limited us to having the
entire template defined in a single entry in the map file. You
couldn't reference other entries in the map file and this would
lead to long templates and redundancy in the map file.

This commit teaches @showview how to instantiate a changeset
templater so we can construct a templater with full access to
the map file. To prove it works, we've split "showwork" into
components.

Patch

diff --git a/hgext/show.py b/hgext/show.py
--- a/hgext/show.py
+++ b/hgext/show.py
@@ -43,7 +43,7 @@  class showcmdfunc(registrar._funcregistr
     # Used by _formatdoc().
     _docformat = '%s -- %s'
 
-    def _extrasetup(self, name, func, fmtopic=None):
+    def _extrasetup(self, name, func, fmtopic=None, csettopic=None):
         """Called with decorator arguments to register a show view.
 
         ``name`` is the sub-command name.
@@ -52,8 +52,16 @@  class showcmdfunc(registrar._funcregistr
 
         ``fmtopic`` is the topic in the style that will be rendered for
         this view.
+
+        ``csettopic`` is the topic in the style to be used for a changeset
+        printer.
+
+        If ``fmtopic`` is specified, the view function will receive a
+        formatter instance. If ``csettopic`` is specified, the view
+        function will receive a changeset printer.
         """
         func._fmtopic = fmtopic
+        func._csettopic = csettopic
 
 showview = showcmdfunc()
 
@@ -109,11 +117,21 @@  def show(ui, repo, view=None, template=N
                           hint=_('run "hg show" to see available views'))
 
     template = template or 'show'
-    fmtopic = 'show%s' % views[view]._fmtopic
 
+    fn = views[view]
     ui.pager('show')
-    with ui.formatter(fmtopic, {'template': template}) as fm:
-        return views[view](ui, repo, fm)
+
+    if fn._fmtopic:
+        fmtopic = 'show%s' % fn._fmtopic
+        with ui.formatter(fmtopic, {'template': template}) as fm:
+            return fn(ui, repo, fm)
+    elif fn._csettopic:
+        ref = 'show%s' % fn._csettopic
+        spec = formatter.lookuptemplate(ui, ref, template)
+        displayer = cmdutil.changeset_templater(ui, repo, spec, buffered=True)
+        return fn(ui, repo, displayer)
+    else:
+        return fn(ui, repo)
 
 @showview('bookmarks', fmtopic='bookmarks')
 def showbookmarks(ui, repo, fm):
@@ -189,15 +207,13 @@  def underwayrevset(repo, subset, x):
 
     return subset & relevant
 
-@showview('work', fmtopic='work')
-def showwork(ui, repo, fm):
+@showview('work', csettopic='work')
+def showwork(ui, repo, displayer):
     """changesets that aren't finished"""
     # TODO support date-based limiting when calling revset.
     revs = repo.revs('sort(_underway(), topo)')
 
     revdag = graphmod.dagwalker(repo, revs)
-    tmpl = fm._t.load(fm._topic)
-    displayer = cmdutil.makelogtemplater(ui, repo, tmpl, buffered=True)
 
     ui.setconfig('experimental', 'graphshorten', True)
     cmdutil.displaygraph(ui, repo, revdag, displayer, graphmod.asciiedges)
diff --git a/mercurial/templates/map-cmdline.show b/mercurial/templates/map-cmdline.show
--- a/mercurial/templates/map-cmdline.show
+++ b/mercurial/templates/map-cmdline.show
@@ -1,9 +1,12 @@ 
 # TODO there are a few deficiencies in this file:
-# * Due to the way the file is loaded, references to other entities in the
-#   template doesn't work. That requires us to inline.
 # * The "namespace" of the labels needs to be worked out. We currently
 #   piggyback on existing values so color works.
 # * Obsolescence isn't considered for node labels. See _cset_labels in
 #   map-cmdline.default.
 showbookmarks = '{if(active, "*", " ")} {pad(bookmark, longestbookmarklen + 4)}{shortest(node, 5)}\n'
-showwork = '{label("log.changeset changeset.{phase}", shortest(node, 5))}{if(branches, " ({label("log.branch", branch)})")}{if(bookmarks, " ({label("log.bookmarks", bookmarks)})")} {label("log.description", desc|firstline)}'
+
+showwork = '{cset_shortnode}{cset_names} {cset_shortdesc}'
+
+cset_shortnode = '{label("log.changeset changeset.{phase}", shortest(node, 5))}'
+cset_names = '{if(branches, " ({label("log.branch", branch)})")}{if(bookmarks, " ({label("log.bookmarks", bookmarks)})")}'
+cset_shortdesc = '{label("log.description", desc|firstline)}'