Patchwork [1,of,9] keyword: restore kwtemplater.restrict at the end of wrapped patch.diff

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 25, 2017, 7:09 p.m.
Message ID <d03c6351573ad632e4f5.1498417774@speaknoevil>
Download mbox | patch
Permalink /patch/21703/
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 1498415892 -32400
#      Mon Jun 26 03:38:12 2017 +0900
# Node ID d03c6351573ad632e4f54aa269c7487e8f185b8a
# Parent  ef46d432e2e4cfecafb1faa4765254bf0650d4ef
keyword: restore kwtemplater.restrict at the end of wrapped patch.diff

Before this patch, kwdiff doesn't restore kwtemplater.restrict after
invocation of wrapped patch.diff(). This suppresses keyword expansion
at subsequent filelog.read().

Typical usecase of this issue is "hg cat" after "hg diff" with command
server. In this case, kwtemplater.restrict=True is kept in command
server process even after "hg diff".

To ensure kwtemplater.restrict=True while original patch.diff()
running, this patch makes kwdiff() yield values returned by it,
because it returns generator object.

Strictly speaking, if filelog.read() is invoked before completely
evaluating the result of previous patch.diff(), keyword expansion is
still suppressed, because kwtemplater.restrict isn't restored yet.

But this fixing should be reasonable enough, because patch.diff() is
consumed immediately, AFAIK.

Patch

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -665,8 +665,13 @@  def reposetup(ui, repo):
 
     def kwdiff(orig, *args, **kwargs):
         '''Monkeypatch patch.diff to avoid expansion.'''
+        restrict = kwt.restrict
         kwt.restrict = True
-        return orig(*args, **kwargs)
+        try:
+            for chunk in orig(*args, **kwargs):
+                yield chunk
+        finally:
+            kwt.restrict = restrict
 
     def kwweb_skip(orig, web, req, tmpl):
         '''Wraps webcommands.x turning off keyword expansion.'''
diff --git a/tests/test-keyword.t b/tests/test-keyword.t
--- a/tests/test-keyword.t
+++ b/tests/test-keyword.t
@@ -1378,4 +1378,35 @@  Test restricted mode with fetch (with me
    $Xinfo$
   +xxxx
 
+Test that patch.diff(), which is implied by "hg diff" or so, doesn't
+suppress expanding keywords at subsequent commands
+
+#if windows
+  $ PYTHONPATH="$TESTDIR/../contrib;$PYTHONPATH"
+#else
+  $ PYTHONPATH="$TESTDIR/../contrib:$PYTHONPATH"
+#endif
+  $ export PYTHONPATH
+
+  $ grep -v '^promptecho ' < $HGRCPATH >> $HGRCPATH.new
+  $ mv $HGRCPATH.new $HGRCPATH
+
+  >>> from __future__ import print_function
+  >>> from hgclient import readchannel, runcommand, check
+  >>> @check
+  ... def check(server):
+  ...     # hello block
+  ...     readchannel(server)
+  ... 
+  ...     runcommand(server, ['cat', 'm'])
+  ...     runcommand(server, ['diff', '-c', '.', 'm'])
+  ...     runcommand(server, ['cat', 'm'])
+  *** runcommand cat m
+  $Id: m 800511b3a22d Thu, 01 Jan 1970 00:00:00 +0000 test $
+  bar
+  *** runcommand diff -c . m
+  *** runcommand cat m
+  $Id: m 800511b3a22d Thu, 01 Jan 1970 00:00:00 +0000 test $
+  bar
+
   $ cd ..