Patchwork [02,of,10] branchmap: store branchcache in a dedicated object

login
register
mail settings
Submitter Pierre-Yves David
Date Dec. 22, 2012, 1:48 a.m.
Message ID <79db6d40bcedd40c7f27.1356140929@yamac.lan>
Download mbox | patch
Permalink /patch/266/
State Superseded, archived
Commit 79db6d40bcedd40c7f27c009459dfe7820893802
Headers show

Comments

Pierre-Yves David - Dec. 22, 2012, 1:48 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at logilab.fr>
# Date 1356137082 -3600
# Node ID 79db6d40bcedd40c7f27c009459dfe7820893802
# Parent  6fb3b8c61775ca53cdd1c69505295fb51c265883
branchmap: store branchcache in a dedicated object

Value and key of branchcache would benefit from being hold by the same object.
Moreover some logic (update, write, validation) could be move on such object.

The creation of this object is the first step toward this move. The result will
clarify branchcache related code and hide most of the detail in the class
itself. This encapsulation will greatly helps implementation of branchcache for
filtered view of the repo.

Patch

diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -7,17 +7,17 @@ 
 
 from node import bin, hex, nullid, nullrev
 import encoding
 
 def read(repo):
-    partial = {}
+    partial = branchcache()
     try:
         f = repo.opener("cache/branchheads")
         lines = f.read().split('\n')
         f.close()
     except (IOError, OSError):
-        return {}, nullid, nullrev
+        return branchcache(), nullid, nullrev
 
     try:
         last, lrev = lines.pop(0).split(" ", 1)
         last, lrev = bin(last), int(lrev)
         if lrev >= len(repo) or repo[lrev].node() != last:
@@ -35,11 +35,11 @@  def read(repo):
     except KeyboardInterrupt:
         raise
     except Exception, inst:
         if repo.ui.debugflag:
             repo.ui.warn(str(inst), '\n')
-        partial, last, lrev = {}, nullid, nullrev
+        partial, last, lrev =  branchcache(), nullid, nullrev
     return partial, last, lrev
 
 def write(repo, branches, tip, tiprev):
     try:
         f = repo.opener("cache/branchheads", "w", atomictemp=True)
@@ -141,5 +141,9 @@  def updatecache(repo):
     if lrev < tiprev:
         ctxgen = (repo[r] for r in cl.revs(lrev + 1, tiprev))
         update(repo, partial, ctxgen)
     repo._branchcache = partial
     repo._branchcachetip = tip
+
+class branchcache(dict):
+    """A dict like object that hold branches heads cache"""
+
diff --git a/mercurial/discovery.py b/mercurial/discovery.py
--- a/mercurial/discovery.py
+++ b/mercurial/discovery.py
@@ -191,12 +191,13 @@  def _headssummary(repo, remote, outgoing
     for branch in remotebranches - touchedbranches:
         del headssum[branch]
 
     # D. Update newmap with outgoing changes.
     # This will possibly add new heads and remove existing ones.
-    newmap = dict((branch, heads[1]) for branch, heads in headssum.iteritems()
-                  if heads[0] is not None)
+    newmap = branchmap.branchcache((branch, heads[1])
+                                 for branch, heads in headssum.iteritems()
+                                 if heads[0] is not None)
     branchmap.update(repo, newmap, missingctx)
     for branch, newheads in newmap.iteritems():
         headssum[branch][1][:] = newheads
     return headssum
 
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -664,11 +664,11 @@  class localrepository(object):
 
     def branchmap(self):
         '''returns a dictionary {branch: [branchheads]}'''
         if self.changelog.filteredrevs:
             # some changeset are excluded we can't use the cache
-            bmap = {}
+            bmap = branchmap.branchcache()
             branchmap.update(self, bmap, (self[r] for r in self))
             return bmap
         else:
             branchmap.updatecache(self)
             return self._branchcache
@@ -2493,11 +2493,11 @@  class localrepository(object):
                     rbheads.extend(bheads)
 
                 if rbheads:
                     rtiprev = max((int(self.changelog.rev(node))
                             for node in rbheads))
-                    self._branchcache = rbranchmap
+                    self._branchcache = branchmap.branchcache(rbranchmap)
                     rtipnode = self._branchcachetip = self[rtiprev].node()
                     branchmap.write(self, self._branchcache, rtipnode, rtiprev)
             self.invalidate()
             return len(self.heads()) + 1
         finally: