changeset 3417:028fff46a4ac

Add branchtags function with cache - cache stores tipmost node and rev and node,label pairs - if cache is out of date, scan new changesets - write new cache out after scan
author Matt Mackall <mpm@selenic.com>
date Tue, 17 Oct 2006 18:31:18 -0500
parents 145a8fde69e6
children 5436c8fe0ff5
files mercurial/localrepo.py
diffstat 1 files changed, 39 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/localrepo.py	Tue Oct 17 18:30:20 2006 -0500
+++ b/mercurial/localrepo.py	Tue Oct 17 18:31:18 2006 -0500
@@ -79,6 +79,7 @@
         self.revlogversion = v
 
         self.tagscache = None
+        self.branchcache = None
         self.nodetagscache = None
         self.encodepats = None
         self.decodepats = None
@@ -288,6 +289,44 @@
                 self.nodetagscache.setdefault(n, []).append(t)
         return self.nodetagscache.get(node, [])
 
+    def branchtags(self):
+        if self.branchcache != None:
+            return self.branchcache
+
+        self.branchcache = {}
+
+        try:
+            f = self.opener("branches.cache")
+            last, lrev = f.readline().rstrip().split(" ", 1)
+            last, lrev = bin(last), int(lrev)
+            if self.changelog.node(lrev) == last: # sanity check
+                for l in f:
+                    node, label = l.rstrip().split(" ", 1)
+                    self.branchcache[label] = bin(node)
+            f.close()
+        except IOError:
+            last, lrev = nullid, -1
+            lrev = self.changelog.rev(last)
+
+        tip = self.changelog.count() - 1
+        if lrev != tip:
+            for r in range(lrev + 1, tip + 1):
+                n = self.changelog.node(r)
+                c = self.changelog.read(n)
+                b = c[5].get("branch")
+                if b:
+                    self.branchcache[b] = n
+            self._writebranchcache()
+
+        return self.branchcache
+
+    def _writebranchcache(self):
+        f = self.opener("branches.cache", "w")
+        t = self.changelog.tip()
+        f.write("%s %s\n" % (hex(t), self.changelog.count() - 1))
+        for label, node in self.branchcache.iteritems():
+            f.write("%s %s\n" % (hex(node), label))
+
     def lookup(self, key):
         try:
             return self.tags()[key]