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

login
register
mail settings
Submitter Augie Fackler
Date May 19, 2017, 9:38 p.m.
Message ID <2f0dd5450eedbf11e49a.1495229921@augie-macbookpro2.roam.corp.google.com>
Download mbox | patch
Permalink /patch/20740/
State Accepted
Headers show

Comments

Augie Fackler - May 19, 2017, 9:38 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# 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
Gregory Szorc - May 20, 2017, 3:57 a.m.
On Fri, May 19, 2017 at 2:38 PM, Augie Fackler <raf@durin42.com> wrote:

> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # 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
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>

Patch

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,