Patchwork [1,of,9,V3] cmdutil: add the class to restore dirstate at unexpected failure easily

login
register
mail settings
Submitter Pierre-Yves David
Date May 12, 2015, 1:41 a.m.
Message ID <55515A66.4030701@ens-lyon.org>
Download mbox | patch
Permalink /patch/9018/
State Not Applicable
Headers show

Comments

Pierre-Yves David - May 12, 2015, 1:41 a.m.
On 05/11/2015 05:49 PM, Pierre-Yves David wrote:
>
>
> On 05/11/2015 08:49 AM, FUJIWARA Katsunori wrote:
>>
>> At Fri, 08 May 2015 19:00:02 -0700,
>> Pierre-Yves David wrote:
>>>
>>>
>>>
>>> On 05/06/2015 08:15 PM, FUJIWARA Katsunori wrote:
>>>> # HG changeset patch
>>>> # User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
>>>> # Date 1430968030 -32400
>>>> #      Thu May 07 12:07:10 2015 +0900
>>>> # Node ID 449a46109f0c96bb8d82d4edf2c8855341558c2f
>>>> # Parent  8174d27576a3fff28fb6f952a4c89d5c0b900214
>>>> cmdutil: add the class to restore dirstate at unexpected failure easily

I've make the following changes to the first patches:

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3274,44 +3274,44 @@  class dirstateguard(object):
      This just removes the backup file at ``close()`` before ``release()``.
      '''

      def __init__(self, repo, name):
          repo.dirstate.write()
-        self.repo = repo
-        self.name = 'dirstate.backup.%s' % name
-        repo.vfs.write(self.name, repo.vfs.tryread('dirstate'))
-        self.active = True
-        self.closed = False
+        self._repo = repo
+        self._filename = 'dirstate.backup.%s.%d' % (name, id(self))
+        repo.vfs.write(self._filename, repo.vfs.tryread('dirstate'))
+        self._active = True
+        self._closed = False

      def __del__(self):
-        if self.active: # still active
+        if self._active: # still active
              # this may occur, even if this class is used correctly:
              # for example, releasing other resources like transaction
              # may raise exception before ``dirstateguard.release`` in
              # ``release(tr, ....)``.
              self._abort()

      def close(self):
-        if not self.active: # already inactivated
+        if not self._active: # already inactivated
              msg = (_("can't close already inactivated backup: %s")
-                   % self.name)
+                   % self._filename)
              raise util.Abort(msg)

-        self.repo.vfs.unlink(self.name)
-        self.active = False
-        self.closed = True
+        self._repo.vfs.unlink(self._filename)
+        self._active = False
+        self._closed = True

      def _abort(self):
          # this "invalidate()" prevents "wlock.release()" from writing
          # changes of dirstate out after restoring to original status
-        self.repo.dirstate.invalidate()
-
-        self.repo.vfs.rename(self.name, 'dirstate')
-        self.active = False
+        self._repo.dirstate.invalidate()
+
+        self._repo.vfs.rename(self._filename, 'dirstate')
+        self._active = False

      def release(self):
-        if not self.closed:
-            if not self.active: # already inactivated
+        if not self._closed:
+            if not self._active: # already inactivated
                  msg = (_("can't release already inactivated backup: %s")
-                       % self.name)
+                       % self._filename)
                  raise util.Abort(msg)
              self._abort()