changeset 2126:839e96521c5a

depth: extract code dedicated to depth of a merge in its own function The merge case is more complicated than the regular one, we extract is for the sake of clarity.
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
date Sun, 19 Mar 2017 00:44:31 +0100
parents e0a25339ff17
children e2770faac2df
files hgext3rd/evolve/stablerange.py
diffstat 1 files changed, 47 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/evolve/stablerange.py	Sat Mar 18 22:48:26 2017 +0100
+++ b/hgext3rd/evolve/stablerange.py	Sun Mar 19 00:44:31 2017 +0100
@@ -24,6 +24,7 @@
         self._depthcache = {}
 
     def depthrev(self, repo, rev):
+        repo = repo.unfiltered()
         cl = repo.changelog
         cache = self._depthcache
         nullrev = nodemod.nullrev
@@ -48,45 +49,7 @@
                     revdepth = parentdepth + 1
             else:
                 # merge case
-                depth_p1 = cache.get(p1)
-                depth_p2 = cache.get(p2)
-                missingparent = False
-                if depth_p1 is None:
-                    stack.append(p1)
-                    missingparent = True
-                if depth_p2 is None:
-                    stack.append(p2)
-                    missingparent = True
-                if missingparent:
-                    continue
-                # computin depth of a merge
-                # XXX the common ancestors heads could be cached
-                ancnodes = cl.commonancestorsheads(cl.node(p1), cl.node(p2))
-                ancrevs = [cl.rev(a) for a in ancnodes]
-                anyunkown = False
-                ancdepth = []
-                for r in ancrevs:
-                    d = cache.get(r)
-                    if d is None:
-                        anyunkown = True
-                        stack.append(r)
-                    ancdepth.append((r, d))
-                if anyunkown:
-                    continue
-                if not ancrevs:
-                    # unrelated branch, (no common root)
-                    revdepth = depth_p1 + depth_p2 + 1
-                elif len(ancrevs) == 1:
-                    # one unique branch point:
-                    # we can compute depth without any walk
-                    depth_anc = ancdepth[0][1]
-                    revdepth = depth_p1 + (depth_p2 - depth_anc) + 1
-                else:
-                    # multiple ancestors, we pick one that is
-                    # * the deepest (less changeset outside of it),
-                    # * lowest revs because more chance to have descendant of other "above"
-                    anc, revdepth = max(ancdepth, key=lambda x: (x[1], -x[0]))
-                    revdepth += len(cl.findmissingrevs(common=[anc], heads=[current]))
+                revdepth = self._depthmerge(cl, current, p1, p2, stack, cache)
             if revdepth is not None:
                 cache[current] = revdepth
                 stack.pop()
@@ -94,6 +57,51 @@
         # assert revdepth == actual_depth, (rev, revdepth, actual_depth)
         return revdepth
 
+    @staticmethod
+    def _depthmerge(cl, rev, p1, p2, stack, cache):
+        # sub method to simplify the main 'depthrev' one
+        revdepth = None
+        depth_p1 = cache.get(p1)
+        depth_p2 = cache.get(p2)
+        missingparent = False
+        if depth_p1 is None:
+            stack.append(p1)
+            missingparent = True
+        if depth_p2 is None:
+            stack.append(p2)
+            missingparent = True
+        if missingparent:
+            return None
+        # computin depth of a merge
+        # XXX the common ancestors heads could be cached
+        ancnodes = cl.commonancestorsheads(cl.node(p1), cl.node(p2))
+        ancrevs = [cl.rev(a) for a in ancnodes]
+        anyunkown = False
+        ancdepth = []
+        for r in ancrevs:
+            d = cache.get(r)
+            if d is None:
+                anyunkown = True
+                stack.append(r)
+            ancdepth.append((r, d))
+        if anyunkown:
+            return None
+        if not ancrevs:
+            # unrelated branch, (no common root)
+            revdepth = depth_p1 + depth_p2 + 1
+        elif len(ancrevs) == 1:
+            # one unique branch point:
+            # we can compute depth without any walk
+            depth_anc = ancdepth[0][1]
+            revdepth = depth_p1 + (depth_p2 - depth_anc) + 1
+        else:
+            # multiple ancestors, we pick one that is
+            # * the deepest (less changeset outside of it),
+            # * lowest revs because more chance to have descendant of other "above"
+            anc, revdepth = max(ancdepth, key=lambda x: (x[1], -x[0]))
+            revdepth += len(cl.findmissingrevs(common=[anc], heads=[rev]))
+        return revdepth
+
 @eh.reposetup
 def setupcache(ui, repo):