Patchwork [10,of,10,V2] branchmap: extract updatebranchcache from repo

login
register
mail settings
Submitter Pierre-Yves David
Date Dec. 20, 2012, 2:08 p.m.
Message ID <6cca11f3799570c8bff1.1356012520@crater2.logilab.fr>
Download mbox | patch
Permalink /patch/212/
State Accepted
Commit f8a13f061a8a4a278c7029c2cac7417dbcb08808
Headers show

Comments

Pierre-Yves David - Dec. 20, 2012, 2:08 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1356011117 -3600
# Node ID 6cca11f3799570c8bff148cf064e279bf451ac9c
# Parent  c00fe6d70cf45bc73a27d09b579a1bbffce2e0e8
branchmap: extract updatebranchcache from repo

Patch

diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -109,5 +109,37 @@  def update(repo, partial, ctxgen):
         nodes = [head for head in partial[branch]
                  if repo.changelog.hasnode(head)]
         if not nodes:
             del partial[branch]
 
+def updatecache(repo):
+    repo = repo.unfiltered()  # Until we get a smarter cache management
+    cl = repo.changelog
+    tip = cl.tip()
+    if repo._branchcache is not None and repo._branchcachetip == tip:
+        return
+
+    oldtip = repo._branchcachetip
+    if oldtip is None or oldtip not in cl.nodemap:
+        partial, last, lrev = read(repo)
+    else:
+        lrev = cl.rev(oldtip)
+        partial = repo._branchcache
+
+    catip = repo._cacheabletip()
+    # if lrev == catip: cache is already up to date
+    # if lrev >  catip: we have uncachable element in `partial` can't write
+    #                   on disk
+    if lrev < catip:
+        ctxgen = (repo[r] for r in cl.revs(lrev + 1, catip))
+        update(repo, partial, ctxgen)
+        write(repo, partial, cl.node(catip), catip)
+        lrev = catip
+    # If cacheable tip were lower than actual tip, we need to update the
+    # cache up to tip. This update (from cacheable to actual tip) is not
+    # written to disk since it's not cacheable.
+    tiprev = len(repo) - 1
+    if lrev < tiprev:
+        ctxgen = (repo[r] for r in cl.revs(lrev + 1, tiprev))
+        update(repo, partial, ctxgen)
+    repo._branchcache = partial
+    repo._branchcachetip = tip
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -643,52 +643,19 @@  class localrepository(object):
         better alternative. But we are using this approach until it is ready.
         """
         cl = self.changelog
         return cl.rev(cl.tip())
 
-    @unfilteredmethod # Until we get a smarter cache management
-    def updatebranchcache(self):
-        cl = self.changelog
-        tip = cl.tip()
-        if self._branchcache is not None and self._branchcachetip == tip:
-            return
-
-        oldtip = self._branchcachetip
-        if oldtip is None or oldtip not in cl.nodemap:
-            partial, last, lrev = branchmap.read(self)
-        else:
-            lrev = cl.rev(oldtip)
-            partial = self._branchcache
-
-        catip = self._cacheabletip()
-        # if lrev == catip: cache is already up to date
-        # if lrev >  catip: we have uncachable element in `partial` can't write
-        #                   on disk
-        if lrev < catip:
-            ctxgen = (self[r] for r in cl.revs(lrev + 1, catip))
-            branchmap.update(self, partial, ctxgen)
-            branchmap.write(self, partial, cl.node(catip), catip)
-            lrev = catip
-        # If cacheable tip were lower than actual tip, we need to update the
-        # cache up to tip. This update (from cacheable to actual tip) is not
-        # written to disk since it's not cacheable.
-        tiprev = len(self) - 1
-        if lrev < tiprev:
-            ctxgen = (self[r] for r in cl.revs(lrev + 1, tiprev))
-            branchmap.update(self, partial, ctxgen)
-        self._branchcache = partial
-        self._branchcachetip = tip
-
     def branchmap(self):
         '''returns a dictionary {branch: [branchheads]}'''
         if self.changelog.filteredrevs:
             # some changeset are excluded we can't use the cache
             bmap = {}
             branchmap.update(self, bmap, (self[r] for r in self))
             return bmap
         else:
-            self.updatebranchcache()
+            branchmap.updatecache(self)
             return self._branchcache
 
 
     def _branchtip(self, heads):
         '''return the tipmost branch head in heads'''
@@ -1421,11 +1388,11 @@  class localrepository(object):
                 # be compliant anyway
                 #
                 # if minimal phase was 0 we don't need to retract anything
                 phases.retractboundary(self, targetphase, [n])
             tr.close()
-            self.updatebranchcache()
+            branchmap.updatecache(self)
             return n
         finally:
             if tr:
                 tr.release()
             lock.release()
@@ -2405,11 +2372,11 @@  class localrepository(object):
             cl.finalize(trp)
 
             tr.close()
 
             if changesets > 0:
-                self.updatebranchcache()
+                branchmap.updatecache(self)
                 def runhooks():
                     # forcefully update the on-disk branch cache
                     self.ui.debug("updating the branch cache\n")
                     self.hook("changegroup", node=hex(cl.node(clstart)),
                               source=srctype, url=url)
diff --git a/mercurial/repair.py b/mercurial/repair.py
--- a/mercurial/repair.py
+++ b/mercurial/repair.py
@@ -4,11 +4,11 @@ 
 # Copyright 2007 Matt Mackall
 #
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-from mercurial import changegroup
+from mercurial import changegroup, branchmap
 from mercurial.node import short
 from mercurial.i18n import _
 import os
 import errno
 
@@ -58,11 +58,11 @@  def _collectbrokencsets(repo, files, str
 def strip(ui, repo, nodelist, backup="all", topic='backup'):
     repo = repo.unfiltered()
     # It simplifies the logic around updating the branchheads cache if we only
     # have to consider the effect of the stripped revisions and not revisions
     # missing because the cache is out-of-date.
-    repo.updatebranchcache()
+    branchmap.updatecache(repo)
 
     cl = repo.changelog
     # TODO handle undo of merge sets
     if isinstance(nodelist, str):
         nodelist = [nodelist]