templates: define a {onelinesummary} keyword
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 12 Nov 2020 14:07:34 -0800
changeset 45868 f67741e8264b
parent 45867 774b1c0f2507
child 45869 63edc384d3b7
templates: define a {onelinesummary} keyword It is sometimes useful to be able to use the configured `command-template.oneline-summary` in higher-level templates. For example, I would like to use it in an internal template that lists commits in a "review unit" (kind of a pull request). This patch adds support for that. We may want to define a way of formatting a context using a command-specific override (from `command-templates.oneline-summary.<command>`), but that will have to be a template function instead. I don't plan to do that, but I'm mentioning it now in case reviewers would prefer that we use a no-arg function (i.e. `{onelinesummary()}`) already today to prepare for that. Differential Revision: https://phab.mercurial-scm.org/D9314
mercurial/templatekw.py
tests/test-template-keywords.t
--- a/mercurial/templatekw.py	Fri Oct 30 12:46:38 2020 -0700
+++ b/mercurial/templatekw.py	Thu Nov 12 14:07:34 2020 -0800
@@ -634,6 +634,19 @@
     return b''
 
 
+@templatekeyword(b'onelinesummary', requires={b'ui', b'ctx'})
+def showonelinesummary(context, mapping):
+    """String. A one-line summary for the ctx (not including trailing newline).
+    The default template be overridden in command-templates.oneline-summary."""
+    # Avoid cycle:
+    # mercurial.cmdutil -> mercurial.templatekw -> mercurial.cmdutil
+    from . import cmdutil
+
+    ui = context.resource(mapping, b'ui')
+    ctx = context.resource(mapping, b'ctx')
+    return cmdutil.format_changeset_summary(ui, ctx)
+
+
 @templatekeyword(b'path', requires={b'fctx'})
 def showpath(context, mapping):
     """String. Repository-absolute path of the current file. (EXPERIMENTAL)"""
--- a/tests/test-template-keywords.t	Fri Oct 30 12:46:38 2020 -0700
+++ b/tests/test-template-keywords.t	Thu Nov 12 14:07:34 2020 -0800
@@ -114,6 +114,30 @@
   |
   o  0 (public): -1 (public) -1 (public)
   
+Test {onelinesummary}
+
+  $ hg log -G -T '{onelinesummary}'
+  @  8:95c24699272e tip "third"
+  |
+  o  7:29114dbae42b "second"
+  
+  o    6:d41e714fe50d "merge"
+  |\
+  | o  5:13207e5a10d9 "new head"
+  | |
+  o |  4:bbe44766e73d "new branch"
+  |/
+  o  3:10e46f2dcbf4 "no user, no domain"
+  |
+  o  2:97054abb4ab8 "no person"
+  |
+  o  1:b608e9d1a3f0 "other 1"
+  |
+  o  0:1e4e1b8f71e0 "line 1"
+  
+  $ hg log -T '{onelinesummary}' -r 0 \
+  > --config command-templates.oneline-summary='{rev} - {desc}'
+  0 - line 1 (no-eol)
 
 Keys work: