undo-files: add a utility function to read the backup-files definition stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Mon, 06 Mar 2023 13:22:47 +0100
branchstable
changeset 50293 5e568d70f54d
parent 50292 97e91001a4e0
child 50294 f34887316f1f
undo-files: add a utility function to read the backup-files definition We will need it in multiple places. so lets factor the logic around.
mercurial/transaction.py
--- a/mercurial/transaction.py	Mon Mar 06 13:05:43 2023 +0100
+++ b/mercurial/transaction.py	Mon Mar 06 13:22:47 2023 +0100
@@ -737,6 +737,32 @@
 )
 
 
+def read_backup_files(report, fp):
+    """parse an (already open) backup file an return contained backup entries
+
+    entries are in the form: (location, file, backupfile, xxx)
+
+    :location:   the vfs identifier (vfsmap's key)
+    :file:       original file path (in the vfs)
+    :backupfile: path of the backup (in the vfs)
+    :cache:      a boolean currently always set to False
+    """
+    lines = fp.readlines()
+    backupentries = []
+    if lines:
+        ver = lines[0][:-1]
+        if ver != (b'%d' % version):
+            report(BAD_VERSION_MSG)
+        else:
+            for line in lines[1:]:
+                if line:
+                    # Shave off the trailing newline
+                    line = line[:-1]
+                    l, f, b, c = line.split(b'\0')
+                    backupentries.append((l, f, b, bool(c)))
+    return backupentries
+
+
 def rollback(
     opener,
     vfsmap,
@@ -776,19 +802,8 @@
 
     backupjournal = b"%s.backupfiles" % file
     if opener.exists(backupjournal):
-        fp = opener.open(backupjournal)
-        lines = fp.readlines()
-        if lines:
-            ver = lines[0][:-1]
-            if ver != (b'%d' % version):
-                report(BAD_VERSION_MSG)
-            else:
-                for line in lines[1:]:
-                    if line:
-                        # Shave off the trailing newline
-                        line = line[:-1]
-                        l, f, b, c = line.split(b'\0')
-                        backupentries.append((l, f, b, bool(c)))
+        with opener.open(backupjournal) as fp:
+            backupentries = read_backup_files(report, fp)
     if skip_journal_pattern is not None:
         keep = lambda x: not skip_journal_pattern.match(x[1])
         backupentries = [x for x in backupentries if keep(x)]