obsdiscovery: extract a smarted depth in utility
authorPierre-Yves David <pierre-yves.david@ens-lyon.org>
Sat, 11 Mar 2017 09:08:20 -0800
changeset 2086 28241509ff6f
parent 2085 6d61c5ed3bfa
child 2087 0c2371542687
obsdiscovery: extract a smarted depth in utility The function is reusing previous depth for ancestors unless this is a merge.
hgext3rd/evolve/obsdiscovery.py
hgext3rd/evolve/utility.py
--- a/hgext3rd/evolve/obsdiscovery.py	Sat Mar 11 08:46:21 2017 -0800
+++ b/hgext3rd/evolve/obsdiscovery.py	Sat Mar 11 09:08:20 2017 -0800
@@ -500,15 +500,6 @@
         d = (r.head, s(n(r.head)), r.index, len(r), r.depth, node.short(r.obshash))
         ui.status('%3d %s %5d %4d %5d %s\n' % d)
 
-_depthcache = {}
-def _depth(repo, rev):
-    cl = repo.changelog
-    n = cl.node(rev)
-    depth = _depthcache.get(n, None)
-    if depth is None:
-        depth = _depthcache[n] = len(list(cl.ancestors([rev], inclusive=True)))
-    return depth
-
 def _hlp2(i):
     """return highest power of two lower than 'i'"""
     return 2 ** int(math.log(i - 1, 2))
@@ -552,7 +543,7 @@
 
     @util.propertycache
     def depth(self):
-        return _depth(self._repo, self.head)
+        return utility.depth(self._repo, self.head)
 
     @util.propertycache
     def _revs(self):
@@ -571,14 +562,14 @@
         result = []
         if len(bheads) == 1:
             newhead = bottom[-1]
-            newstart = _depth(self._repo, newhead) - len(bottom)
+            newstart = utility.depth(self._repo, newhead) - len(bottom)
             result.append(_range(self._repo, newhead, newstart, bottom))
         else:
             cl = self._repo.changelog
             for h in bheads:
                 subset = cl.ancestors([h], inclusive=True)
                 hrevs = [r for r in bottom if r in subset]
-                start = _depth(self._repo, h) - len(hrevs)
+                start = utility.depth(self._repo, h) - len(hrevs)
                 entry = _range(self._repo, h, start, [r for r in bottom if r in subset])
                 result.append(entry)
         result.append(_range(self._repo, self.head, globalindex, top))
--- a/hgext3rd/evolve/utility.py	Sat Mar 11 08:46:21 2017 -0800
+++ b/hgext3rd/evolve/utility.py	Sat Mar 11 09:08:20 2017 -0800
@@ -4,6 +4,7 @@
 #
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
+from mercurial import node
 
 def obsexcmsg(ui, message, important=False):
     verbose = ui.configbool('experimental', 'verbose-obsolescence-exchange',
@@ -18,3 +19,20 @@
     if ui.configbool('experimental', 'verbose-obsolescence-exchange', False):
         topic = 'OBSEXC'
     ui.progress(topic, *args, **kwargs)
+
+_depthcache = {}
+def depth(repo, rev):
+    cl = repo.changelog
+    n = cl.node(rev)
+    revdepth = _depthcache.get(n, None)
+    if revdepth is None:
+        p1, p2 = cl.parentrevs(rev)
+        if p1 == node.nullrev:
+            revdepth = 1
+        elif p2 == node.nullrev:
+            revdepth = depth(repo, p1) + 1
+        else:
+            # XXX we should just find the common ancestors
+            revdepth = len(list(cl.ancestors([rev], inclusive=True)))
+        _depthcache[n] = revdepth
+    return revdepth