changeset 46273:efc71bb71682

shelve: introduce class representing a shelf I'm about to make phase-based shelve not write `.hg` and `.patch` files. Having a class that represents a single shelf, regardless of which files it uses will help. I'm starting small with just a `.exists()` function. I plan to eventually remove the `shelvedfile` class once all functionality has been moved to the new class. By the way, I know that things you shelve are not typically themselves shelves. I still picked `Shelf` for the class because it's short (compared to e.g. `ShelvedChange`). Differential Revision: https://phab.mercurial-scm.org/D9700
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 07 Jan 2021 11:07:21 -0800
parents a68d3386138c
children a344ec05b99c
files mercurial/shelve.py
diffstat 1 files changed, 22 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/shelve.py	Thu Jan 07 12:26:32 2021 -0800
+++ b/mercurial/shelve.py	Thu Jan 07 11:07:21 2021 -0800
@@ -170,6 +170,23 @@
         return scmutil.simplekeyvaluefile(self.vfs, self.fname).read()
 
 
+class Shelf(object):
+    """Represents a shelf, including possibly multiple files storing it.
+
+    Old shelves will have a .patch and a .hg file. Newer shelves will
+    also have a .shelve file. This class abstracts away some of the
+    differences and lets you work with the shelf as a whole.
+    """
+
+    def __init__(self, repo, name):
+        self.repo = repo
+        self.name = name
+        self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
+
+    def exists(self):
+        return self.vfs.exists(self.name + b'.' + patchextension)
+
+
 class shelvedstate(object):
     """Handle persistence during unshelving operations.
 
@@ -364,7 +381,7 @@
         label = label.replace(b'.', b'_', 1)
 
     if name:
-        if shelvedfile(repo, name, patchextension).exists():
+        if Shelf(repo, name).exists():
             e = _(b"a shelved change named '%s' already exists") % name
             raise error.Abort(e)
 
@@ -378,7 +395,7 @@
 
     else:
         for n in gennames():
-            if not shelvedfile(repo, n, patchextension).exists():
+            if not Shelf(repo, n).exists():
                 name = n
                 break
 
@@ -595,7 +612,7 @@
         raise error.InputError(_(b'no shelved changes specified!'))
     with repo.wlock():
         for name in pats:
-            if not shelvedfile(repo, name, patchextension).exists():
+            if not Shelf(repo, name).exists():
                 raise error.InputError(
                     _(b"shelved change '%s' not found") % name
                 )
@@ -682,7 +699,7 @@
         pats = [sname]
 
     for shelfname in pats:
-        if not shelvedfile(repo, shelfname, patchextension).exists():
+        if not Shelf(repo, shelfname).exists():
             raise error.Abort(_(b"cannot find shelf %s") % shelfname)
 
     listcmd(ui, repo, pats, opts)
@@ -1104,7 +1121,7 @@
     else:
         basename = shelved[0]
 
-    if not shelvedfile(repo, basename, patchextension).exists():
+    if not Shelf(repo, basename).exists():
         raise error.InputError(_(b"shelved change '%s' not found") % basename)
 
     return _dounshelve(ui, repo, basename, opts)