changeset 403:37c96b78b8c0

uisetup: use a single loop/abstraction for wrapping all the commands
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Wed, 10 Jun 2009 12:06:31 +0200
parents d453cf1aafa3
children 28e4b47b2179
files hgsubversion/__init__.py hgsubversion/wrappers.py tests/test_utility_commands.py
diffstat 3 files changed, 49 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/__init__.py
+++ b/hgsubversion/__init__.py
@@ -36,55 +36,57 @@ import cmdutil
 import svnrepo
 import wrappers
 
-svnopts = (('', 'stupid', None, 'use slower, but more compatible, protocol for '
-            'Subversion'),)
-
-svncloneopts = (('T', 'tagpaths', '', 'list of path s to search for tags '
-                 'in Subversion repositories'),
-                ('A', 'authors', '', 'path to file mapping Subversion '
-                 'usernames to Mercurial authors'),
-                ('', 'filemap', '', 'path to file containing rules for '
-                 'remapping Subversion repository paths'),)
-
-wraptype = {False: wrappers.generic, True: wrappers.clone}
+svnopts = [
+    ('', 'stupid', None,
+     'use slower, but more compatible, protocol for Subversion'),
+]
+
+wrapcmds = { # cmd: generic, target, fixdoc, ppopts, opts
+    'parents': (False, None, False, False, [
+        ('', 'svn', None, 'show parent svn revision instead'),
+    ]),
+    'diff': (False, None, False, False, [
+        ('', 'svn', None, 'show svn diffs against svn parent'),
+    ]),
+    'pull': (True, 'sources', True, True, []),
+    'push': (True, 'destinations', True, True, []),
+    'clone': (False, 'sources', True, True, [
+        ('T', 'tagpaths', '',
+         'list of paths to search for tags in Subversion repositories'),
+        ('A', 'authors', '',
+         'file mapping Subversion usernames to Mercurial authors'),
+        ('', 'filemap', '',
+         'file containing rules for remapping Subversion repository paths'),
+    ]),
+}
 
 def uisetup(ui):
-    """Do our UI setup.
-
-    Does the following wrappings:
-     * parent -> utility_commands.parent
-     * outgoing -> utility_commands.outgoing
-     """
-    entry = extensions.wrapcommand(commands.table, 'parents',
-                                   wrappers.parent)
-    entry[1].append(('', 'svn', None, "show parent svn revision instead"))
-    entry = extensions.wrapcommand(commands.table, 'diff',
-                                   wrappers.diff)
-    entry[1].append(('', 'svn', None,
-                     "show svn-style diffs, default against svn parent"))
+    """insert command wrappers for a bunch of commands"""
 
     docvals = {'extension': 'hgsubversion'}
-    for command, target, isclone in [('clone', 'sources', True),
-                                     ('pull', 'sources', False),
-                                     ('push', 'destinations', False)]:
-
-        docvals['command'] = command
-        docvals['Command'] = command.capitalize()
-        docvals['target'] = target
-        doc = wrappers.generic.__doc__.strip() % docvals
-        fn = getattr(commands, command)
-        fn.__doc__ = fn.__doc__.rstrip() + '\n\n    ' + doc
-
-        wrapped = wraptype[isclone]
-        entry = extensions.wrapcommand(commands.table, command, wrapped)
-        entry[1].extend(svnopts)
-        if isclone: entry[1].extend(svncloneopts)
+    for cmd, (generic, target, fixdoc, ppopts, opts) in wrapcmds.iteritems():
+
+        if fixdoc:
+            docvals['command'] = cmd
+            docvals['Command'] = cmd.capitalize()
+            docvals['target'] = target
+            doc = wrappers.generic.__doc__.strip() % docvals
+            fn = getattr(commands, cmd)
+            fn.__doc__ = fn.__doc__.rstrip() + '\n\n    ' + doc
+
+        wrapped = generic and wrappers.generic or getattr(wrappers, cmd)
+        entry = extensions.wrapcommand(commands.table, cmd, wrapped)
+        if ppopts:
+            entry[1].extend(svnopts)
+        if opts:
+            entry[1].extend(opts)
 
     try:
         rebase = extensions.find('rebase')
-        if rebase:
-            entry = extensions.wrapcommand(rebase.cmdtable, 'rebase', wrappers.rebase)
-            entry[1].append(('', 'svn', None, 'automatic svn rebase', ))
+        if not rebase:
+            return
+        entry = extensions.wrapcommand(rebase.cmdtable, 'rebase', wrappers.rebase)
+        entry[1].append(('', 'svn', None, 'automatic svn rebase'))
     except:
         pass
 
--- a/hgsubversion/wrappers.py
+++ b/hgsubversion/wrappers.py
@@ -24,7 +24,7 @@ pullfuns = {
     False: stupidmod.convert_rev,
 }
 
-def parent(orig, ui, repo, *args, **opts):
+def parents(orig, ui, repo, *args, **opts):
     """show Mercurial & Subversion parents of the working dir or revision
     """
     if not opts.get('svn', False):
--- a/tests/test_utility_commands.py
+++ b/tests/test_utility_commands.py
@@ -76,7 +76,7 @@ class UtilityTests(test_util.TestBase):
                              {'branch': 'localbranch', })
         new = self.repo.commitctx(ctx)
         hg.update(self.repo, new)
-        wrappers.parent(lambda x, y: None, u, self.repo, svn=True)
+        wrappers.parents(lambda x, y: None, u, self.repo, svn=True)
         actual = u.popbuffer()
         self.assertEqual(actual,
                          'changeset:   3:4e256962fc5d\n'
@@ -88,19 +88,19 @@ class UtilityTests(test_util.TestBase):
         hg.update(self.repo, 'default')
         # Make sure styles work
         u.pushbuffer()
-        wrappers.parent(lambda x, y: None, u, self.repo, svn=True, style='compact')
+        wrappers.parents(lambda x, y: None, u, self.repo, svn=True, style='compact')
         actual = u.popbuffer()
         self.assertEqual(actual,
                          '4:1   1083037b18d8   2008-10-08 01:39 +0000   durin\n'
                          '  Add gamma on trunk.\n\n')
         # custom templates too
         u.pushbuffer()
-        wrappers.parent(lambda x, y: None, u, self.repo, svn=True, template='{node}\n')
+        wrappers.parents(lambda x, y: None, u, self.repo, svn=True, template='{node}\n')
         actual = u.popbuffer()
         self.assertEqual(actual, '1083037b18d85cd84fa211c5adbaeff0fea2cd9f\n')
 
         u.pushbuffer()
-        wrappers.parent(lambda x, y: None, u, self.repo, svn=True)
+        wrappers.parents(lambda x, y: None, u, self.repo, svn=True)
         actual = u.popbuffer()
         self.assertEqual(actual,
                          'changeset:   4:1083037b18d8\n'