~bzr-pqm/bzr/bzr.dev

2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
1
# Copyright (C) 2007 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
16
17
"""Helper classes for repository implementation tests."""
18
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
19
from cStringIO import StringIO
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
20
21
from bzrlib import (
22
    inventory,
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
23
    osutils,
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
24
    revision as _mod_revision,
25
    )
26
from bzrlib.repofmt.knitrepo import RepositoryFormatKnit
3689.1.1 by John Arbash Meinel
Rename repository_implementations tests into per_repository tests
27
from bzrlib.tests.per_repository import TestCaseWithRepository
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
28
from bzrlib.tests import TestNotApplicable
29
30
31
class TestCaseWithBrokenRevisionIndex(TestCaseWithRepository):
32
33
    def make_repo_with_extra_ghost_index(self):
34
        """Make a corrupt repository.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
35
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
36
        It will contain one revision, 'revision-id'.  The knit index will claim
37
        that it has one parent, 'incorrect-parent', but the revision text will
38
        claim it has no parents.
39
40
        Note: only the *cache* of the knit index is corrupted.  Thus the
41
        corruption will only last while the repository is locked.  For this
42
        reason, the returned repo is locked.
43
        """
44
        if not isinstance(self.repository_format, RepositoryFormatKnit):
45
            # XXX: Broken revision graphs can happen to weaves too, but they're
46
            # pretty deprecated.  Ideally these tests should apply to any repo
47
            # where repo.revision_graph_can_have_wrong_parents() is True, but
48
            # at the moment we only know how to corrupt knit repos.
49
            raise TestNotApplicable(
50
                "%s isn't a knit format" % self.repository_format)
51
52
        repo = self.make_repository('broken')
2592.3.214 by Robert Collins
Merge bzr.dev.
53
        repo.lock_write()
54
        repo.start_write_group()
55
        try:
56
            inv = inventory.Inventory(revision_id='revision-id')
57
            inv.root.revision = 'revision-id'
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
58
            inv_sha1 = repo.add_inventory('revision-id', inv, [])
2951.1.5 by Robert Collins
Some work towards including the correct changes for TREE_ROOT in check parameterised tests.
59
            if repo.supports_rich_root():
60
                root_id = inv.root.file_id
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
61
                repo.texts.add_lines((root_id, 'revision-id'), [], [])
2592.3.214 by Robert Collins
Merge bzr.dev.
62
            revision = _mod_revision.Revision('revision-id',
63
                committer='jrandom@example.com', timestamp=0,
2951.1.3 by Robert Collins
Partial support for native reconcile with packs.
64
                inventory_sha1=inv_sha1, timezone=0, message='message',
65
                parent_ids=[])
66
            # Manually add the revision text using the RevisionStore API, with
67
            # bad parents.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
68
            rev_text = repo._serializer.write_revision_to_string(revision)
69
            repo.revisions.add_lines((revision.revision_id,),
70
                [('incorrect-parent',)],
71
                osutils.split_lines(rev_text))
2592.3.214 by Robert Collins
Merge bzr.dev.
72
        except:
73
            repo.abort_write_group()
74
            repo.unlock()
75
            raise
2592.3.215 by Robert Collins
Review feedback.
76
        else:
2592.3.214 by Robert Collins
Merge bzr.dev.
77
            repo.commit_write_group()
78
            repo.unlock()
2819.2.5 by Andrew Bennetts
Make reconcile abort gracefully if the revision index has bad parents.
79
80
        repo.lock_write()
81
        self.addCleanup(repo.unlock)
82
        return repo
83