changeset 44710:eca82eb9d777

revset: implement a simple 'foo#generations' expression The result of this expression is equivalent to 'foo#g[:]': all reachable ancestors and descendants of a revset foo. While not very useful functionality on its own, its implementation can be tested and revset.relations dict can be easily used by extensions to add other non-subscript relations. Differential Revision: https://phab.mercurial-scm.org/D8394
author Anton Shestakov <av6@dwimlabs.net>
date Fri, 10 Apr 2020 22:23:44 +0800
parents 8859de3e83dc
children 637eb7f7559b
files mercurial/revset.py tests/test-revset.t
diffstat 2 files changed, 49 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revset.py	Fri Apr 10 22:22:09 2020 +0800
+++ b/mercurial/revset.py	Fri Apr 10 22:23:44 2020 +0800
@@ -247,7 +247,15 @@
 
 
 def relationset(repo, subset, x, y, order):
-    raise error.ParseError(_(b"can't use a relation in this context"))
+    # this is pretty basic implementation of 'x#y' operator, still
+    # experimental so undocumented. see the wiki for further ideas.
+    # https://www.mercurial-scm.org/wiki/RevsetOperatorPlan
+    rel = getsymbol(y)
+    if rel in relations:
+        return relations[rel](repo, subset, x, rel, order)
+
+    relnames = [r for r in relations.keys() if len(r) > 1]
+    raise error.UnknownIdentifier(rel, relnames)
 
 
 def _splitrange(a, b):
@@ -281,6 +289,11 @@
     return ancdepths, descdepths
 
 
+def generationsrel(repo, subset, x, rel, order):
+    z = (b'rangeall', None)
+    return generationssubrel(repo, subset, x, rel, z, order)
+
+
 def generationssubrel(repo, subset, x, rel, z, order):
     # TODO: rewrite tests, and drop startdepth argument from ancestors() and
     # descendants() predicates
@@ -2649,6 +2662,11 @@
     b"smartset": rawsmartset,
 }
 
+relations = {
+    b"g": generationsrel,
+    b"generations": generationsrel,
+}
+
 subscriptrelations = {
     b"g": generationssubrel,
     b"generations": generationssubrel,
--- a/tests/test-revset.t	Fri Apr 10 22:22:09 2020 +0800
+++ b/tests/test-revset.t	Fri Apr 10 22:23:44 2020 +0800
@@ -1274,6 +1274,36 @@
   5
   7
 
+test ancestors/descendants relation:
+
+  $ log 'tip#generations'
+  0
+  1
+  2
+  4
+  8
+  9
+
+  $ log '3#g'
+  0
+  1
+  3
+  5
+  6
+  7
+
+  $ hg debugrevspec -p parsed 'tip#g'
+  * parsed:
+  (relation
+    (symbol 'tip')
+    (symbol 'g'))
+  0
+  1
+  2
+  4
+  8
+  9
+
 test ancestors/descendants relation subscript:
 
   $ log 'tip#generations[0]'