# HG changeset patch # User Pierre-Yves David # Date 1395276825 25200 # Node ID 2631204d73052c8e2b8113083a90bc3f9a0ebf18 # Parent 0641b41b0b4968d4d984d7a7302a7df058d31924 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. diff -r 0641b41b0b49 -r 2631204d7305 mercurial/bundle2.py --- 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 diff -r 0641b41b0b49 -r 2631204d7305 tests/test-bundle2.t --- 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