diff mercurial/formatter.py @ 43100:90b9a7e06c2c

formatter: parse name of built-in formatter templates in standard way This slightly makes it easier to add "-Tjson(...)" handling, which should be enabled only if the template specifier doesn't look like a literal template. In other words, it should be handled after "if '{' in tmpl". This makes "log -Tpickle" and "log -Tdebug" abort, which I think is better than just printing "picklepicklepickle...".
author Yuya Nishihara <yuya@tcha.org>
date Sat, 05 Oct 2019 23:20:35 -0400
parents f1c5358f0d65
children 1d12ae5096d1
line wrap: on
line diff
--- a/mercurial/formatter.py	Sat Oct 05 23:04:45 2019 -0400
+++ b/mercurial/formatter.py	Sat Oct 05 23:20:35 2019 -0400
@@ -531,6 +531,7 @@
     'tmpl' can be any of the following:
 
      - a literal template (e.g. '{rev}')
+     - a reference to built-in template (i.e. formatter)
      - a map-file name or path (e.g. 'changelog')
      - a reference to [templates] in config file
      - a path to raw template file
@@ -544,10 +545,17 @@
     available as well as aliases in [templatealias].
     """
 
+    if not tmpl:
+        return templatespec(None, None, None)
+
     # looks like a literal template?
     if b'{' in tmpl:
         return templatespec(b'', tmpl, None)
 
+    # a reference to built-in (formatter) template
+    if tmpl in {b'cbor', b'json', b'pickle', b'debug'}:
+        return templatespec(tmpl, None, None)
+
     # perhaps a stock style?
     if not os.path.split(tmpl)[0]:
         mapname = templater.templatepath(
@@ -712,17 +720,16 @@
 
 
 def formatter(ui, out, topic, opts):
-    template = opts.get(b"template", b"")
-    if template == b"cbor":
+    spec = lookuptemplate(ui, topic, opts.get(b'template', b''))
+    if spec.ref == b"cbor":
         return cborformatter(ui, out, topic, opts)
-    elif template == b"json":
+    elif spec.ref == b"json":
         return jsonformatter(ui, out, topic, opts)
-    elif template == b"pickle":
+    elif spec.ref == b"pickle":
         return pickleformatter(ui, out, topic, opts)
-    elif template == b"debug":
+    elif spec.ref == b"debug":
         return debugformatter(ui, out, topic, opts)
-    elif template != b"":
-        spec = lookuptemplate(ui, topic, opts.get(b'template', b''))
+    elif spec.ref or spec.tmpl or spec.mapfile:
         return templateformatter(ui, out, topic, opts, spec)
     # developer config: ui.formatdebug
     elif ui.configbool(b'ui', b'formatdebug'):