Writing a template which displays only relevant parents

Dr Rainer Woitok rainer.woitok at gmail.com
Sun Feb 7 15:38:06 UTC 2016


Yuya,

On Friday, 2016-02-05 23:04:14 +0900, you wrote:

> ...
> You need to pass a string query. "hg help template" has some examples.
> 
>   {ifeq(count(revset('{p1rev}:{rev}')), 2, '*', '{p1rev}:{p1node|short}')}
> or
>   {ifeq(count(revset('%d:%d', p1rev, rev)), 2, '*', '{p1rev}:{p1node|short}')}

Thanks for pointing this out.   I missed these  two examples in "hg help
template".  Works like a charm now :-) at least result-wise.  But effic-
iency-wise?  Consider what we are doing here:  we first compute a revset
(which might be huge)  and then  we count its members  (which might take
some time,  because the  revset might be huge)  just to  finally compare
this count to 2.   But all we really wanted  is a trivial  piece of code
along the lines of

   {ifeq(sum(rev, -1), p1rev, then, else)}

So all I am saying is give math a chance! :-)

I have a dream:

What about a template specific function "py(expr)" which returns the re-
sult of an arbitrary Python expression?

   - This would provide us with a modern, feature rich,  and well docum-
     ented language to write more complex templates in.

   - Since a Python expression  may contain  calls to  Python functions,
     user defined template specific  Python functions could  be provided
     in a "[python]" section within file ".hgrc".

   - This would immediately solve the problem  of calling a user defined
     template from  within a  user defined template  (see the discussion
     below) because this would boil down to a user defined Python funct-
     ion calling another user defined Python function.

   - It would allow us to write templates despite of their complexity in
     an easily comprehendable way,  thus making the  maintenance of com-
     plex templates more easy.

   - It would get us  rid of some  template specific build-in  functions
     and the need to thoroughly document them (although "revset()" would
     still have to be provided and documented).

   - And it would allow us to do any kind of math in a template ... :-)

> ...
> You can insert whitespaces in non-string part.
> 
>   x = blah blah{
>       func(x,
>       y)}

Did you test this with a real template?  I'm on Mercurial 3.7-rc+epsilon
and I didn't get this to work.  What did work, however, was

   x = 'blah blah{
       func(x,
       y)}'

that is, to quote the whole template.  But since template syntax requir-
es quoted strings embedded in quoted strings  embedded in quoted strings
embedded ...  and so on,  and since  there are only  two types of string
quotes, this now requires still more backslashes :-(

> >    - Is there a way to "call" a user defined template from within anoth-
> >      er user defined template?
> > ...
> 
> Not yet, but I'm thinking of it.

Is there already some time-line or version-line for this new feature?

Sincerely,
  Rainer



More information about the Mercurial mailing list