# HG changeset patch # User Martin von Zweigbergk # Date 2021-01-07 19:07:21 # Node ID efc71bb71682b8642051fee2dc519e57f43358d5 # Parent a68d3386138c042e5af4cb40f508ed5a7847970b 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 diff --git a/mercurial/shelve.py b/mercurial/shelve.py --- a/mercurial/shelve.py +++ b/mercurial/shelve.py @@ -170,6 +170,23 @@ class shelvedfile(object): 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 @@ def getshelvename(repo, parent, opts): 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 @@ def getshelvename(repo, parent, opts): 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 @@ def deletecmd(ui, repo, pats): 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 @@ def patchcmds(ui, repo, pats, opts): 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 @@ def unshelvecmd(ui, repo, *shelved, **op 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)