bundle2: implement the mandatory/advisory logic for parameter
authorPierre-Yves David <pierre-yves.david@fb.com>
Wed, 19 Mar 2014 17:53:45 -0700
changeset 20844 2631204d7305
parent 20843 0641b41b0b49
child 20845 bc95143446e8
bundle2: implement the mandatory/advisory logic for parameter Parameter starting with an upper case are mandatory, the one starting with a lower case are advisory and may be ignored.
mercurial/bundle2.py
tests/test-bundle2.t
--- a/mercurial/bundle2.py	Wed Mar 19 17:11:49 2014 -0700
+++ b/mercurial/bundle2.py	Wed Mar 19 17:53:45 2014 -0700
@@ -47,9 +47,10 @@
 
   Empty name are obviously forbidden.
 
-  Name MUST start with a letter. This first character has to be capitalizable.
-  The capitalisation of the first letter will be used to know if an option is
-  advisory or mandatory. This is not implemented yet.
+  Name MUST start with a letter. If this first letter is lower case, the
+  parameter is advisory and can be safefly ignored. However when the first
+  letter is capital, the parameter is mandatory and the bundling process MUST
+  stop if he is not able to proceed it.
 
   Stream parameters use a simple textual format for two main reasons:
 
@@ -173,9 +174,32 @@
                 p = [urllib.unquote(i) for i in p]
                 if len(p) < 2:
                     p.append(None)
+                self._processparam(*p)
                 params[p[0]] = p[1]
         return params
 
+    def _processparam(self, name, value):
+        """process a parameter, applying its effect if needed
+
+        Parameter starting with a lower case letter are advisory and will be
+        ignored when unknown.  Those starting with an upper case letter are
+        mandatory and will this function will raise a KeyError when unknown.
+
+        Note: no option are currently supported. Any input will be either
+              ignored or failing.
+        """
+        if not name:
+            raise ValueError('empty parameter name')
+        if name[0] not in string.letters:
+            raise ValueError('non letter first character: %r' % name)
+        # Some logic will be later added here to try to process the option for
+        # a dict of known parameter.
+        if name[0].islower():
+            self.ui.debug("ignoring unknown parameter %r\n" % name)
+        else:
+            raise KeyError(name)
+
+
     def __iter__(self):
         """yield all parts contained in the stream"""
         # make sure param have been loaded
--- a/tests/test-bundle2.t	Wed Mar 19 17:11:49 2014 -0700
+++ b/tests/test-bundle2.t	Wed Mar 19 17:53:45 2014 -0700
@@ -40,10 +40,14 @@
   > def cmdunbundle2(ui, repo):
   >     """read a bundle2 container from standard input"""
   >     unbundler = bundle2.unbundle20(ui, sys.stdin)
-  >     ui.write('options count: %i\n' % len(unbundler.params))
-  >     for key in sorted(unbundler.params):
+  >     try:
+  >         params = unbundler.params
+  >     except KeyError, exc:
+  >        raise util.Abort('unknown parameters: %s' % exc)
+  >     ui.write('options count: %i\n' % len(params))
+  >     for key in sorted(params):
   >         ui.write('- %s\n' % key)
-  >         value = unbundler.params[key]
+  >         value = params[key]
   >         if value is not None:
   >             ui.write('    %s\n' % value)
   >     parts = list(unbundler)
@@ -159,6 +163,13 @@
   - simple
   parts count:   0
 
+Test unknown mandatory option
+---------------------------------------------------
+
+  $ hg bundle2 --param 'Gravity' | hg unbundle2
+  abort: unknown parameters: 'Gravity'
+  [255]
+
 Test debug output
 ---------------------------------------------------
 
@@ -179,6 +190,8 @@
   $ hg unbundle2 --debug < ../out.hg2
   start processing of HG20 stream
   reading bundle2 stream parameters
+  ignoring unknown parameter 'e|! 7/'
+  ignoring unknown parameter 'simple'
   options count: 2
   - e|! 7/
       babar%#==tutu