[PATCH] alias: fail gracefully when invalid global options are given (issue2442)

Steve Losh steve at stevelosh.com
Sun Oct 17 12:25:29 CDT 2010


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1287336277 14400
# Node ID d10369fefd01f165f76263d9b92423ae018b84bc
# Parent  175fb1b193f494683801c3156a338da477b82bb0
alias: fail gracefully when invalid global options are given (issue2442)

This patch modifies the check for shell aliases to prevent crashing when an invalid
global option is given.

When an invalid global option is given the check will simply return and let the
normal error handling for this case happen.

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -431,11 +431,15 @@
 
 def _checkshellalias(ui, args):
     cwd = os.getcwd()
     norepo = commands.norepo
     options = {}
-    args = fancyopts.fancyopts(args, commands.globalopts, options)
+
+    try:
+        args = fancyopts.fancyopts(args, commands.globalopts, options)
+    except fancyopts.getopt.GetoptError:
+        return
 
     if not args:
         return
 
     _parseconfig(ui, options['config'])
diff --git a/tests/test-alias.t b/tests/test-alias.t
--- a/tests/test-alias.t
+++ b/tests/test-alias.t
@@ -259,5 +259,87 @@
   
       Returns 0 on success.
   
   use "hg -v help rt" to show global options
   [255]
+
+invalid global arguments for normal commands, aliases, and shell aliases
+
+  $ hg --invalid root
+  hg: option --invalid not recognized
+  Mercurial Distributed SCM
+  
+  basic commands:
+  
+   add        add the specified files on the next commit
+   annotate   show changeset information by line for each file
+   clone      make a copy of an existing repository
+   commit     commit the specified files or all outstanding changes
+   diff       diff repository (or selected files)
+   export     dump the header and diffs for one or more changesets
+   forget     forget the specified files on the next commit
+   init       create a new repository in the given directory
+   log        show revision history of entire repository or files
+   merge      merge working directory with another revision
+   pull       pull changes from the specified source
+   push       push changes to the specified destination
+   remove     remove the specified files on the next commit
+   serve      start stand-alone webserver
+   status     show changed files in the working directory
+   summary    summarize working directory state
+   update     update working directory (or switch revisions)
+  
+  use "hg help" for the full list of commands or "hg -v" for details
+  [255]
+  $ hg --invalid mylog
+  hg: option --invalid not recognized
+  Mercurial Distributed SCM
+  
+  basic commands:
+  
+   add        add the specified files on the next commit
+   annotate   show changeset information by line for each file
+   clone      make a copy of an existing repository
+   commit     commit the specified files or all outstanding changes
+   diff       diff repository (or selected files)
+   export     dump the header and diffs for one or more changesets
+   forget     forget the specified files on the next commit
+   init       create a new repository in the given directory
+   log        show revision history of entire repository or files
+   merge      merge working directory with another revision
+   pull       pull changes from the specified source
+   push       push changes to the specified destination
+   remove     remove the specified files on the next commit
+   serve      start stand-alone webserver
+   status     show changed files in the working directory
+   summary    summarize working directory state
+   update     update working directory (or switch revisions)
+  
+  use "hg help" for the full list of commands or "hg -v" for details
+  [255]
+  $ hg --invalid blank
+  hg: option --invalid not recognized
+  Mercurial Distributed SCM
+  
+  basic commands:
+  
+   add        add the specified files on the next commit
+   annotate   show changeset information by line for each file
+   clone      make a copy of an existing repository
+   commit     commit the specified files or all outstanding changes
+   diff       diff repository (or selected files)
+   export     dump the header and diffs for one or more changesets
+   forget     forget the specified files on the next commit
+   init       create a new repository in the given directory
+   log        show revision history of entire repository or files
+   merge      merge working directory with another revision
+   pull       pull changes from the specified source
+   push       push changes to the specified destination
+   remove     remove the specified files on the next commit
+   serve      start stand-alone webserver
+   status     show changed files in the working directory
+   summary    summarize working directory state
+   update     update working directory (or switch revisions)
+  
+  use "hg help" for the full list of commands or "hg -v" for details
+  [255]
+


More information about the Mercurial-devel mailing list