Patchwork [8,of,8,V2] extensions: show deprecation warning for the use of cmdutil.command

login
register
mail settings
Submitter Yuya Nishihara
Date May 19, 2017, 3:25 p.m.
Message ID <8fa71763798046041d38.1495207518@mimosa>
Download mbox | patch
Permalink /patch/20727/
State Accepted
Headers show

Comments

Yuya Nishihara - May 19, 2017, 3:25 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1452349492 -32400
#      Sat Jan 09 23:24:52 2016 +0900
# Node ID 8fa71763798046041d385a16b45e1d5646a5293c
# Parent  fd9a80f0a473c47983f5312ad20ce01b1f259630
extensions: show deprecation warning for the use of cmdutil.command

Since this is a fundamental API for extensions, we set 1-year period until
actually removing it.
Augie Fackler - May 19, 2017, 10:46 p.m.
On Sat, May 20, 2017 at 12:25:18AM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1452349492 -32400
> #      Sat Jan 09 23:24:52 2016 +0900
> # Node ID 8fa71763798046041d385a16b45e1d5646a5293c
> # Parent  fd9a80f0a473c47983f5312ad20ce01b1f259630
> extensions: show deprecation warning for the use of cmdutil.command

queued, thanks

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3335,7 +3335,10 @@  def _performrevert(repo, parents, ctx, a
         if f in copied:
             repo.dirstate.copy(copied[f], f)
 
-command = registrar.command
+class command(registrar.command):
+    def _doregister(self, func, name, *args, **kwargs):
+        func._deprecatedregistrar = True  # flag for deprecwarn in extensions.py
+        return super(command, self)._doregister(func, name, *args, **kwargs)
 
 # a list of (ui, repo, otherpeer, opts, missing) functions called by
 # commands.outgoing.  "missing" is "missing" of the result of
diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -121,10 +121,13 @@  def _reportimporterror(ui, err, failed, 
 # attributes set by registrar.command
 _cmdfuncattrs = ('norepo', 'optionalrepo', 'inferrepo')
 
-def _validatecmdtable(cmdtable):
+def _validatecmdtable(ui, cmdtable):
     """Check if extension commands have required attributes"""
     for c, e in cmdtable.iteritems():
         f = e[0]
+        if getattr(f, '_deprecatedregistrar', False):
+            ui.deprecwarn("cmdutil.command is deprecated, use "
+                          "registrar.command to register '%s'" % c, '4.6')
         missing = [a for a in _cmdfuncattrs if not util.safehasattr(f, a)]
         if not missing:
             continue
@@ -153,7 +156,7 @@  def load(ui, name, path):
         ui.warn(_('(third party extension %s requires version %s or newer '
                   'of Mercurial; disabling)\n') % (shortname, minver))
         return
-    _validatecmdtable(getattr(mod, 'cmdtable', {}))
+    _validatecmdtable(ui, getattr(mod, 'cmdtable', {}))
 
     _extensions[shortname] = mod
     _order.append(shortname)
diff --git a/tests/test-extension.t b/tests/test-extension.t
--- a/tests/test-extension.t
+++ b/tests/test-extension.t
@@ -1590,4 +1590,19 @@  Test synopsis and docstring extending
   $ hg help bookmarks | grep GREPME
   hg bookmarks [OPTIONS]... [NAME]... GREPME [--foo] [-x]
       GREPME make sure that this is in the help!
+  $ cd ..
 
+Show deprecation warning for the use of cmdutil.command
+
+  $ cat > nonregistrar.py <<EOF
+  > from mercurial import cmdutil
+  > cmdtable = {}
+  > command = cmdutil.command(cmdtable)
+  > @command('foo', [], norepo=True)
+  > def foo(ui):
+  >     pass
+  > EOF
+
+  $ hg --config extensions.nonregistrar=`pwd`/nonregistrar.py version > /dev/null
+  devel-warn: cmdutil.command is deprecated, use registrar.command to register 'foo'
+  (compatibility will be dropped after Mercurial-4.6, update your code.) * (glob)