[PATCH 1 of 9 changegroup-apis] exchange: use early return

Gregory Szorc gregory.szorc at gmail.com
Mon Aug 1 14:20:31 EDT 2016


This is the first half of the series. The second half can be found at
https://hg.mozilla.org/users/gszorc_mozilla.com/hg/rev/26eeb1e66e88 if
someone wants to review the whole thing.

Word of warning: if you pull that repo, you'll get tons of obsolescence
markers. You may want to disable obsolescence when pulling.

On Mon, Aug 1, 2016 at 11:18 AM, Gregory Szorc <gregory.szorc at gmail.com>
wrote:

> # HG changeset patch
> # User Gregory Szorc <gregory.szorc at gmail.com>
> # Date 1469900681 25200
> #      Sat Jul 30 10:44:41 2016 -0700
> # Node ID 18275f2b6d2b8fcb50ecaf331011e3d233ca1cfa
> # Parent  73ff159923c1f05899c27238409ca398342d9ae0
> exchange: use early return
>
> Avoid excessive indentation and confusing control flow by returning
> early if there is no work to be done.
>
> diff --git a/mercurial/exchange.py b/mercurial/exchange.py
> --- a/mercurial/exchange.py
> +++ b/mercurial/exchange.py
> @@ -1547,39 +1547,42 @@ def getbundle(repo, source, heads=None,
>               **kwargs)
>
>      return util.chunkbuffer(bundler.getchunks())
>
>  @getbundle2partsgenerator('changegroup')
>  def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None,
>                                b2caps=None, heads=None, common=None,
> **kwargs):
>      """add a changegroup part to the requested bundle"""
> -    cg = None
> -    if kwargs.get('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 = changegroup.computeoutgoing(repo, heads, common)
> -        cg = changegroup.getlocalchangegroupraw(repo, source, outgoing,
> -                                                bundlecaps=bundlecaps,
> -                                                version=version)
> +    if not kwargs.get('cg', True):
> +        return
>
> -    if cg:
> -        part = bundler.newpart('changegroup', data=cg)
> -        if cgversions:
> -            part.addparam('version', version)
> -        part.addparam('nbchanges', str(len(outgoing.missing)),
> mandatory=False)
> -        if 'treemanifest' in repo.requirements:
> -            part.addparam('treemanifest', '1')
> +    # 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 = changegroup.computeoutgoing(repo, heads, common)
> +    cg = changegroup.getlocalchangegroupraw(repo, source, outgoing,
> +                                            bundlecaps=bundlecaps,
> +                                            version=version)
> +
> +    if not cg:
> +        return
> +
> +    part = bundler.newpart('changegroup', data=cg)
> +    if cgversions:
> +        part.addparam('version', version)
> +    part.addparam('nbchanges', str(len(outgoing.missing)),
> mandatory=False)
> +    if 'treemanifest' in repo.requirements:
> +        part.addparam('treemanifest', '1')
>
>  @getbundle2partsgenerator('listkeys')
>  def _getbundlelistkeysparts(bundler, repo, source, bundlecaps=None,
>                              b2caps=None, **kwargs):
>      """add parts containing listkeys namespaces to the requested bundle"""
>      listkeys = kwargs.get('listkeys', ())
>      for namespace in listkeys:
>          part = bundler.newpart('listkeys')
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mercurial-scm.org/pipermail/mercurial-devel/attachments/20160801/b7a69c00/attachment.html>


More information about the Mercurial-devel mailing list