changeset 50002:e358f6e0e50e

dirstate: do not write an empty dirstate just for backup This will get in the way when we get more strict about holding the lock when writing the dirstate. Instead, we simply don't copy dirstate files around if there are None at backup time. A couple of tests are impacted they no longer need to backup such "empty" dirstate.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 14 Feb 2023 23:05:18 +0100
parents 9313d8666a0f
children f4eef08575ce
files mercurial/dirstate.py tests/test-empty.t tests/test-inherit-mode.t
diffstat 3 files changed, 25 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dirstate.py	Tue Feb 14 22:46:26 2023 +0100
+++ b/mercurial/dirstate.py	Tue Feb 14 23:05:18 2023 +0100
@@ -1486,6 +1486,9 @@
         """return a filename to backup a data-file or None"""
         if not self._use_dirstate_v2:
             return None
+        if self._map.docket.uuid is None:
+            # not created yet, nothing to backup
+            return None
         data_filename = self._map.docket.data_filename()
         return data_filename, self.data_backup_filename(backupname)
 
@@ -1506,7 +1509,7 @@
         # use '_writedirstate' instead of 'write' to write changes certainly,
         # because the latter omits writing out if transaction is running.
         # output file will be used to create backup of dirstate at this point.
-        if self._dirty or not self._opener.exists(filename):
+        if self._dirty:
             self._writedirstate(
                 tr,
                 self._opener(filename, b"w", atomictemp=True, checkambig=True),
@@ -1530,7 +1533,7 @@
             tr.registertmp(filename, location=b'plain')
 
         self._opener.tryunlink(backupname)
-        if True:
+        if self._opener.exists(filename):
             # hardlink backup is okay because _writedirstate is always called
             # with an "atomictemp=True" file.
             util.copyfile(
@@ -1557,10 +1560,23 @@
         # this "invalidate()" prevents "wlock.release()" from writing
         # changes of dirstate out after restoring from backup file
         self.invalidate()
+        o = self._opener
+        if not o.exists(backupname):
+            # there was no file backup, delete existing files
+            filename = self._actualfilename(tr)
+            data_file = None
+            if self._use_dirstate_v2:
+                data_file = self._map.docket.data_filename()
+            if o.exists(filename):
+                o.unlink(filename)
+            if data_file is not None and o.exists(data_file):
+                o.unlink(data_file)
+            return
         filename = self._actualfilename(tr)
-        o = self._opener
         data_pair = self.backup_data_file(backupname)
-        if util.samefile(o.join(backupname), o.join(filename)):
+        if o.exists(filename) and util.samefile(
+            o.join(backupname), o.join(filename)
+        ):
             o.unlink(backupname)
         else:
             o.rename(backupname, filename, checkambig=True)
@@ -1577,11 +1593,11 @@
     def clearbackup(self, tr, backupname):
         '''Clear backup file'''
         o = self._opener
-        data_backup = self.backup_data_file(backupname)
-        o.unlink(backupname)
-
-        if data_backup is not None:
-            o.unlink(data_backup[0])
+        if o.exists(backupname):
+            data_backup = self.backup_data_file(backupname)
+            o.unlink(backupname)
+            if data_backup is not None:
+                o.unlink(data_backup[0])
 
     def verify(self, m1, m2, p1, narrow_matcher=None):
         """
--- a/tests/test-empty.t	Tue Feb 14 22:46:26 2023 +0100
+++ b/tests/test-empty.t	Tue Feb 14 23:05:18 2023 +0100
@@ -36,7 +36,6 @@
   $ ls .hg
   00changelog.i
   cache
-  dirstate
   hgrc
   requires
   store
--- a/tests/test-inherit-mode.t	Tue Feb 14 22:46:26 2023 +0100
+++ b/tests/test-inherit-mode.t	Tue Feb 14 23:05:18 2023 +0100
@@ -137,7 +137,6 @@
   00660 ../push/.hg/cache/branch2-base
   00660 ../push/.hg/cache/rbc-names-v1
   00660 ../push/.hg/cache/rbc-revs-v1
-  00660 ../push/.hg/dirstate
   00660 ../push/.hg/requires
   00770 ../push/.hg/store/
   00660 ../push/.hg/store/00changelog.i
@@ -160,7 +159,6 @@
   00660 ../push/.hg/undo.bookmarks
   00660 ../push/.hg/undo.branch
   00660 ../push/.hg/undo.desc
-  00660 ../push/.hg/undo.dirstate
   00770 ../push/.hg/wcache/