Patchwork D9712: shelve: move method for creating backup to new shelf class

login
register
mail settings
Submitter phabricator
Date Jan. 8, 2021, 8:37 p.m.
Message ID <differential-rev-PHID-DREV-lfv2qftgg3ovedeiioqx-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48023/
State Superseded
Headers show

Comments

phabricator - Jan. 8, 2021, 8:37 p.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9712

AFFECTED FILES
  mercurial/shelve.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/shelve.py b/mercurial/shelve.py
--- a/mercurial/shelve.py
+++ b/mercurial/shelve.py
@@ -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)