rewrite: use a lock and transaction as spotted by devel warnings
authorAugie Fackler <augie@google.com>
Thu, 18 Jun 2015 15:46:26 -0400
changeset 1856 7d7f5f9e2f8c
parent 1855 f241a00e93a7
child 1857 a506ed8ab8da
rewrite: use a lock and transaction as spotted by devel warnings
src/topic/__init__.py
--- a/src/topic/__init__.py	Fri Jun 12 16:49:46 2015 -0500
+++ b/src/topic/__init__.py	Thu Jun 18 15:46:26 2015 -0400
@@ -17,11 +17,12 @@
 from mercurial import commands
 from mercurial import context
 from mercurial import extensions
+from mercurial import lock
+from mercurial import merge
 from mercurial import namespaces
 from mercurial import obsolete
 from mercurial import phases
 from mercurial import util
-from mercurial import merge
 
 from . import constants
 from . import revset as topicrevset
@@ -82,29 +83,41 @@
             raise util.Abort("can't change topic of a public change")
         rewrote = 0
         needevolve = False
-        for c in repo.set('%r', change):
-            def filectxfn(repo, ctx, path):
-                try:
-                    return c[path]
-                except error.ManifestLookupError:
-                    return None
-            fixedextra = dict(c.extra())
-            newtopic = None if clear else topic
-            if fixedextra.get(constants.extrakey, None) == topic:
-                continue
-            if clear and constants.extrakey in fixedextra:
-                del fixedextra[constants.extrakey]
-            else:
-                fixedextra[constants.extrakey] = topic
-            c.parents()
-            mc = context.memctx(
-                repo, (c.p1().node(), c.p2().node()), c.description(),
-                c.files(), filectxfn,
-                user=c.user(), date=c.date(), extra=fixedextra)
-            newnode = repo.commitctx(mc)
-            needevolve = needevolve or (len(c.children()) > 0)
-            obsolete.createmarkers(repo, [(c, (repo[newnode],))])
-            rewrote += 1
+        l = repo.lock()
+        txn = repo.transaction('rewrite-topics')
+        try:
+           for c in repo.set('%r', change):
+               def filectxfn(repo, ctx, path):
+                   try:
+                       return c[path]
+                   except error.ManifestLookupError:
+                       return None
+               fixedextra = dict(c.extra())
+               newtopic = None if clear else topic
+               if fixedextra.get(constants.extrakey, None) == topic:
+                   continue
+               if clear and constants.extrakey in fixedextra:
+                   del fixedextra[constants.extrakey]
+               else:
+                   fixedextra[constants.extrakey] = topic
+               c.parents()
+               mc = context.memctx(
+                   repo, (c.p1().node(), c.p2().node()), c.description(),
+                   c.files(), filectxfn,
+                   user=c.user(), date=c.date(), extra=fixedextra)
+               newnode = repo.commitctx(mc)
+               needevolve = needevolve or (len(c.children()) > 0)
+               obsolete.createmarkers(repo, [(c, (repo[newnode],))])
+               rewrote += 1
+           txn.close()
+        except:
+            try:
+                txn.abort()
+            finally:
+                repo.invalidate()
+            raise
+        finally:
+            lock.release(txn, l)
         ui.status('changed topic on %d changes\n' % rewrote)
         if needevolve:
             evolvetarget = 'topic(%s)' % topic if topic else 'not topic()'