[PATCH 4 of 6 RFC] perf: define command annotation locally if Mercurial is earlier than 3.1

FUJIWARA Katsunori foozy at lares.dti.ne.jp
Fri Jun 10 15:20:18 EDT 2016


# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1465585986 -32400
#      Sat Jun 11 04:13:06 2016 +0900
# Node ID 9f50ea27207df58c0a520392b3a052b55dbf5b37
# Parent  7c22b492f189110d98fae2b133a8456c5ff3ccf3
perf: define command annotation locally if Mercurial is earlier than 3.1

Before this patch, using cmdutil.command() for "@command" annotation
prevents perf.py from being loaded by Mercurial earlier than 1.9 (or
2daa5179e73f), because cmdutil.command() isn't available in such
Mercurial, even though there are some code paths for Mercurial earlier
than 1.9.

For example, setting "_prereadsize" attribute in perfindex() and
perfnodelookup() is effective only with hg earlier than 1.8 (or
61c9bc3da402).

In addition to it, "norepo" option of command annotation has been
available since 3.1 (or 75a96326cecb), and this is another blocker for
loading perf.py with earlier Mercurial.

  ============ ============ ======
               command of
  hg version   cmdutil      norepo
  ============ ============ ======
  3.1 or later      o         o
  1.9 or later      o         x
  earlier           x         x
  ============ ============ ======

This patch defines "command()" for annotation locally as below:

  - define wrapper of existing cmdutil.command(), if cmdutil.command()
    doesn't support "norepo"
    (for Mercurial earlier than 3.1)

  - define full command() locally with minimum function, if
    cmdutil.command() isn't available at runtime
    (for Mercurial earlier than 1.9)

This patch also defines parsealiases() locally without examining
whether it is available or not, because it is small enough to define
locally.

BTW, making perf.py available for Mercurial earlier than 1.9 needs
discussion, because repository format feature 'usegeneraldelta' is
enabled by default since 1.9, and Mercurial earlier than 1.9 can't
accept repositories enabling this feature.

diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -43,7 +43,35 @@ revlogopts = getattr(commands, "debugrev
         ])
 
 cmdtable = {}
-command = cmdutil.command(cmdtable)
+
+# define parsealiases locally, because cmdutil.parsealiases has been
+# available since 1.5 (or 6252852b4332)
+def parsealiases(cmd):
+    return cmd.lstrip("^").split("|")
+
+if safehasattr(cmdutil, 'command'):
+    import inspect
+    command = cmdutil.command(cmdtable)
+    if 'norepo' not in inspect.getargspec(command)[0]:
+        # "norepo" option has been available since 3.1 (or 75a96326cecb)
+        _command = command
+        def command(name, options=(), synopsis=None, norepo=False):
+            if norepo:
+                commands.norepo += ' %s' % ' '.join(parsealiases(name))
+            return _command(name, list(options), synopsis)
+else:
+    # define "@command" annotation locally, because cmdutil.command
+    # has been available since 1.9 (or 2daa5179e73f)
+    def command(name, options=(), synopsis=None, norepo=False):
+        def decorator(func):
+            if synopsis:
+                cmdtable[name] = func, list(options), synopsis
+            else:
+                cmdtable[name] = func, list(options)
+            if norepo:
+                commands.norepo += ' %s' % ' '.join(parsealiases(name))
+            return func
+        return decorator
 
 def getlen(ui):
     if ui.configbool("perf", "stub"):


More information about the Mercurial-devel mailing list