/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 bzrlib/reconcile.py

Fixup pb usage to use nested_progress_bar.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005, 2006 Canonical Limited.
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Reconcilers are able to fix some potential data errors in a branch."""
 
18
 
 
19
 
 
20
__all__ = ['reconcile', 'Reconciler', 'RepoReconciler']
 
21
 
 
22
 
 
23
import bzrlib.branch
 
24
import bzrlib.errors as errors
 
25
import bzrlib.progress
 
26
from bzrlib.trace import mutter
 
27
from bzrlib.tsort import TopoSorter
 
28
import bzrlib.ui as ui
 
29
 
 
30
 
 
31
def reconcile(dir):
 
32
    """Reconcile the data in dir.
 
33
 
 
34
    Currently this is limited to a inventory 'reweave'.
 
35
 
 
36
    This is a convenience method, for using a Reconciler object.
 
37
 
 
38
    Directly using Reconciler is recommended for library users that
 
39
    desire fine grained control or analysis of the found issues.
 
40
    """
 
41
    reconciler = Reconciler(dir)
 
42
    reconciler.reconcile()
 
43
 
 
44
 
 
45
class Reconciler(object):
 
46
    """Reconcilers are used to reconcile existing data."""
 
47
 
 
48
    def __init__(self, dir):
 
49
        self.bzrdir = dir
 
50
 
 
51
    def reconcile(self):
 
52
        """Perform reconciliation.
 
53
        
 
54
        After reconciliation the following attributes document found issues:
 
55
        inconsistent_parents: The number of revisions in the repository whose
 
56
                              ancestry was being reported incorrectly.
 
57
        garbage_inventories: The number of inventory objects without revisions
 
58
                             that were garbage collected.
 
59
        """
 
60
        self.pb = ui.ui_factory.nested_progress_bar()
 
61
        try:
 
62
            self._reconcile()
 
63
        finally:
 
64
            self.pb.finished()
 
65
 
 
66
    def _reconcile(self):
 
67
        """Helper function for performing reconciliation."""
 
68
        self.repo = self.bzrdir.find_repository()
 
69
        self.pb.note('Reconciling repository %s',
 
70
                     self.repo.bzrdir.root_transport.base)
 
71
        repo_reconciler = RepoReconciler(self.repo)
 
72
        repo_reconciler.reconcile()
 
73
        self.inconsistent_parents = repo_reconciler.inconsistent_parents
 
74
        self.garbage_inventories = repo_reconciler.garbage_inventories
 
75
        self.pb.note('Reconciliation complete.')
 
76
 
 
77
 
 
78
class RepoReconciler(object):
 
79
    """Reconciler that reconciles a repository.
 
80
 
 
81
    Currently this consists of an inventory reweave with revision cross-checks.
 
82
    """
 
83
 
 
84
    def __init__(self, repo):
 
85
        self.repo = repo
 
86
 
 
87
    def reconcile(self):
 
88
        """Perform reconciliation.
 
89
        
 
90
        After reconciliation the following attributes document found issues:
 
91
        inconsistent_parents: The number of revisions in the repository whose
 
92
                              ancestry was being reported incorrectly.
 
93
        garbage_inventories: The number of inventory objects without revisions
 
94
                             that were garbage collected.
 
95
        """
 
96
        self.repo.lock_write()
 
97
        try:
 
98
            self.pb = ui.ui_factory.nested_progress_bar()
 
99
            try:
 
100
                self._reweave_inventory()
 
101
            finally:
 
102
                self.pb.finished()
 
103
        finally:
 
104
            self.repo.unlock()
 
105
 
 
106
    def _reweave_inventory(self):
 
107
        """Regenerate the inventory weave for the repository from scratch."""
 
108
        self.pb.update('Reading inventory data.')
 
109
        self.inventory = self.repo.get_inventory_weave()
 
110
        # the total set of revisions to process
 
111
        self.pending = set([file_id for file_id in self.repo.revision_store])
 
112
 
 
113
        # mapping from revision_id to parents
 
114
        self._rev_graph = {}
 
115
        # errors that we detect
 
116
        self.inconsistent_parents = 0
 
117
        # we need the revision id of each revision and its available parents list
 
118
        self._setup_steps(len(self.pending))
 
119
        for rev_id in self.pending:
 
120
            # put a revision into the graph.
 
121
            self._graph_revision(rev_id)
 
122
        # we gc unreferenced inventories too
 
123
        self.garbage_inventories = len(self.inventory.names()) \
 
124
                                   - len(self._rev_graph)
 
125
 
 
126
        if not self.inconsistent_parents and not self.garbage_inventories:
 
127
            self.pb.note('Inventory ok.')
 
128
            return
 
129
        self.pb.update('Backing up inventory...', 0, 0)
 
130
        self.repo.control_weaves.put_weave('inventory.backup',
 
131
                                           self.inventory,
 
132
                                           self.repo.get_transaction())
 
133
        self.pb.note('Backup Inventory created.')
 
134
        # asking for '' should never return a non-empty weave
 
135
        new_inventory = self.repo.control_weaves.get_weave_or_empty('',
 
136
            self.repo.get_transaction())
 
137
 
 
138
        # we have topological order of revisions and non ghost parents ready.
 
139
        self._setup_steps(len(self._rev_graph))
 
140
        for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
 
141
            parents = self._rev_graph[rev_id]
 
142
            # double check this really is in topological order.
 
143
            unavailable = [p for p in parents if p not in new_inventory]
 
144
            assert len(unavailable) == 0
 
145
            # this entry has all the non ghost parents in the inventory
 
146
            # file already.
 
147
            self._reweave_step('adding inventories')
 
148
            new_inventory.add(rev_id, parents, self.inventory.get(rev_id))
 
149
 
 
150
        # if this worked, the set of new_inventory.names should equal
 
151
        # self.pending
 
152
        assert set(new_inventory.names()) == self.pending
 
153
        self.pb.update('Writing weave')
 
154
        self.repo.control_weaves.put_weave('inventory',
 
155
                                           new_inventory,
 
156
                                           self.repo.get_transaction())
 
157
        self.inventory = None
 
158
        self.pb.note('Inventory regenerated.')
 
159
 
 
160
    def _setup_steps(self, new_total):
 
161
        """Setup the markers we need to control the progress bar."""
 
162
        self.total = new_total
 
163
        self.count = 0
 
164
 
 
165
    def _graph_revision(self, rev_id):
 
166
        """Load a revision into the revision graph."""
 
167
        # pick a random revision
 
168
        # analyse revision id rev_id and put it in the stack.
 
169
        self._reweave_step('loading revisions')
 
170
        rev = self.repo.get_revision_reconcile(rev_id)
 
171
        assert rev.revision_id == rev_id
 
172
        parents = []
 
173
        for parent in rev.parent_ids:
 
174
            if self._parent_is_available(parent):
 
175
                parents.append(parent)
 
176
            else:
 
177
                mutter('found ghost %s', parent)
 
178
        self._rev_graph[rev_id] = parents   
 
179
        if set(self.inventory.parent_names(rev_id)) != set(parents):
 
180
            self.inconsistent_parents += 1
 
181
 
 
182
    def _parent_is_available(self, parent):
 
183
        """True if parent is a fully available revision
 
184
 
 
185
        A fully available revision has a inventory and a revision object in the
 
186
        repository.
 
187
        """
 
188
        return (parent in self._rev_graph or 
 
189
                (parent in self.inventory and self.repo.has_revision(parent)))
 
190
 
 
191
    def _reweave_step(self, message):
 
192
        """Mark a single step of regeneration complete."""
 
193
        self.pb.update(message, self.count, self.total)
 
194
        self.count += 1