undo-files: extract the cleanup code from strip in a function stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 06 Mar 2023 12:57:46 +0100
branchstable
changeset 50276 8011017629be
parent 50275 ada9a0245fd7
child 50277 48b90d18e9de
undo-files: extract the cleanup code from strip in a function This logic is duplicated in multiple places and it missing some important parts. So lets start dealing with the duplication first.
mercurial/repair.py
--- a/mercurial/repair.py	Tue Mar 07 23:38:14 2023 -0500
+++ b/mercurial/repair.py	Mon Mar 06 12:57:46 2023 +0100
@@ -113,6 +113,22 @@
     return s
 
 
+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.
+    """
+    for undovfs, undofile in repo.undofiles():
+        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 strip(ui, repo, nodelist, backup=True, topic=b'backup'):
     # This function requires the caller to lock the repo, but it operates
     # within a transaction of its own, and thus requires there to be no current
@@ -261,19 +277,7 @@
                 bmchanges = [(m, repo[newbmtarget].node()) for m in updatebm]
                 repo._bookmarks.applychanges(repo, tr, bmchanges)
 
-            # remove undo files
-            for undovfs, undofile in repo.undofiles():
-                try:
-                    undovfs.unlink(undofile)
-                except OSError as e:
-                    if e.errno != errno.ENOENT:
-                        ui.warn(
-                            _(b'error removing %s: %s\n')
-                            % (
-                                undovfs.join(undofile),
-                                stringutil.forcebytestr(e),
-                            )
-                        )
+            cleanup_undo_files(repo)
 
         except:  # re-raises
             if backupfile: