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

Alexander Plavin alexander at plav.in
Tue Aug 6 16:30:56 CDT 2013


# HG changeset patch
# User Alexander Plavin <alexander at 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.

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()


More information about the Mercurial-devel mailing list