/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
1
# Copyright (C) 2009-2011 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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
17
from __future__ import absolute_import
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
18
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
19
from ...controldir import ControlDir
20
from ...commands import Command, Option
21
from ... import errors
22
from ...bzr.vf_search import PendingAncestryResult
23
from ...revision import NULL_REVISION
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
24
25
26
class cmd_fix_missing_keys_for_stacking(Command):
27
    """Fix missing keys for stacking.
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
28
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
29
    This is the fixer script for <https://bugs.launchpad.net/bzr/+bug/354036>.
30
    """
31
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
32
    hidden = True
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
33
    takes_args = ['branch_url']
34
    takes_options = [
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
35
        Option(
36
            'dry-run',
37
            help="Show what would be done, but don't actually do anything."),
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
38
        ]
39
40
    def run(self, branch_url, dry_run=False):
41
        try:
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
42
            bd = ControlDir.open(branch_url)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
43
            b = bd.open_branch(ignore_fallbacks=True)
44
        except (errors.NotBranchError, errors.InvalidURL):
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
45
            raise errors.BzrCommandError(
46
                    "Not a branch or invalid URL: %s" % branch_url)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
47
        b.lock_read()
48
        try:
49
            url = b.get_stacked_on_url()
50
        except (errors.UnstackableRepositoryFormat, errors.NotStacked,
51
            errors.UnstackableBranchFormat):
52
            b.unlock()
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
53
            raise errors.BzrCommandError("Not stacked: %s" % branch_url)
54
        raw_r = b.repository.controldir.open_repository()
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
55
        if dry_run:
56
            raw_r.lock_read()
57
        else:
58
            b.unlock()
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
59
            b = b.controldir.open_branch()
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
60
            b.lock_read()
61
            raw_r.lock_write()
62
        try:
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
63
            revs = raw_r.all_revision_ids()
64
            rev_parents = raw_r.get_graph().get_parent_map(revs)
65
            needed = set()
66
            map(needed.update, rev_parents.itervalues())
67
            needed.discard(NULL_REVISION)
68
            needed = set((rev,) for rev in needed)
69
            needed = needed - raw_r.inventories.keys()
70
            if not needed:
71
                # Nothing to see here.
72
                return
73
            self.outf.write("Missing inventories: %r\n" % needed)
74
            if dry_run:
75
                return
76
            assert raw_r._format.network_name() == b.repository._format.network_name()
77
            stream = b.repository.inventories.get_record_stream(
78
                    needed, 'topological', True)
79
            raw_r.start_write_group()
80
            try:
81
                raw_r.inventories.insert_record_stream(stream)
82
            except:
83
                raw_r.abort_write_group()
84
                raise
85
            else:
86
                raw_r.commit_write_group()
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
87
        finally:
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
88
            raw_r.unlock()
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
89
        b.unlock()
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
90
        self.outf.write("Fixed: %s\n" % branch_url)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
91
92
93
class cmd_mirror_revs_into(Command):
94
    """Mirror all revs from one repo into another."""
95
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
96
    hidden = True
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
97
    takes_args = ['source', 'destination']
98
99
    _see_also = ['fetch-all-records']
100
101
    def run(self, source, destination):
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
102
        bd = ControlDir.open(source)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
103
        source_r = bd.open_branch().repository
6779.1.1 by Jelmer Vernooij
Merge lp:bzr-repodebug.
104
        bd = ControlDir.open(destination)
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
105
        target_r = bd.open_branch().repository
6779.1.3 by Jelmer Vernooij
Add some basic tests for repodebug.
106
        with source_r.lock_read(), target_r.lock_write():
0.198.1 by Andrew Bennetts
Collate various hackish repair and debug plugins into one plugin.
107
            revs = [k[-1] for k in source_r.revisions.keys()]
108
            target_r.fetch(
109
                source_r, fetch_spec=PendingAncestryResult(revs, source_r))