keyword: make wrapped repository and kwtemplater refer to each other
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Mon, 26 Jun 2017 03:42:17 +0900
changeset 33067 5a51db8bf41b
parent 33066 cf0da12afe8a
child 33068 a15da610ea20
keyword: make wrapped repository and kwtemplater refer to each other Wrapper functions of keyword extension are defined in reposetup(), because they refer to kwtemplater instantiated in reposetup(). But these functions can be defined statically, if kwtemplater can be obtained via repository at runtime. This is a part of preparations for defining them statically. To avoid cyclic reference, this patch makes kwtemplater use weakref to refer related repository instance.
hgext/keyword.py
--- a/hgext/keyword.py	Mon Jun 26 03:40:57 2017 +0900
+++ b/hgext/keyword.py	Mon Jun 26 03:42:17 2017 +0900
@@ -88,6 +88,7 @@
 import os
 import re
 import tempfile
+import weakref
 
 from mercurial.i18n import _
 from mercurial.hgweb import webcommands
@@ -212,7 +213,7 @@
 
     def __init__(self, ui, repo, inc, exc):
         self.ui = ui
-        self.repo = repo
+        self._repo = weakref.ref(repo)
         self.match = match.match(repo.root, '', [], inc, exc)
         self.restrict = kwtools['hgcmd'] in restricted.split()
         self.postcommit = False
@@ -223,6 +224,10 @@
         else:
             self.templates = _defaultkwmaps(self.ui)
 
+    @property
+    def repo(self):
+        return self._repo()
+
     @util.propertycache
     def escape(self):
         '''Returns bar-separated and escaped keywords.'''
@@ -658,6 +663,9 @@
                 finally:
                     kwt.restrict = origrestrict
 
+    repo.__class__ = kwrepo
+    repo._keywordkwt = kwt
+
     # monkeypatches
     def kwpatchfile_init(orig, self, ui, gp, backend, store, eolmode=None):
         '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
@@ -768,4 +776,3 @@
     extensions.wrapfunction(cmdutil, 'dorecord', kw_dorecord)
     for c in nokwwebcommands.split():
         extensions.wrapfunction(webcommands, c, kwweb_skip)
-    repo.__class__ = kwrepo