comparison mercurial/cmdutil.py @ 30488:751639bf6fc4

dirstateguard: move to new module so I can break some layering violations Recently in a review I noticed that localrepo almost has no reason to import cmdutil anymore. Also, cmdutil is a little on the enormous side, so breaking this class out strikes me as a win.
author Augie Fackler <augie@google.com>
date Mon, 21 Nov 2016 21:29:32 -0500
parents 39d13b8c101d
children 762c8a128357
comparison
equal deleted inserted replaced
30487:88a448a12ae8 30488:751639bf6fc4
25 from . import ( 25 from . import (
26 bookmarks, 26 bookmarks,
27 changelog, 27 changelog,
28 copies, 28 copies,
29 crecord as crecordmod, 29 crecord as crecordmod,
30 dirstateguard as dirstateguardmod,
30 encoding, 31 encoding,
31 error, 32 error,
32 formatter, 33 formatter,
33 graphmod, 34 graphmod,
34 lock as lockmod, 35 lock as lockmod,
3519 hint = None 3520 hint = None
3520 if after[1]: 3521 if after[1]:
3521 hint = after[0] 3522 hint = after[0]
3522 raise error.Abort(_('no %s in progress') % task, hint=hint) 3523 raise error.Abort(_('no %s in progress') % task, hint=hint)
3523 3524
3524 class dirstateguard(object): 3525 dirstateguard = dirstateguardmod.dirstateguard
3525 '''Restore dirstate at unexpected failure.
3526
3527 At the construction, this class does:
3528
3529 - write current ``repo.dirstate`` out, and
3530 - save ``.hg/dirstate`` into the backup file
3531
3532 This restores ``.hg/dirstate`` from backup file, if ``release()``
3533 is invoked before ``close()``.
3534
3535 This just removes the backup file at ``close()`` before ``release()``.
3536 '''
3537
3538 def __init__(self, repo, name):
3539 self._repo = repo
3540 self._active = False
3541 self._closed = False
3542 self._suffix = '.backup.%s.%d' % (name, id(self))
3543 repo.dirstate.savebackup(repo.currenttransaction(), self._suffix)
3544 self._active = True
3545
3546 def __del__(self):
3547 if self._active: # still active
3548 # this may occur, even if this class is used correctly:
3549 # for example, releasing other resources like transaction
3550 # may raise exception before ``dirstateguard.release`` in
3551 # ``release(tr, ....)``.
3552 self._abort()
3553
3554 def close(self):
3555 if not self._active: # already inactivated
3556 msg = (_("can't close already inactivated backup: dirstate%s")
3557 % self._suffix)
3558 raise error.Abort(msg)
3559
3560 self._repo.dirstate.clearbackup(self._repo.currenttransaction(),
3561 self._suffix)
3562 self._active = False
3563 self._closed = True
3564
3565 def _abort(self):
3566 self._repo.dirstate.restorebackup(self._repo.currenttransaction(),
3567 self._suffix)
3568 self._active = False
3569
3570 def release(self):
3571 if not self._closed:
3572 if not self._active: # already inactivated
3573 msg = (_("can't release already inactivated backup:"
3574 " dirstate%s")
3575 % self._suffix)
3576 raise error.Abort(msg)
3577 self._abort()