src/topic/__init__.py
changeset 1888 dfaf0de6f4d8
parent 1887 68125d026b07
child 1889 d9b929bcc3ad
--- a/src/topic/__init__.py	Sat Mar 12 18:42:16 2016 +0000
+++ b/src/topic/__init__.py	Mon Mar 14 00:12:22 2016 +0000
@@ -11,6 +11,7 @@
 series instead of a single revision.
 """
 import functools
+import contextlib
 
 from mercurial.i18n import _
 from mercurial import branchmap
@@ -62,6 +63,23 @@
 def uisetup(ui):
     destination.setupdest()
 
+@contextlib.contextmanager
+def usetopicmap(repo):
+    """use awful monkey patching to update the topic cache"""
+    oldbranchcache = branchmap.branchcache
+    oldfilename = branchmap._filename
+    oldcaches =  getattr(repo, '_branchcaches', {})
+    try:
+        branchmap.branchcache = topicmap.topiccache
+        branchmap._filename = topicmap._filename
+        repo._branchcaches = getattr(repo, '_topiccaches', {})
+        yield
+        repo._topiccaches = repo._branchcaches
+    finally:
+        repo._branchcaches = oldcaches
+        branchmap.branchcache = oldbranchcache
+        branchmap._filename = oldfilename
+
 def reposetup(ui, repo):
     orig = repo.__class__
     if not isinstance(repo, localrepo.localrepository):
@@ -105,20 +123,9 @@
         def branchmap(self, topic=True):
             if not topic:
                 super(topicrepo, self).branchmap()
-            oldbranchcache = branchmap.branchcache
-            oldfilename = branchmap._filename
-            oldcaches =  getattr(self, '_branchcaches', {})
-            try:
-                branchmap.branchcache = topicmap.topiccache
-                branchmap._filename = topicmap._filename
-                self._branchcaches = getattr(self, '_topiccaches', {})
+            with usetopicmap(self):
                 branchmap.updatecache(self)
-                self._topiccaches = self._branchcaches
-                return self._topiccaches[self.filtername]
-            finally:
-                self._branchcaches = oldcaches
-                branchmap.branchcache = oldbranchcache
-                branchmap._filename = oldfilename
+            return self._topiccaches[self.filtername]
 
         def invalidatecaches(self):
             super(topicrepo, self).invalidatecaches()