extdiff: refactor closure of saved diff command as a top-level class
authorYuya Nishihara <yuya@tcha.org>
Wed, 27 Jul 2016 21:40:42 +0900
changeset 29721 479076db51be
parent 29720 041fecbb588a
child 29722 14c3afcb1c26
extdiff: refactor closure of saved diff command as a top-level class This allows us to collect __doc__ for translation.
hgext/extdiff.py
--- a/hgext/extdiff.py	Wed Jul 27 21:53:14 2016 +0900
+++ b/hgext/extdiff.py	Wed Jul 27 21:40:42 2016 +0900
@@ -324,6 +324,32 @@
     cmdline = ' '.join(map(util.shellquote, [program] + option))
     return dodiff(ui, repo, cmdline, pats, opts)
 
+class savedcmd(object):
+    """use %(path)s to diff repository (or selected files)
+
+    Show differences between revisions for the specified files, using
+    the %(path)s program.
+
+    When two revision arguments are given, then changes are shown
+    between those revisions. If only one revision is specified then
+    that revision is compared to the working directory, and, when no
+    revisions are specified, the working directory files are compared
+    to its parent.
+    """
+
+    def __init__(self, path, cmdline):
+        # We can't pass non-ASCII through docstrings (and path is
+        # in an unknown encoding anyway)
+        docpath = path.encode("string-escape")
+        self.__doc__ = self.__doc__ % {'path': util.uirepr(docpath)}
+        self._cmdline = cmdline
+
+    def __call__(self, ui, repo, *pats, **opts):
+        options = ' '.join(map(util.shellquote, opts['option']))
+        if options:
+            options = ' ' + options
+        return dodiff(ui, repo, self._cmdline + options, pats, opts)
+
 def uisetup(ui):
     for cmd, path in ui.configitems('extdiff'):
         path = util.expandpath(path)
@@ -357,28 +383,5 @@
                    ui.config('merge-tools', cmd+'.diffargs')
             if args:
                 cmdline += ' ' + args
-        def save(cmdline):
-            '''use closure to save diff command to use'''
-            def mydiff(ui, repo, *pats, **opts):
-                options = ' '.join(map(util.shellquote, opts['option']))
-                if options:
-                    options = ' ' + options
-                return dodiff(ui, repo, cmdline + options, pats, opts)
-            # We can't pass non-ASCII through docstrings (and path is
-            # in an unknown encoding anyway)
-            docpath = path.encode("string-escape")
-            mydiff.__doc__ = '''\
-use %(path)s to diff repository (or selected files)
-
-    Show differences between revisions for the specified files, using
-    the %(path)s program.
-
-    When two revision arguments are given, then changes are shown
-    between those revisions. If only one revision is specified then
-    that revision is compared to the working directory, and, when no
-    revisions are specified, the working directory files are compared
-    to its parent.\
-''' % {'path': util.uirepr(docpath)}
-            return mydiff
         command(cmd, extdiffopts[:], _('hg %s [OPTION]... [FILE]...') % cmd,
-                inferrepo=True)(save(cmdline))
+                inferrepo=True)(savedcmd(path, cmdline))