/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

  • Committer: Martin Pool
  • Date: 2006-03-16 20:20:32 UTC
  • mto: (1615.1.1 bzr.mbp.integration)
  • mto: This revision was merged to the branch mainline in revision 1616.
  • Revision ID: mbp@sourcefrog.net-20060316202032-d044f29e7d81b333
Update version numbers

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._reconcile_steps()
 
101
            finally:
 
102
                self.pb.finished()
 
103
        finally:
 
104
            self.repo.unlock()
 
105
 
 
106
    def _reconcile_steps(self):
 
107
        """Perform the steps to reconcile this repository."""
 
108
        self._reweave_inventory()
 
109
 
 
110
    def _reweave_inventory(self):
 
111
        """Regenerate the inventory weave for the repository from scratch."""
 
112
        # local because its really a wart we want to hide
 
113
        from bzrlib.weave import WeaveFile, Weave
 
114
        transaction = self.repo.get_transaction()
 
115
        self.pb.update('Reading inventory data.')
 
116
        self.inventory = self.repo.get_inventory_weave()
 
117
        # the total set of revisions to process
 
118
        self.pending = set([rev_id for rev_id in self.repo._revision_store.all_revision_ids(transaction)])
 
119
 
 
120
        # mapping from revision_id to parents
 
121
        self._rev_graph = {}
 
122
        # errors that we detect
 
123
        self.inconsistent_parents = 0
 
124
        # we need the revision id of each revision and its available parents list
 
125
        self._setup_steps(len(self.pending))
 
126
        for rev_id in self.pending:
 
127
            # put a revision into the graph.
 
128
            self._graph_revision(rev_id)
 
129
        self._check_garbage_inventories()
 
130
        if not self.inconsistent_parents and not self.garbage_inventories:
 
131
            self.pb.note('Inventory ok.')
 
132
            return
 
133
        self.pb.update('Backing up inventory...', 0, 0)
 
134
        self.repo.control_weaves.copy(self.inventory, 'inventory.backup', self.repo.get_transaction())
 
135
        self.pb.note('Backup Inventory created.')
 
136
        # asking for '' should never return a non-empty weave
 
137
        new_inventory = self.repo.control_weaves.get_empty('inventory.new',
 
138
            self.repo.get_transaction())
 
139
 
 
140
        # we have topological order of revisions and non ghost parents ready.
 
141
        self._setup_steps(len(self._rev_graph))
 
142
        for rev_id in TopoSorter(self._rev_graph.items()).iter_topo_order():
 
143
            parents = self._rev_graph[rev_id]
 
144
            # double check this really is in topological order.
 
145
            unavailable = [p for p in parents if p not in new_inventory]
 
146
            assert len(unavailable) == 0
 
147
            # this entry has all the non ghost parents in the inventory
 
148
            # file already.
 
149
            self._reweave_step('adding inventories')
 
150
            # ugly but needed, weaves are just way tooooo slow else.
 
151
            if isinstance(new_inventory, WeaveFile):
 
152
                Weave.add_lines(new_inventory, rev_id, parents, self.inventory.get_lines(rev_id))
 
153
            else:
 
154
                new_inventory.add_lines(rev_id, parents, self.inventory.get_lines(rev_id))
 
155
 
 
156
        if isinstance(new_inventory, WeaveFile):
 
157
            new_inventory._save()
 
158
        # if this worked, the set of new_inventory.names should equal
 
159
        # self.pending
 
160
        assert set(new_inventory.versions()) == self.pending
 
161
        self.pb.update('Writing weave')
 
162
        self.repo.control_weaves.copy(new_inventory, 'inventory', self.repo.get_transaction())
 
163
        self.repo.control_weaves.delete('inventory.new', self.repo.get_transaction())
 
164
        self.inventory = None
 
165
        self.pb.note('Inventory regenerated.')
 
166
 
 
167
    def _setup_steps(self, new_total):
 
168
        """Setup the markers we need to control the progress bar."""
 
169
        self.total = new_total
 
170
        self.count = 0
 
171
 
 
172
    def _graph_revision(self, rev_id):
 
173
        """Load a revision into the revision graph."""
 
174
        # pick a random revision
 
175
        # analyse revision id rev_id and put it in the stack.
 
176
        self._reweave_step('loading revisions')
 
177
        rev = self.repo.get_revision_reconcile(rev_id)
 
178
        assert rev.revision_id == rev_id
 
179
        parents = []
 
180
        for parent in rev.parent_ids:
 
181
            if self._parent_is_available(parent):
 
182
                parents.append(parent)
 
183
            else:
 
184
                mutter('found ghost %s', parent)
 
185
        self._rev_graph[rev_id] = parents   
 
186
        if set(self.inventory.get_parents(rev_id)) != set(parents):
 
187
            self.inconsistent_parents += 1
 
188
            mutter('Inconsistent inventory parents: id {%s} '
 
189
                   'inventory claims %r, '
 
190
                   'available parents are %r, '
 
191
                   'unavailable parents are %r',
 
192
                   rev_id, 
 
193
                   set(self.inventory.get_parents(rev_id)),
 
194
                   set(parents),
 
195
                   set(rev.parent_ids).difference(set(parents)))
 
196
 
 
197
    def _check_garbage_inventories(self):
 
198
        """Check for garbage inventories which we cannot trust
 
199
 
 
200
        We cant trust them because their pre-requisite file data may not
 
201
        be present - all we know is that their revision was not installed.
 
202
        """
 
203
        inventories = set(self.inventory.versions())
 
204
        revisions = set(self._rev_graph.keys())
 
205
        garbage = inventories.difference(revisions)
 
206
        self.garbage_inventories = len(garbage)
 
207
        for revision_id in garbage:
 
208
            mutter('Garbage inventory {%s} found.', revision_id)
 
209
 
 
210
    def _parent_is_available(self, parent):
 
211
        """True if parent is a fully available revision
 
212
 
 
213
        A fully available revision has a inventory and a revision object in the
 
214
        repository.
 
215
        """
 
216
        return (parent in self._rev_graph or 
 
217
                (parent in self.inventory and self.repo.has_revision(parent)))
 
218
 
 
219
    def _reweave_step(self, message):
 
220
        """Mark a single step of regeneration complete."""
 
221
        self.pb.update(message, self.count, self.total)
 
222
        self.count += 1
 
223
 
 
224
 
 
225
class KnitReconciler(RepoReconciler):
 
226
    """Reconciler that reconciles a knit format repository.
 
227
 
 
228
    This will detect garbage inventories and remove them.
 
229
 
 
230
    Inconsistent parentage is checked for in the revision weave.
 
231
    """
 
232
 
 
233
    def _reconcile_steps(self):
 
234
        """Perform the steps to reconcile this repository."""
 
235
        self._load_indexes()
 
236
        # knits never suffer this
 
237
        self.inconsistent_parents = 0
 
238
        self._gc_inventory()
 
239
 
 
240
    def _load_indexes(self):
 
241
        """Load indexes for the reconciliation."""
 
242
        self.transaction = self.repo.get_transaction()
 
243
        self.pb.update('Reading indexes.', 0, 2)
 
244
        self.inventory = self.repo.get_inventory_weave()
 
245
        self.pb.update('Reading indexes.', 1, 2)
 
246
        self.revisions = self.repo._revision_store.get_revision_file(self.transaction)
 
247
        self.pb.update('Reading indexes.', 2, 2)
 
248
 
 
249
    def _gc_inventory(self):
 
250
        """Remove inventories that are not referenced from the revision store."""
 
251
        self.pb.update('Checking unused inventories.', 0, 1)
 
252
        self._check_garbage_inventories()
 
253
        self.pb.update('Checking unused inventories.', 1, 3)
 
254
        if not self.garbage_inventories:
 
255
            self.pb.note('Inventory ok.')
 
256
            return
 
257
        self.pb.update('Backing up inventory...', 0, 0)
 
258
        self.repo.control_weaves.copy(self.inventory, 'inventory.backup', self.transaction)
 
259
        self.pb.note('Backup Inventory created.')
 
260
        # asking for '' should never return a non-empty weave
 
261
        new_inventory = self.repo.control_weaves.get_empty('inventory.new',
 
262
            self.transaction)
 
263
 
 
264
        # we have topological order of revisions and non ghost parents ready.
 
265
        self._setup_steps(len(self.revisions))
 
266
        for rev_id in TopoSorter(self.revisions.get_graph().items()).iter_topo_order():
 
267
            parents = self.revisions.get_parents(rev_id)
 
268
            # double check this really is in topological order.
 
269
            unavailable = [p for p in parents if p not in new_inventory]
 
270
            assert len(unavailable) == 0
 
271
            # this entry has all the non ghost parents in the inventory
 
272
            # file already.
 
273
            self._reweave_step('adding inventories')
 
274
            # ugly but needed, weaves are just way tooooo slow else.
 
275
            new_inventory.add_lines(rev_id, parents, self.inventory.get_lines(rev_id))
 
276
 
 
277
        # if this worked, the set of new_inventory.names should equal
 
278
        # self.pending
 
279
        assert set(new_inventory.versions()) == set(self.revisions.versions())
 
280
        self.pb.update('Writing weave')
 
281
        self.repo.control_weaves.copy(new_inventory, 'inventory', self.transaction)
 
282
        self.repo.control_weaves.delete('inventory.new', self.transaction)
 
283
        self.inventory = None
 
284
        self.pb.note('Inventory regenerated.')
 
285
 
 
286
    def _reinsert_revisions(self):
 
287
        """Correct the revision history for revisions in the revision knit."""
 
288
        # the total set of revisions to process
 
289
        self.pending = set(self.revisions.versions())
 
290
 
 
291
        # mapping from revision_id to parents
 
292
        self._rev_graph = {}
 
293
        # errors that we detect
 
294
        self.inconsistent_parents = 0
 
295
        # we need the revision id of each revision and its available parents list
 
296
        self._setup_steps(len(self.pending))
 
297
        for rev_id in self.pending:
 
298
            # put a revision into the graph.
 
299
            self._graph_revision(rev_id)
 
300
 
 
301
        if not self.inconsistent_parents:
 
302
            self.pb.note('Revision history accurate.')
 
303
            return
 
304
        self._setup_steps(len(self._rev_graph))
 
305
        for rev_id, parents in self._rev_graph.items():
 
306
            if parents != self.revisions.get_parents(rev_id):
 
307
                self.revisions.fix_parents(rev_id, parents)
 
308
            self._reweave_step('Fixing parents')
 
309
        self.pb.note('Ancestry corrected.')
 
310
 
 
311
    def _graph_revision(self, rev_id):
 
312
        """Load a revision into the revision graph."""
 
313
        # pick a random revision
 
314
        # analyse revision id rev_id and put it in the stack.
 
315
        self._reweave_step('loading revisions')
 
316
        rev = self.repo._revision_store.get_revision(rev_id, self.transaction)
 
317
        assert rev.revision_id == rev_id
 
318
        parents = []
 
319
        for parent in rev.parent_ids:
 
320
            if self.revisions.has_version(parent):
 
321
                parents.append(parent)
 
322
            else:
 
323
                mutter('found ghost %s', parent)
 
324
        self._rev_graph[rev_id] = parents   
 
325
        if set(self.inventory.get_parents(rev_id)) != set(parents):
 
326
            self.inconsistent_parents += 1
 
327
            mutter('Inconsistent inventory parents: id {%s} '
 
328
                   'inventory claims %r, '
 
329
                   'available parents are %r, '
 
330
                   'unavailable parents are %r',
 
331
                   rev_id, 
 
332
                   set(self.inventory.get_parents(rev_id)),
 
333
                   set(parents),
 
334
                   set(rev.parent_ids).difference(set(parents)))
 
335
 
 
336
    def _check_garbage_inventories(self):
 
337
        """Check for garbage inventories which we cannot trust
 
338
 
 
339
        We cant trust them because their pre-requisite file data may not
 
340
        be present - all we know is that their revision was not installed.
 
341
        """
 
342
        inventories = set(self.inventory.versions())
 
343
        revisions = set(self.revisions.versions())
 
344
        garbage = inventories.difference(revisions)
 
345
        self.garbage_inventories = len(garbage)
 
346
        for revision_id in garbage:
 
347
            mutter('Garbage inventory {%s} found.', revision_id)