[PATCH 1 of 2] debugcommands: introduce standalone module for debug commands

Gregory Szorc gregory.szorc at gmail.com
Thu Nov 10 17:47:31 UTC 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1471493258 25200
#      Wed Aug 17 21:07:38 2016 -0700
# Node ID 52e8ce0d0992ad87be2815bd7c5f0d92aab21851
# Parent  5564fcd031df482d4fe4eb50d959878f55bc960d
debugcommands: introduce standalone module for debug commands

commands.py is our largest .py file by nearly 2x. Debug commands live
in a world of their own. So let's extract them to their own module.

We start with "debugancestor."

We currently reuse the commands table with commands.py and have a hack
in dispatch.py for loading debugcommands.py. In the future, we could
potentially use a separate commands table and avoid the import of
debugcommands.py.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1862,35 +1862,16 @@ def copy(ui, repo, *pats, **opts):
     This command takes effect with the next commit. To undo a copy
     before that, see :hg:`revert`.
 
     Returns 0 on success, 1 if errors are encountered.
     """
     with repo.wlock(False):
         return cmdutil.copy(ui, repo, pats, opts)
 
- at command('debugancestor', [], _('[INDEX] REV1 REV2'), optionalrepo=True)
-def debugancestor(ui, repo, *args):
-    """find the ancestor revision of two revisions in a given index"""
-    if len(args) == 3:
-        index, rev1, rev2 = args
-        r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), index)
-        lookup = r.lookup
-    elif len(args) == 2:
-        if not repo:
-            raise error.Abort(_("there is no Mercurial repository here "
-                               "(.hg not found)"))
-        rev1, rev2 = args
-        r = repo.changelog
-        lookup = repo.lookup
-    else:
-        raise error.Abort(_('either two or three arguments required'))
-    a = r.ancestor(lookup(rev1), lookup(rev2))
-    ui.write("%d:%s\n" % (r.rev(a), hex(a)))
-
 @command('debugbuilddag',
     [('m', 'mergeable-file', None, _('add single file mergeable changes')),
     ('o', 'overwritten-file', None, _('add single file all revs overwrite')),
     ('n', 'new-file', None, _('add new file at each rev'))],
     _('[OPTION]... [TEXT]'))
 def debugbuilddag(ui, repo, text=None,
                   mergeable_file=False,
                   overwritten_file=False,
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
new file mode 100644
--- /dev/null
+++ b/mercurial/debugcommands.py
@@ -0,0 +1,42 @@
+# debugcommands.py - command processing for debug* commands
+#
+# Copyright 2005-2016 Matt Mackall <mpm at selenic.com>
+#
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+import os
+
+from .i18n import _
+from . import (
+    cmdutil,
+    commands,
+    error,
+    revlog,
+    scmutil,
+)
+
+# We reuse the command table from commands because it is easier than
+# teaching dispatch about multiple tables.
+command = cmdutil.command(commands.table)
+
+ at command('debugancestor', [], _('[INDEX] REV1 REV2'), optionalrepo=True)
+def debugancestor(ui, repo, *args):
+    """find the ancestor revision of two revisions in a given index"""
+    if len(args) == 3:
+        index, rev1, rev2 = args
+        r = revlog.revlog(scmutil.opener(os.getcwd(), audit=False), index)
+        lookup = r.lookup
+    elif len(args) == 2:
+        if not repo:
+            raise error.Abort(_('there is no Mercurial repository here '
+                                '(.hg not found)'))
+        rev1, rev2 = args
+        r = repo.changelog
+        lookup = repo.lookup
+    else:
+        raise error.Abort(_('either two or three arguments required'))
+    a = r.ancestor(lookup(rev1), lookup(rev2))
+    ui.write('%d:%s\n' % (r.rev(a), hex(a)))
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -21,16 +21,17 @@ import time
 import traceback
 
 
 from .i18n import _
 
 from . import (
     cmdutil,
     commands,
+    debugcommands,
     demandimport,
     encoding,
     error,
     extensions,
     fancyopts,
     fileset,
     hg,
     hook,
@@ -763,16 +764,20 @@ def _dispatch(req):
         for objname, loadermod, loadername in extraloaders:
             extraobj = getattr(module, objname, None)
             if extraobj is not None:
                 getattr(loadermod, loadername)(ui, name, extraobj)
         _loaded.add(name)
 
     # (reposetup is handled in hg.repository)
 
+    # Side-effect of accessing is debugcommands module is guaranteed to be
+    # imported and commands.table is populated.
+    debugcommands.command
+
     addaliases(lui, commands.table)
 
     # All aliases and commands are completely defined, now.
     # Check abbreviation/ambiguity of shell alias.
     shellaliasfn = _checkshellalias(lui, ui, args)
     if shellaliasfn:
         with profiling.maybeprofile(lui):
             return shellaliasfn()


More information about the Mercurial-devel mailing list