Patchwork [1,of,3] revsetlang: factor out helper to match ancestors() in parsed tree

login
register
mail settings
Submitter Yuya Nishihara
Date June 19, 2017, 2:43 p.m.
Message ID <8785d4150c89c608d598.1497883415@mimosa>
Download mbox | patch
Permalink /patch/21503/
State Accepted
Headers show

Comments

Yuya Nishihara - June 19, 2017, 2:43 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1497753543 -32400
#      Sun Jun 18 11:39:03 2017 +0900
# Node ID 8785d4150c89c608d598c6ebaa2ca2e57d2e7f9a
# Parent  2851b24eecc483ce1546d63d155fa2e9250e3f44
revsetlang: factor out helper to match ancestors() in parsed tree

More checks will be added.

Patch

diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
--- a/mercurial/revsetlang.py
+++ b/mercurial/revsetlang.py
@@ -236,6 +236,20 @@  def getargsdict(x, funcname, keys):
     return parser.buildargsdict(getlist(x), funcname, parser.splitargspec(keys),
                                 keyvaluenode='keyvalue', keynode='symbol')
 
+def _isnamedfunc(x, funcname):
+    """Check if given tree matches named function"""
+    return x and x[0] == 'func' and getsymbol(x[1]) == funcname
+
+def _matchnamedfunc(x, funcname):
+    """Return args tree if given tree matches named function; otherwise None
+
+    This can't be used for testing a nullary function since its args tree
+    is also None. Use _isnamedfunc() instead.
+    """
+    if not _isnamedfunc(x, funcname):
+        return
+    return x[2]
+
 # Constants for ordering requirement, used in _analyze():
 #
 # If 'define', any nested functions and operations can change the ordering of
@@ -286,14 +300,10 @@  def _matchonly(revs, bases):
     >>> f('ancestors(A)', 'not ancestors(B)')
     ('list', ('symbol', 'A'), ('symbol', 'B'))
     """
-    if (revs is not None
-        and revs[0] == 'func'
-        and getsymbol(revs[1]) == 'ancestors'
-        and bases is not None
-        and bases[0] == 'not'
-        and bases[1][0] == 'func'
-        and getsymbol(bases[1][1]) == 'ancestors'):
-        return ('list', revs[2], bases[1][2])
+    ta = _matchnamedfunc(revs, 'ancestors')
+    tb = bases and bases[0] == 'not' and _matchnamedfunc(bases[1], 'ancestors')
+    if ta and tb:
+        return ('list', ta, tb)
 
 def _fixops(x):
     """Rewrite raw parsed tree to resolve ambiguous syntax which cannot be