Patchwork [v2] templates: limit -Tlist to supported templates

login
register
mail settings
Submitter timeless@mozdev.org
Date Dec. 15, 2015, 4:51 p.m.
Message ID <df108fa16c4f680cd402.1450198272@waste.org>
Download mbox | patch
Permalink /patch/12056/
State Changes Requested
Delegated to: Yuya Nishihara
Headers show

Comments

timeless@mozdev.org - Dec. 15, 2015, 4:51 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1450065308 0
#      Mon Dec 14 03:55:08 2015 +0000
# Node ID df108fa16c4f680cd402f983d5f0a1830f00c629
# Parent  944af8e2eb4cddf96ba5b8a96854528b40979715
templates: limit -Tlist to supported templates
Yuya Nishihara - Dec. 16, 2015, 2:41 p.m.
On Tue, 15 Dec 2015 10:51:12 -0600, timeless wrote:
> # HG changeset patch
> # User timeless <timeless@mozdev.org>
> # Date 1450065308 0
> #      Mon Dec 14 03:55:08 2015 +0000
> # Node ID df108fa16c4f680cd402f983d5f0a1830f00c629
> # Parent  944af8e2eb4cddf96ba5b8a96854528b40979715
> templates: limit -Tlist to supported templates
> 
> diff --git a/mercurial/formatter.py b/mercurial/formatter.py
> --- a/mercurial/formatter.py
> +++ b/mercurial/formatter.py
> @@ -178,7 +178,7 @@
>          return tmpl, None
>  
>      if tmpl == 'list':
> -        ui.write(_("available styles: %s\n") % templater.stylelist())
> +        ui.write(_("available styles: %s\n") % templater.stylelist(topic))
>          raise error.Abort(_("specify a template"))
>  
>      # perhaps it's a path to a map or a template
> diff --git a/mercurial/templater.py b/mercurial/templater.py
> --- a/mercurial/templater.py
> +++ b/mercurial/templater.py
> @@ -828,16 +828,20 @@
>  
>  engines = {'default': engine}
>  
> -def stylelist():
> +def stylelist(topic=None):
>      paths = templatepaths()
>      if not paths:
>          return _('no templates found, try `hg debuginstall` for more info')
>      dirlist =  os.listdir(paths[0])
>      stylelist = []
>      for file in dirlist:
> +        # FIXME if file is actually a directory, this will break
> +        # resulting in an exception instead of a list.

Good point. The other possible exceptions are ParseError and SyntaxError.
(templater abuses SyntaxError to tell parsing error of mapfile.)

>          split = file.split(".")
>          if split[0] == "map-cmdline":
> -            stylelist.append(split[1])
> +            t = templater(templatepath(file), liststyles=False)
> +            if topic is None or topic in t:
> +                stylelist.append(split[1])
>      return ", ".join(sorted(stylelist))
>  
>  class TemplateNotFound(error.Abort):
> @@ -846,7 +850,7 @@
>  class templater(object):
>  
>      def __init__(self, mapfile, filters=None, defaults=None, cache=None,
> -                 minchunk=1024, maxchunk=65536):
> +                 minchunk=1024, maxchunk=65536, liststyles=True):
>          '''set up template engine.
>          mapfile is name of file to read map definitions from.
>          filters is dict of functions. each transforms a value into another.
> @@ -873,8 +877,11 @@
>          if not mapfile:
>              return
>          if not os.path.exists(mapfile):
> +            hint = None
> +            if liststyles:
> +                hint=_("available styles: %s") % stylelist()
>              raise error.Abort(_("style '%s' not found") % mapfile,
> -                             hint=_("available styles: %s") % stylelist())
> +                             hint=hint)

Do we need this hack? stylelist() won't create a templater for missing mapfile
unless it is removed after templatepath(file) called.

Other than this, the patch looks good to me.

Patch

diff --git a/mercurial/formatter.py b/mercurial/formatter.py
--- a/mercurial/formatter.py
+++ b/mercurial/formatter.py
@@ -178,7 +178,7 @@ 
         return tmpl, None
 
     if tmpl == 'list':
-        ui.write(_("available styles: %s\n") % templater.stylelist())
+        ui.write(_("available styles: %s\n") % templater.stylelist(topic))
         raise error.Abort(_("specify a template"))
 
     # perhaps it's a path to a map or a template
diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -828,16 +828,20 @@ 
 
 engines = {'default': engine}
 
-def stylelist():
+def stylelist(topic=None):
     paths = templatepaths()
     if not paths:
         return _('no templates found, try `hg debuginstall` for more info')
     dirlist =  os.listdir(paths[0])
     stylelist = []
     for file in dirlist:
+        # FIXME if file is actually a directory, this will break
+        # resulting in an exception instead of a list.
         split = file.split(".")
         if split[0] == "map-cmdline":
-            stylelist.append(split[1])
+            t = templater(templatepath(file), liststyles=False)
+            if topic is None or topic in t:
+                stylelist.append(split[1])
     return ", ".join(sorted(stylelist))
 
 class TemplateNotFound(error.Abort):
@@ -846,7 +850,7 @@ 
 class templater(object):
 
     def __init__(self, mapfile, filters=None, defaults=None, cache=None,
-                 minchunk=1024, maxchunk=65536):
+                 minchunk=1024, maxchunk=65536, liststyles=True):
         '''set up template engine.
         mapfile is name of file to read map definitions from.
         filters is dict of functions. each transforms a value into another.
@@ -873,8 +877,11 @@ 
         if not mapfile:
             return
         if not os.path.exists(mapfile):
+            hint = None
+            if liststyles:
+                hint=_("available styles: %s") % stylelist()
             raise error.Abort(_("style '%s' not found") % mapfile,
-                             hint=_("available styles: %s") % stylelist())
+                             hint=hint)
 
         conf = config.config(includepaths=templatepaths())
         conf.read(mapfile)
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
@@ -997,6 +997,13 @@ 
   abort: specify a template
   [255]
 
+Only supported styles (this test will change with time):
+
+  $ hg manifest -T list
+  available styles: bisect, default, phases, status
+  abort: specify a template
+  [255]
+
 Error if style missing key:
 
   $ echo 'q = q' > t