# HG changeset patch # User Matt Mackall # Date 1161127878 18000 # Node ID 028fff46a4acc38241be29f8d9f64339ed2da263 # Parent 145a8fde69e611a4de4f0c115f28dd124b9488b3 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 diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -79,6 +79,7 @@ class localrepository(repo.repository): self.revlogversion = v self.tagscache = None + self.branchcache = None self.nodetagscache = None self.encodepats = None self.decodepats = None @@ -288,6 +289,44 @@ class localrepository(repo.repository): 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]