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

Gregory Szorc gregory.szorc at gmail.com
Mon Aug 1 18:18:18 UTC 2016


# 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')


More information about the Mercurial-devel mailing list