/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/repodebug/missing_keys_for_stacking_fixer.py

  • Committer: Jelmer Vernooij
  • Date: 2017-09-02 22:39:35 UTC
  • mfrom: (0.198.9 bzr-repodebug)
  • mto: This revision was merged to the branch mainline in revision 6786.
  • Revision ID: jelmer@jelmer.uk-20170902223935-tdznuv85ppvlmd8u
Merge lp:bzr-repodebug.

Show diffs side-by-side

added added

removed removed

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