Patchwork D9719: shelve: don't include invalid shelves in `hg shelve --list`

login
register
mail settings
Submitter phabricator
Date Jan. 11, 2021, 6:05 p.m.
Message ID <differential-rev-PHID-DREV-m2xpka7eto5p6gktiaa2-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48030/
State Superseded
Headers show

Comments

phabricator - Jan. 11, 2021, 6:05 p.m.
martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Before this patch, if a shelved change is missing its `.hg` file, we
  still list it in `hg shelve --list`, but then `hg unshelve`
  crashes. This patch makes it so we only list valid shelved changes.
  
  This patch means that users who do `touch .hg/shelve/buy-milk.patch`
  as a form of TODO list will no longer see their TODO items in `hg
  shelve --list` (OTOH, we also no longer garbage collect their TOOD
  items, for better or worse).

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/shelve.py
  tests/test-shelve2.t

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-shelve2.t b/tests/test-shelve2.t
--- a/tests/test-shelve2.t
+++ b/tests/test-shelve2.t
@@ -753,16 +753,15 @@ 
 # A (corrupt) .patch file without a .hg file
   $ touch .hg/shelved/junk1.patch
   $ hg shelve -l
-  junk1           (* ago)     (glob)
   $ hg unshelve
-  unshelving change 'junk1'
-  abort: $ENOENT$: '$TESTTMP/corrupt-shelves/.hg/shelved/junk1.hg'
-  [255]
+  abort: no shelved changes to apply!
+  [20]
   $ hg shelve -d junk1
+  abort: shelved change 'junk1' not found
+  [10]
   $ find .hg/shelve*
-  .hg/shelve-backup
-  .hg/shelve-backup/junk1.patch
   .hg/shelved
+  .hg/shelved/junk1.patch
 
 # A .hg file without a .patch file
   $ touch .hg/shelved/junk2.hg
@@ -774,9 +773,8 @@ 
   abort: shelved change 'junk2' not found
   [10]
   $ find .hg/shelve*
-  .hg/shelve-backup
-  .hg/shelve-backup/junk1.patch
   .hg/shelved
+  .hg/shelved/junk1.patch
   .hg/shelved/junk2.hg
 
 # A file with an unexpected extension
@@ -789,9 +787,8 @@ 
   abort: shelved change 'junk3' not found
   [10]
   $ find .hg/shelve*
-  .hg/shelve-backup
-  .hg/shelve-backup/junk1.patch
   .hg/shelved
+  .hg/shelved/junk1.patch
   .hg/shelved/junk3
   .hg/shelved/junk2.hg
 
diff --git a/mercurial/shelve.py b/mercurial/shelve.py
--- a/mercurial/shelve.py
+++ b/mercurial/shelve.py
@@ -87,7 +87,9 @@ 
         self.backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
 
     def exists(self):
-        return self.vfs.exists(self.name + b'.' + patchextension)
+        return self.vfs.exists(
+            self.name + b'.' + patchextension
+        ) and self.vfs.exists(self.name + b'.hg')
 
     def mtime(self):
         return self.vfs.stat(self.name + b'.' + patchextension)[stat.ST_MTIME]
@@ -621,12 +623,17 @@ 
             raise
         return []
     info = []
-    for (name, _type) in names:
-        pfx, sfx = name.rsplit(b'.', 1)
-        if not pfx or sfx != patchextension:
+    seen = set()
+    for (filename, _type) in names:
+        name, ext = filename.rsplit(b'.', 1)
+        if name in seen:
             continue
-        mtime = Shelf(repo, pfx).mtime()
-        info.append((mtime, pfx))
+        seen.add(name)
+        shelve = Shelf(repo, name)
+        if not shelve.exists():
+            continue
+        mtime = shelve.mtime()
+        info.append((mtime, name))
     return sorted(info, reverse=True)