~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconcile.py

  • Committer: Vincent Ladeuil
  • Date: 2010-03-10 09:33:04 UTC
  • mto: (5082.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5083.
  • Revision ID: v.ladeuil+lp@free.fr-20100310093304-4245t4tazd4sxoav
Cleanup test from overly cautious checks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Reconcilers are able to fix some potential data errors in a branch."""
18
18
 
19
 
from __future__ import absolute_import
20
19
 
21
20
__all__ = [
22
21
    'KnitReconciler',
30
29
from bzrlib import (
31
30
    cleanup,
32
31
    errors,
33
 
    revision as _mod_revision,
34
32
    ui,
 
33
    repository,
35
34
    )
36
35
from bzrlib.trace import mutter
37
36
from bzrlib.tsort import topo_sort
38
37
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
39
 
from bzrlib.i18n import gettext
40
 
 
41
 
 
42
 
def reconcile(dir, canonicalize_chks=False):
 
38
 
 
39
 
 
40
def reconcile(dir, other=None):
43
41
    """Reconcile the data in dir.
44
42
 
45
43
    Currently this is limited to a inventory 'reweave'.
49
47
    Directly using Reconciler is recommended for library users that
50
48
    desire fine grained control or analysis of the found issues.
51
49
 
52
 
    :param canonicalize_chks: Make sure CHKs are in canonical form.
 
50
    :param other: another bzrdir to reconcile against.
53
51
    """
54
 
    reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
 
52
    reconciler = Reconciler(dir, other=other)
55
53
    reconciler.reconcile()
56
54
 
57
55
 
58
56
class Reconciler(object):
59
57
    """Reconcilers are used to reconcile existing data."""
60
58
 
61
 
    def __init__(self, dir, other=None, canonicalize_chks=False):
 
59
    def __init__(self, dir, other=None):
62
60
        """Create a Reconciler."""
63
61
        self.bzrdir = dir
64
 
        self.canonicalize_chks = canonicalize_chks
65
62
 
66
63
    def reconcile(self):
67
64
        """Perform reconciliation.
68
65
 
69
66
        After reconciliation the following attributes document found issues:
70
 
 
71
 
        * `inconsistent_parents`: The number of revisions in the repository
72
 
          whose ancestry was being reported incorrectly.
73
 
        * `garbage_inventories`: The number of inventory objects without
74
 
          revisions that were garbage collected.
75
 
        * `fixed_branch_history`: None if there was no branch, False if the
76
 
          branch history was correct, True if the branch history needed to be
77
 
          re-normalized.
 
67
        inconsistent_parents: The number of revisions in the repository whose
 
68
                              ancestry was being reported incorrectly.
 
69
        garbage_inventories: The number of inventory objects without revisions
 
70
                             that were garbage collected.
 
71
        fixed_branch_history: None if there was no branch, False if the branch
 
72
                              history was correct, True if the branch history
 
73
                              needed to be re-normalized.
78
74
        """
79
75
        self.pb = ui.ui_factory.nested_progress_bar()
80
76
        try:
94
90
            # Nothing to check here
95
91
            self.fixed_branch_history = None
96
92
            return
97
 
        ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
 
93
        ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
98
94
        branch_reconciler = self.branch.reconcile(thorough=True)
99
95
        self.fixed_branch_history = branch_reconciler.fixed_history
100
96
 
101
97
    def _reconcile_repository(self):
102
98
        self.repo = self.bzrdir.find_repository()
103
 
        ui.ui_factory.note(gettext('Reconciling repository %s') %
104
 
            self.repo.user_url)
105
 
        self.pb.update(gettext("Reconciling repository"), 0, 1)
106
 
        if self.canonicalize_chks:
107
 
            try:
108
 
                self.repo.reconcile_canonicalize_chks
109
 
            except AttributeError:
110
 
                raise errors.BzrError(
111
 
                    gettext("%s cannot canonicalize CHKs.") % (self.repo,))
112
 
            repo_reconciler = self.repo.reconcile_canonicalize_chks()
113
 
        else:
114
 
            repo_reconciler = self.repo.reconcile(thorough=True)
 
99
        ui.ui_factory.note('Reconciling repository %s' %
 
100
            self.repo.bzrdir.root_transport.base)
 
101
        self.pb.update("Reconciling repository", 0, 1)
 
102
        repo_reconciler = self.repo.reconcile(thorough=True)
115
103
        self.inconsistent_parents = repo_reconciler.inconsistent_parents
116
104
        self.garbage_inventories = repo_reconciler.garbage_inventories
117
105
        if repo_reconciler.aborted:
118
 
            ui.ui_factory.note(gettext(
119
 
                'Reconcile aborted: revision index has inconsistent parents.'))
120
 
            ui.ui_factory.note(gettext(
121
 
                'Run "bzr check" for more details.'))
 
106
            ui.ui_factory.note(
 
107
                'Reconcile aborted: revision index has inconsistent parents.')
 
108
            ui.ui_factory.note(
 
109
                'Run "bzr check" for more details.')
122
110
        else:
123
 
            ui.ui_factory.note(gettext('Reconciliation complete.'))
 
111
            ui.ui_factory.note('Reconciliation complete.')
124
112
 
125
113
 
126
114
class BranchReconciler(object):
147
135
        self._reconcile_revision_history()
148
136
 
149
137
    def _reconcile_revision_history(self):
 
138
        repo = self.branch.repository
150
139
        last_revno, last_revision_id = self.branch.last_revision_info()
151
140
        real_history = []
152
 
        graph = self.branch.repository.get_graph()
153
141
        try:
154
 
            for revid in graph.iter_lefthand_ancestry(
155
 
                    last_revision_id, (_mod_revision.NULL_REVISION,)):
 
142
            for revid in repo.iter_reverse_revision_history(
 
143
                    last_revision_id):
156
144
                real_history.append(revid)
157
145
        except errors.RevisionNotPresent:
158
146
            pass # Hit a ghost left hand parent
163
151
            # set_revision_history, as this will regenerate it again.
164
152
            # Not really worth a whole BranchReconciler class just for this,
165
153
            # though.
166
 
            ui.ui_factory.note(gettext('Fixing last revision info {0} '\
167
 
                                       ' => {1}').format(
168
 
                                       last_revno, len(real_history)))
 
154
            ui.ui_factory.note('Fixing last revision info %s => %s' % (
 
155
                 last_revno, len(real_history)))
169
156
            self.branch.set_last_revision_info(len(real_history),
170
157
                                               last_revision_id)
171
158
        else:
172
159
            self.fixed_history = False
173
 
            ui.ui_factory.note(gettext('revision_history ok.'))
 
160
            ui.ui_factory.note('revision_history ok.')
174
161
 
175
162
 
176
163
class RepoReconciler(object):
201
188
        """Perform reconciliation.
202
189
 
203
190
        After reconciliation the following attributes document found issues:
204
 
 
205
 
        * `inconsistent_parents`: The number of revisions in the repository
206
 
          whose ancestry was being reported incorrectly.
207
 
        * `garbage_inventories`: The number of inventory objects without
208
 
          revisions that were garbage collected.
 
191
        inconsistent_parents: The number of revisions in the repository whose
 
192
                              ancestry was being reported incorrectly.
 
193
        garbage_inventories: The number of inventory objects without revisions
 
194
                             that were garbage collected.
209
195
        """
210
196
        operation = cleanup.OperationWithCleanups(self._reconcile)
211
197
        self.add_cleanup = operation.add_cleanup
230
216
        only data-loss causing issues (!self.thorough) or all issues
231
217
        (self.thorough) are treated as requiring the reweave.
232
218
        """
 
219
        # local because needing to know about WeaveFile is a wart we want to hide
 
220
        from bzrlib.weave import WeaveFile, Weave
233
221
        transaction = self.repo.get_transaction()
234
 
        self.pb.update(gettext('Reading inventory data'))
 
222
        self.pb.update('Reading inventory data')
235
223
        self.inventory = self.repo.inventories
236
224
        self.revisions = self.repo.revisions
237
225
        # the total set of revisions to process
251
239
        # (no garbage inventories or we are not doing a thorough check)
252
240
        if (not self.inconsistent_parents and
253
241
            (not self.garbage_inventories or not self.thorough)):
254
 
            ui.ui_factory.note(gettext('Inventory ok.'))
 
242
            ui.ui_factory.note('Inventory ok.')
255
243
            return
256
 
        self.pb.update(gettext('Backing up inventory'), 0, 0)
 
244
        self.pb.update('Backing up inventory', 0, 0)
257
245
        self.repo._backup_inventory()
258
 
        ui.ui_factory.note(gettext('Backup inventory created.'))
 
246
        ui.ui_factory.note('Backup inventory created.')
259
247
        new_inventories = self.repo._temp_inventories()
260
248
 
261
249
        # we have topological order of revisions and non ghost parents ready.
271
259
        if not (set(new_inventories.keys()) ==
272
260
            set([(revid,) for revid in self.pending])):
273
261
            raise AssertionError()
274
 
        self.pb.update(gettext('Writing weave'))
 
262
        self.pb.update('Writing weave')
275
263
        self.repo._activate_new_inventory()
276
264
        self.inventory = None
277
 
        ui.ui_factory.note(gettext('Inventory regenerated.'))
 
265
        ui.ui_factory.note('Inventory regenerated.')
278
266
 
279
267
    def _new_inv_parents(self, revision_key):
280
268
        """Lookup ghost-filtered parents for revision_key."""
368
356
    def _load_indexes(self):
369
357
        """Load indexes for the reconciliation."""
370
358
        self.transaction = self.repo.get_transaction()
371
 
        self.pb.update(gettext('Reading indexes'), 0, 2)
 
359
        self.pb.update('Reading indexes', 0, 2)
372
360
        self.inventory = self.repo.inventories
373
 
        self.pb.update(gettext('Reading indexes'), 1, 2)
 
361
        self.pb.update('Reading indexes', 1, 2)
374
362
        self.repo._check_for_inconsistent_revision_parents()
375
363
        self.revisions = self.repo.revisions
376
 
        self.pb.update(gettext('Reading indexes'), 2, 2)
 
364
        self.pb.update('Reading indexes', 2, 2)
377
365
 
378
366
    def _gc_inventory(self):
379
367
        """Remove inventories that are not referenced from the revision store."""
380
 
        self.pb.update(gettext('Checking unused inventories'), 0, 1)
 
368
        self.pb.update('Checking unused inventories', 0, 1)
381
369
        self._check_garbage_inventories()
382
 
        self.pb.update(gettext('Checking unused inventories'), 1, 3)
 
370
        self.pb.update('Checking unused inventories', 1, 3)
383
371
        if not self.garbage_inventories:
384
 
            ui.ui_factory.note(gettext('Inventory ok.'))
 
372
            ui.ui_factory.note('Inventory ok.')
385
373
            return
386
 
        self.pb.update(gettext('Backing up inventory'), 0, 0)
 
374
        self.pb.update('Backing up inventory', 0, 0)
387
375
        self.repo._backup_inventory()
388
 
        ui.ui_factory.note(gettext('Backup Inventory created'))
 
376
        ui.ui_factory.note('Backup Inventory created')
389
377
        # asking for '' should never return a non-empty weave
390
378
        new_inventories = self.repo._temp_inventories()
391
379
        # we have topological order of revisions and non ghost parents ready.
402
390
        # the revisionds list
403
391
        if not(set(new_inventories.keys()) == set(revision_keys)):
404
392
            raise AssertionError()
405
 
        self.pb.update(gettext('Writing weave'))
 
393
        self.pb.update('Writing weave')
406
394
        self.repo._activate_new_inventory()
407
395
        self.inventory = None
408
 
        ui.ui_factory.note(gettext('Inventory regenerated.'))
 
396
        ui.ui_factory.note('Inventory regenerated.')
409
397
 
410
398
    def _fix_text_parents(self):
411
399
        """Fix bad versionedfile parent entries.
443
431
            versions_list.append(text_key[1])
444
432
        # Do the reconcile of individual weaves.
445
433
        for num, file_id in enumerate(per_id_bad_parents):
446
 
            self.pb.update(gettext('Fixing text parents'), num,
 
434
            self.pb.update('Fixing text parents', num,
447
435
                           len(per_id_bad_parents))
448
436
            versions_with_bad_parents = per_id_bad_parents[file_id]
449
437
            id_unused_versions = set(key[-1] for key in unused_versions
507
495
    #  - lock the names list
508
496
    #  - perform a customised pack() that regenerates data as needed
509
497
    #  - unlock the names list
510
 
    # https://bugs.launchpad.net/bzr/+bug/154173
511
 
 
512
 
    def __init__(self, repo, other=None, thorough=False,
513
 
            canonicalize_chks=False):
514
 
        super(PackReconciler, self).__init__(repo, other=other,
515
 
            thorough=thorough)
516
 
        self.canonicalize_chks = canonicalize_chks
 
498
    # https://bugs.edge.launchpad.net/bzr/+bug/154173
517
499
 
518
500
    def _reconcile_steps(self):
519
501
        """Perform the steps to reconcile this repository."""
528
510
        total_inventories = len(list(
529
511
            collection.inventory_index.combined_index.iter_all_entries()))
530
512
        if len(all_revisions):
531
 
            if self.canonicalize_chks:
532
 
                reconcile_meth = self.repo._canonicalize_chks_pack
533
 
            else:
534
 
                reconcile_meth = self.repo._reconcile_pack
535
 
            new_pack = reconcile_meth(collection, packs, ".reconcile",
536
 
                all_revisions, self.pb)
 
513
            new_pack =  self.repo._reconcile_pack(collection, packs,
 
514
                ".reconcile", all_revisions, self.pb)
537
515
            if new_pack is not None:
538
516
                self._discard_and_save(packs)
539
517
        else: