[PATCH 4 of 4] extensions: add wrapfunctionduringcommand() double wrapper

Dan Villiom Podlaski Christiansen danchr at gmail.com
Wed Jul 7 05:18:51 CDT 2010


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1278496623 -7200
# Node ID d5315743766a67c2f93a52cb2694348e1c14d766
# Parent  2acf029768ae99af4746fd3a425564c168d4e601
extensions: add wrapfunctionduringcommand() double wrapper.

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -175,6 +175,32 @@ def wrapfunction(container, funcname, wr
     setattr(container, funcname, wrap)
     return origfn
 
+def wrapfunctionduringcommand(container, funcname, table, command, wrapper):
+    '''Like wrapfunction(), but only wrap during the execution of command
+
+    The typical example of this is to perform expansion in 'commitctx' during
+    invocation of the 'commit' command.
+    '''
+    # use a list to enable assigning to it from within nested functions
+    shouldwrap = [False]
+
+    def commandwrapper(orig, *args, **kwargs):
+        originalwrap = shouldwrap[0]
+        shouldwrap[0] = True
+        try:
+            return orig(*args, **kwargs)
+        finally:
+            shouldwrap[0] = originalwrap
+
+    def conditionalwrapper(orig, *args, **kwargs):
+        if shouldwrap[0]:
+            return wrapper(orig, *args, **kwargs)
+        else:
+            return orig(*args, **kwargs)
+
+    wrapcommand(table, command, commandwrapper)
+    return wrapfunction(container, funcname, conditionallywrap)
+
 def _disabledpaths(strip_init=False):
     '''find paths of disabled extensions. returns a dict of {name: path}
     removes /__init__.py from packages if strip_init is True'''


More information about the Mercurial-devel mailing list