comparison mercurial/shelve.py @ 46285:e79f8ae0901b

shelve: move method for creating backup to new shelf class Differential Revision: https://phab.mercurial-scm.org/D9712
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 07 Jan 2021 22:45:17 -0800
parents d7f763c8c58e
children 3b08f56c8a11
comparison
equal deleted inserted replaced
46284:d7f763c8c58e 46285:e79f8ae0901b
79 the vfs layer""" 79 the vfs layer"""
80 80
81 def __init__(self, repo, name, filetype=None): 81 def __init__(self, repo, name, filetype=None):
82 self.name = name 82 self.name = name
83 self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir)) 83 self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
84 self.backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
85 if filetype: 84 if filetype:
86 self.fname = name + b'.' + filetype 85 self.fname = name + b'.' + filetype
87 else: 86 else:
88 self.fname = name 87 self.fname = name
89 88
90 def exists(self): 89 def exists(self):
91 return self.vfs.exists(self.fname) 90 return self.vfs.exists(self.fname)
92 91
93 def backupfilename(self):
94 def gennames(base):
95 yield base
96 base, ext = base.rsplit(b'.', 1)
97 for i in itertools.count(1):
98 yield b'%s-%d.%s' % (base, i, ext)
99
100 for n in gennames(self.fname):
101 if not self.backupvfs.exists(n):
102 return self.backupvfs.join(n)
103
104 def movetobackup(self):
105 if not self.backupvfs.isdir():
106 self.backupvfs.makedir()
107 util.rename(self.vfs.join(self.fname), self.backupfilename())
108
109 92
110 class Shelf(object): 93 class Shelf(object):
111 """Represents a shelf, including possibly multiple files storing it. 94 """Represents a shelf, including possibly multiple files storing it.
112 95
113 Old shelves will have a .patch and a .hg file. Newer shelves will 96 Old shelves will have a .patch and a .hg file. Newer shelves will
117 100
118 def __init__(self, repo, name): 101 def __init__(self, repo, name):
119 self.repo = repo 102 self.repo = repo
120 self.name = name 103 self.name = name
121 self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir)) 104 self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
105 self.backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
122 106
123 def exists(self): 107 def exists(self):
124 return self.vfs.exists(self.name + b'.' + patchextension) 108 return self.vfs.exists(self.name + b'.' + patchextension)
125 109
126 def mtime(self): 110 def mtime(self):
185 finally: 169 finally:
186 fp.close() 170 fp.close()
187 171
188 def open_patch(self, mode=b'rb'): 172 def open_patch(self, mode=b'rb'):
189 return self.vfs(self.name + b'.patch', mode) 173 return self.vfs(self.name + b'.patch', mode)
174
175 def _backupfilename(self, filename):
176 def gennames(base):
177 yield base
178 base, ext = base.rsplit(b'.', 1)
179 for i in itertools.count(1):
180 yield b'%s-%d.%s' % (base, i, ext)
181
182 for n in gennames(filename):
183 if not self.backupvfs.exists(n):
184 return self.backupvfs.join(n)
185
186 def movetobackup(self):
187 if not self.backupvfs.isdir():
188 self.backupvfs.makedir()
189 for suffix in shelvefileextensions:
190 filename = self.name + b'.' + suffix
191 if self.vfs.exists(filename):
192 util.rename(
193 self.vfs.join(filename), self._backupfilename(filename)
194 )
190 195
191 196
192 class shelvedstate(object): 197 class shelvedstate(object):
193 """Handle persistence during unshelving operations. 198 """Handle persistence during unshelving operations.
194 199
600 def cleanupcmd(ui, repo): 605 def cleanupcmd(ui, repo):
601 """subcommand that deletes all shelves""" 606 """subcommand that deletes all shelves"""
602 607
603 with repo.wlock(): 608 with repo.wlock():
604 for _mtime, name in listshelves(repo): 609 for _mtime, name in listshelves(repo):
605 for suffix in shelvefileextensions: 610 Shelf(repo, name).movetobackup()
606 shfile = shelvedfile(repo, name, suffix)
607 if shfile.exists():
608 shfile.movetobackup()
609 cleanupoldbackups(repo) 611 cleanupoldbackups(repo)
610 612
611 613
612 def deletecmd(ui, repo, pats): 614 def deletecmd(ui, repo, pats):
613 """subcommand that deletes a specific shelve""" 615 """subcommand that deletes a specific shelve"""
617 for name in pats: 619 for name in pats:
618 if not Shelf(repo, name).exists(): 620 if not Shelf(repo, name).exists():
619 raise error.InputError( 621 raise error.InputError(
620 _(b"shelved change '%s' not found") % name 622 _(b"shelved change '%s' not found") % name
621 ) 623 )
622 for suffix in shelvefileextensions: 624 Shelf(repo, name).movetobackup()
623 shfile = shelvedfile(repo, name, suffix)
624 if shfile.exists():
625 shfile.movetobackup()
626 cleanupoldbackups(repo) 625 cleanupoldbackups(repo)
627 626
628 627
629 def listshelves(repo): 628 def listshelves(repo):
630 """return all shelves in repo as list of (time, name)""" 629 """return all shelves in repo as list of (time, name)"""
789 788
790 789
791 def unshelvecleanup(ui, repo, name, opts): 790 def unshelvecleanup(ui, repo, name, opts):
792 """remove related files after an unshelve""" 791 """remove related files after an unshelve"""
793 if not opts.get(b'keep'): 792 if not opts.get(b'keep'):
794 for filetype in shelvefileextensions: 793 Shelf(repo, name).movetobackup()
795 shfile = shelvedfile(repo, name, filetype)
796 if shfile.exists():
797 shfile.movetobackup()
798 cleanupoldbackups(repo) 794 cleanupoldbackups(repo)
799 795
800 796
801 def unshelvecontinue(ui, repo, state, opts): 797 def unshelvecontinue(ui, repo, state, opts):
802 """subcommand to continue an in-progress unshelve""" 798 """subcommand to continue an in-progress unshelve"""