Patchwork [3,of,3] dispatch: show deprecation warning if command has no attributes (issue5137)

login
register
mail settings
Submitter Yuya Nishihara
Date March 24, 2016, 3:27 p.m.
Message ID <2493068041457e811af4.1458833249@mimosa>
Download mbox | patch
Permalink /patch/14057/
State Accepted
Headers show

Comments

Yuya Nishihara - March 24, 2016, 3:27 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1452347186 -32400
#      Sat Jan 09 22:46:26 2016 +0900
# Node ID 2493068041457e811af4753f39248583f1c459e6
# Parent  941933978d9902ccf2a3f2bff2d017c70309e2bc
dispatch: show deprecation warning if command has no attributes (issue5137)

norepo/optionalrepo/inferrepo were removed by aa73d6a5d9ea, which would be
significant API change. This patch tries to avoid crash even if ancient
third-party extensions are enabled.
Sean Farley - March 24, 2016, 6:17 p.m.
Yuya Nishihara <yuya@tcha.org> writes:

> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1452347186 -32400
> #      Sat Jan 09 22:46:26 2016 +0900
> # Node ID 2493068041457e811af4753f39248583f1c459e6
> # Parent  941933978d9902ccf2a3f2bff2d017c70309e2bc
> dispatch: show deprecation warning if command has no attributes (issue5137)
>
> norepo/optionalrepo/inferrepo were removed by aa73d6a5d9ea, which would be
> significant API change. This patch tries to avoid crash even if ancient
> third-party extensions are enabled.

I like this direction.
Pierre-Yves David - March 25, 2016, 5:12 p.m.
On 03/24/2016 11:17 AM, Sean Farley wrote:
>
> Yuya Nishihara <yuya@tcha.org> writes:
>
>> # HG changeset patch
>> # User Yuya Nishihara <yuya@tcha.org>
>> # Date 1452347186 -32400
>> #      Sat Jan 09 22:46:26 2016 +0900
>> # Node ID 2493068041457e811af4753f39248583f1c459e6
>> # Parent  941933978d9902ccf2a3f2bff2d017c70309e2bc
>> dispatch: show deprecation warning if command has no attributes (issue5137)
>>
>> norepo/optionalrepo/inferrepo were removed by aa73d6a5d9ea, which would be
>> significant API change. This patch tries to avoid crash even if ancient
>> third-party extensions are enabled.
>
> I like this direction.

Pushed, thanks for flagging this as prereviewed.

Patch

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -747,7 +747,12 @@  def _checkshellalias(lui, ui, args, prec
                                   [], {})
 
 def _cmdattr(ui, cmd, func, attr):
-    return getattr(func, attr)
+    try:
+        return getattr(func, attr)
+    except AttributeError:
+        ui.deprecwarn("missing attribute '%s', use @command decorator "
+                      "to register '%s'" % (attr, cmd), '3.8')
+        return False
 
 _loaded = set()
 
diff --git a/tests/test-extension.t b/tests/test-extension.t
--- a/tests/test-extension.t
+++ b/tests/test-extension.t
@@ -1203,6 +1203,48 @@  disabling in command line overlays with 
 
   $ cd ..
 
+Test compatibility with extension commands that don't use @command (issue5137)
+
+  $ hg init deprecated
+  $ cd deprecated
+
+  $ cat <<EOF > deprecatedcmd.py
+  > def deprecatedcmd(repo, ui):
+  >     pass
+  > cmdtable = {
+  >     'deprecatedcmd': (deprecatedcmd, [], ''),
+  > }
+  > EOF
+  $ cat <<EOF > .hg/hgrc
+  > [extensions]
+  > deprecatedcmd = `pwd`/deprecatedcmd.py
+  > mq = !
+  > hgext.mq = !
+  > hgext/mq = !
+  > [alias]
+  > deprecatedalias = deprecatedcmd
+  > EOF
+
+  $ hg deprecatedcmd
+  devel-warn: missing attribute 'norepo', use @command decorator to register 'deprecatedcmd'
+  (compatibility will be dropped after Mercurial-3.8, update your code.) at: * (glob)
+
+  $ hg deprecatedalias
+  devel-warn: missing attribute 'norepo', use @command decorator to register 'deprecatedalias'
+  (compatibility will be dropped after Mercurial-3.8, update your code.) at: * (glob)
+
+ no warning unless command is executed:
+
+  $ hg paths
+
+ but mq iterates over command table:
+
+  $ hg --config extensions.mq= paths
+  devel-warn: missing attribute 'norepo', use @command decorator to register 'deprecatedcmd'
+  (compatibility will be dropped after Mercurial-3.8, update your code.) at: * (glob)
+
+  $ cd ..
+
 Test synopsis and docstring extending
 
   $ hg init exthelp