localrepo: stop doing special dirstate backup at transaction open
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 16 Feb 2023 04:02:36 +0100
changeset 50140 ff12f42415f5
parent 50139 5b9c3ae807c8
child 50141 76d44983a398
localrepo: stop doing special dirstate backup at transaction open Since the dirstate writes are already managed by the transaction, we already do a backup of the dirstate when necessary (and even trigger one to keep `hg rollback` happy). We needs some special code to deal with the initial empty checkout, but it is not too complicated. Managing variable filename (as dirstate-v2 uses) at the "journalfile" level, is complex and fragile (which is consistent with the fact these files are not journal…). If we no longer do it, our life is significantly simpler. In some sense, we apply the xkcd-1134¹ solution to our savebackup/restorebackup problem. [1] https://xkcd.com/1134/ (the change to test-hardlink are expect as decreasing the number of duplicated backup drive the hardlink count down)
mercurial/localrepo.py
tests/test-hardlinks.t
tests/test-rebase-abort.t
--- a/mercurial/localrepo.py	Thu Feb 16 11:42:43 2023 +0100
+++ b/mercurial/localrepo.py	Thu Feb 16 04:02:36 2023 +0100
@@ -2548,8 +2548,6 @@
                 # out) in this transaction
                 narrowspec.restorebackup(self, b'journal.narrowspec')
                 narrowspec.restorewcbackup(self, b'journal.narrowspec.dirstate')
-                if repo.currentwlock() is not None:
-                    repo.dirstate.restorebackup(None, b'journal.dirstate')
 
                 repo.invalidate(clearfilecache=True)
 
@@ -2676,31 +2674,21 @@
         return tr
 
     def _journalfiles(self):
-        first = (
+        return (
             (self.svfs, b'journal'),
             (self.svfs, b'journal.narrowspec'),
             (self.vfs, b'journal.narrowspec.dirstate'),
-            (self.vfs, b'journal.dirstate'),
-        )
-        middle = []
-        dirstate_data = self.dirstate.data_backup_filename(b'journal.dirstate')
-        if dirstate_data is not None:
-            middle.append((self.vfs, dirstate_data))
-        end = (
             (self.vfs, b'journal.branch'),
             (self.vfs, b'journal.desc'),
             (bookmarks.bookmarksvfs(self), b'journal.bookmarks'),
             (self.svfs, b'journal.phaseroots'),
         )
-        return first + tuple(middle) + end
 
     def undofiles(self):
         return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
 
     @unfilteredmethod
     def _writejournal(self, desc):
-        if self.currentwlock() is not None:
-            self.dirstate.savebackup(None, b'journal.dirstate')
         narrowspec.savewcbackup(self, b'journal.narrowspec.dirstate')
         narrowspec.savebackup(self, b'journal.narrowspec')
         self.vfs.write(
@@ -2808,11 +2796,20 @@
         if self.svfs.exists(b'undo.phaseroots'):
             self.svfs.rename(b'undo.phaseroots', b'phaseroots', checkambig=True)
         self.invalidate()
+        self.dirstate.invalidate()
 
         if parentgone:
+            # replace this with some explicit parent update in the future.
+            has_node = self.changelog.index.has_node
+            if not all(has_node(p) for p in self.dirstate._pl):
+                # There was no dirstate to backup initially, we need to drop
+                # the existing one.
+                with self.dirstate.changing_parents(self):
+                    self.dirstate.setparents(self.nullid)
+                    self.dirstate.clear()
+
             narrowspec.restorebackup(self, b'undo.narrowspec')
             narrowspec.restorewcbackup(self, b'undo.narrowspec.dirstate')
-            self.dirstate.restorebackup(None, b'undo.dirstate')
             try:
                 branch = self.vfs.read(b'undo.branch')
                 self.dirstate.setbranch(encoding.tolocal(branch))
--- a/tests/test-hardlinks.t	Thu Feb 16 11:42:43 2023 +0100
+++ b/tests/test-hardlinks.t	Thu Feb 16 04:02:36 2023 +0100
@@ -261,11 +261,10 @@
   2 r4/.hg/store/undo.backup.phaseroots
   2 r4/.hg/store/undo.backupfiles
   2 r4/.hg/store/undo.phaseroots
-  [24] r4/\.hg/undo\.backup\.dirstate (re)
+  2 r4/\.hg/undo\.backup\.dirstate (re)
   2 r4/.hg/undo.bookmarks
   2 r4/.hg/undo.branch
   2 r4/.hg/undo.desc
-  [24] r4/\.hg/undo\.dirstate (re)
   2 r4/.hg/wcache/checkisexec (execbit !)
   2 r4/.hg/wcache/checklink-target (symlink !)
   2 r4/.hg/wcache/checknoexec (execbit !)
@@ -279,7 +278,7 @@
 #if hardlink-whitelisted
   $ nlinksdir r4/.hg/undo.backup.dirstate r4/.hg/dirstate
   2 r4/.hg/dirstate
-  4 r4/.hg/undo.backup.dirstate
+  2 r4/.hg/undo.backup.dirstate
 #endif
 
 
@@ -319,11 +318,10 @@
   2 r4/.hg/store/undo.backup.phaseroots
   2 r4/.hg/store/undo.backupfiles
   2 r4/.hg/store/undo.phaseroots
-  [24] r4/\.hg/undo\.backup\.dirstate (re)
+  2 r4/\.hg/undo\.backup\.dirstate (re)
   2 r4/.hg/undo.bookmarks
   2 r4/.hg/undo.branch
   2 r4/.hg/undo.desc
-  [24] r4/\.hg/undo\.dirstate (re)
   2 r4/.hg/wcache/checkisexec (execbit !)
   2 r4/.hg/wcache/checklink-target (symlink !)
   2 r4/.hg/wcache/checknoexec (execbit !)
@@ -337,7 +335,7 @@
 #if hardlink-whitelisted
   $ nlinksdir r4/.hg/undo.backup.dirstate r4/.hg/dirstate
   1 r4/.hg/dirstate
-  4 r4/.hg/undo.backup.dirstate
+  2 r4/.hg/undo.backup.dirstate
 #endif
 
 Test hardlinking outside hg:
--- a/tests/test-rebase-abort.t	Thu Feb 16 11:42:43 2023 +0100
+++ b/tests/test-rebase-abort.t	Thu Feb 16 04:02:36 2023 +0100
@@ -393,7 +393,6 @@
   .hg/merge/state
   .hg/rebasestate
   .hg/undo.backup.dirstate
-  .hg/undo.dirstate
   .hg/updatestate
 
   $ hg rebase -s 3 -d tip