Patchwork [3,of,4] unbundle: extract the core logic in another function

login
register
mail settings
Submitter Pierre-Yves David
Date April 10, 2014, 5:13 p.m.
Message ID <28d1d63edc24d908d974.1397149996@marginatus.alto.octopoid.net>
Download mbox | patch
Permalink /patch/4281/
State Accepted
Commit 33d5fdd9bd991902851972795459c086b059af8a
Headers show

Comments

Pierre-Yves David - April 10, 2014, 5:13 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@fb.com>
# Date 1396657739 25200
#      Fri Apr 04 17:28:59 2014 -0700
# Node ID 28d1d63edc24d908d974f29605a6af44613c800b
# Parent  f3a8d0e8c734e5aaf76d57d5a080c13f37d64979
unbundle: extract the core logic in another function

The `exchange` module now contains an `unbundle` function that hold the core
unbundle logic. The wire protocol keeps his own unbundle function. It enforce
wireprotocol specific logic and then call the extracted function.

This aims at implemention unbundle for local repo.

Patch

diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -3,10 +3,11 @@ 
 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
 #
 # 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
 import errno
 import util, scmutil, changegroup, base85
@@ -627,5 +628,30 @@  def check_heads(repo, their_heads, conte
             their_heads == ['hashed', heads_hash]):
         # someone else committed/pushed/unbundled while we
         # 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
diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py
--- a/mercurial/wireproto.py
+++ b/mercurial/wireproto.py
@@ -763,25 +763,14 @@  def unbundle(repo, proto, heads):
         fd, tempname = tempfile.mkstemp(prefix='hg-unbundle-')
         fp = os.fdopen(fd, 'wb+')
         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:
             fp.close()
             os.unlink(tempname)