changeset 7535:75f746df73e2

hg: improve implementations of `successors` and `precursors` properties of Mercurial changesets * On Mercurial versions supporting it, make the properties return the closest changesets in the obsolescence chain that are in the repository. * On older Mercurial versions, fall back to the previous implementations.
author Manuel Jacob <me@manueljacob.de>
date Mon, 18 Feb 2019 21:17:58 +0100
parents b06e1141f20f
children 3922aa544fbb
files kallithea/lib/vcs/backends/hg/changeset.py kallithea/tests/vcs/test_hg.py
diffstat 2 files changed, 17 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/vcs/backends/hg/changeset.py	Mon Feb 18 17:00:51 2019 +0800
+++ b/kallithea/lib/vcs/backends/hg/changeset.py	Mon Feb 18 21:17:58 2019 +0100
@@ -89,9 +89,11 @@
     @LazyProperty
     def successors(self):
         try:
+            # This works starting from Mercurial 4.3: the function `successorssets` was moved to the mercurial.obsutil module and gained the `closest` parameter.
             from mercurial import obsutil
-            successors = obsutil.successorssets(self._ctx._repo, self._ctx.node())
-        except ImportError:  # moved in Mercurial 4.3 (4f49810a1011)
+            successors = obsutil.successorssets(self._ctx._repo, self._ctx.node(), closest=True)
+        except ImportError:
+            # fallback for older versions
             successors = obsolete.successorssets(self._ctx._repo, self._ctx.node())
         if successors:
             # flatten the list here handles both divergent (len > 1)
@@ -102,17 +104,19 @@
 
     @LazyProperty
     def predecessors(self):
-        predecessors = set()
-        nm = self._ctx._repo.changelog.nodemap
         try:
-            raw_predecessors = self._ctx._repo.obsstore.predecessors
-        except AttributeError:  # renamed in Mercurial 4.4 (d5acd967f95a)
-            raw_predecessors = self._ctx._repo.obsstore.precursors
-        for p in raw_predecessors.get(self._ctx.node(), ()):
-            pr = nm.get(p[0])
-            if pr is not None:
-                predecessors.add(hex(p[0])[:12])
-        return predecessors
+            # This works starting from Mercurial 4.3: the function `closestpredecessors` was added.
+            from mercurial import obsutil
+            return [hex(n)[:12] for n in obsutil.closestpredecessors(self._ctx._repo, self._ctx.node())]
+        except ImportError:
+            # fallback for older versions
+            predecessors = set()
+            nm = self._ctx._repo.changelog.nodemap
+            for p in self._ctx._repo.obsstore.precursors.get(self._ctx.node(), ()):
+                pr = nm.get(p[0])
+                if pr is not None:
+                    predecessors.add(hex(p[0])[:12])
+            return predecessors
 
     @LazyProperty
     def bookmarks(self):
--- a/kallithea/tests/vcs/test_hg.py	Mon Feb 18 17:00:51 2019 +0800
+++ b/kallithea/tests/vcs/test_hg.py	Mon Feb 18 21:17:58 2019 +0100
@@ -591,4 +591,4 @@
 
     def test_predecessors(self):
         init_chset = self.repo.get_changeset(0)
-        assert init_chset.predecessors == set([])
+        assert len(init_chset.predecessors) == 0