mercurial/transaction.py
author Henrik Stuart <hg@hstuart.dk>
Sun, 19 Apr 2009 20:02:32 +0200
changeset 8290 560af1bbfd6e
parent 8289 fe8a3e56039f
child 8294 48a382c23226
permissions -rw-r--r--
transaction: reset transaction on abort Prevent the use of the transaction after it has been aborted. Co-contributor: Sune Foldager <cryo@cyanite.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     1
# transaction.py - simple journalling scheme for mercurial
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     2
#
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     3
# This transaction scheme is intended to gracefully handle program
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     4
# errors and interruptions. More serious failures like system crashes
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     5
# can be recovered with an fsck-like tool. As the whole repository is
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     6
# effectively log-structured, this should amount to simply truncating
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     7
# anything that isn't referenced in the changelog.
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
     8
#
2859
345bac2bc4ec update copyrights.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents: 2470
diff changeset
     9
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    10
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8071
diff changeset
    11
# This software may be used and distributed according to the terms of the
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8071
diff changeset
    12
# GNU General Public License version 2, incorporated herein by reference.
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    13
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 3877
diff changeset
    14
from i18n import _
7335
866d2715aff5 add missing import from 618140c75d8d
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7334
diff changeset
    15
import os, errno
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    16
import error
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    17
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    18
def active(func):
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    19
    def _active(self, *args, **kwds):
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    20
        if self.count == 0:
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    21
            raise error.Abort(_(
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    22
                'cannot use transaction when it is already committed/aborted'))
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    23
        return func(self, *args, **kwds)
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    24
    return _active
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    25
1559
59b3639df0a9 Convert all classes to new-style classes by deriving them from object.
Eric Hopper <hopper@omnifarious.org>
parents: 1541
diff changeset
    26
class transaction(object):
6065
53ed9b40cfc4 make the journal/undo files from transactions inherit the mode from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5865
diff changeset
    27
    def __init__(self, report, opener, journal, after=None, createmode=None):
162
5dcbe4d9a30c Implement recover and undo commands
mpm@selenic.com
parents: 108
diff changeset
    28
        self.journal = None
5dcbe4d9a30c Implement recover and undo commands
mpm@selenic.com
parents: 108
diff changeset
    29
1806
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    30
        self.count = 1
582
df8a5a0098d4 Remove all remaining print statements
mpm@selenic.com
parents: 573
diff changeset
    31
        self.report = report
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    32
        self.opener = opener
95
589f507bb259 Beginnings of transaction undo support
mpm@selenic.com
parents: 43
diff changeset
    33
        self.after = after
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    34
        self.entries = []
42
91f1fa847158 Fix multiple changes to file per transaction
mpm@selenic.com
parents: 13
diff changeset
    35
        self.map = {}
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    36
        self.journal = journal
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    37
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    38
        self.file = open(self.journal, "w")
6065
53ed9b40cfc4 make the journal/undo files from transactions inherit the mode from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5865
diff changeset
    39
        if createmode is not None:
53ed9b40cfc4 make the journal/undo files from transactions inherit the mode from .hg/store
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5865
diff changeset
    40
            os.chmod(self.journal, createmode & 0666)
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    41
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    42
    def __del__(self):
558
0ceea19182a9 transaction: __del__ should do nothing if the journal already exists
mpm@selenic.com
parents: 515
diff changeset
    43
        if self.journal:
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    44
            if self.entries: self._abort()
558
0ceea19182a9 transaction: __del__ should do nothing if the journal already exists
mpm@selenic.com
parents: 515
diff changeset
    45
            self.file.close()
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    46
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    47
    @active
2084
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    48
    def add(self, file, offset, data=None):
42
91f1fa847158 Fix multiple changes to file per transaction
mpm@selenic.com
parents: 13
diff changeset
    49
        if file in self.map: return
2084
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    50
        self.entries.append((file, offset, data))
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    51
        self.map[file] = len(self.entries) - 1
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    52
        # add enough data to the journal to do the truncate
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    53
        self.file.write("%s\0%d\n" % (file, offset))
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    54
        self.file.flush()
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    55
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    56
    @active
2084
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    57
    def find(self, file):
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    58
        if file in self.map:
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    59
            return self.entries[self.map[file]]
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    60
        return None
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    61
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    62
    @active
2084
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    63
    def replace(self, file, offset, data=None):
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    64
        if file not in self.map:
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    65
            raise KeyError(file)
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    66
        index = self.map[file]
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    67
        self.entries[index] = (file, offset, data)
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    68
        self.file.write("%s\0%d\n" % (file, offset))
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    69
        self.file.flush()
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
    70
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    71
    @active
1806
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    72
    def nest(self):
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    73
        self.count += 1
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    74
        return self
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    75
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    76
    def running(self):
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    77
        return self.count > 0
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    78
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    79
    @active
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    80
    def close(self):
1806
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    81
        self.count -= 1
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    82
        if self.count != 0:
a2c69737e65e Automatic nesting into running transactions in the same repository.
mason@suse.com
parents: 1559
diff changeset
    83
            return
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    84
        self.file.close()
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    85
        self.entries = []
95
589f507bb259 Beginnings of transaction undo support
mpm@selenic.com
parents: 43
diff changeset
    86
        if self.after:
785
46a8dd3145cc Fix undo after aborted commit bug
mpm@selenic.com
parents: 582
diff changeset
    87
            self.after()
95
589f507bb259 Beginnings of transaction undo support
mpm@selenic.com
parents: 43
diff changeset
    88
        else:
589f507bb259 Beginnings of transaction undo support
mpm@selenic.com
parents: 43
diff changeset
    89
            os.unlink(self.journal)
573
fbfbd4e506c3 transaction: nullify journal after close()
mpm@selenic.com
parents: 558
diff changeset
    90
        self.journal = None
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    91
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    92
    @active
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
    93
    def abort(self):
8289
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    94
        self._abort()
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    95
fe8a3e56039f transaction: ensure finished transactions are not reused
Henrik Stuart <hg@hstuart.dk>
parents: 8225
diff changeset
    96
    def _abort(self):
8290
560af1bbfd6e transaction: reset transaction on abort
Henrik Stuart <hg@hstuart.dk>
parents: 8289
diff changeset
    97
        self.count = 0
560af1bbfd6e transaction: reset transaction on abort
Henrik Stuart <hg@hstuart.dk>
parents: 8289
diff changeset
    98
        self.file.close()
560af1bbfd6e transaction: reset transaction on abort
Henrik Stuart <hg@hstuart.dk>
parents: 8289
diff changeset
    99
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   100
        if not self.entries: return
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   101
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   102
        self.report(_("transaction abort!\n"))
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   103
8071
9f14b66830a8 transaction: only delete journal on successful abort/commit
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7335
diff changeset
   104
        failed = False
2084
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
   105
        for f, o, ignore in self.entries:
108
8d55c2d72c7c Warn if we fail to truncate something
mpm@selenic.com
parents: 95
diff changeset
   106
            try:
8d55c2d72c7c Warn if we fail to truncate something
mpm@selenic.com
parents: 95
diff changeset
   107
                self.opener(f, "a").truncate(o)
8d55c2d72c7c Warn if we fail to truncate something
mpm@selenic.com
parents: 95
diff changeset
   108
            except:
8071
9f14b66830a8 transaction: only delete journal on successful abort/commit
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7335
diff changeset
   109
                failed = True
1402
9d2c2e6b32b5 i18n part2: use '_' for all strings who are part of the user interface
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1400
diff changeset
   110
                self.report(_("failed to truncate %s\n") % f)
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   111
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   112
        self.entries = []
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   113
8071
9f14b66830a8 transaction: only delete journal on successful abort/commit
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7335
diff changeset
   114
        if not failed:
9f14b66830a8 transaction: only delete journal on successful abort/commit
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7335
diff changeset
   115
            os.unlink(self.journal)
9f14b66830a8 transaction: only delete journal on successful abort/commit
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7335
diff changeset
   116
            self.report(_("rollback completed\n"))
9f14b66830a8 transaction: only delete journal on successful abort/commit
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7335
diff changeset
   117
        else:
9f14b66830a8 transaction: only delete journal on successful abort/commit
Henrik Stuart <henrik.stuart@edlund.dk>
parents: 7335
diff changeset
   118
            self.report(_("rollback failed - please run hg recover\n"))
515
03f27b1381f9 Whitespace cleanups
mpm@selenic.com
parents: 429
diff changeset
   119
8290
560af1bbfd6e transaction: reset transaction on abort
Henrik Stuart <hg@hstuart.dk>
parents: 8289
diff changeset
   120
        self.journal = None
560af1bbfd6e transaction: reset transaction on abort
Henrik Stuart <hg@hstuart.dk>
parents: 8289
diff changeset
   121
560af1bbfd6e transaction: reset transaction on abort
Henrik Stuart <hg@hstuart.dk>
parents: 8289
diff changeset
   122
162
5dcbe4d9a30c Implement recover and undo commands
mpm@selenic.com
parents: 108
diff changeset
   123
def rollback(opener, file):
2084
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
   124
    files = {}
162
5dcbe4d9a30c Implement recover and undo commands
mpm@selenic.com
parents: 108
diff changeset
   125
    for l in open(file).readlines():
5dcbe4d9a30c Implement recover and undo commands
mpm@selenic.com
parents: 108
diff changeset
   126
        f, o = l.split('\0')
6441
c9b8f2820bc0 rollback: unlink files truncated to length 0
Brendan Cully <brendan@kublai.com>
parents: 6065
diff changeset
   127
        files[f] = int(o)
2084
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
   128
    for f in files:
Chris Mason <mason@suse.com>
parents: 1806
diff changeset
   129
        o = files[f]
6441
c9b8f2820bc0 rollback: unlink files truncated to length 0
Brendan Cully <brendan@kublai.com>
parents: 6065
diff changeset
   130
        if o:
c9b8f2820bc0 rollback: unlink files truncated to length 0
Brendan Cully <brendan@kublai.com>
parents: 6065
diff changeset
   131
            opener(f, "a").truncate(int(o))
c9b8f2820bc0 rollback: unlink files truncated to length 0
Brendan Cully <brendan@kublai.com>
parents: 6065
diff changeset
   132
        else:
7334
618140c75d8d fix restart of interrupted recover
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6441
diff changeset
   133
            try:
618140c75d8d fix restart of interrupted recover
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6441
diff changeset
   134
                fn = opener(f).name
618140c75d8d fix restart of interrupted recover
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6441
diff changeset
   135
                os.unlink(fn)
618140c75d8d fix restart of interrupted recover
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6441
diff changeset
   136
            except OSError, inst:
618140c75d8d fix restart of interrupted recover
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6441
diff changeset
   137
                if inst.errno != errno.ENOENT:
618140c75d8d fix restart of interrupted recover
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 6441
diff changeset
   138
                    raise
162
5dcbe4d9a30c Implement recover and undo commands
mpm@selenic.com
parents: 108
diff changeset
   139
    os.unlink(file)
0
9117c6561b0b Add back links from file revisions to changeset revisions
mpm@selenic.com
parents:
diff changeset
   140