Patchwork [V4] revset: support ranges in #generations relation

login
register
mail settings
Submitter Anton Shestakov
Date Jan. 17, 2019, 8:09 a.m.
Message ID <3238fcf91b3a8d234287.1547712560@neuro>
Download mbox | patch
Permalink /patch/37854/
State Superseded
Headers show

Comments

Anton Shestakov - Jan. 17, 2019, 8:09 a.m.
# HG changeset patch
# User Anton Shestakov <av6@dwimlabs.net>
# Date 1547564229 -28800
#      Tue Jan 15 22:57:09 2019 +0800
# Node ID 3238fcf91b3a8d234287054b79920743e44a3058
# Parent  8aca89a694d4bd7d25877b3652fb83e187ea1802
revset: support ranges in #generations relation

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -225,24 +225,58 @@  def notset(repo, subset, x, order):
 def relationset(repo, subset, x, y, order):
     raise error.ParseError(_("can't use a relation in this context"))
 
-def generationsrel(repo, subset, x, rel, n, order):
-    # TODO: support range, rewrite tests, and drop startdepth argument
-    # from ancestors() and descendants() predicates
-    if n <= 0:
-        n = -n
-        return _ancestors(repo, subset, x, startdepth=n, stopdepth=n + 1)
-    else:
-        return _descendants(repo, subset, x, startdepth=n, stopdepth=n + 1)
+def _generationsancestors(repo, subset, x, a, b):
+    if a is not None and a >= 0:
+        return baseset()
+
+    if a is None and (b is None or b >= 0):
+        return _ancestors(repo, subset, x, startdepth=1, stopdepth=None)
+    elif a is None and (b is not None or b < 0):
+        return _ancestors(repo, subset, x, startdepth=-b, stopdepth=None)
+    elif a is not None and (b is None or b >= 0):
+        return _ancestors(repo, subset, x, startdepth=1, stopdepth=-a + 1)
+    elif a is not None and (b is not None or b < 0):
+        return _ancestors(repo, subset, x, startdepth=-b, stopdepth=-a + 1)
+
+def _generationsdescendants(repo, subset, x, a, b):
+    if b is not None and b < 0:
+        return baseset()
+
+    if (a is None or a < 0) and b is None:
+        return _descendants(repo, subset, x, startdepth=0, stopdepth=None)
+    elif (a is None or a < 0) and b is not None:
+        return _descendants(repo, subset, x, startdepth=0, stopdepth=b + 1)
+    elif (a is not None or a >= 0) and b is None:
+        return _descendants(repo, subset, x, startdepth=a, stopdepth=None)
+    elif (a is not None or a >= 0) and b is not None:
+        return _descendants(repo, subset, x, startdepth=a, stopdepth=b + 1)
+
+def generationsrel(repo, subset, x, rel, a, b, order):
+    # TODO: rewrite tests, and drop startdepth argument from ancestors() and
+    # descendants() predicates
+    return (_generationsancestors(repo, subset, x, a, b)
+            + _generationsdescendants(repo, subset, x, a, b))
 
 def relsubscriptset(repo, subset, x, y, z, order):
     # this is pretty basic implementation of 'x#y[z]' operator, still
     # experimental so undocumented. see the wiki for further ideas.
     # https://www.mercurial-scm.org/wiki/RevsetOperatorPlan
     rel = getsymbol(y)
-    n = getinteger(z, _("relation subscript must be an integer"))
+    try:
+        a, b = getrange(z, '')
+    except error.ParseError:
+        a = getinteger(z, _("relation subscript must be an integer"))
+        b = a
+    else:
+        def getbound(i):
+            if i is None:
+                return None
+            msg = _("relation subscript bounds must be integers")
+            return getinteger(i, msg)
+        a, b = [getbound(i) for i in (a, b)]
 
     if rel in subscriptrelations:
-        return subscriptrelations[rel](repo, subset, x, rel, n, order)
+        return subscriptrelations[rel](repo, subset, x, rel, a, b, order)
 
     relnames = [r for r in subscriptrelations.keys() if len(r) > 1]
     raise error.UnknownIdentifier(rel, relnames)
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -648,6 +648,9 @@  parse errors of relation, subscript and 
   $ hg debugrevspec '.#generations[1-2]'
   hg: parse error: relation subscript must be an integer
   [255]
+  $ hg debugrevspec '.#generations[foo:bar]'
+  hg: parse error: relation subscript bounds must be integers
+  [255]
 
 suggested relations
 
@@ -1274,6 +1277,30 @@  test ancestors/descendants relation subs
   $ log '.#g[(-1)]'
   8
 
+  $ log '6#generations[0:1]'
+  6
+  7
+  $ log '6#generations[-1:1]'
+  4
+  5
+  6
+  7
+  $ log '6#generations[0:]'
+  6
+  7
+  $ log '5#generations[:0]'
+  0
+  1
+  3
+  5
+  $ log '3#generations[:]'
+  0
+  1
+  3
+  5
+  6
+  7
+
   $ hg debugrevspec -p parsed 'roots(:)#g[2]'
   * parsed:
   (relsubscript