shelve: move method for reading .hg to new shelf class
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 07 Jan 2021 13:57:21 -0800
changeset 46277 ed2f2150d57c
parent 46276 eb7b2929ae49
child 46278 58ca94869287
shelve: move method for reading .hg to new shelf class Differential Revision: https://phab.mercurial-scm.org/D9704
mercurial/shelve.py
--- a/mercurial/shelve.py	Thu Jan 07 11:25:39 2021 -0800
+++ b/mercurial/shelve.py	Thu Jan 07 13:57:21 2021 -0800
@@ -80,7 +80,6 @@
     the vfs layer"""
 
     def __init__(self, repo, name, filetype=None):
-        self.repo = repo
         self.name = name
         self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
         self.backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
@@ -118,30 +117,6 @@
     def opener(self, mode=b'rb'):
         return self.vfs(self.fname, mode)
 
-    def applybundle(self, tr):
-        fp = self.opener()
-        try:
-            targetphase = phases.internal
-            if not phases.supportinternal(self.repo):
-                targetphase = phases.secret
-            gen = exchange.readbundle(self.repo.ui, fp, self.fname, self.vfs)
-            pretip = self.repo[b'tip']
-            bundle2.applybundle(
-                self.repo,
-                gen,
-                tr,
-                source=b'unshelve',
-                url=b'bundle:' + self.vfs.join(self.fname),
-                targetphase=targetphase,
-            )
-            shelvectx = self.repo[b'tip']
-            if pretip == shelvectx:
-                shelverev = tr.changes[b'revduplicates'][-1]
-                shelvectx = self.repo[shelverev]
-            return shelvectx
-        finally:
-            fp.close()
-
 
 class Shelf(object):
     """Represents a shelf, including possibly multiple files storing it.
@@ -193,6 +168,31 @@
             compression=compression,
         )
 
+    def applybundle(self, tr):
+        filename = self.name + b'.hg'
+        fp = self.vfs(filename)
+        try:
+            targetphase = phases.internal
+            if not phases.supportinternal(self.repo):
+                targetphase = phases.secret
+            gen = exchange.readbundle(self.repo.ui, fp, filename, self.vfs)
+            pretip = self.repo[b'tip']
+            bundle2.applybundle(
+                self.repo,
+                gen,
+                tr,
+                source=b'unshelve',
+                url=b'bundle:' + self.vfs.join(filename),
+                targetphase=targetphase,
+            )
+            shelvectx = self.repo[b'tip']
+            if pretip == shelvectx:
+                shelverev = tr.changes[b'revduplicates'][-1]
+                shelvectx = self.repo[shelverev]
+            return shelvectx
+        finally:
+            fp.close()
+
 
 class shelvedstate(object):
     """Handle persistence during unshelving operations.
@@ -904,7 +904,7 @@
         node = Shelf(repo, basename).readinfo()[b'node']
     if node is None or node not in repo:
         with ui.configoverride({(b'ui', b'quiet'): True}):
-            shelvectx = shelvedfile(repo, basename, b'hg').applybundle(tr)
+            shelvectx = Shelf(repo, basename).applybundle(tr)
         # We might not strip the unbundled changeset, so we should keep track of
         # the unshelve node in case we need to reuse it (eg: unshelve --keep)
         if node is None: