##// END OF EJS Templates
shelve: move listshelves() to new ShelfDir class, so caller need not pass vfs...
Martin von Zweigbergk -
r46923:9182177a default draft
parent child Browse files
Show More
@@ -80,6 +80,28 b' class ShelfDir(object):'
80 def get(self, name):
80 def get(self, name):
81 return Shelf(self.vfs, name)
81 return Shelf(self.vfs, name)
82
82
83 def listshelves(self):
84 """return all shelves in repo as list of (time, name)"""
85 try:
86 names = self.vfs.listdir()
87 except OSError as err:
88 if err.errno != errno.ENOENT:
89 raise
90 return []
91 info = []
92 seen = set()
93 for filename in names:
94 name = filename.rsplit(b'.', 1)[0]
95 if name in seen:
96 continue
97 seen.add(name)
98 shelf = self.get(name)
99 if not shelf.exists():
100 continue
101 mtime = shelf.mtime()
102 info.append((mtime, name))
103 return sorted(info, reverse=True)
104
83
105
84 class Shelf(object):
106 class Shelf(object):
85 """Represents a shelf, including possibly multiple files storing it.
107 """Represents a shelf, including possibly multiple files storing it.
@@ -332,9 +354,9 b' class shelvedstate(object):'
332
354
333
355
334 def cleanupoldbackups(repo):
356 def cleanupoldbackups(repo):
335 vfs = vfsmod.vfs(repo.vfs.join(backupdir))
336 maxbackups = repo.ui.configint(b'shelve', b'maxbackups')
357 maxbackups = repo.ui.configint(b'shelve', b'maxbackups')
337 hgfiles = listshelves(vfs)
358 backup_dir = ShelfDir(repo, for_backups=True)
359 hgfiles = backup_dir.listshelves()
338 if maxbackups > 0 and maxbackups < len(hgfiles):
360 if maxbackups > 0 and maxbackups < len(hgfiles):
339 bordermtime = hgfiles[maxbackups - 1][0]
361 bordermtime = hgfiles[maxbackups - 1][0]
340 else:
362 else:
@@ -343,7 +365,7 b' def cleanupoldbackups(repo):'
343 if mtime == bordermtime:
365 if mtime == bordermtime:
344 # keep it, because timestamp can't decide exact order of backups
366 # keep it, because timestamp can't decide exact order of backups
345 continue
367 continue
346 Shelf.open_backup(repo, name).delete()
368 backup_dir.get(name).delete()
347
369
348
370
349 def _backupactivebookmark(repo):
371 def _backupactivebookmark(repo):
@@ -604,10 +626,10 b' def cleanupcmd(ui, repo):'
604 """subcommand that deletes all shelves"""
626 """subcommand that deletes all shelves"""
605
627
606 with repo.wlock():
628 with repo.wlock():
607 vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
629 shelf_dir = ShelfDir(repo)
608 backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
630 backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
609 for _mtime, name in listshelves(vfs):
631 for _mtime, name in shelf_dir.listshelves():
610 Shelf(vfs, name).movetobackup(backupvfs)
632 shelf_dir.get(name).movetobackup(backupvfs)
611 cleanupoldbackups(repo)
633 cleanupoldbackups(repo)
612
634
613
635
@@ -627,29 +649,6 b' def deletecmd(ui, repo, pats):'
627 cleanupoldbackups(repo)
649 cleanupoldbackups(repo)
628
650
629
651
630 def listshelves(vfs):
631 """return all shelves in repo as list of (time, name)"""
632 try:
633 names = vfs.listdir()
634 except OSError as err:
635 if err.errno != errno.ENOENT:
636 raise
637 return []
638 info = []
639 seen = set()
640 for filename in names:
641 name = filename.rsplit(b'.', 1)[0]
642 if name in seen:
643 continue
644 seen.add(name)
645 shelf = Shelf(vfs, name)
646 if not shelf.exists():
647 continue
648 mtime = shelf.mtime()
649 info.append((mtime, name))
650 return sorted(info, reverse=True)
651
652
653 def listcmd(ui, repo, pats, opts):
652 def listcmd(ui, repo, pats, opts):
654 """subcommand that displays the list of shelves"""
653 """subcommand that displays the list of shelves"""
655 pats = set(pats)
654 pats = set(pats)
@@ -658,9 +657,8 b' def listcmd(ui, repo, pats, opts):'
658 width = ui.termwidth()
657 width = ui.termwidth()
659 namelabel = b'shelve.newest'
658 namelabel = b'shelve.newest'
660 ui.pager(b'shelve')
659 ui.pager(b'shelve')
661 vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
662 shelf_dir = ShelfDir(repo)
660 shelf_dir = ShelfDir(repo)
663 for mtime, name in listshelves(vfs):
661 for mtime, name in shelf_dir.listshelves():
664 if pats and name not in pats:
662 if pats and name not in pats:
665 continue
663 continue
666 ui.write(name, label=namelabel)
664 ui.write(name, label=namelabel)
@@ -700,15 +698,14 b' def listcmd(ui, repo, pats, opts):'
700
698
701 def patchcmds(ui, repo, pats, opts):
699 def patchcmds(ui, repo, pats, opts):
702 """subcommand that displays shelves"""
700 """subcommand that displays shelves"""
701 shelf_dir = ShelfDir(repo)
703 if len(pats) == 0:
702 if len(pats) == 0:
704 vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
703 shelves = shelf_dir.listshelves()
705 shelves = listshelves(vfs)
706 if not shelves:
704 if not shelves:
707 raise error.Abort(_(b"there are no shelves to show"))
705 raise error.Abort(_(b"there are no shelves to show"))
708 mtime, name = shelves[0]
706 mtime, name = shelves[0]
709 pats = [name]
707 pats = [name]
710
708
711 shelf_dir = ShelfDir(repo)
712 for shelfname in pats:
709 for shelfname in pats:
713 if not shelf_dir.get(shelfname).exists():
710 if not shelf_dir.get(shelfname).exists():
714 raise error.Abort(_(b"cannot find shelf %s") % shelfname)
711 raise error.Abort(_(b"cannot find shelf %s") % shelfname)
@@ -1123,8 +1120,7 b' def unshelvecmd(ui, repo, *shelved, **op'
1123 elif len(shelved) > 1:
1120 elif len(shelved) > 1:
1124 raise error.InputError(_(b'can only unshelve one change at a time'))
1121 raise error.InputError(_(b'can only unshelve one change at a time'))
1125 elif not shelved:
1122 elif not shelved:
1126 vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
1123 shelved = ShelfDir(repo).listshelves()
1127 shelved = listshelves(vfs)
1128 if not shelved:
1124 if not shelved:
1129 raise error.StateError(_(b'no shelved changes to apply!'))
1125 raise error.StateError(_(b'no shelved changes to apply!'))
1130 basename = shelved[0][1]
1126 basename = shelved[0][1]
General Comments 0
You need to be logged in to leave comments. Login now