Patchwork D1732: revsetlang: add utility function to return hash like symbols from the tree

login
register
mail settings
Submitter phabricator
Date Dec. 26, 2017, 2:11 p.m.
Message ID <a659af0ac006112a1f86b56a2b1f6f35@localhost.localdomain>
Download mbox | patch
Permalink /patch/26441/
State Not Applicable
Headers show

Comments

phabricator - Dec. 26, 2017, 2:11 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHGdd911f95cbda: revsetlang: add utility function to return hash like symbols from the tree (authored by pulkit, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1732?vs=4608&id=4617

REVISION DETAIL
  https://phab.mercurial-scm.org/D1732

AFFECTED FILES
  mercurial/revsetlang.py

CHANGE DETAILS




To: pulkit, #hg-reviewers, yuja
Cc: yuja, mercurial-devel

Patch

diff --git a/mercurial/revsetlang.py b/mercurial/revsetlang.py
--- a/mercurial/revsetlang.py
+++ b/mercurial/revsetlang.py
@@ -661,3 +661,34 @@ 
         if tree[0] == 'func':
             funcs.add(tree[1][1])
         return funcs
+
+_hashre = util.re.compile('[0-9a-fA-F]{1,40}$')
+
+def _ishashlikesymbol(symbol):
+    """returns true if the symbol looks like a hash"""
+    return _hashre.match(symbol)
+
+def gethashlikesymbols(tree):
+    """returns the list of symbols of the tree that look like hashes
+
+    >>> gethashlikesymbols(('dagrange', ('symbol', '3'), ('symbol', 'abe3ff')))
+    ['3', 'abe3ff']
+    >>> gethashlikesymbols(('func', ('symbol', 'precursors'), ('symbol', '.')))
+    []
+    >>> gethashlikesymbols(('func', ('symbol', 'precursors'), ('symbol', '34')))
+    ['34']
+    >>> gethashlikesymbols(('symbol', 'abe3ffZ'))
+    []
+    """
+    if not tree:
+        return []
+
+    if tree[0] == "symbol":
+        if _ishashlikesymbol(tree[1]):
+            return [tree[1]]
+    elif len(tree) >= 3:
+        results = []
+        for subtree in tree[1:]:
+            results += gethashlikesymbols(subtree)
+        return results
+    return []