Patchwork [2,of,3] dispatch: extract function that tests command attributes

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

Comments

Yuya Nishihara - March 24, 2016, 3:27 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1457882083 -32400
#      Mon Mar 14 00:14:43 2016 +0900
# Node ID 941933978d9902ccf2a3f2bff2d017c70309e2bc
# Parent  748200cca34890204c0a82fd6c6f8416d902e111
dispatch: extract function that tests command attributes

This function will host the compatibility layer for old third-party commands.
See the next patch for details.

Patch

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -66,6 +66,7 @@  from mercurial.i18n import _
 from mercurial.node import bin, hex, short, nullid, nullrev
 from mercurial.lock import release
 from mercurial import commands, cmdutil, hg, scmutil, util, revset
+from mercurial import dispatch
 from mercurial import extensions, error, phases
 from mercurial import patch as patchmod
 from mercurial import lock as lockmod
@@ -3566,7 +3567,7 @@  def extsetup(ui):
         for cmd, entry in cmdtable.iteritems():
             cmd = cmdutil.parsealiases(cmd)[0]
             func = entry[0]
-            if func.norepo:
+            if dispatch._cmdattr(ui, cmd, func, 'norepo'):
                 continue
             entry = extensions.wrapcommand(cmdtable, cmd, mqcommand)
             entry[1].extend(mqopt)
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -746,6 +746,9 @@  def _checkshellalias(lui, ui, args, prec
         return lambda: runcommand(lui, None, cmd, args[:1], ui, options, d,
                                   [], {})
 
+def _cmdattr(ui, cmd, func, attr):
+    return getattr(func, attr)
+
 _loaded = set()
 
 # list of (objname, loadermod, loadername) tuple:
@@ -874,7 +877,7 @@  def _dispatch(req):
 
     repo = None
     cmdpats = args[:]
-    if not func.norepo:
+    if not _cmdattr(ui, cmd, func, 'norepo'):
         # use the repo from the request only if we don't have -R
         if not rpath and not cwd:
             repo = req.repo
@@ -895,8 +898,9 @@  def _dispatch(req):
             except error.RepoError:
                 if rpath and rpath[-1]: # invalid -R path
                     raise
-                if not func.optionalrepo:
-                    if func.inferrepo and args and not path:
+                if not _cmdattr(ui, cmd, func, 'optionalrepo'):
+                    if (_cmdattr(ui, cmd, func, 'inferrepo') and
+                        args and not path):
                         # try to infer -R from command args
                         repos = map(cmdutil.findrepo, args)
                         guess = repos[0]