diff mercurial/revset.py @ 40931:e54bfde922f2

revset: move subscript relation functions to its own dict This will help adding more relation functions in extensions. We skip short names (that consist of one letter) while raising UnknownIdentifier because such names cannot be suggested anyway: the similarity cutoff in dispatch._getsimilar() is currently 0.6.
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 13 Dec 2018 17:18:57 +0800
parents 7ed611c60168
children 8aca89a694d4
line wrap: on
line diff
--- a/mercurial/revset.py	Thu Dec 13 00:18:47 2018 -0500
+++ b/mercurial/revset.py	Thu Dec 13 17:18:57 2018 +0800
@@ -218,6 +218,15 @@
 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 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.
@@ -225,17 +234,11 @@
     rel = getsymbol(y)
     n = getinteger(z, _("relation subscript must be an integer"))
 
-    # TODO: perhaps this should be a table of relation functions
-    if rel in ('g', 'generations'):
-        # 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)
+    if rel in subscriptrelations:
+        return subscriptrelations[rel](repo, subset, x, rel, n, order)
 
-    raise error.UnknownIdentifier(rel, ['generations'])
+    relnames = [r for r in subscriptrelations.keys() if len(r) > 1]
+    raise error.UnknownIdentifier(rel, relnames)
 
 def subscriptset(repo, subset, x, y, order):
     raise error.ParseError(_("can't use a subscript in this context"))
@@ -2215,6 +2218,11 @@
     "parentpost": parentpost,
 }
 
+subscriptrelations = {
+    "g": generationsrel,
+    "generations": generationsrel,
+}
+
 def lookupfn(repo):
     return lambda symbol: scmutil.isrevsymbol(repo, symbol)