transaction: move the restoration of backup file in a small closure stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 15 Mar 2023 13:20:12 +0100
branchstable
changeset 50339 86dc9e097bed
parent 50338 cab3defe6d3b
child 50340 4c1061b3e55a
transaction: move the restoration of backup file in a small closure We are about to use that logic in two different location, making is a small reusable closure prepares that.
mercurial/transaction.py
--- a/mercurial/transaction.py	Wed Mar 15 12:13:08 2023 +0100
+++ b/mercurial/transaction.py	Wed Mar 15 13:20:12 2023 +0100
@@ -105,6 +105,19 @@
     unlink=True,
     checkambigfiles=None,
 ):
+    backupfiles = []
+
+    def restore_one_backup(vfs, f, b, checkambig):
+        filepath = vfs.join(f)
+        backuppath = vfs.join(b)
+        try:
+            util.copyfile(backuppath, filepath, checkambig=checkambig)
+            backupfiles.append((vfs, b))
+        except IOError as exc:
+            e_msg = stringutil.forcebytestr(exc)
+            report(_(b"failed to recover %s (%s)\n") % (f, e_msg))
+            raise
+
     for f, o in sorted(dict(entries).items()):
         if o or not unlink:
             checkambig = checkambigfiles and (f, b'') in checkambigfiles
@@ -129,23 +142,14 @@
             except FileNotFoundError:
                 pass
 
-    backupfiles = []
     for l, f, b, c in backupentries:
         if l not in vfsmap and c:
             report(b"couldn't handle %s: unknown cache location %s\n" % (b, l))
         vfs = vfsmap[l]
         try:
+            checkambig = checkambigfiles and (f, l) in checkambigfiles
             if f and b:
-                filepath = vfs.join(f)
-                backuppath = vfs.join(b)
-                checkambig = checkambigfiles and (f, l) in checkambigfiles
-                try:
-                    util.copyfile(backuppath, filepath, checkambig=checkambig)
-                    backupfiles.append((vfs, b))
-                except IOError as exc:
-                    e_msg = stringutil.forcebytestr(exc)
-                    report(_(b"failed to recover %s (%s)\n") % (f, e_msg))
-                    raise
+                restore_one_backup(vfs, f, b, checkambig)
             else:
                 target = f or b
                 try: