Patchwork [STABLE] templater: enable alias predicates to be used in "revset()" function

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Aug. 23, 2014, 12:26 p.m.
Message ID <fb9c214a877be3c2350e.1408796803@feefifofum>
Download mbox | patch
Permalink /patch/5559/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - Aug. 23, 2014, 12:26 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1408796582 -32400
#      Sat Aug 23 21:23:02 2014 +0900
# Branch stable
# Node ID fb9c214a877be3c2350ed2a51222bce7751690c5
# Parent  510cafe720043af3d0c11b4b70345b63b54d17b1
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.
Matt Mackall - Aug. 27, 2014, 10:53 a.m.
On Sat, 2014-08-23 at 21:26 +0900, FUJIWARA Katsunori wrote:
> # HG changeset patch
> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
> # Date 1408796582 -32400
> #      Sat Aug 23 21:23:02 2014 +0900
> # Branch stable
> # Node ID fb9c214a877be3c2350ed2a51222bce7751690c5
> # Parent  510cafe720043af3d0c11b4b70345b63b54d17b1
> templater: enable alias predicates to be used in "revset()" function

Queued for stable, thanks.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -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
 
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
@@ -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