Patchwork [5,of,9] keyword: make wrapped repository and kwtemplater refer to each other

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 25, 2017, 7:09 p.m.
Message ID <0a1031814123dd19212a.1498417778@speaknoevil>
Download mbox | patch
Permalink /patch/21708/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - June 25, 2017, 7:09 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1498416137 -32400
#      Mon Jun 26 03:42:17 2017 +0900
# Node ID 0a1031814123dd19212a48d26130e4b790baf3d1
# Parent  133d12414af420ccfb9c2353877c1efc1903d00e
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.

Patch

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -88,6 +88,7 @@  from __future__ import absolute_import
 import os
 import re
 import tempfile
+import weakref
 
 from mercurial.i18n import _
 from mercurial.hgweb import webcommands
@@ -212,7 +213,7 @@  class kwtemplater(object):
 
     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 @@  class kwtemplater(object):
         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 @@  def reposetup(ui, repo):
                 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 @@  def reposetup(ui, repo):
     extensions.wrapfunction(cmdutil, 'dorecord', kw_dorecord)
     for c in nokwwebcommands.split():
         extensions.wrapfunction(webcommands, c, kwweb_skip)
-    repo.__class__ = kwrepo