configwizard: support loading extension on older Mercurial versions (bug 1282293); r=glob draft
authorGregory Szorc <gps@mozilla.com>
Tue, 28 Jun 2016 07:24:04 -0700
changeset 8656 b575f3d2d86d035c214b81e5fe54e8ced46ad1d2
parent 8653 2d69b5c7a0f56b309dba5acb815aa5afc8d8c880
child 8657 5672b13555b0d63d6ff99745b1781143881af735
push id951
push usergszorc@mozilla.com
push dateTue, 28 Jun 2016 14:28:55 +0000
reviewersglob
bugs1282293
configwizard: support loading extension on older Mercurial versions (bug 1282293); r=glob See the inline comment for why we do this. With this change, the extension now loads with Mercurial versions down to 2.0. MozReview-Commit-ID: G6cVOHtERvs
hgext/configwizard/__init__.py
--- a/hgext/configwizard/__init__.py
+++ b/hgext/configwizard/__init__.py
@@ -276,19 +276,17 @@ wizardsteps = {
     'firefoxtree',
     'codereview',
     'pushtotry',
     'multiplevct',
     'configchange',
     'permissions',
 }
 
-@command('configwizard', [
-    ('s', 'statedir', '', _('directory to store state')),
-    ], _('hg configwizard'), optionalrepo=True)
+
 def configwizard(ui, repo, statedir=None, **opts):
     """Ensure your Mercurial configuration is up to date."""
     runsteps = set(wizardsteps)
     if ui.hasconfig('configwizard', 'steps'):
         runsteps = set(ui.configlist('configwizard', 'steps'))
 
     hgversion = util.versiontuple(n=3)
 
@@ -354,16 +352,32 @@ def configwizard(ui, repo, statedir=None
         _handleconfigchange(ui, cw)
 
     if 'permissions' in runsteps:
         _checkpermissions(ui, cw)
 
     return 0
 
 
+# Older versions of Mercurial don't support the "optionalrepo" named
+# argument on the command decorator. While we don't support these older
+# versions of Mercurial, this could cause extension loading to fail.
+# So we handle the error to enable the extension to load and the command
+# to run.
+cwargs = [
+    ('s', 'statedir', '', _('directory to store state')),
+]
+try:
+    configwizard = command('configwizard', cwargs, _('hg configwizard'),
+                           optionalrepo=True)(configwizard)
+except TypeError:
+    from mercurial import commands
+    configwizard = command('configwizard', cwargs, _('hg configwizard'))(configwizard)
+    commands.optionalrepo += ' configwizard'
+
 def _checkhgversion(ui, hgversion):
     if hgversion >= OLDEST_NON_LEGACY_VERSION:
         return
 
     ui.warn(LEGACY_MERCURIAL_MESSAGE % util.version())
     ui.warn('\n')
 
     if os.name == 'nt':