mercurial/branchmap.py
changeset 18132 db25bf1dc828
parent 18131 f0eeb9b3444a
child 18166 3a2e810dd3d8
--- a/mercurial/branchmap.py	Sat Dec 22 17:08:15 2012 +0100
+++ b/mercurial/branchmap.py	Mon Dec 24 02:49:59 2012 +0100
@@ -9,7 +9,6 @@
 import encoding
 
 def read(repo):
-    partial = branchcache()
     try:
         f = repo.opener("cache/branchheads")
         lines = f.read().split('\n')
@@ -20,7 +19,8 @@
     try:
         last, lrev = lines.pop(0).split(" ", 1)
         last, lrev = bin(last), int(lrev)
-        if lrev >= len(repo) or repo[lrev].node() != last:
+        partial = branchcache(tipnode=last, tiprev=lrev)
+        if not partial.validfor(repo):
             # invalidate the cache
             raise ValueError('invalidating branch cache (tip differs)')
         for l in lines:
@@ -32,8 +32,6 @@
                 raise ValueError('invalidating branch cache because node '+
                                  '%s does not exist' % node)
             partial.setdefault(label, []).append(bin(node))
-        partial.tipnode = last
-        partial.tiprev = lrev
     except KeyboardInterrupt:
         raise
     except Exception, inst:
@@ -47,12 +45,9 @@
 def updatecache(repo):
     repo = repo.unfiltered()  # Until we get a smarter cache management
     cl = repo.changelog
-    tip = cl.tip()
     partial = repo._branchcache
-    if partial is not None and partial.tipnode == tip:
-        return
 
-    if partial is None or partial.tipnode not in cl.nodemap:
+    if partial is None or not partial.validfor(repo):
         partial = read(repo)
 
     catip = repo._cacheabletip()
@@ -80,6 +75,17 @@
         self.tipnode = tipnode
         self.tiprev = tiprev
 
+    def validfor(self, repo):
+        """Is the cache content valide regarding a repo
+
+        - False when cached tipnode are unknown or if we detect a strip.
+        - True when cache is up to date or a subset of current repo."""
+        try:
+            return self.tipnode == repo.changelog.node(self.tiprev)
+        except IndexError:
+            return False
+
+
     def write(self, repo):
         try:
             f = repo.opener("cache/branchheads", "w", atomictemp=True)
@@ -157,12 +163,8 @@
             if not nodes:
                 droppednodes.extend(nodes)
                 del self[branch]
-        try:
-            node = cl.node(self.tiprev)
-        except IndexError:
-            node = None
-        if ((self.tipnode != node)
-            or (self.tipnode in droppednodes)):
+        if ((not self.validfor(repo)) or (self.tipnode in droppednodes)):
+
             # cache key are not valid anymore
             self.tipnode = nullid
             self.tiprev = nullrev