changeset 34982:d9aba3730d30 stable

dispatch: abort if early boolean options can't be parsed Perhaps we'll need to restrict the parsing rules of --debugger and --profile, where this patch will help us know why the --debugger option doesn't work. I have another series to extend this feature to --config/--cwd/-R, but even with that, shell aliases can be used to get around the restriction.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 11 Nov 2017 14:02:41 +0900
parents e16f68c4abe3
children e273b6671827
files mercurial/dispatch.py tests/test-dispatch.t
diffstat 2 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dispatch.py	Fri Nov 10 22:27:26 2017 +0900
+++ b/mercurial/dispatch.py	Sat Nov 11 14:02:41 2017 +0900
@@ -55,6 +55,9 @@
         self.fout = fout
         self.ferr = ferr
 
+        # remember options pre-parsed by _earlyreqopt*()
+        self.earlyoptions = {}
+
         # reposetups which run before extensions, useful for chg to pre-fill
         # low-level repo state (for example, changelog) before extensions.
         self.prereposetups = prereposetups or []
@@ -707,19 +710,19 @@
 
     >>> req = request([b'x', b'--', b'--debugger'])
     >>> _earlyreqoptbool(req, b'debugger', [b'--debugger'])
-    False
     """
     try:
         argcount = req.args.index("--")
     except ValueError:
         argcount = len(req.args)
-    value = False
+    value = None
     pos = 0
     while pos < argcount:
         arg = req.args[pos]
         if arg in aliases:
             value = True
         pos += 1
+    req.earlyoptions[name] = value
     return value
 
 def runcommand(lui, repo, cmd, fullargs, ui, options, d, cmdpats, cmdoptions):
@@ -849,6 +852,9 @@
             raise error.Abort(_(
                 "option -R has to be separated from other options (e.g. not "
                 "-qR) and --repository may only be abbreviated as --repo!"))
+        if options["debugger"] != req.earlyoptions["debugger"]:
+            raise error.Abort(_("option --debugger may not be abbreviated!"))
+        # don't validate --profile/--traceback, which can be enabled from now
 
         if options["encoding"]:
             encoding.encoding = options["encoding"]
--- a/tests/test-dispatch.t	Fri Nov 10 22:27:26 2017 +0900
+++ b/tests/test-dispatch.t	Sat Nov 11 14:02:41 2017 +0900
@@ -51,6 +51,12 @@
   --debugger: no such file in rev cb9a9f314b8b
   [1]
 
+Unparsable form of early options:
+
+  $ hg cat --debugg
+  abort: option --debugger may not be abbreviated!
+  [255]
+
 [defaults]
 
   $ hg cat a