~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconcile.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-13 06:08:53 UTC
  • mfrom: (4737.1.1 merge-2.0-into-devel)
  • Revision ID: pqm@pqm.ubuntu.com-20091013060853-erk2aaj80fnkrv25
(andrew) Merge lp:bzr/2.0 into lp:bzr, including fixes for #322807,
        #389413, #402623 and documentation improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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',
28
27
 
29
28
 
30
29
from bzrlib import (
31
 
    cleanup,
32
30
    errors,
33
 
    revision as _mod_revision,
34
31
    ui,
 
32
    repository,
 
33
    repofmt,
35
34
    )
36
 
from bzrlib.trace import mutter
 
35
from bzrlib.trace import mutter, note
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):
132
120
        self.branch = a_branch
133
121
 
134
122
    def reconcile(self):
135
 
        operation = cleanup.OperationWithCleanups(self._reconcile)
136
 
        self.add_cleanup = operation.add_cleanup
137
 
        operation.run_simple()
138
 
 
139
 
    def _reconcile(self):
140
123
        self.branch.lock_write()
141
 
        self.add_cleanup(self.branch.unlock)
142
 
        self.pb = ui.ui_factory.nested_progress_bar()
143
 
        self.add_cleanup(self.pb.finished)
144
 
        self._reconcile_steps()
 
124
        try:
 
125
            self.pb = ui.ui_factory.nested_progress_bar()
 
126
            try:
 
127
                self._reconcile_steps()
 
128
            finally:
 
129
                self.pb.finished()
 
130
        finally:
 
131
            self.branch.unlock()
145
132
 
146
133
    def _reconcile_steps(self):
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
 
        operation = cleanup.OperationWithCleanups(self._reconcile)
211
 
        self.add_cleanup = operation.add_cleanup
212
 
        operation.run_simple()
213
 
 
214
 
    def _reconcile(self):
215
195
        self.repo.lock_write()
216
 
        self.add_cleanup(self.repo.unlock)
217
 
        self.pb = ui.ui_factory.nested_progress_bar()
218
 
        self.add_cleanup(self.pb.finished)
219
 
        self._reconcile_steps()
 
196
        try:
 
197
            self.pb = ui.ui_factory.nested_progress_bar()
 
198
            try:
 
199
                self._reconcile_steps()
 
200
            finally:
 
201
                self.pb.finished()
 
202
        finally:
 
203
            self.repo.unlock()
220
204
 
221
205
    def _reconcile_steps(self):
222
206
        """Perform the steps to reconcile this repository."""
230
214
        only data-loss causing issues (!self.thorough) or all issues
231
215
        (self.thorough) are treated as requiring the reweave.
232
216
        """
 
217
        # local because needing to know about WeaveFile is a wart we want to hide
 
218
        from bzrlib.weave import WeaveFile, Weave
233
219
        transaction = self.repo.get_transaction()
234
 
        self.pb.update(gettext('Reading inventory data'))
 
220
        self.pb.update('Reading inventory data')
235
221
        self.inventory = self.repo.inventories
236
222
        self.revisions = self.repo.revisions
237
223
        # the total set of revisions to process
251
237
        # (no garbage inventories or we are not doing a thorough check)
252
238
        if (not self.inconsistent_parents and
253
239
            (not self.garbage_inventories or not self.thorough)):
254
 
            ui.ui_factory.note(gettext('Inventory ok.'))
 
240
            ui.ui_factory.note('Inventory ok.')
255
241
            return
256
 
        self.pb.update(gettext('Backing up inventory'), 0, 0)
 
242
        self.pb.update('Backing up inventory', 0, 0)
257
243
        self.repo._backup_inventory()
258
 
        ui.ui_factory.note(gettext('Backup inventory created.'))
 
244
        ui.ui_factory.note('Backup inventory created.')
259
245
        new_inventories = self.repo._temp_inventories()
260
246
 
261
247
        # we have topological order of revisions and non ghost parents ready.
271
257
        if not (set(new_inventories.keys()) ==
272
258
            set([(revid,) for revid in self.pending])):
273
259
            raise AssertionError()
274
 
        self.pb.update(gettext('Writing weave'))
 
260
        self.pb.update('Writing weave')
275
261
        self.repo._activate_new_inventory()
276
262
        self.inventory = None
277
 
        ui.ui_factory.note(gettext('Inventory regenerated.'))
 
263
        ui.ui_factory.note('Inventory regenerated.')
278
264
 
279
265
    def _new_inv_parents(self, revision_key):
280
266
        """Lookup ghost-filtered parents for revision_key."""
368
354
    def _load_indexes(self):
369
355
        """Load indexes for the reconciliation."""
370
356
        self.transaction = self.repo.get_transaction()
371
 
        self.pb.update(gettext('Reading indexes'), 0, 2)
 
357
        self.pb.update('Reading indexes', 0, 2)
372
358
        self.inventory = self.repo.inventories
373
 
        self.pb.update(gettext('Reading indexes'), 1, 2)
 
359
        self.pb.update('Reading indexes', 1, 2)
374
360
        self.repo._check_for_inconsistent_revision_parents()
375
361
        self.revisions = self.repo.revisions
376
 
        self.pb.update(gettext('Reading indexes'), 2, 2)
 
362
        self.pb.update('Reading indexes', 2, 2)
377
363
 
378
364
    def _gc_inventory(self):
379
365
        """Remove inventories that are not referenced from the revision store."""
380
 
        self.pb.update(gettext('Checking unused inventories'), 0, 1)
 
366
        self.pb.update('Checking unused inventories', 0, 1)
381
367
        self._check_garbage_inventories()
382
 
        self.pb.update(gettext('Checking unused inventories'), 1, 3)
 
368
        self.pb.update('Checking unused inventories', 1, 3)
383
369
        if not self.garbage_inventories:
384
 
            ui.ui_factory.note(gettext('Inventory ok.'))
 
370
            ui.ui_factory.note('Inventory ok.')
385
371
            return
386
 
        self.pb.update(gettext('Backing up inventory'), 0, 0)
 
372
        self.pb.update('Backing up inventory', 0, 0)
387
373
        self.repo._backup_inventory()
388
 
        ui.ui_factory.note(gettext('Backup Inventory created'))
 
374
        ui.ui_factory.note('Backup Inventory created')
389
375
        # asking for '' should never return a non-empty weave
390
376
        new_inventories = self.repo._temp_inventories()
391
377
        # we have topological order of revisions and non ghost parents ready.
402
388
        # the revisionds list
403
389
        if not(set(new_inventories.keys()) == set(revision_keys)):
404
390
            raise AssertionError()
405
 
        self.pb.update(gettext('Writing weave'))
 
391
        self.pb.update('Writing weave')
406
392
        self.repo._activate_new_inventory()
407
393
        self.inventory = None
408
 
        ui.ui_factory.note(gettext('Inventory regenerated.'))
 
394
        ui.ui_factory.note('Inventory regenerated.')
409
395
 
410
396
    def _fix_text_parents(self):
411
397
        """Fix bad versionedfile parent entries.
443
429
            versions_list.append(text_key[1])
444
430
        # Do the reconcile of individual weaves.
445
431
        for num, file_id in enumerate(per_id_bad_parents):
446
 
            self.pb.update(gettext('Fixing text parents'), num,
 
432
            self.pb.update('Fixing text parents', num,
447
433
                           len(per_id_bad_parents))
448
434
            versions_with_bad_parents = per_id_bad_parents[file_id]
449
435
            id_unused_versions = set(key[-1] for key in unused_versions
507
493
    #  - lock the names list
508
494
    #  - perform a customised pack() that regenerates data as needed
509
495
    #  - 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
 
496
    # https://bugs.edge.launchpad.net/bzr/+bug/154173
517
497
 
518
498
    def _reconcile_steps(self):
519
499
        """Perform the steps to reconcile this repository."""
522
502
        collection = self.repo._pack_collection
523
503
        collection.ensure_loaded()
524
504
        collection.lock_names()
525
 
        self.add_cleanup(collection._unlock_names)
526
 
        packs = collection.all_packs()
527
 
        all_revisions = self.repo.all_revision_ids()
528
 
        total_inventories = len(list(
529
 
            collection.inventory_index.combined_index.iter_all_entries()))
530
 
        if len(all_revisions):
531
 
            if self.canonicalize_chks:
532
 
                reconcile_meth = self.repo._canonicalize_chks_pack
 
505
        try:
 
506
            packs = collection.all_packs()
 
507
            all_revisions = self.repo.all_revision_ids()
 
508
            total_inventories = len(list(
 
509
                collection.inventory_index.combined_index.iter_all_entries()))
 
510
            if len(all_revisions):
 
511
                new_pack =  self.repo._reconcile_pack(collection, packs,
 
512
                    ".reconcile", all_revisions, self.pb)
 
513
                if new_pack is not None:
 
514
                    self._discard_and_save(packs)
533
515
            else:
534
 
                reconcile_meth = self.repo._reconcile_pack
535
 
            new_pack = reconcile_meth(collection, packs, ".reconcile",
536
 
                all_revisions, self.pb)
537
 
            if new_pack is not None:
 
516
                # only make a new pack when there is data to copy.
538
517
                self._discard_and_save(packs)
539
 
        else:
540
 
            # only make a new pack when there is data to copy.
541
 
            self._discard_and_save(packs)
542
 
        self.garbage_inventories = total_inventories - len(list(
543
 
            collection.inventory_index.combined_index.iter_all_entries()))
 
518
            self.garbage_inventories = total_inventories - len(list(
 
519
                collection.inventory_index.combined_index.iter_all_entries()))
 
520
        finally:
 
521
            collection._unlock_names()
544
522
 
545
523
    def _discard_and_save(self, packs):
546
524
        """Discard some packs from the repository.