changeset 22304:5678b0e3608f

templater: enable alias predicates to be used in "revset()" function Before this patch, predicates defined in "[revsetalias]" can't be used in the query specified to template function "revset()", because: - "revset()" uses "localrepository.revs()" to get query result, but - "localrepository.revs()" passes "None" as "ui" to "revset.match()", then - "revset.match()" can't recognize any alias predicates To enable alias predicates to be used in "revset()" function, this patch invokes "revset.match()" directly with "repo.ui". This patch doesn't make "localrepository.revs()" pass "self.ui" to "revset.match()", because this may be intentional implementation to prevent alias predicates from shadowing built-in ones and breaking functions internally using "localrepository.revs()". Even if it isn't intentional one, the check for shadowing should be implemented (maybe on default branch) before fixing it for safety.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sat, 23 Aug 2014 21:23:02 +0900
parents 35ab037de989
children bdc0e04df243
files mercurial/templater.py tests/test-command-template.t
diffstat 2 files changed, 16 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Tue Aug 26 22:03:32 2014 +0200
+++ b/mercurial/templater.py	Sat Aug 23 21:23:02 2014 +0900
@@ -8,6 +8,7 @@
 from i18n import _
 import sys, os, re
 import util, config, templatefilters, templatekw, parser, error
+import revset as revsetmod
 import types
 import minirst
 
@@ -370,16 +371,20 @@
     ctx = mapping['ctx']
     repo = ctx._repo
 
+    def query(expr):
+        m = revsetmod.match(repo.ui, expr)
+        return m(repo, revsetmod.spanset(repo))
+
     if len(args) > 1:
         formatargs = list([a[0](context, mapping, a[1]) for a in args[1:]])
-        revs = repo.revs(raw, *formatargs)
+        revs = query(revsetmod.formatspec(raw, *formatargs))
         revs = list([str(r) for r in revs])
     else:
         revsetcache = mapping['cache'].setdefault("revsetcache", {})
         if raw in revsetcache:
             revs = revsetcache[raw]
         else:
-            revs = repo.revs(raw)
+            revs = query(raw)
             revs = list([str(r) for r in revs])
             revsetcache[raw] = revs
 
--- a/tests/test-command-template.t	Tue Aug 26 22:03:32 2014 +0200
+++ b/tests/test-command-template.t	Sat Aug 23 21:23:02 2014 +0900
@@ -1834,6 +1834,15 @@
   1 Parents: 0
   0 Parents: 
 
+  $ cat >> .hg/hgrc <<EOF
+  > [revsetalias]
+  > myparents(\$1) = parents(\$1)
+  > EOF
+  $ hg log --template '{rev} Parents: {revset("myparents(%s)", rev)}\n'
+  2 Parents: 1
+  1 Parents: 0
+  0 Parents: 
+
   $ hg log --template 'Rev: {rev}\n{revset("::%s", rev) % "Ancestor: {revision}\n"}\n'
   Rev: 2
   Ancestor: 0