changeset 12018:fd206fabc14f

contrib/compress: proper lock handling Lock the src repo before beginning the compression and lock the destination repo before copying the revlogs. Proper handling of exceptions.
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Sun, 22 Aug 2010 18:29:30 +0200
parents 9182f434ec58
children 456f0fed8f17
files contrib/compress.py
diffstat 1 files changed, 45 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/compress.py	Sun Aug 22 18:23:32 2010 +0200
+++ b/contrib/compress.py	Sun Aug 22 18:29:30 2010 +0200
@@ -8,46 +8,59 @@
 
 
 from mercurial import hg, changegroup, localrepo
+from mercurial.lock import release
+import weakref
 
 def compress(ui, repo, dest):
     dest = hg.localpath(ui.expandpath(dest))
     target = localrepo.instance(ui, dest, create=True)
-    tr = target.transaction("compress")
-    src_cl = repo.changelog
-    tar_cl = target.changelog
-    changedfiles = set()
-    mmfs = {}
-    collect = changegroup.collector(src_cl, mmfs, changedfiles)
-    total = len(repo)
+
+    tr = lock = tlock = None
+    try:
+        lock = repo.lock()
+        tlock = target.lock()
+        tr = target.transaction("compress")
+        trp = weakref.proxy(tr)
 
-    for r in src_cl:
-        p = [src_cl.node(i) for i in src_cl.parentrevs(r)]
-        nd = tar_cl.addrevision(src_cl.revision(src_cl.node(r)), tr,
-                                 src_cl.linkrev(r), p[0], p[1])
-        collect(nd)
-        ui.progress(('adding changesets'), r, unit=('revisions'),
-                    total=total)
+        src_cl = repo.changelog
+        tar_cl = target.changelog
+        changedfiles = set()
+        mmfs = {}
+        collect = changegroup.collector(src_cl, mmfs, changedfiles)
+        total = len(repo)
+
+        for r in src_cl:
+            p = [src_cl.node(i) for i in src_cl.parentrevs(r)]
+            nd = tar_cl.addrevision(src_cl.revision(src_cl.node(r)), trp,
+                                     src_cl.linkrev(r), p[0], p[1])
+            collect(nd)
+            ui.progress(('adding changesets'), r, unit=('revisions'),
+                        total=total)
 
-    src_mnfst = repo.manifest
-    tar_mnfst = target.manifest
-    for r in src_mnfst:
-        p = [src_mnfst.node(i) for i in src_mnfst.parentrevs(r)]
-        tar_mnfst.addrevision(src_mnfst.revision(src_mnfst.node(r)), tr,
-                               src_mnfst.linkrev(r), p[0], p[1])
-        ui.progress(('adding manifest'), r, unit=('revisions'),
-                    total=total)
+        src_mnfst = repo.manifest
+        tar_mnfst = target.manifest
+        for r in src_mnfst:
+            p = [src_mnfst.node(i) for i in src_mnfst.parentrevs(r)]
+            tar_mnfst.addrevision(src_mnfst.revision(src_mnfst.node(r)), trp,
+                                   src_mnfst.linkrev(r), p[0], p[1])
+            ui.progress(('adding manifest'), r, unit=('revisions'),
+                        total=total)
 
-    total = len(changedfiles)
-    for cnt, f in enumerate(changedfiles):
-        sf = repo.file(f)
-        tf = target.file(f)
-        for r in sf:
-            p = [sf.node(i) for i in sf.parentrevs(r)]
-            tf.addrevision(sf.revision(sf.node(r)), tr, sf.linkrev(r),
+        total = len(changedfiles)
+        for cnt, f in enumerate(changedfiles):
+            sf = repo.file(f)
+            tf = target.file(f)
+            for r in sf:
+                p = [sf.node(i) for i in sf.parentrevs(r)]
+                tf.addrevision(sf.revision(sf.node(r)), trp, sf.linkrev(r),
                                p[0], p[1])
-        ui.progress(('adding files'), cnt, item=f, unit=('file'), total=total)
-
-    tr.close()
+            ui.progress(('adding files'), cnt, item=f, unit=('file'),
+                        total=total)
+        tr.close()
+    finally:
+        if tr:
+            tr.release()
+        release(tlock, lock)
 
 cmdtable = {
     "compress" : (compress, [], "DEST")