Patchwork revset: move subscript relation functions to its own dict

login
register
mail settings
Submitter Anton Shestakov
Date Dec. 13, 2018, 11:03 a.m.
Message ID <ae0a18541e4354201303.1544699002@neuro>
Download mbox | patch
Permalink /patch/37130/
State Accepted
Headers show

Comments

Anton Shestakov - Dec. 13, 2018, 11:03 a.m.
# HG changeset patch
# User Anton Shestakov <av6@dwimlabs.net>
# Date 1544692737 -28800
#      Thu Dec 13 17:18:57 2018 +0800
# Node ID ae0a18541e4354201303032cd37ccc819d64c5a0
# Parent  008f3491dc5377e9e6f210e0a3f161323049db5d
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.
Yuya Nishihara - Dec. 13, 2018, 11:25 a.m.
On Thu, 13 Dec 2018 19:03:22 +0800, Anton Shestakov wrote:
> # HG changeset patch
> # User Anton Shestakov <av6@dwimlabs.net>
> # Date 1544692737 -28800
> #      Thu Dec 13 17:18:57 2018 +0800
> # Node ID ae0a18541e4354201303032cd37ccc819d64c5a0
> # Parent  008f3491dc5377e9e6f210e0a3f161323049db5d
> revset: move subscript relation functions to its own dict

Queued, thanks.

> -    # 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 = (n for n in subscriptrelations.keys() if len(n) > 1)

Changed this to list comprehension, and renamed the variable to silence
pyflakes. We shouldn't expect that the exception attribute would be consumed
only once.

> +    raise error.UnknownIdentifier(rel, relnames)

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -218,6 +218,15 @@  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 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 @@  def relsubscriptset(repo, subset, x, y, 
     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 = (n for n in subscriptrelations.keys() if len(n) > 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 @@  methods = {
     "parentpost": parentpost,
 }
 
+subscriptrelations = {
+    "g": generationsrel,
+    "generations": generationsrel,
+}
+
 def lookupfn(repo):
     return lambda symbol: scmutil.isrevsymbol(repo, symbol)
 
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -649,6 +649,17 @@  parse errors of relation, subscript and 
   hg: parse error: relation subscript must be an integer
   [255]
 
+suggested relations
+
+  $ hg debugrevspec '.#generafions[0]'
+  hg: parse error: unknown identifier: generafions
+  (did you mean generations?)
+  [255]
+
+  $ hg debugrevspec '.#f[0]'
+  hg: parse error: unknown identifier: f
+  [255]
+
 parsed tree at stages:
 
   $ hg debugrevspec -p all '()'