diff hgext3rd/topic/topicmap.py @ 1949:79c08d17a3d7

topicmap: move the 'usetopicmap' context manager into the topicmap module There is no good reason to not have it gathered with the rest.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Wed, 30 Mar 2016 22:25:17 -0700
parents 60b7de2b3dd1
children 99c1a26abf3f
line wrap: on
line diff
--- a/hgext3rd/topic/topicmap.py	Wed Mar 30 22:05:49 2016 -0700
+++ b/hgext3rd/topic/topicmap.py	Wed Mar 30 22:25:17 2016 -0700
@@ -1,3 +1,5 @@
+import contextlib
+
 from mercurial.node import hex, bin, nullid
 from mercurial import (
     branchmap,
@@ -35,6 +37,29 @@
         key = s.digest()
     return key
 
+@contextlib.contextmanager
+def usetopicmap(repo):
+    """use awful monkey patching to ensure topic map usage
+
+    During the extend of the context block, The topicmap should be used and
+    updated instead of the branchmap."""
+    oldbranchcache = branchmap.branchcache
+    oldfilename = branchmap._filename
+    oldread = branchmap.read
+    oldcaches = getattr(repo, '_branchcaches', {})
+    try:
+        branchmap.branchcache = topiccache
+        branchmap._filename = _filename
+        branchmap.read = readtopicmap
+        repo._branchcaches = getattr(repo, '_topiccaches', {})
+        yield
+        repo._topiccaches = repo._branchcaches
+    finally:
+        repo._branchcaches = oldcaches
+        branchmap.branchcache = oldbranchcache
+        branchmap._filename = oldfilename
+        branchmap.read = oldread
+
 class topiccache(oldbranchcache):
 
     def __init__(self, *args, **kwargs):