shelve: move method for creating backup to new shelf class
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 07 Jan 2021 22:45:17 -0800
changeset 46285 e79f8ae0901b
parent 46284 d7f763c8c58e
child 46286 3b08f56c8a11
shelve: move method for creating backup to new shelf class Differential Revision: https://phab.mercurial-scm.org/D9712
mercurial/shelve.py
--- a/mercurial/shelve.py	Thu Jan 07 23:09:04 2021 -0800
+++ b/mercurial/shelve.py	Thu Jan 07 22:45:17 2021 -0800
@@ -81,7 +81,6 @@
     def __init__(self, repo, name, filetype=None):
         self.name = name
         self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
-        self.backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
         if filetype:
             self.fname = name + b'.' + filetype
         else:
@@ -90,22 +89,6 @@
     def exists(self):
         return self.vfs.exists(self.fname)
 
-    def backupfilename(self):
-        def gennames(base):
-            yield base
-            base, ext = base.rsplit(b'.', 1)
-            for i in itertools.count(1):
-                yield b'%s-%d.%s' % (base, i, ext)
-
-        for n in gennames(self.fname):
-            if not self.backupvfs.exists(n):
-                return self.backupvfs.join(n)
-
-    def movetobackup(self):
-        if not self.backupvfs.isdir():
-            self.backupvfs.makedir()
-        util.rename(self.vfs.join(self.fname), self.backupfilename())
-
 
 class Shelf(object):
     """Represents a shelf, including possibly multiple files storing it.
@@ -119,6 +102,7 @@
         self.repo = repo
         self.name = name
         self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
+        self.backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
 
     def exists(self):
         return self.vfs.exists(self.name + b'.' + patchextension)
@@ -188,6 +172,27 @@
     def open_patch(self, mode=b'rb'):
         return self.vfs(self.name + b'.patch', mode)
 
+    def _backupfilename(self, filename):
+        def gennames(base):
+            yield base
+            base, ext = base.rsplit(b'.', 1)
+            for i in itertools.count(1):
+                yield b'%s-%d.%s' % (base, i, ext)
+
+        for n in gennames(filename):
+            if not self.backupvfs.exists(n):
+                return self.backupvfs.join(n)
+
+    def movetobackup(self):
+        if not self.backupvfs.isdir():
+            self.backupvfs.makedir()
+        for suffix in shelvefileextensions:
+            filename = self.name + b'.' + suffix
+            if self.vfs.exists(filename):
+                util.rename(
+                    self.vfs.join(filename), self._backupfilename(filename)
+                )
+
 
 class shelvedstate(object):
     """Handle persistence during unshelving operations.
@@ -602,10 +607,7 @@
 
     with repo.wlock():
         for _mtime, name in listshelves(repo):
-            for suffix in shelvefileextensions:
-                shfile = shelvedfile(repo, name, suffix)
-                if shfile.exists():
-                    shfile.movetobackup()
+            Shelf(repo, name).movetobackup()
             cleanupoldbackups(repo)
 
 
@@ -619,10 +621,7 @@
                 raise error.InputError(
                     _(b"shelved change '%s' not found") % name
                 )
-            for suffix in shelvefileextensions:
-                shfile = shelvedfile(repo, name, suffix)
-                if shfile.exists():
-                    shfile.movetobackup()
+            Shelf(repo, name).movetobackup()
             cleanupoldbackups(repo)
 
 
@@ -791,10 +790,7 @@
 def unshelvecleanup(ui, repo, name, opts):
     """remove related files after an unshelve"""
     if not opts.get(b'keep'):
-        for filetype in shelvefileextensions:
-            shfile = shelvedfile(repo, name, filetype)
-            if shfile.exists():
-                shfile.movetobackup()
+        Shelf(repo, name).movetobackup()
         cleanupoldbackups(repo)