Patchwork [7,of,9] keyword: obtain kwtemplater instance via repository at runtime

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 25, 2017, 7:09 p.m.
Message ID <ec9915b24ba6de7c5bb4.1498417780@speaknoevil>
Download mbox | patch
Permalink /patch/21709/
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 1498416290 -32400
#      Mon Jun 26 03:44:50 2017 +0900
# Node ID ec9915b24ba6de7c5bb4464912a11e46950aad60
# Parent  ec02b73c11a5824c23be7fa20834bc1a20eac19e
keyword: obtain kwtemplater instance via repository at runtime

Wrapper functions of keyword extension are defined in reposetup(),
because they refer to kwtemplater instantiated in reposetup().

This patch makes them obtain kwtemplater instance via repository at
runtime. For reviewability, this patch focuses on wrapper functions,
which handle generator.

This is a part of preparations for defining them statically.

Patch

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -676,25 +676,31 @@  def reposetup(ui, repo):
             # shrink keywords read from working dir
             self.lines = kwt.shrinklines(self.fname, self.lines)
 
-    def kwdiff(orig, *args, **kwargs):
+    def kwdiff(orig, repo, *args, **kwargs):
         '''Monkeypatch patch.diff to avoid expansion.'''
-        restrict = kwt.restrict
-        kwt.restrict = True
+        kwt = getattr(repo, '_keywordkwt', None)
+        if kwt:
+            restrict = kwt.restrict
+            kwt.restrict = True
         try:
-            for chunk in orig(*args, **kwargs):
+            for chunk in orig(repo, *args, **kwargs):
                 yield chunk
         finally:
-            kwt.restrict = restrict
+            if kwt:
+                kwt.restrict = restrict
 
     def kwweb_skip(orig, web, req, tmpl):
         '''Wraps webcommands.x turning off keyword expansion.'''
-        origmatch = kwt.match
-        kwt.match = util.never
+        kwt = getattr(web.repo, '_keywordkwt', None)
+        if kwt:
+            origmatch = kwt.match
+            kwt.match = util.never
         try:
             for chunk in orig(web, req, tmpl):
                 yield chunk
         finally:
-            kwt.match = origmatch
+            if kwt:
+                kwt.match = origmatch
 
     def kw_amend(orig, ui, repo, commitfunc, old, extra, pats, opts):
         '''Wraps cmdutil.amend expanding keywords after amend.'''