[PATCH 2 of 2] speed up 'hg branches' command

John Mulligan phlogistonjohn at asynchrono.us
Mon Jun 8 19:21:44 CDT 2009


# HG changeset patch
# User John Mulligan <phlogistonjohn at asynchrono.us>
# Date 1244506677 14400
# Node ID 2c45d6e0b775359d9dd13b1c61b19cd3eaabde0a
# Parent  f190d25e75f804fb025f23466f61e5221b20608c
speed up 'hg branches' command

create a function that returns a list of tuples (head, isclosed)
so that functions do not need to call heads/branchheads more than
once when trying to get the available heads and the closed state

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -447,8 +447,9 @@
     Use the command 'hg update' to switch to an existing branch.
     """
     hexfunc = ui.debugflag and hex or short
-    _closed = set(repo.heads(closed=True))
-    _open = set(repo.heads(closed=False))
+    _heads = repo.allheads()
+    _closed = set(h for h, c in _heads if c)
+    _open = set(h for h, c in _heads if not c)
     _states = {0:' (inactive)', 1:' (closed)', 2:''}
     def _branches():
         for tag, node in repo.branchtags().items():
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1145,6 +1145,16 @@
         heads = [(-self.changelog.rev(h), h) for h in heads if display(h)]
         return [n for (r, n) in sorted(heads)]
 
+    def allheads(self, start=None):
+        heads = self.changelog.heads(start)
+        def _heads():
+            for h in heads:
+                rev = self.changelog.rev(h)
+                extras = self.changelog.read(h)[5]
+                yield (-rev, h, 'close' in extras)
+        return [(n, c) for (r, n, c) in sorted(_heads())]
+        
+
     def branchheads(self, branch=None, start=None, closed=False):
         if branch is None:
             branch = self[None].branch()


More information about the Mercurial-devel mailing list