changeset 20968:33d5fdd9bd99

unbundle: extract the core logic in another function The `exchange` module now contains an `unbundle` function that holds the core unbundle logic. The wire protocol keeps its own unbundle function. It enforces wireprotocol-specific logic and then calls the extracted function. This aims at implementing unbundle for localrepo.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Fri, 04 Apr 2014 17:28:59 -0700
parents 984850270acb
children 7a679918ee2b
files mercurial/exchange.py mercurial/wireproto.py
diffstat 2 files changed, 30 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/exchange.py	Fri Apr 04 17:15:25 2014 -0700
+++ b/mercurial/exchange.py	Fri Apr 04 17:28:59 2014 -0700
@@ -5,6 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import sys
 from i18n import _
 from node import hex, nullid
 import cStringIO
@@ -628,3 +629,28 @@
         # were transferring data
         raise PushRaced('repository changed while %s - '
                         'please try again' % context)
+
+def unbundle(repo, cg, heads, source, url):
+    """Apply a bundle to a repo.
+
+    this function makes sure the repo is locked during the application and have
+    mechanism to check that no push race occured between the creation of the
+    bundle and its application.
+
+    If the push was raced as PushRaced exception is raised."""
+    r = 0
+    lock = repo.lock()
+    try:
+        check_heads(repo, heads, 'uploading changes')
+        # push can proceed
+        try:
+            r = changegroup.addchangegroup(repo, cg, source, url)
+        except util.Abort, inst:
+            # The old code we moved used sys.stderr directly.
+            # We did not changed it to minise code change.
+            # This need to be moved to something proper.
+            # Feel free to do it.
+            sys.stderr.write("abort: %s\n" % inst)
+    finally:
+        lock.release()
+    return r
--- a/mercurial/wireproto.py	Fri Apr 04 17:15:25 2014 -0700
+++ b/mercurial/wireproto.py	Fri Apr 04 17:28:59 2014 -0700
@@ -765,21 +765,10 @@
         r = 0
         try:
             proto.getfile(fp)
-            lock = repo.lock()
-            try:
-                exchange.check_heads(repo, their_heads, 'uploading changes')
-
-                # push can proceed
-                fp.seek(0)
-                gen = changegroupmod.readbundle(fp, None)
-
-                try:
-                    r = changegroupmod.addchangegroup(repo, gen, 'serve',
-                                                      proto._client())
-                except util.Abort, inst:
-                    sys.stderr.write("abort: %s\n" % inst)
-            finally:
-                lock.release()
+            fp.seek(0)
+            gen = changegroupmod.readbundle(fp, None)
+            r = exchange.unbundle(repo, gen, their_heads, 'serve',
+                                  proto._client())
             return pushres(r)
 
         finally: