comparison hgext/shelve.py @ 30378:c5126aab9c37

shelve: move possible shelve file extensions to a single place This and a couple of following patches are a preparation to implementing obsolescense-enabled shelve which was discussed on a Sprint. If this refactoring is not done, shelve is going to look even more hackish than now. This particular commit introduces a slight behavior change. Previously, if only .hg/shelve/name.patch file exists, but .hg/name.hg does not, 'hg shelve -d name' would fail saying "shelve not found". Now deletion will only fail if .patch file does not exist (since .patch is used as an indicator of an existing shelve). Other shelve files being absent are skipped silently to accommodate for future introduction of obs-based shelve, which will mean that for some shelves .hg and .patch files exist, while for others .hg and .oshelve.
author Kostia Balytskyi <ikostia@fb.com>
date Thu, 10 Nov 2016 03:07:20 -0800
parents d5883fd055c6
children 684068d24658
comparison
equal deleted inserted replaced
30377:2019fbdab075 30378:c5126aab9c37
60 # leave the attribute unspecified. 60 # leave the attribute unspecified.
61 testedwith = 'ships-with-hg-core' 61 testedwith = 'ships-with-hg-core'
62 62
63 backupdir = 'shelve-backup' 63 backupdir = 'shelve-backup'
64 shelvedir = 'shelved' 64 shelvedir = 'shelved'
65 shelvefileextensions = ['hg', 'patch']
65 66
66 class shelvedfile(object): 67 class shelvedfile(object):
67 """Helper for the file storing a single shelve 68 """Helper for the file storing a single shelve
68 69
69 Handles common functions on shelve files (.hg/.patch) using 70 Handles common functions on shelve files (.hg/.patch) using
219 for mtime, f in hgfiles[:len(hgfiles) - maxbackups]: 220 for mtime, f in hgfiles[:len(hgfiles) - maxbackups]:
220 if mtime == bordermtime: 221 if mtime == bordermtime:
221 # keep it, because timestamp can't decide exact order of backups 222 # keep it, because timestamp can't decide exact order of backups
222 continue 223 continue
223 base = f[:-3] 224 base = f[:-3]
224 for ext in 'hg patch'.split(): 225 for ext in shelvefileextensions:
225 try: 226 try:
226 vfs.unlink(base + '.' + ext) 227 vfs.unlink(base + '.' + ext)
227 except OSError as err: 228 except OSError as err:
228 if err.errno != errno.ENOENT: 229 if err.errno != errno.ENOENT:
229 raise 230 raise
397 """subcommand that deletes all shelves""" 398 """subcommand that deletes all shelves"""
398 399
399 with repo.wlock(): 400 with repo.wlock():
400 for (name, _type) in repo.vfs.readdir(shelvedir): 401 for (name, _type) in repo.vfs.readdir(shelvedir):
401 suffix = name.rsplit('.', 1)[-1] 402 suffix = name.rsplit('.', 1)[-1]
402 if suffix in ('hg', 'patch'): 403 if suffix in shelvefileextensions:
403 shelvedfile(repo, name).movetobackup() 404 shelvedfile(repo, name).movetobackup()
404 cleanupoldbackups(repo) 405 cleanupoldbackups(repo)
405 406
406 def deletecmd(ui, repo, pats): 407 def deletecmd(ui, repo, pats):
407 """subcommand that deletes a specific shelve""" 408 """subcommand that deletes a specific shelve"""
408 if not pats: 409 if not pats:
409 raise error.Abort(_('no shelved changes specified!')) 410 raise error.Abort(_('no shelved changes specified!'))
410 with repo.wlock(): 411 with repo.wlock():
411 try: 412 try:
412 for name in pats: 413 for name in pats:
413 for suffix in 'hg patch'.split(): 414 for suffix in shelvefileextensions:
414 shelvedfile(repo, name, suffix).movetobackup() 415 shfile = shelvedfile(repo, name, suffix)
416 # patch file is necessary, as it should
417 # be present for any kind of shelve,
418 # but the .hg file is optional as in future we
419 # will add obsolete shelve with does not create a
420 # bundle
421 if shfile.exists() or suffix == 'patch':
422 shfile.movetobackup()
415 cleanupoldbackups(repo) 423 cleanupoldbackups(repo)
416 except OSError as err: 424 except OSError as err:
417 if err.errno != errno.ENOENT: 425 if err.errno != errno.ENOENT:
418 raise 426 raise
419 raise error.Abort(_("shelved change '%s' not found") % name) 427 raise error.Abort(_("shelved change '%s' not found") % name)
555 % branchtorestore) 563 % branchtorestore)
556 564
557 def unshelvecleanup(ui, repo, name, opts): 565 def unshelvecleanup(ui, repo, name, opts):
558 """remove related files after an unshelve""" 566 """remove related files after an unshelve"""
559 if not opts.get('keep'): 567 if not opts.get('keep'):
560 for filetype in 'hg patch'.split(): 568 for filetype in shelvefileextensions:
561 shelvedfile(repo, name, filetype).movetobackup() 569 shfile = shelvedfile(repo, name, filetype)
570 if shfile.exists():
571 shfile.movetobackup()
562 cleanupoldbackups(repo) 572 cleanupoldbackups(repo)
563 573
564 def unshelvecontinue(ui, repo, state, opts): 574 def unshelvecontinue(ui, repo, state, opts):
565 """subcommand to continue an in-progress unshelve""" 575 """subcommand to continue an in-progress unshelve"""
566 # We're finishing off a merge. First parent is our original 576 # We're finishing off a merge. First parent is our original