# HG changeset patch
# User Matt Mackall <mpm@selenic.com>
# Date 1277491648 18000
#      Fri Jun 25 13:47:28 2010 -0500
# Branch stable
# Node ID ee1ed6afac219c578a6bd2c9a85c2ef354fbb4b3
# Parent  d74fe370ab04fcc1d4bb0180ba28191316b7ee44
addchangegroup: pass in lock to release it before changegroup hook is called

Currently, callers of addchangegroup first acquire the repository
lock, usually to check that an unbundle request isn't racing. This
means that changegroup hook actions that might write to a repo get
stuck waiting for a lock. Here, we add a new optional lock parameter
and update all the callers. Post-1.6 we may make it non-optional.

diff --git a/hgext/bookmarks.py b/hgext/bookmarks.py
--- a/hgext/bookmarks.py
+++ b/hgext/bookmarks.py
@@ -339,11 +339,10 @@
 
             return result
 
-        def addchangegroup(self, source, srctype, url, emptyok=False):
+        def addchangegroup(self, *args, **kwargs):
             parents = self.dirstate.parents()
 
-            result = super(bookmark_repo, self).addchangegroup(
-                source, srctype, url, emptyok)
+            result = super(bookmark_repo, self).addchangegroup(*args, **kwargs)
             if result > 1:
                 # We have more heads than before
                 return result
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3745,7 +3745,8 @@
         for fname in fnames:
             f = url.open(ui, fname)
             gen = changegroup.readbundle(f, fname)
-            modheads = repo.addchangegroup(gen, 'unbundle', 'bundle:' + fname)
+            modheads = repo.addchangegroup(gen, 'unbundle', 'bundle:' + fname,
+                                           lock=lock)
     finally:
         lock.release()
 
diff --git a/mercurial/hgweb/protocol.py b/mercurial/hgweb/protocol.py
--- a/mercurial/hgweb/protocol.py
+++ b/mercurial/hgweb/protocol.py
@@ -164,7 +164,7 @@
                           urllib.quote(req.env.get('REMOTE_HOST', '')),
                           urllib.quote(req.env.get('REMOTE_USER', '')))
                     try:
-                        ret = repo.addchangegroup(gen, 'serve', url)
+                        ret = repo.addchangegroup(gen, 'serve', url, lock=lock)
                     except util.Abort, inst:
                         sys.stdout.write("abort: %s\n" % inst)
                         ret = 0
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1199,7 +1199,7 @@
                                        "other repository doesn't support "
                                        "changegroupsubset."))
                 cg = remote.changegroupsubset(fetch, heads, 'pull')
-            return self.addchangegroup(cg, 'pull', remote.url())
+            return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
         finally:
             lock.release()
 
@@ -1233,8 +1233,8 @@
             ret = discovery.prepush(self, remote, force, revs, newbranch)
             if ret[0] is not None:
                 cg, remote_heads = ret
-                # here, we return an integer indicating remote head count change
-                return remote.addchangegroup(cg, 'push', self.url())
+                # we return an integer indicating remote head count change
+                return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
             # and here we return 0 for "nothing to push" or 1 for
             # "something to push but I refuse"
             return ret[1]
@@ -1620,7 +1620,7 @@
 
         return util.chunkbuffer(gengroup())
 
-    def addchangegroup(self, source, srctype, url, emptyok=False):
+    def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
         """Add the changegroup returned by source.read() to this repo.
         srctype is a string like 'push', 'pull', or 'unbundle'.  url is
         the URL of the repo where this changegroup is coming from.
@@ -1760,6 +1760,8 @@
             tr.close()
         finally:
             tr.release()
+            if lock:
+                lock.release()
 
         if changesets > 0:
             # forcefully update the on-disk branch cache
diff --git a/mercurial/sshserver.py b/mercurial/sshserver.py
--- a/mercurial/sshserver.py
+++ b/mercurial/sshserver.py
@@ -161,7 +161,8 @@
             return
 
         self.respond("")
-        r = self.repo.addchangegroup(self.fin, 'serve', self.client_url())
+        r = self.repo.addchangegroup(self.fin, 'serve', self.client_url(),
+                                     lock=self.lock)
         self.respond(str(r))
 
     def client_url(self):
@@ -205,7 +206,8 @@
                 # push can proceed
 
                 fp.seek(0)
-                r = self.repo.addchangegroup(fp, 'serve', self.client_url())
+                r = self.repo.addchangegroup(fp, 'serve', self.client_url(),
+                                             lock=self.lock)
                 self.respond(str(r))
             finally:
                 if not was_locked: