view mercurial/dirstateguard.py @ 40424:7caf632e30c3

filecache: unimplement __set__() and __delete__() (API) Implementing __set__() implies that the descriptor can't be overridden by obj.__dict__, which means any property access involves slow function call. "Data descriptors with __set__() and __get__() defined always override a redefinition in an instance dictionary. In contrast, non-data descriptors can be overridden by instances." https://docs.python.org/2.7/reference/datamodel.html#invoking-descriptors This patch basically backs out 236bb604dc39, "scmutil: update cached copy when filecached attribute is assigned (issue3263)." The problem described in issue3263 (which is #3264 in Bugzilla) should no longer happen since repo._bookmarkcurrent has been moved to repo._bookmarks.active. We still have a risk of introducing similar bugs, but I think that's the cost we have to pay. $ hg perfrevset 'branch(tip)' -R mercurial (orig) wall 0.139511 comb 0.140000 user 0.140000 sys 0.000000 (best of 66) (prev) wall 0.114195 comb 0.110000 user 0.110000 sys 0.000000 (best of 81) (this) wall 0.099038 comb 0.110000 user 0.100000 sys 0.010000 (best of 93)
author Yuya Nishihara <yuya@tcha.org>
date Sat, 20 Oct 2018 17:56:00 +0900
parents ad24b581e4d9
children b74481038438
line wrap: on
line source

# dirstateguard.py - class to allow restoring dirstate after failure
#
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from __future__ import absolute_import

from .i18n import _

from . import (
    error,
    narrowspec,
    util,
)

class dirstateguard(util.transactional):
    '''Restore dirstate at unexpected failure.

    At the construction, this class does:

    - write current ``repo.dirstate`` out, and
    - save ``.hg/dirstate`` into the backup file

    This restores ``.hg/dirstate`` from backup file, if ``release()``
    is invoked before ``close()``.

    This just removes the backup file at ``close()`` before ``release()``.
    '''

    def __init__(self, repo, name):
        self._repo = repo
        self._active = False
        self._closed = False
        self._backupname = 'dirstate.backup.%s.%d' % (name, id(self))
        self._narrowspecbackupname = ('narrowspec.backup.%s.%d' %
                                      (name, id(self)))
        repo.dirstate.savebackup(repo.currenttransaction(), self._backupname)
        narrowspec.savebackup(repo, self._narrowspecbackupname)
        self._active = True

    def __del__(self):
        if self._active: # still active
            # this may occur, even if this class is used correctly:
            # for example, releasing other resources like transaction
            # may raise exception before ``dirstateguard.release`` in
            # ``release(tr, ....)``.
            self._abort()

    def close(self):
        if not self._active: # already inactivated
            msg = (_("can't close already inactivated backup: %s")
                   % self._backupname)
            raise error.Abort(msg)

        self._repo.dirstate.clearbackup(self._repo.currenttransaction(),
                                         self._backupname)
        narrowspec.clearbackup(self._repo, self._narrowspecbackupname)
        self._active = False
        self._closed = True

    def _abort(self):
        narrowspec.restorebackup(self._repo, self._narrowspecbackupname)
        self._repo.dirstate.restorebackup(self._repo.currenttransaction(),
                                           self._backupname)
        self._active = False

    def release(self):
        if not self._closed:
            if not self._active: # already inactivated
                msg = (_("can't release already inactivated backup: %s")
                       % self._backupname)
                raise error.Abort(msg)
            self._abort()