Patchwork [3,of,6,V2] dirstate: move code paths for backup from dirstateguard to dirstate

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Oct. 13, 2015, 5:54 p.m.
Message ID <636e8f3548e17963ba27.1444758892@feefifofum>
Download mbox | patch
Permalink /patch/11013/
State Accepted
Headers show

Comments

Katsunori FUJIWARA - Oct. 13, 2015, 5:54 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1444758557 -32400
#      Wed Oct 14 02:49:17 2015 +0900
# Node ID 636e8f3548e17963ba271fa483b15acdee1b0f3f
# Parent  04f94f9be97fcff64587440aa1e1ec3afb4f7639
dirstate: move code paths for backup from dirstateguard to dirstate

This can centralize the logic to write in-memory changes out correctly
according to transaction activity into dirstate.

Passing 'repo' object to newly added functions is needed to examine
current transaction activity in subsequent patches, because 'dirstate'
itself doesn't have direct reference to it.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3330,10 +3330,9 @@ 
     '''
 
     def __init__(self, repo, name):
-        repo.dirstate.write()
         self._repo = repo
-        self._filename = 'dirstate.backup.%s.%d' % (name, id(self))
-        repo.vfs.write(self._filename, repo.vfs.tryread('dirstate'))
+        self._suffix = '.backup.%s.%d' % (name, id(self))
+        repo.dirstate._savebackup(repo, self._suffix)
         self._active = True
         self._closed = False
 
@@ -3347,27 +3346,24 @@ 
 
     def close(self):
         if not self._active: # already inactivated
-            msg = (_("can't close already inactivated backup: %s")
-                   % self._filename)
+            msg = (_("can't close already inactivated backup: dirstate%s")
+                   % self._suffix)
             raise error.Abort(msg)
 
-        self._repo.vfs.unlink(self._filename)
+        self._repo.dirstate._clearbackup(self._repo, self._suffix)
         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._filename, 'dirstate')
+        self._repo.dirstate._restorebackup(self._repo, self._suffix)
         self._active = False
 
     def release(self):
         if not self._closed:
             if not self._active: # already inactivated
-                msg = (_("can't release already inactivated backup: %s")
-                       % self._filename)
+                msg = (_("can't release already inactivated backup:"
+                         " dirstate%s")
+                       % self._suffix)
                 raise error.Abort(msg)
             self._abort()
 
diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -1051,3 +1051,22 @@ 
             # that
             return list(files)
         return [f for f in dmap if match(f)]
+
+    def _savebackup(self, repo, suffix):
+        '''Save current dirstate into backup file with suffix'''
+        self.write()
+        filename = self._filename
+        self._opener.write(filename + suffix, self._opener.tryread(filename))
+
+    def _restorebackup(self, repo, suffix):
+        '''Restore dirstate by backup file with suffix'''
+        # this "invalidate()" prevents "wlock.release()" from writing
+        # changes of dirstate out after restoring from backup file
+        self.invalidate()
+        filename = self._filename
+        self._opener.rename(filename + suffix, filename)
+
+    def _clearbackup(self, repo, suffix):
+        '''Clear backup file with suffix'''
+        filename = self._filename
+        self._opener.unlink(filename + suffix)