diff mercurial/transaction.py @ 50286:3d0b5760851c stable

undo-files: move the undo cleanup code in the transaction module Now that undo creation is gathered in the transaction module, let us move the code cleaning them up there too. This will be useful to better clean previous undo files up before creating new ones.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 06 Mar 2023 21:03:45 +0100
parents a43f0562220c
children 7ce9862fca7c
line wrap: on
line diff
--- a/mercurial/transaction.py	Mon Mar 06 19:39:35 2023 +0100
+++ b/mercurial/transaction.py	Mon Mar 06 21:03:45 2023 +0100
@@ -11,6 +11,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import errno
 import os
 
 from .i18n import _
@@ -39,6 +40,43 @@
     return _active
 
 
+UNDO_BACKUP = b'undo.backupfiles'
+
+
+def cleanup_undo_files(repo):
+    """remove "undo" files used by the rollback logic
+
+    This is useful to prevent rollback running in situation were it does not
+    make sense. For example after a strip.
+    """
+    backup_entries = []
+    undo_files = []
+    vfsmap = repo.vfs_map
+    try:
+        with repo.svfs(UNDO_BACKUP) as f:
+            backup_entries = read_backup_files(repo.ui.warn, f)
+    except OSError as e:
+        if e.errno != errno.ENOENT:
+            msg = _(b'could not read %s: %s\n')
+            msg %= (repo.svfs.join(UNDO_BACKUP), stringutil.forcebytestr(e))
+            repo.ui.warn(msg)
+
+    for location, f, backup_path, c in backup_entries:
+        if location in vfsmap and backup_path:
+            undo_files.append((vfsmap[location], backup_path))
+
+    undo_files.append((repo.svfs, UNDO_BACKUP))
+    undo_files.extend(repo.undofiles())
+    for undovfs, undofile in undo_files:
+        try:
+            undovfs.unlink(undofile)
+        except OSError as e:
+            if e.errno != errno.ENOENT:
+                msg = _(b'error removing %s: %s\n')
+                msg %= (undovfs.join(undofile), stringutil.forcebytestr(e))
+                repo.ui.warn(msg)
+
+
 def _playback(
     journal,
     report,