mercurial/dirstateguard.py
author Raphaël Gomès <rgomes@octobus.net>
Mon, 25 Jul 2022 15:39:04 +0200
changeset 49489 7c93e38a0bbd
parent 48875 6000f5b25c9b
permissions -rw-r--r--
rhg-status: add support for narrow clones
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30488
751639bf6fc4 dirstateguard: move to new module so I can break some layering violations
Augie Fackler <augie@google.com>
parents: 30473
diff changeset
     1
# dirstateguard.py - class to allow restoring dirstate after failure
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     2
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 45942
diff changeset
     3
# Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8210
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 10249
diff changeset
     6
# GNU General Public License version 2 or any later version.
2874
4ec58b157265 refactor text diff/patch code.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff changeset
     7
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
     8
47013
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
     9
import os
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    10
from .i18n import _
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    11
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    12
from . import (
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    13
    error,
38869
ad24b581e4d9 narrow: call narrowspec.{save,restore,clear}backup directly
Martin von Zweigbergk <martinvonz@google.com>
parents: 33793
diff changeset
    14
    narrowspec,
47013
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    15
    requirements,
33793
bbbbd3c30bfc util: add base class for transactional context managers
Martin von Zweigbergk <martinvonz@google.com>
parents: 33619
diff changeset
    16
    util,
28322
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    17
)
27624
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
    18
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    19
33793
bbbbd3c30bfc util: add base class for transactional context managers
Martin von Zweigbergk <martinvonz@google.com>
parents: 33619
diff changeset
    20
class dirstateguard(util.transactional):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43077
diff changeset
    21
    """Restore dirstate at unexpected failure.
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    22
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    23
    At the construction, this class does:
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    24
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    25
    - write current ``repo.dirstate`` out, and
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    26
    - save ``.hg/dirstate`` into the backup file
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    27
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    28
    This restores ``.hg/dirstate`` from backup file, if ``release()``
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    29
    is invoked before ``close()``.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    30
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    31
    This just removes the backup file at ``close()`` before ``release()``.
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43077
diff changeset
    32
    """
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    33
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    34
    def __init__(self, repo, name):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    35
        self._repo = repo
30182
144d8fe266d9 cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails
Mads Kiilerich <madski@unity3d.com>
parents: 30151
diff changeset
    36
        self._active = False
144d8fe266d9 cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails
Mads Kiilerich <madski@unity3d.com>
parents: 30151
diff changeset
    37
        self._closed = False
47013
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    38
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    39
        def getname(prefix):
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    40
            fd, fname = repo.vfs.mkstemp(prefix=prefix)
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    41
            os.close(fd)
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    42
            return fname
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    43
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    44
        self._backupname = getname(b'dirstate.backup.%s.' % name)
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30488
diff changeset
    45
        repo.dirstate.savebackup(repo.currenttransaction(), self._backupname)
47013
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    46
        # Don't make this the empty string, things may join it with stuff and
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    47
        # blindly try to unlink it, which could be bad.
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    48
        self._narrowspecbackupname = None
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    49
        if requirements.NARROW_REQUIREMENT in repo.requirements:
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    50
            self._narrowspecbackupname = getname(
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    51
                b'narrowspec.backup.%s.' % name
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    52
            )
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    53
            narrowspec.savewcbackup(repo, self._narrowspecbackupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    54
        self._active = True
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    55
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    56
    def __del__(self):
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    57
        if self._active:  # still active
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    58
            # this may occur, even if this class is used correctly:
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    59
            # for example, releasing other resources like transaction
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    60
            # may raise exception before ``dirstateguard.release`` in
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    61
            # ``release(tr, ....)``.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    62
            self._abort()
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    63
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    64
    def close(self):
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    65
        if not self._active:  # already inactivated
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    66
            msg = (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    67
                _(b"can't close already inactivated backup: %s")
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    68
                % self._backupname
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    69
            )
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
    70
            raise error.Abort(msg)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    71
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    72
        self._repo.dirstate.clearbackup(
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    73
            self._repo.currenttransaction(), self._backupname
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    74
        )
47013
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    75
        if self._narrowspecbackupname:
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    76
            narrowspec.clearwcbackup(self._repo, self._narrowspecbackupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    77
        self._active = False
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    78
        self._closed = True
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    79
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    80
    def _abort(self):
47013
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    81
        if self._narrowspecbackupname:
222a42ac5b2d dirstateguard: use mktemp-like functionality to generate the backup filenames
Kyle Lippincott <spectral@google.com>
parents: 46819
diff changeset
    82
            narrowspec.restorewcbackup(self._repo, self._narrowspecbackupname)
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    83
        self._repo.dirstate.restorebackup(
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    84
            self._repo.currenttransaction(), self._backupname
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    85
        )
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    86
        self._active = False
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    87
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    88
    def release(self):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    89
        if not self._closed:
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    90
            if not self._active:  # already inactivated
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    91
                msg = (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43075
diff changeset
    92
                    _(b"can't release already inactivated backup: %s")
43075
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    93
                    % self._backupname
57875cf423c9 style: run a patched black on a subset of mercurial
Augie Fackler <augie@google.com>
parents: 41227
diff changeset
    94
                )
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
    95
                raise error.Abort(msg)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    96
            self._abort()