exchange: refactor control flow of _getbundlechangegrouppart()
The use of early return makes the control flow of this function much
easier to reason about IMO.
Differential Revision: https://phab.mercurial-scm.org/D4010
--- a/mercurial/exchange.py Mon Jul 02 18:32:20 2018 -0700
+++ b/mercurial/exchange.py Mon Jul 02 18:39:48 2018 -0700
@@ -2096,30 +2096,34 @@
def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None,
b2caps=None, heads=None, common=None, **kwargs):
"""add a changegroup part to the requested bundle"""
- cgstream = None
- if kwargs.get(r'cg', True):
- # build changegroup bundle here.
- version = '01'
- cgversions = b2caps.get('changegroup')
- if cgversions: # 3.1 and 3.2 ship with an empty value
- cgversions = [v for v in cgversions
- if v in changegroup.supportedoutgoingversions(repo)]
- if not cgversions:
- raise ValueError(_('no common changegroup version'))
- version = max(cgversions)
- outgoing = _computeoutgoing(repo, heads, common)
- if outgoing.missing:
- cgstream = changegroup.makestream(repo, outgoing, version, source,
- bundlecaps=bundlecaps)
+ if not kwargs.get(r'cg', True):
+ return
+
+ version = '01'
+ cgversions = b2caps.get('changegroup')
+ if cgversions: # 3.1 and 3.2 ship with an empty value
+ cgversions = [v for v in cgversions
+ if v in changegroup.supportedoutgoingversions(repo)]
+ if not cgversions:
+ raise ValueError(_('no common changegroup version'))
+ version = max(cgversions)
- if cgstream:
- part = bundler.newpart('changegroup', data=cgstream)
- if cgversions:
- part.addparam('version', version)
- part.addparam('nbchanges', '%d' % len(outgoing.missing),
- mandatory=False)
- if 'treemanifest' in repo.requirements:
- part.addparam('treemanifest', '1')
+ outgoing = _computeoutgoing(repo, heads, common)
+ if not outgoing.missing:
+ return
+
+ cgstream = changegroup.makestream(repo, outgoing, version, source,
+ bundlecaps=bundlecaps)
+
+ part = bundler.newpart('changegroup', data=cgstream)
+ if cgversions:
+ part.addparam('version', version)
+
+ part.addparam('nbchanges', '%d' % len(outgoing.missing),
+ mandatory=False)
+
+ if 'treemanifest' in repo.requirements:
+ part.addparam('treemanifest', '1')
@getbundle2partsgenerator('bookmarks')
def _getbundlebookmarkpart(bundler, repo, source, bundlecaps=None,