[PATCH 1 of 7 v2] dirstate: introduce new context manager for marking dirstate parent changes

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 1 of 7 v2] dirstate: introduce new context manager for marking dirstate parent changes

Augie Fackler-2
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495141830 14400
#      Thu May 18 17:10:30 2017 -0400
# Node ID 2f0dd5450eedbf11e49a529544654b3b399310c2
# Parent  531e6a57abd252bef59a5921e3761f1e5d80abba
dirstate: introduce new context manager for marking dirstate parent changes

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -8,6 +8,7 @@
 from __future__ import absolute_import
 
 import collections
+import contextlib
 import errno
 import os
 import stat
@@ -99,6 +100,23 @@ class dirstate(object):
         # for consistent view between _pl() and _read() invocations
         self._pendingmode = None
 
+    @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
+
     def beginparentchange(self):
         '''Marks the beginning of a set of changes that involve changing
         the dirstate parents. If there is an exception during this time,
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 2 of 7 v2] mq: migrate to context manager for changing dirstate parents

Augie Fackler-2
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495141853 14400
#      Thu May 18 17:10:53 2017 -0400
# Node ID f495181d4e42e53d4f3b1a9ca5132ff6f7703890
# Parent  2f0dd5450eedbf11e49a529544654b3b399310c2
mq: migrate to context manager for changing dirstate parents

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -932,14 +932,13 @@ class queue(object):
                         merged.append(f)
                     else:
                         removed.append(f)
-                repo.dirstate.beginparentchange()
-                for f in removed:
-                    repo.dirstate.remove(f)
-                for f in merged:
-                    repo.dirstate.merge(f)
-                p1, p2 = repo.dirstate.parents()
-                repo.setparents(p1, merge)
-                repo.dirstate.endparentchange()
+                with repo.dirstate.parentchange():
+                    for f in removed:
+                        repo.dirstate.remove(f)
+                    for f in merged:
+                        repo.dirstate.merge(f)
+                    p1, p2 = repo.dirstate.parents()
+                    repo.setparents(p1, merge)
 
             if all_files and '.hgsubstate' in all_files:
                 wctx = repo[None]
@@ -1580,16 +1579,15 @@ class queue(object):
                 if keepchanges and tobackup:
                     raise error.Abort(_("local changes found, qrefresh first"))
                 self.backup(repo, tobackup)
-                repo.dirstate.beginparentchange()
-                for f in a:
-                    repo.wvfs.unlinkpath(f, ignoremissing=True)
-                    repo.dirstate.drop(f)
-                for f in m + r:
-                    fctx = ctx[f]
-                    repo.wwrite(f, fctx.data(), fctx.flags())
-                    repo.dirstate.normal(f)
-                repo.setparents(qp, nullid)
-                repo.dirstate.endparentchange()
+                with repo.dirstate.parentchange():
+                    for f in a:
+                        repo.wvfs.unlinkpath(f, ignoremissing=True)
+                        repo.dirstate.drop(f)
+                    for f in m + r:
+                        fctx = ctx[f]
+                        repo.wwrite(f, fctx.data(), fctx.flags())
+                        repo.dirstate.normal(f)
+                    repo.setparents(qp, nullid)
             for patch in reversed(self.applied[start:end]):
                 self.ui.status(_("popping %s\n") % patch.name)
             del self.applied[start:end]
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 3 of 7 v2] rebase: migrate to context manager for changing dirstate parents

Augie Fackler-2
In reply to this post by Augie Fackler-2
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495141861 14400
#      Thu May 18 17:11:01 2017 -0400
# Node ID 96a232ad4958bad1de7366360fecac987ca51721
# Parent  f495181d4e42e53d4f3b1a9ca5132ff6f7703890
rebase: migrate to context manager for changing dirstate parents

diff --git a/hgext/rebase.py b/hgext/rebase.py
--- a/hgext/rebase.py
+++ b/hgext/rebase.py
@@ -427,9 +427,8 @@ class rebaseruntime(object):
                         mergemod.mergestate.clean(repo)
                 else:
                     # Skip commit if we are collapsing
-                    repo.dirstate.beginparentchange()
-                    repo.setparents(repo[p1].node())
-                    repo.dirstate.endparentchange()
+                    with repo.dirstate.parentchange():
+                        repo.setparents(repo[p1].node())
                     newnode = None
                 # Update the state
                 if newnode is not None:
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 4 of 7 v2] context: migrate to context manager for changing dirstate parents

Augie Fackler-2
In reply to this post by Augie Fackler-2
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495141867 14400
#      Thu May 18 17:11:07 2017 -0400
# Node ID 3a82feb644a3cc6c6b9e81a28a155ceb204e0ca5
# Parent  96a232ad4958bad1de7366360fecac987ca51721
context: migrate to context manager for changing dirstate parents

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -1447,13 +1447,12 @@ class committablectx(basectx):
 
         """
 
-        self._repo.dirstate.beginparentchange()
-        for f in self.modified() + self.added():
-            self._repo.dirstate.normal(f)
-        for f in self.removed():
-            self._repo.dirstate.drop(f)
-        self._repo.dirstate.setparents(node)
-        self._repo.dirstate.endparentchange()
+        with self._repo.dirstate.parentchange():
+            for f in self.modified() + self.added():
+                self._repo.dirstate.normal(f)
+            for f in self.removed():
+                self._repo.dirstate.drop(f)
+            self._repo.dirstate.setparents(node)
 
         # write changes out explicitly, because nesting wlock at
         # runtime may prevent 'wlock.release()' in 'repo.commit()'
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 5 of 7 v2] localrepo: migrate to context manager for changing dirstate parents

Augie Fackler-2
In reply to this post by Augie Fackler-2
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495141874 14400
#      Thu May 18 17:11:14 2017 -0400
# Node ID 5bd022476c63199c76166cb6157b5923181a5290
# Parent  3a82feb644a3cc6c6b9e81a28a155ceb204e0ca5
localrepo: migrate to context manager for changing dirstate parents

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -850,21 +850,20 @@ class localrepository(object):
         return self[changeid]
 
     def setparents(self, p1, p2=nullid):
-        self.dirstate.beginparentchange()
-        copies = self.dirstate.setparents(p1, p2)
-        pctx = self[p1]
-        if copies:
-            # Adjust copy records, the dirstate cannot do it, it
-            # requires access to parents manifests. Preserve them
-            # only for entries added to first parent.
-            for f in copies:
-                if f not in pctx and copies[f] in pctx:
-                    self.dirstate.copy(copies[f], f)
-        if p2 == nullid:
-            for f, s in sorted(self.dirstate.copies().items()):
-                if f not in pctx and s not in pctx:
-                    self.dirstate.copy(None, f)
-        self.dirstate.endparentchange()
+        with self.dirstate.parentchange():
+            copies = self.dirstate.setparents(p1, p2)
+            pctx = self[p1]
+            if copies:
+                # Adjust copy records, the dirstate cannot do it, it
+                # requires access to parents manifests. Preserve them
+                # only for entries added to first parent.
+                for f in copies:
+                    if f not in pctx and copies[f] in pctx:
+                        self.dirstate.copy(copies[f], f)
+            if p2 == nullid:
+                for f, s in sorted(self.dirstate.copies().items()):
+                    if f not in pctx and s not in pctx:
+                        self.dirstate.copy(None, f)
 
     def filectx(self, path, changeid=None, fileid=None):
         """changeid can be a changeset revision, node, or tag.
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 6 of 7 v2] merge: migrate to context manager for changing dirstate parents

Augie Fackler-2
In reply to this post by Augie Fackler-2
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495141884 14400
#      Thu May 18 17:11:24 2017 -0400
# Node ID bf1e831b74a414ce05f006176c0f1d79a6f8dcd0
# Parent  5bd022476c63199c76166cb6157b5923181a5290
merge: migrate to context manager for changing dirstate parents

diff --git a/mercurial/merge.py b/mercurial/merge.py
--- a/mercurial/merge.py
+++ b/mercurial/merge.py
@@ -1696,15 +1696,14 @@ def update(repo, node, branchmerge, forc
         stats = applyupdates(repo, actions, wc, p2, overwrite, labels=labels)
 
         if not partial:
-            repo.dirstate.beginparentchange()
-            repo.setparents(fp1, fp2)
-            recordupdates(repo, actions, branchmerge)
-            # update completed, clear state
-            util.unlink(repo.vfs.join('updatestate'))
+            with repo.dirstate.parentchange():
+                repo.setparents(fp1, fp2)
+                recordupdates(repo, actions, branchmerge)
+                # update completed, clear state
+                util.unlink(repo.vfs.join('updatestate'))
 
-            if not branchmerge:
-                repo.dirstate.setbranch(p2.branch())
-            repo.dirstate.endparentchange()
+                if not branchmerge:
+                    repo.dirstate.setbranch(p2.branch())
 
     if not partial:
         repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
@@ -1742,10 +1741,9 @@ def graft(repo, ctx, pctx, labels, keepp
         parents.remove(pctx)
         pother = parents[0].node()
 
-    repo.dirstate.beginparentchange()
-    repo.setparents(repo['.'].node(), pother)
-    repo.dirstate.write(repo.currenttransaction())
-    # fix up dirstate for copies and renames
-    copies.duplicatecopies(repo, ctx.rev(), pctx.rev())
-    repo.dirstate.endparentchange()
+    with repo.dirstate.parentchange():
+        repo.setparents(repo['.'].node(), pother)
+        repo.dirstate.write(repo.currenttransaction())
+        # fix up dirstate for copies and renames
+        copies.duplicatecopies(repo, ctx.rev(), pctx.rev())
     return stats
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 7 of 7 v2] dirstate: mark {begin, end}parentchange as deprecated

Augie Fackler-2
In reply to this post by Augie Fackler-2
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495142012 14400
#      Thu May 18 17:13:32 2017 -0400
# Node ID 712593263a6be0c7250a2f31dbb1693084410916
# Parent  bf1e831b74a414ce05f006176c0f1d79a6f8dcd0
dirstate: mark {begin,end}parentchange as deprecated

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -124,6 +124,8 @@ class dirstate(object):
         prevents writing an incoherent dirstate where the parent doesn't
         match the contents.
         '''
+        self._ui.deprecwarn('beginparentchange is obsoleted by the '
+                            'parentchange context manager.', '4.3')
         self._parentwriters += 1
 
     def endparentchange(self):
@@ -131,6 +133,8 @@ class dirstate(object):
         dirstate parents. Once all parent changes have been marked done,
         the wlock will be free to write the dirstate on release.
         '''
+        self._ui.deprecwarn('endparentchange is obsoleted by the '
+                            'parentchange context manager.', '4.3')
         if self._parentwriters > 0:
             self._parentwriters -= 1
 
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 1 of 7 v2] dirstate: introduce new context manager for marking dirstate parent changes

Gregory Szorc
In reply to this post by Augie Fackler-2
On Fri, May 19, 2017 at 2:38 PM, Augie Fackler <[hidden email]> wrote:
# HG changeset patch
# User Augie Fackler <[hidden email]>
# Date 1495141830 14400
#      Thu May 18 17:10:30 2017 -0400
# Node ID 2f0dd5450eedbf11e49a529544654b3b399310c2
# Parent  531e6a57abd252bef59a5921e3761f1e5d80abba
dirstate: introduce new context manager for marking dirstate parent changes

I wish this were "changeparent" instead of "parentchange." But you are just reusing the existing terminology. So meh.

Queued for default.
 

diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -8,6 +8,7 @@
 from __future__ import absolute_import

 import collections
+import contextlib
 import errno
 import os
 import stat
@@ -99,6 +100,23 @@ class dirstate(object):
         # for consistent view between _pl() and _read() invocations
         self._pendingmode = None

+    @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
+
     def beginparentchange(self):
         '''Marks the beginning of a set of changes that involve changing
         the dirstate parents. If there is an exception during this time,
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH 7 of 7 v2] dirstate: mark {begin, end}parentchange as deprecated

Yuya Nishihara
In reply to this post by Augie Fackler-2
On Fri, 19 May 2017 17:38:47 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <[hidden email]>
> # Date 1495142012 14400
> #      Thu May 18 17:13:32 2017 -0400
> # Node ID 712593263a6be0c7250a2f31dbb1693084410916
> # Parent  bf1e831b74a414ce05f006176c0f1d79a6f8dcd0
> dirstate: mark {begin,end}parentchange as deprecated

Flagged this as (API).
_______________________________________________
Mercurial-devel mailing list
[hidden email]
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel