Patchwork [3,of,5,modernize-streamclone] branchmap: move branch cache code out of streamclone.py

login
register
mail settings
Submitter Gregory Szorc
Date Oct. 4, 2015, 6:39 p.m.
Message ID <d01a373e1434f4e36d4f.1443983980@126.1.168.192.in-addr.arpa>
Download mbox | patch
Permalink /patch/10781/
State Accepted
Headers show

Comments

Gregory Szorc - Oct. 4, 2015, 6:39 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1443891236 25200
#      Sat Oct 03 09:53:56 2015 -0700
# Node ID d01a373e1434f4e36d4f5f7560e620c9e97fd3dc
# Parent  5ac0d9d646e498321c709e9bcd9467cfe037b2c1
branchmap: move branch cache code out of streamclone.py

This is low-level branch map and cache manipulation code. It deserves to
live next to similar code in branchmap.py. Moving it also paves the road
for multiple consumers, such as a bundle2 part handler that receives
branch mappings from a remote.

This is largely a mechanical move, with only variable names and
indentation being changed.

Patch

diff --git a/mercurial/branchmap.py b/mercurial/branchmap.py
--- a/mercurial/branchmap.py
+++ b/mercurial/branchmap.py
@@ -115,8 +115,40 @@  def updatecache(repo):
 
     assert partial.validfor(repo), filtername
     repo._branchcaches[repo.filtername] = partial
 
+def replacecache(repo, bm):
+    """Replace the branchmap cache for a repo with a branch mapping.
+
+    This is likely only called during clone with a branch map from a remote.
+    """
+    rbheads = []
+    closed = []
+    for bheads in bm.itervalues():
+        rbheads.extend(bheads)
+        for h in bheads:
+            r = repo.changelog.rev(h)
+            b, c = repo.changelog.branchinfo(r)
+            if c:
+                closed.append(h)
+
+    if rbheads:
+        rtiprev = max((int(repo.changelog.rev(node))
+                for node in rbheads))
+        cache = branchcache(bm,
+                            repo[rtiprev].node(),
+                            rtiprev,
+                            closednodes=closed)
+
+        # Try to stick it as low as possible
+        # filter above served are unlikely to be fetch from a clone
+        for candidate in ('base', 'immutable', 'served'):
+            rview = repo.filtered(candidate)
+            if cache.validfor(rview):
+                repo._branchcaches[candidate] = cache
+                cache.write(rview)
+                break
+
 class branchcache(dict):
     """A dict like object that hold branches heads cache.
 
     This cache is used to avoid costly computations to determine all the
diff --git a/mercurial/streamclone.py b/mercurial/streamclone.py
--- a/mercurial/streamclone.py
+++ b/mercurial/streamclone.py
@@ -252,32 +252,9 @@  def applyremotedata(repo, remotereqs, re
         repo._applyopenerreqs()
         repo._writerequirements()
 
         if remotebranchmap:
-            rbheads = []
-            closed = []
-            for bheads in remotebranchmap.itervalues():
-                rbheads.extend(bheads)
-                for h in bheads:
-                    r = repo.changelog.rev(h)
-                    b, c = repo.changelog.branchinfo(r)
-                    if c:
-                        closed.append(h)
+            branchmap.replacecache(repo, remotebranchmap)
 
-            if rbheads:
-                rtiprev = max((int(repo.changelog.rev(node))
-                        for node in rbheads))
-                cache = branchmap.branchcache(remotebranchmap,
-                                              repo[rtiprev].node(),
-                                              rtiprev,
-                                              closednodes=closed)
-                # Try to stick it as low as possible
-                # filter above served are unlikely to be fetch from a clone
-                for candidate in ('base', 'immutable', 'served'):
-                    rview = repo.filtered(candidate)
-                    if cache.validfor(rview):
-                        repo._branchcaches[candidate] = cache
-                        cache.write(rview)
-                        break
         repo.invalidate()
     finally:
         lock.release()