shelve: also create class representing whole directory of shelves
authorMartin von Zweigbergk <martinvonz@google.com>
Tue, 12 Jan 2021 09:02:47 -0800
changeset 46296 eec8899407f4
parent 46295 f8c5e6ecd008
child 46297 82edad33fd81
shelve: also create class representing whole directory of shelves It's a little annoying to have to create and pass in a vfs into `listshelves()`. This patch attempts to start addressing that by creating a class that represents a directory containing shelves (the directory can be either `.hg/shelved/` or `.hg/shelve-backup/`). Differential Revision: https://phab.mercurial-scm.org/D9743
mercurial/shelve.py
--- a/mercurial/shelve.py	Mon Jan 11 23:08:37 2021 -0800
+++ b/mercurial/shelve.py	Tue Jan 12 09:02:47 2021 -0800
@@ -70,6 +70,17 @@
 shelveuser = b'shelve@localhost'
 
 
+class ShelfDir(object):
+    def __init__(self, repo, for_backups=False):
+        if for_backups:
+            self.vfs = vfsmod.vfs(repo.vfs.join(backupdir))
+        else:
+            self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
+
+    def get(self, name):
+        return Shelf(self.vfs, name)
+
+
 class Shelf(object):
     """Represents a shelf, including possibly multiple files storing it.
 
@@ -82,14 +93,6 @@
         self.vfs = vfs
         self.name = name
 
-    @staticmethod
-    def open(repo, name):
-        return Shelf(vfsmod.vfs(repo.vfs.join(shelvedir)), name)
-
-    @staticmethod
-    def open_backup(repo, name):
-        return Shelf(vfsmod.vfs(repo.vfs.join(backupdir)), name)
-
     def exists(self):
         return self.vfs.exists(self.name + b'.patch') and self.vfs.exists(
             self.name + b'.hg'
@@ -381,7 +384,7 @@
         label = label.replace(b'.', b'_', 1)
 
     if name:
-        if Shelf.open(repo, name).exists():
+        if ShelfDir(repo).get(name).exists():
             e = _(b"a shelved change named '%s' already exists") % name
             raise error.Abort(e)
 
@@ -394,8 +397,9 @@
             raise error.Abort(_(b"shelved change names can not start with '.'"))
 
     else:
+        shelf_dir = ShelfDir(repo)
         for n in gennames():
-            if not Shelf.open(repo, n).exists():
+            if not shelf_dir.get(n).exists():
                 name = n
                 break
 
@@ -471,7 +475,7 @@
 
 def _shelvecreatedcommit(repo, node, name, match):
     info = {b'node': hex(node)}
-    shelf = Shelf.open(repo, name)
+    shelf = ShelfDir(repo).get(name)
     shelf.writeinfo(info)
     bases = list(mutableancestors(repo[node]))
     shelf.writebundle(repo, bases, node)
@@ -614,7 +618,7 @@
     with repo.wlock():
         backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
         for name in pats:
-            shelf = Shelf.open(repo, name)
+            shelf = ShelfDir(repo).get(name)
             if not shelf.exists():
                 raise error.InputError(
                     _(b"shelved change '%s' not found") % name
@@ -655,6 +659,7 @@
     namelabel = b'shelve.newest'
     ui.pager(b'shelve')
     vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
+    shelf_dir = ShelfDir(repo)
     for mtime, name in listshelves(vfs):
         if pats and name not in pats:
             continue
@@ -670,7 +675,7 @@
         ui.write(age, label=b'shelve.age')
         ui.write(b' ' * (12 - len(age)))
         used += 12
-        with Shelf.open(repo, name).open_patch() as fp:
+        with shelf_dir.get(name).open_patch() as fp:
             while True:
                 line = fp.readline()
                 if not line:
@@ -703,8 +708,9 @@
         mtime, name = shelves[0]
         pats = [name]
 
+    shelf_dir = ShelfDir(repo)
     for shelfname in pats:
-        if not Shelf.open(repo, shelfname).exists():
+        if not shelf_dir.get(shelfname).exists():
             raise error.Abort(_(b"cannot find shelf %s") % shelfname)
 
     listcmd(ui, repo, pats, opts)
@@ -796,7 +802,7 @@
     """remove related files after an unshelve"""
     if not opts.get(b'keep'):
         backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
-        Shelf.open(repo, name).movetobackup(backupvfs)
+        ShelfDir(repo).get(name).movetobackup(backupvfs)
         cleanupoldbackups(repo)
 
 
@@ -896,7 +902,7 @@
     """Recreate commit in the repository during the unshelve"""
     repo = repo.unfiltered()
     node = None
-    shelf = Shelf.open(repo, basename)
+    shelf = ShelfDir(repo).get(basename)
     if shelf.hasinfo():
         node = shelf.readinfo()[b'node']
     if node is None or node not in repo:
@@ -1126,7 +1132,7 @@
     else:
         basename = shelved[0]
 
-    if not Shelf.open(repo, basename).exists():
+    if not ShelfDir(repo).get(basename).exists():
         raise error.InputError(_(b"shelved change '%s' not found") % basename)
 
     return _dounshelve(ui, repo, basename, opts)