Patchwork [1,of,5] dirstate: move code paths for backup from dirstateguard to dirstate

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Oct. 9, 2015, 6:59 p.m.
Message ID <557573b9247d6bac06d2.1444417165@feefifofum>
Download mbox | patch
Permalink /patch/10928/
State Superseded
Commit 59b5e8844eb073e8c15b5a96e00974c5ce3ad9f0
Delegated to: Pierre-Yves David
Headers show

Comments

Katsunori FUJIWARA - Oct. 9, 2015, 6:59 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1444416862 -32400
#      Sat Oct 10 03:54:22 2015 +0900
# Node ID 557573b9247d6bac06d253d32105acfec1253604
# Parent  6e715040c1725b5debce888c4f7d3fdbf55cc900
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 util.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 util.Abort(msg)
             self._abort()
 
diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -1045,3 +1045,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)