# HG changeset patch # User Pierre-Yves David # Date 2014-04-03 05:24:44 # Node ID 329cd74b52bd9a2e72e179b5e49276c0c62b949e # Parent c33d7bf53812e963cea5fa9fe54caae7adbe8d99 bundle2: introduce a bundleoperation object This object will hold all data and state gathered through the processing of a bundle. This will allow: - each handler to be aware of the things unbundled so far - the caller to retrieve data about the execution - bear useful object and logic (like repo, transaction) - bear possible bundle2 reply triggered by the unbundling. For now the object is very simple but it will grow at the same time as the bundle2 implementation. diff --git a/mercurial/bundle2.py b/mercurial/bundle2.py --- a/mercurial/bundle2.py +++ b/mercurial/bundle2.py @@ -183,6 +183,26 @@ def parthandler(parttype): return func return _decorator +class bundleoperation(object): + """an object that represents a single bundling process + + Its purpose is to carry unbundle-related objects and states. + + A new object should be created at the beginning of each bundle processing. + The object is to be returned by the processing function. + + The object has very little content now it will ultimately contain: + * an access to the repo the bundle is applied to, + * a ui object, + * a way to retrieve a transaction to add changes to the repo, + * a way to record the result of processing each part, + * a way to construct a bundle response when applicable. + """ + + def __init__(self, repo): + self.repo = repo + self.ui = repo.ui + def processbundle(repo, unbundler): """This function process a bundle, apply effect to/from a repo @@ -194,7 +214,7 @@ def processbundle(repo, unbundler): Unknown Mandatory part will abort the process. """ - ui = repo.ui + op = bundleoperation(repo) # todo: # - replace this is a init function soon. # - exception catching @@ -207,17 +227,17 @@ def processbundle(repo, unbundler): key = parttype.lower() try: handler = parthandlermapping[key] - ui.debug('found an handler for part %r\n' % parttype) + op.ui.debug('found a handler for part %r\n' % parttype) except KeyError: if key != parttype: # mandatory parts # todo: # - use a more precise exception raise - ui.debug('ignoring unknown advisory part %r\n' % key) + op.ui.debug('ignoring unknown advisory part %r\n' % key) # todo: # - consume the part once we use streaming continue - handler(repo, part) + handler(op, part) except Exception: for part in iterparts: pass # consume the bundle content diff --git a/tests/test-bundle2.t b/tests/test-bundle2.t --- a/tests/test-bundle2.t +++ b/tests/test-bundle2.t @@ -21,11 +21,11 @@ Create an extension to test bundle2 API > assert len(ELEPHANTSSONG) == 178 # future test say 178 bytes, trust it. > > @bundle2.parthandler('test:song') - > def songhandler(repo, part): + > def songhandler(op, part): > """handle a "test:song" bundle2 part, printing the lyrics on stdin""" - > repo.ui.write('The choir start singing:\n') + > op.ui.write('The choir starts singing:\n') > for line in part.data.split('\n'): - > repo.ui.write(' %s\n' % line) + > op.ui.write(' %s\n' % line) > > @command('bundle2', > [('', 'param', [], 'stream level parameter'),