/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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',
27
26
    ]
28
27
 
29
28
 
30
 
from . import (
 
29
from bzrlib import (
31
30
    cleanup,
32
31
    errors,
33
 
    revision as _mod_revision,
34
32
    ui,
35
33
    )
36
 
from .trace import mutter
37
 
from .tsort import topo_sort
38
 
from .bzr.versionedfile import AdapterFactory, FulltextContentFactory
39
 
from .i18n import gettext
40
 
 
41
 
 
42
 
def reconcile(dir, canonicalize_chks=False):
 
34
from bzrlib.trace import mutter
 
35
from bzrlib.tsort import topo_sort
 
36
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
 
37
 
 
38
 
 
39
def reconcile(dir, other=None):
43
40
    """Reconcile the data in dir.
44
41
 
45
42
    Currently this is limited to a inventory 'reweave'.
49
46
    Directly using Reconciler is recommended for library users that
50
47
    desire fine grained control or analysis of the found issues.
51
48
 
52
 
    :param canonicalize_chks: Make sure CHKs are in canonical form.
 
49
    :param other: another bzrdir to reconcile against.
53
50
    """
54
 
    reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
 
51
    reconciler = Reconciler(dir, other=other)
55
52
    reconciler.reconcile()
56
53
 
57
54
 
58
55
class Reconciler(object):
59
56
    """Reconcilers are used to reconcile existing data."""
60
57
 
61
 
    def __init__(self, dir, other=None, canonicalize_chks=False):
 
58
    def __init__(self, dir, other=None):
62
59
        """Create a Reconciler."""
63
 
        self.controldir = dir
64
 
        self.canonicalize_chks = canonicalize_chks
 
60
        self.bzrdir = dir
65
61
 
66
62
    def reconcile(self):
67
63
        """Perform reconciliation.
68
64
 
69
65
        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.
 
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.
78
73
        """
79
 
        operation = cleanup.OperationWithCleanups(self._reconcile)
80
 
        self.add_cleanup = operation.add_cleanup
81
 
        operation.run_simple()
 
74
        self.pb = ui.ui_factory.nested_progress_bar()
 
75
        try:
 
76
            self._reconcile()
 
77
        finally:
 
78
            self.pb.finished()
82
79
 
83
80
    def _reconcile(self):
84
81
        """Helper function for performing reconciliation."""
85
 
        self.pb = ui.ui_factory.nested_progress_bar()
86
 
        self.add_cleanup(self.pb.finished)
87
82
        self._reconcile_branch()
88
83
        self._reconcile_repository()
89
84
 
90
85
    def _reconcile_branch(self):
91
86
        try:
92
 
            self.branch = self.controldir.open_branch()
 
87
            self.branch = self.bzrdir.open_branch()
93
88
        except errors.NotBranchError:
94
89
            # Nothing to check here
95
90
            self.fixed_branch_history = None
96
91
            return
97
 
        ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
 
92
        ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
98
93
        branch_reconciler = self.branch.reconcile(thorough=True)
99
94
        self.fixed_branch_history = branch_reconciler.fixed_history
100
95
 
101
96
    def _reconcile_repository(self):
102
 
        self.repo = self.controldir.find_repository()
103
 
        ui.ui_factory.note(gettext('Reconciling repository %s') %
 
97
        self.repo = self.bzrdir.find_repository()
 
98
        ui.ui_factory.note('Reconciling repository %s' %
104
99
            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)
 
100
        self.pb.update("Reconciling repository", 0, 1)
 
101
        repo_reconciler = self.repo.reconcile(thorough=True)
115
102
        self.inconsistent_parents = repo_reconciler.inconsistent_parents
116
103
        self.garbage_inventories = repo_reconciler.garbage_inventories
117
104
        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 "brz 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.')
122
109
        else:
123
 
            ui.ui_factory.note(gettext('Reconciliation complete.'))
 
110
            ui.ui_factory.note('Reconciliation complete.')
124
111
 
125
112
 
126
113
class BranchReconciler(object):
147
134
        self._reconcile_revision_history()
148
135
 
149
136
    def _reconcile_revision_history(self):
 
137
        repo = self.branch.repository
150
138
        last_revno, last_revision_id = self.branch.last_revision_info()
151
139
        real_history = []
152
 
        graph = self.branch.repository.get_graph()
153
140
        try:
154
 
            for revid in graph.iter_lefthand_ancestry(
155
 
                    last_revision_id, (_mod_revision.NULL_REVISION,)):
 
141
            for revid in repo.iter_reverse_revision_history(
 
142
                    last_revision_id):
156
143
                real_history.append(revid)
157
144
        except errors.RevisionNotPresent:
158
145
            pass # Hit a ghost left hand parent
163
150
            # set_revision_history, as this will regenerate it again.
164
151
            # Not really worth a whole BranchReconciler class just for this,
165
152
            # though.
166
 
            ui.ui_factory.note(gettext('Fixing last revision info {0} '\
167
 
                                       ' => {1}').format(
168
 
                                       last_revno, len(real_history)))
 
153
            ui.ui_factory.note('Fixing last revision info %s => %s' % (
 
154
                 last_revno, len(real_history)))
169
155
            self.branch.set_last_revision_info(len(real_history),
170
156
                                               last_revision_id)
171
157
        else:
172
158
            self.fixed_history = False
173
 
            ui.ui_factory.note(gettext('revision_history ok.'))
 
159
            ui.ui_factory.note('revision_history ok.')
174
160
 
175
161
 
176
162
class RepoReconciler(object):
201
187
        """Perform reconciliation.
202
188
 
203
189
        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.
 
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.
209
194
        """
210
195
        operation = cleanup.OperationWithCleanups(self._reconcile)
211
196
        self.add_cleanup = operation.add_cleanup
230
215
        only data-loss causing issues (!self.thorough) or all issues
231
216
        (self.thorough) are treated as requiring the reweave.
232
217
        """
 
218
        # local because needing to know about WeaveFile is a wart we want to hide
 
219
        from bzrlib.weave import WeaveFile, Weave
233
220
        transaction = self.repo.get_transaction()
234
 
        self.pb.update(gettext('Reading inventory data'))
 
221
        self.pb.update('Reading inventory data')
235
222
        self.inventory = self.repo.inventories
236
223
        self.revisions = self.repo.revisions
237
224
        # the total set of revisions to process
238
 
        self.pending = {key[-1] for key in self.revisions.keys()}
 
225
        self.pending = set([key[-1] for key in self.revisions.keys()])
239
226
 
240
227
        # mapping from revision_id to parents
241
228
        self._rev_graph = {}
251
238
        # (no garbage inventories or we are not doing a thorough check)
252
239
        if (not self.inconsistent_parents and
253
240
            (not self.garbage_inventories or not self.thorough)):
254
 
            ui.ui_factory.note(gettext('Inventory ok.'))
 
241
            ui.ui_factory.note('Inventory ok.')
255
242
            return
256
 
        self.pb.update(gettext('Backing up inventory'), 0, 0)
 
243
        self.pb.update('Backing up inventory', 0, 0)
257
244
        self.repo._backup_inventory()
258
 
        ui.ui_factory.note(gettext('Backup inventory created.'))
 
245
        ui.ui_factory.note('Backup inventory created.')
259
246
        new_inventories = self.repo._temp_inventories()
260
247
 
261
248
        # we have topological order of revisions and non ghost parents ready.
269
256
        # if this worked, the set of new_inventories.keys should equal
270
257
        # self.pending
271
258
        if not (set(new_inventories.keys()) ==
272
 
            {(revid,) for revid in self.pending}):
 
259
            set([(revid,) for revid in self.pending])):
273
260
            raise AssertionError()
274
 
        self.pb.update(gettext('Writing weave'))
 
261
        self.pb.update('Writing weave')
275
262
        self.repo._activate_new_inventory()
276
263
        self.inventory = None
277
 
        ui.ui_factory.note(gettext('Inventory regenerated.'))
 
264
        ui.ui_factory.note('Inventory regenerated.')
278
265
 
279
266
    def _new_inv_parents(self, revision_key):
280
267
        """Lookup ghost-filtered parents for revision_key."""
368
355
    def _load_indexes(self):
369
356
        """Load indexes for the reconciliation."""
370
357
        self.transaction = self.repo.get_transaction()
371
 
        self.pb.update(gettext('Reading indexes'), 0, 2)
 
358
        self.pb.update('Reading indexes', 0, 2)
372
359
        self.inventory = self.repo.inventories
373
 
        self.pb.update(gettext('Reading indexes'), 1, 2)
 
360
        self.pb.update('Reading indexes', 1, 2)
374
361
        self.repo._check_for_inconsistent_revision_parents()
375
362
        self.revisions = self.repo.revisions
376
 
        self.pb.update(gettext('Reading indexes'), 2, 2)
 
363
        self.pb.update('Reading indexes', 2, 2)
377
364
 
378
365
    def _gc_inventory(self):
379
366
        """Remove inventories that are not referenced from the revision store."""
380
 
        self.pb.update(gettext('Checking unused inventories'), 0, 1)
 
367
        self.pb.update('Checking unused inventories', 0, 1)
381
368
        self._check_garbage_inventories()
382
 
        self.pb.update(gettext('Checking unused inventories'), 1, 3)
 
369
        self.pb.update('Checking unused inventories', 1, 3)
383
370
        if not self.garbage_inventories:
384
 
            ui.ui_factory.note(gettext('Inventory ok.'))
 
371
            ui.ui_factory.note('Inventory ok.')
385
372
            return
386
 
        self.pb.update(gettext('Backing up inventory'), 0, 0)
 
373
        self.pb.update('Backing up inventory', 0, 0)
387
374
        self.repo._backup_inventory()
388
 
        ui.ui_factory.note(gettext('Backup Inventory created'))
 
375
        ui.ui_factory.note('Backup Inventory created')
389
376
        # asking for '' should never return a non-empty weave
390
377
        new_inventories = self.repo._temp_inventories()
391
378
        # we have topological order of revisions and non ghost parents ready.
402
389
        # the revisionds list
403
390
        if not(set(new_inventories.keys()) == set(revision_keys)):
404
391
            raise AssertionError()
405
 
        self.pb.update(gettext('Writing weave'))
 
392
        self.pb.update('Writing weave')
406
393
        self.repo._activate_new_inventory()
407
394
        self.inventory = None
408
 
        ui.ui_factory.note(gettext('Inventory regenerated.'))
 
395
        ui.ui_factory.note('Inventory regenerated.')
409
396
 
410
397
    def _fix_text_parents(self):
411
398
        """Fix bad versionedfile parent entries.
430
417
            # NB: This is really not needed, reconcile != pack.
431
418
            per_id_bad_parents[key[0]] = {}
432
419
        # Generate per-knit/weave data.
433
 
        for key, details in bad_parents.items():
 
420
        for key, details in bad_parents.iteritems():
434
421
            file_id = key[0]
435
422
            rev_id = key[1]
436
423
            knit_parents = tuple([parent[-1] for parent in details[0]])
443
430
            versions_list.append(text_key[1])
444
431
        # Do the reconcile of individual weaves.
445
432
        for num, file_id in enumerate(per_id_bad_parents):
446
 
            self.pb.update(gettext('Fixing text parents'), num,
 
433
            self.pb.update('Fixing text parents', num,
447
434
                           len(per_id_bad_parents))
448
435
            versions_with_bad_parents = per_id_bad_parents[file_id]
449
436
            id_unused_versions = set(key[-1] for key in unused_versions
507
494
    #  - lock the names list
508
495
    #  - perform a customised pack() that regenerates data as needed
509
496
    #  - 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
 
497
    # https://bugs.edge.launchpad.net/bzr/+bug/154173
517
498
 
518
499
    def _reconcile_steps(self):
519
500
        """Perform the steps to reconcile this repository."""
528
509
        total_inventories = len(list(
529
510
            collection.inventory_index.combined_index.iter_all_entries()))
530
511
        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)
 
512
            new_pack =  self.repo._reconcile_pack(collection, packs,
 
513
                ".reconcile", all_revisions, self.pb)
537
514
            if new_pack is not None:
538
515
                self._discard_and_save(packs)
539
516
        else: