~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconcile.py

  • Committer: Andrew Bennetts
  • Date: 2010-07-29 11:17:57 UTC
  • mfrom: (5050.3.17 2.2)
  • mto: This revision was merged to the branch mainline in revision 5365.
  • Revision ID: andrew.bennetts@canonical.com-20100729111757-018h3pcefo7z0dnq
Merge lp:bzr/2.2 into lp:bzr.

Show diffs side-by-side

added added

removed removed

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