deprecation: fix dirstate.beginparentchange deprecation warning stable
authorBoris Feld <boris.feld@octobus.net>
Mon, 22 May 2017 12:00:57 +0200
branchstable
changeset 2465 479646a3873a
parent 2464 2b53a2a21bbb
child 2466 e5e502407ab0
child 2470 5d015dfd7753
child 2471 0af99106b075
deprecation: fix dirstate.beginparentchange deprecation warning Replace calls to dirstart.beginparentchange and dirstate.endparentchange to dirstate.parentchange context manager.
hgext3rd/evolve/__init__.py
--- a/hgext3rd/evolve/__init__.py	Mon May 22 14:57:47 2017 +0200
+++ b/hgext3rd/evolve/__init__.py	Mon May 22 12:00:57 2017 +0200
@@ -145,6 +145,7 @@
     commands,
     context,
     copies,
+    dirstate,
     error,
     extensions,
     help,
@@ -221,6 +222,29 @@
 reposetup = eh.final_reposetup
 cmdtable = eh.cmdtable
 
+# pre hg 4.0 compat
+
+if not util.safehasattr(dirstate.dirstate, 'parentchange'):
+    import contextlib
+
+    @contextlib.contextmanager
+    def parentchange(self):
+        '''Context manager for handling dirstate parents.
+
+        If an exception occurs in the scope of the context manager,
+        the incoherent dirstate won't be written when wlock is
+        released.
+        '''
+        self._parentwriters += 1
+        yield
+        # Typically we want the "undo" step of a context manager in a
+        # finally block so it happens even when an exception
+        # occurs. In this case, however, we only want to decrement
+        # parentwriters if the code in the with statement exits
+        # normally, so we don't have a try/finally here on purpose.
+        self._parentwriters -= 1
+    dirstate.dirstate.parentchange = parentchange
+
 #####################################################################
 ### Option configuration                                          ###
 #####################################################################
@@ -866,12 +890,11 @@
                                 '(see hg help resolve)'))
         nodenew = _relocatecommit(repo, orig, commitmsg)
     except error.Abort as exc:
-        repo.dirstate.beginparentchange()
-        repo.setparents(repo['.'].node(), nullid)
-        repo.dirstate.write(tr)
-        # fix up dirstate for copies and renames
-        copies.duplicatecopies(repo, dest.rev(), orig.p1().rev())
-        repo.dirstate.endparentchange()
+        with repo.dirstate.parentchange():
+            repo.setparents(repo['.'].node(), nullid)
+            repo.dirstate.write(tr)
+            # fix up dirstate for copies and renames
+            copies.duplicatecopies(repo, dest.rev(), orig.p1().rev())
 
         class LocalMergeFailure(MergeFailure, exc.__class__):
             pass
@@ -1780,9 +1803,8 @@
     bmupdate(newid)
     repo.ui.status(_('committed as %s\n') % node.short(newid))
     # reroute the working copy parent to the new changeset
-    repo.dirstate.beginparentchange()
-    repo.dirstate.setparents(newid, node.nullid)
-    repo.dirstate.endparentchange()
+    with repo.dirstate.parentchange():
+        repo.dirstate.setparents(newid, node.nullid)
 
 def _solvedivergent(ui, repo, divergent, dryrun=False, confirm=False,
                     progresscb=None):
@@ -1882,9 +1904,8 @@
     assert tr is not None
     try:
         repo.ui.setconfig('ui', 'allowemptycommit', True, 'evolve')
-        repo.dirstate.beginparentchange()
-        repo.dirstate.setparents(divergent.node(), node.nullid)
-        repo.dirstate.endparentchange()
+        with repo.dirstate.parentchange():
+            repo.dirstate.setparents(divergent.node(), node.nullid)
         oldlen = len(repo)
         amend(ui, repo, message='', logfile='')
         if oldlen == len(repo):
@@ -2537,10 +2558,9 @@
         # Move local changes on filtered changeset
         obsolete.createmarkers(repo, [(old, (repo[newid],))])
         phases.retractboundary(repo, tr, oldphase, [newid])
-        repo.dirstate.beginparentchange()
-        repo.dirstate.setparents(newid, node.nullid)
-        _uncommitdirstate(repo, old, match)
-        repo.dirstate.endparentchange()
+        with repo.dirstate.parentchange():
+            repo.dirstate.setparents(newid, node.nullid)
+            _uncommitdirstate(repo, old, match)
         updatebookmarks(newid)
         if not repo[newid].files():
             ui.warn(_("new changeset is empty\n"))
@@ -2773,9 +2793,8 @@
                 obsolete.createmarkers(repo, [(ctx, (repo[new],))])
             phases.retractboundary(repo, tr, ctx.phase(), [new])
             if ctx in repo[None].parents():
-                repo.dirstate.beginparentchange()
-                repo.dirstate.setparents(new, node.nullid)
-                repo.dirstate.endparentchange()
+                with repo.dirstate.parentchange():
+                    repo.dirstate.setparents(new, node.nullid)
         tr.close()
     finally:
         lockmod.release(tr, lock, wlock)