mercurial/dirstateguard.py
author Jun Wu <quark@fb.com>
Sun, 06 Aug 2017 11:40:53 -0700
changeset 33734 71b77b61ed60
parent 33691 609606d21765
child 33814 bbbbd3c30bfc
permissions -rw-r--r--
test-rebase: add a brute force test Rebase is becoming more complex and it looks like a good idea to try some brute force enumeration to cover cases that are hard to find manually. Using brute force to generate repos in different shapes and enumerating the rebase source and destination would generate too many cases that takes too long to compute. This patch limits the "brute force" to only the "rebase source" part. Repo and destination are still manual. The added test cases are crafted manually to reveal some behaviors that are not covered by other tests: - "revlog index out of range" crash - after rebase, p1 == p2, p2 != null - "nothing to merge" abort In the future we might want to add more tests here. For now I'm more interested in revealing interesting behaviors in a minified way. I tried some more complex cases but didn't find other interesting behaviors. Differential Revision: https://phab.mercurial-scm.org/D262
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30497
751639bf6fc4 dirstateguard: move to new module so I can break some layering violations
Augie Fackler <augie@google.com>
parents: 30482
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
#
4635
63b9d2deed48 Updated copyright notices and add "and others" to "hg version"
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4633
diff changeset
     3
# Copyright 2005-2007 Matt Mackall <mpm@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
from __future__ import absolute_import
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
     9
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,
ebd0e86bdf89 cmdutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28313
diff changeset
    14
)
27624
d46db4390715 resolve: suggest the next action
timeless <timeless@mozdev.org>
parents: 27622
diff changeset
    15
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    16
class dirstateguard(object):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    17
    '''Restore dirstate at unexpected failure.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    18
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    19
    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
    20
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    21
    - 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
    22
    - 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
    23
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    24
    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
    25
    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
    26
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    27
    This just removes the backup file at ``close()`` before ``release()``.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    28
    '''
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    29
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    30
    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
    31
        self._repo = repo
30182
144d8fe266d9 cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails
Mads Kiilerich <madski@unity3d.com>
parents: 30151
diff changeset
    32
        self._active = False
144d8fe266d9 cmdutil: satisfy expections in dirstateguard.__del__, even if __init__ fails
Mads Kiilerich <madski@unity3d.com>
parents: 30151
diff changeset
    33
        self._closed = False
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    34
        self._backupname = 'dirstate.backup.%s.%d' % (name, id(self))
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    35
        repo.dirstate.savebackup(repo.currenttransaction(), self._backupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    36
        self._active = True
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    37
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    38
    def __del__(self):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    39
        if self._active: # still active
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    40
            # 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
    41
            # 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
    42
            # 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
    43
            # ``release(tr, ....)``.
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    44
            self._abort()
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    45
33691
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    46
    def __enter__(self):
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    47
        return self
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    48
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    49
    def __exit__(self, exc_type, exc_val, exc_tb):
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    50
        try:
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    51
            if exc_type is None:
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    52
                self.close()
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    53
        finally:
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    54
            self.release()
609606d21765 rebase: use one dirstateguard for when using rebase.singletransaction
Durham Goode <durham@fb.com>
parents: 33440
diff changeset
    55
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    56
    def close(self):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    57
        if not self._active: # already inactivated
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    58
            msg = (_("can't close already inactivated backup: %s")
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    59
                   % self._backupname)
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
    60
            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
    61
29137
d115cbf5980b dirstate: make backup methods public
Mateusz Kwapich <mitrandir@fb.com>
parents: 29103
diff changeset
    62
        self._repo.dirstate.clearbackup(self._repo.currenttransaction(),
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    63
                                         self._backupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    64
        self._active = False
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    65
        self._closed = True
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    66
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    67
    def _abort(self):
29137
d115cbf5980b dirstate: make backup methods public
Mateusz Kwapich <mitrandir@fb.com>
parents: 29103
diff changeset
    68
        self._repo.dirstate.restorebackup(self._repo.currenttransaction(),
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    69
                                           self._backupname)
24991
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    70
        self._active = False
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    71
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    72
    def release(self):
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    73
        if not self._closed:
4169a4f83548 cmdutil: add class to restore dirstate during unexpected failure
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 24988
diff changeset
    74
            if not self._active: # already inactivated
33440
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    75
                msg = (_("can't release already inactivated backup: %s")
ec306bc6915b dirstate: update backup functions to take full backup filename
Adam Simpkins <simpkins@fb.com>
parents: 30497
diff changeset
    76
                       % self._backupname)
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 26579
diff changeset
    77
                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
    78
            self._abort()