Patchwork D9700: shelve: introduce class representing a shelf

login
register
mail settings
Submitter phabricator
Date Jan. 8, 2021, 8:37 p.m.
Message ID <differential-rev-PHID-DREV-dwrzyictgyz3fbrqduwq-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48010/
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.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

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
@@ -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)