Patchwork [2,of,6] alias: keep error message in "badalias" so that help can see it

login
register
mail settings
Submitter Yuya Nishihara
Date Aug. 14, 2014, 2:31 a.m.
Message ID <b884588ea7541d7c64d7.1407983484@mimosa>
Download mbox | patch
Permalink /patch/5387/
State Accepted
Headers show

Comments

Yuya Nishihara - Aug. 14, 2014, 2:31 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1400328811 -32400
#      Sat May 17 21:13:31 2014 +0900
# Node ID b884588ea7541d7c64d7e7143ce6349ab3853981
# Parent  98f5cd65e88e2fbd1708c9ea74e0f003a2e5ef1a
alias: keep error message in "badalias" so that help can see it

Upcoming patches will

 - change help_() to get badalias message without executing cmdalias()
 - raise Abort on bad alias

Patch

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -336,12 +336,13 @@  class cmdalias(object):
         self.name = self.cmd = name
         self.cmdname = ''
         self.definition = definition
+        self.fn = None
         self.args = []
         self.opts = []
         self.help = ''
         self.norepo = True
         self.optionalrepo = False
-        self.badalias = False
+        self.badalias = None
 
         try:
             aliases, entry = cmdutil.findcmd(self.name, cmdtable)
@@ -354,11 +355,7 @@  class cmdalias(object):
             self.shadows = False
 
         if not self.definition:
-            def fn(ui, *args):
-                ui.warn(_("no definition for alias '%s'\n") % self.name)
-                return -1
-            self.fn = fn
-            self.badalias = True
+            self.badalias = _("no definition for alias '%s'") % self.name
             return
 
         if self.definition.startswith('!'):
@@ -387,26 +384,17 @@  class cmdalias(object):
         try:
             args = shlex.split(self.definition)
         except ValueError, inst:
-            def fn(ui, *args):
-                ui.warn(_("error in definition for alias '%s': %s\n")
-                        % (self.name, inst))
-                return -1
-            self.fn = fn
-            self.badalias = True
+            self.badalias = (_("error in definition for alias '%s': %s")
+                             % (self.name, inst))
             return
         self.cmdname = cmd = args.pop(0)
         args = map(util.expandpath, args)
 
         for invalidarg in ("--cwd", "-R", "--repository", "--repo", "--config"):
             if _earlygetopt([invalidarg], args):
-                def fn(ui, *args):
-                    ui.warn(_("error in definition for alias '%s': %s may only "
-                              "be given on the command line\n")
-                            % (self.name, invalidarg))
-                    return -1
-
-                self.fn = fn
-                self.badalias = True
+                self.badalias = (_("error in definition for alias '%s': %s may "
+                                   "only be given on the command line")
+                                 % (self.name, invalidarg))
                 return
 
         try:
@@ -428,8 +416,6 @@  class cmdalias(object):
 
         except error.UnknownCommand:
             def fn(ui, *args):
-                ui.warn(_("alias '%s' resolves to unknown command '%s'\n") \
-                            % (self.name, cmd))
                 try:
                     # check if the command is in a disabled extension
                     commands.help_(ui, cmd, unknowncmd=True)
@@ -437,16 +423,18 @@  class cmdalias(object):
                     pass
                 return -1
             self.fn = fn
-            self.badalias = True
+            self.badalias = (_("alias '%s' resolves to unknown command '%s'")
+                             % (self.name, cmd))
         except error.AmbiguousCommand:
-            def fn(ui, *args):
-                ui.warn(_("alias '%s' resolves to ambiguous command '%s'\n") \
-                            % (self.name, cmd))
-                return -1
-            self.fn = fn
-            self.badalias = True
+            self.badalias = (_("alias '%s' resolves to ambiguous command '%s'")
+                             % (self.name, cmd))
 
     def __call__(self, ui, *args, **opts):
+        if self.badalias:
+            ui.warn(self.badalias + '\n')
+            if self.fn:
+                return self.fn(ui, *args, **opts)
+            return -1
         if self.shadows:
             ui.debug("alias '%s' shadows command '%s'\n" %
                      (self.name, self.cmdname))
diff --git a/mercurial/help.py b/mercurial/help.py
--- a/mercurial/help.py
+++ b/mercurial/help.py
@@ -235,7 +235,7 @@  def help_(ui, name, unknowncmd=False, fu
         rst = []
 
         # check if it's an invalid alias and display its error if it is
-        if getattr(entry[0], 'badalias', False):
+        if getattr(entry[0], 'badalias', None):
             if not unknowncmd:
                 ui.pushbuffer()
                 entry[0](ui)