changeset 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 54810b543bf4
children 99c1a26abf3f
files hgext3rd/topic/__init__.py hgext3rd/topic/topicmap.py
diffstat 2 files changed, 29 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Wed Mar 30 22:05:49 2016 -0700
+++ b/hgext3rd/topic/__init__.py	Wed Mar 30 22:25:17 2016 -0700
@@ -12,7 +12,6 @@
 """
 from __future__ import absolute_import
 
-import contextlib
 import re
 
 from mercurial.i18n import _
@@ -92,28 +91,8 @@
     discovery.modsetup(ui)
     setupimportexport(ui)
 
-@contextlib.contextmanager
-def usetopicmap(repo):
-    """use awful monkey patching to update the topic cache"""
-    oldbranchcache = branchmap.branchcache
-    oldfilename = branchmap._filename
-    oldread = branchmap.read
-    oldcaches = getattr(repo, '_branchcaches', {})
-    try:
-        branchmap.branchcache = topicmap.topiccache
-        branchmap._filename = topicmap._filename
-        branchmap.read = topicmap.readtopicmap
-        repo._branchcaches = getattr(repo, '_topiccaches', {})
-        yield
-        repo._topiccaches = repo._branchcaches
-    finally:
-        repo._branchcaches = oldcaches
-        branchmap.branchcache = oldbranchcache
-        branchmap._filename = oldfilename
-        branchmap.read = oldread
-
 def cgapply(orig, repo, *args, **kwargs):
-    with usetopicmap(repo):
+    with topicmap.usetopicmap(repo):
         return orig(repo, *args, **kwargs)
 
 def reposetup(ui, repo):
@@ -148,7 +127,7 @@
                 not self.currenttopic):
                 # we are amending and need to remove a topic
                 del ctx.extra()[constants.extrakey]
-            with usetopicmap(self):
+            with topicmap.usetopicmap(self):
                 return orig.commitctx(self, ctx, error=error)
 
         @property
@@ -166,12 +145,12 @@
         def branchmap(self, topic=True):
             if not topic:
                 super(topicrepo, self).branchmap()
-            with usetopicmap(self):
+            with topicmap.usetopicmap(self):
                 branchmap.updatecache(self)
             return self._topiccaches[self.filtername]
 
         def destroyed(self, *args, **kwargs):
-            with usetopicmap(self):
+            with topicmap.usetopicmap(self):
                 return super(topicrepo, self).destroyed(*args, **kwargs)
 
         def invalidatecaches(self):
--- 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):