~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/reconcile.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-19 10:58:39 UTC
  • mfrom: (6383 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6386.
  • Revision ID: jelmer@canonical.com-20111219105839-uji05ck4rkm1mj4j
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

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