Patchwork [1,of,3,RFC] revset: add helper functions for tree depth and functions used

login
register
mail settings
Submitter Alexander Plavin
Date Aug. 6, 2013, 9:30 p.m.
Message ID <0afbd16e3fc2427180e9.1375824656@debian-alexander.dolgopa>
Download mbox | patch
Permalink /patch/2006/
State Superseded
Headers show

Comments

Alexander Plavin - Aug. 6, 2013, 9:30 p.m.
# HG changeset patch
# User Alexander Plavin <alexander@plav.in>
# Date 1375824091 -14400
#      Wed Aug 07 01:21:31 2013 +0400
# Node ID 0afbd16e3fc2427180e91a0a5293f784b014d8f1
# Parent  5230d8599bcc722c7f2a4bab4306760de1d558f6
revset: add helper functions for tree depth and functions used

depth(tree) calculates tree depth, and funcsused(tree) returns a list of
revset functions used in the tree.


Is it the right place for the functions? I'm going to use them in
hgweb revset search, but they may be useful something else as well,
as both of them a quite general.

Patch

diff -r 5230d8599bcc -r 0afbd16e3fc2 mercurial/revset.py
--- a/mercurial/revset.py	Fri Jul 19 02:09:13 2013 +0400
+++ b/mercurial/revset.py	Wed Aug 07 01:21:31 2013 +0400
@@ -1972,5 +1972,22 @@ 
     output = '\n'.join(('  '*l + s) for l, s in lines)
     return output
 
+def depth(tree):
+    if isinstance(tree, tuple):
+        return max(map(depth, tree)) + 1
+    else:
+        return 0
+
+def funcsused(tree):
+    if not isinstance(tree, tuple) or tree[0] in ('string', 'symbol'):
+        return set()
+    else:
+        funcs = set()
+        for s in tree[1:]:
+            funcs |= funcsused(s)
+        if tree[0] == 'func':
+            funcs.add(tree[1][1])
+        return funcs
+
 # tell hggettext to extract docstrings from these functions:
 i18nfunctions = symbols.values()